def addRCJKGlyphToVarCoUFO( ufo, rcjkGlyphSet, srcGlyphName, dstGlyphName, unicodes, renameTable, componentSourceGlyphSet, globalAxisNames, ): if renameTable is None: renameTable = {} rcjkGlyph = rcjkGlyphSet.getGlyph(srcGlyphName) if rcjkGlyph.components and not rcjkGlyph.outline.isEmpty(): logger.warning( f"glyph {srcGlyphName} has both outlines and components") glyph = UGlyph(dstGlyphName) glyph.unicodes = unicodes glyph.width = max(0, rcjkGlyph.width) # width can't be negative rcjkGlyphToVarCoGlyph(rcjkGlyph, glyph, renameTable, componentSourceGlyphSet) if globalAxisNames is None: axisNameMapping = _makeAxisNameMapping(rcjkGlyph.axes) axisNames = set(axisNameMapping.values()) else: axisNames = globalAxisNames for varIndex, rcjkVarGlyph in enumerate(rcjkGlyph.variations): location = rcjkVarGlyph.location location = normalizeLocation(location, rcjkGlyph.axes) if globalAxisNames is None: location = {axisNameMapping[k]: v for k, v in location.items()} sparseLocation = {k: v for k, v in location.items() if v != 0} layerName = layerNameFromLocation(sparseLocation, axisNames) assert layerName, (srcGlyphName, varIndex, location, rcjkGlyph.axes) layer = getUFOLayer(ufo, layerName) varGlyph = UGlyph(dstGlyphName) varGlyph.width = max(0, rcjkVarGlyph.width) # width can't be negative rcjkGlyphToVarCoGlyph(rcjkVarGlyph, varGlyph, renameTable, componentSourceGlyphSet) layer[dstGlyphName] = varGlyph ufo[dstGlyphName] = glyph
def layer() -> Layer: a = Glyph("a") pen = a.getPen() pen.moveTo((8, 0)) pen.lineTo((18, 0)) pen.lineTo((18, 20)) pen.lineTo((8, 20)) pen.closePath() a.width = 30 a.appendAnchor({"x": 10, "y": 30, "name": "top"}) b = Glyph("b", width=a.width, components=[Component("a", (1, 0, 0, 1, 2, -5))]) layer = Layer(glyphs=[a, b]) return layer
def addFlattenedGlyphsToUFO(self, ufo, location, numDecimalsRounding=0, characterSet=None, glyphSet=None): if characterSet is not None and glyphSet is not None: raise TypeError("can't pass both characterSet and glyphSet") if numDecimalsRounding == 1: roundFunc = roundFuncOneDecimal elif numDecimalsRounding != 0: assert 0, numDecimalsRounding else: roundFunc = otRound revCmap = self.getGlyphNamesAndUnicodes() glyphNames = filterGlyphNames(sorted(revCmap)) for glyphName in glyphNames: if glyphSet is not None: if glyphName not in glyphSet: continue elif characterSet is not None: codePoints = set(revCmap[glyphName]) if not codePoints & characterSet: continue glyph = UGlyph(glyphName) glyph.unicodes = revCmap[glyphName] copyMarkColor(self.characterGlyphGlyphSet.getGlyph(glyphName), glyph) pen = RoundingPointPen(glyph.getPointPen(), roundFunc) try: width = self.drawPointsCharacterGlyph(glyphName, location, pen) except InterpolationError as e: logger.warning( f"glyph {glyphName} can't be interpolated ({e})") except Exception as e: logger.warning(f"glyph {glyphName} caused an error: {e!r}") raise else: glyph.width = max(0, width) # can't be negative ufo[glyphName] = glyph
def test_copyDataFromGlyph(ufo_UbuTestData): font = ufo_UbuTestData a = font["a"] a.height = 500 a.image = Image("a.png") a.note = "a note" a.lib = {"bar": [3, 2, 1]} a.anchors = [Anchor(250, 0, "bottom")] a.guidelines = [Guideline(y=500)] a.components = [Component("A")] b = Glyph("b") b.width = 350 b.height = 1000 b.image = Image("b.png") b.note = "b note" b.lib = {"foo": [1, 2, 3]} b.anchors = [Anchor(350, 800, "top")] b.guidelines = [Guideline(x=50)] assert b.name != a.name assert b.width != a.width assert b.height != a.height assert b.unicodes != a.unicodes assert b.image != a.image assert b.note != a.note assert b.lib != a.lib assert b.anchors != a.anchors assert b.guidelines != a.guidelines assert b.contours != a.contours assert b.components != a.components def _assert_equal_but_distinct_objects(glyph1, glyph2): assert glyph1.width == glyph2.width assert glyph1.height == glyph2.height assert glyph1.unicodes == glyph2.unicodes assert glyph1.unicodes is not glyph2.unicodes assert glyph1.image == glyph2.image assert glyph1.image is not glyph2.image assert glyph1.note == glyph2.note assert glyph1.lib == glyph2.lib assert glyph1.lib is not glyph2.lib assert glyph1.lib["bar"] == glyph2.lib["bar"] assert glyph1.lib["bar"] is not glyph2.lib["bar"] assert glyph1.anchors == glyph2.anchors assert glyph1.anchors is not glyph2.anchors assert glyph1.anchors[0] is not glyph2.anchors[0] assert glyph1.guidelines == glyph2.guidelines assert glyph1.guidelines is not glyph2.guidelines assert glyph1.guidelines[0] is not glyph2.guidelines[0] assert glyph1.contours == glyph2.contours assert glyph1.contours is not glyph2.contours assert glyph1.contours[0] is not glyph2.contours[0] assert glyph1.components == glyph2.components assert glyph1.components is not glyph2.components assert glyph1.components[0] is not glyph2.components[0] b.copyDataFromGlyph(a) assert b.name != a.name _assert_equal_but_distinct_objects(b, a) c = a.copy() assert c.name == a.name _assert_equal_but_distinct_objects(c, a) d = a.copy(name="d") assert d.name == "d" _assert_equal_but_distinct_objects(d, a)