def test_insertGlyph(self): font = Font(getTestFontPath()) glyph = Glyph() glyph.name = "NewGlyphTest" self.assertEqual(sorted(font.keys()), ["A", "B", "C"]) font.insertGlyph(glyph) self.assertEqual(sorted(font.keys()), ["A", "B", "C", "NewGlyphTest"])
def create_glyph(codepoint, width, contours): glyph = Glyph() glyph.name = get_glyph_name(codepoint) glyph.unicode = ord(codepoint) glyph.width = width for contour in contours: glyph.appendContour(contour) return glyph
def test_copyDataFromGlyph(self): source = Glyph() source.name = "a" source.width = 1 source.height = 2 source.unicodes = [3, 4] source.note = "test image" source.image = dict(fileName="test image", xScale=1, xyScale=1, yxScale=1, yScale=1, xOffset=0, yOffset=0, color=None) source.anchors = [dict(x=100, y=200, name="test anchor")] source.guidelines = [dict(x=10, y=20, name="test guideline")] source.lib = {"foo": "bar"} pen = source.getPointPen() pen.beginPath() pen.addPoint((100, 200), segmentType="line") pen.addPoint((300, 400), segmentType="line") pen.endPath() component = Component() component.base = "b" source.appendComponent(component) dest = Glyph() dest.copyDataFromGlyph(source) self.assertNotEqual(source.name, dest.name) self.assertEqual(source.width, dest.width) self.assertEqual(source.height, dest.height) self.assertEqual(source.unicodes, dest.unicodes) self.assertEqual(source.note, dest.note) self.assertEqual(source.image.items(), dest.image.items()) self.assertEqual([g.items() for g in source.guidelines], [g.items() for g in dest.guidelines]) self.assertEqual([g.items() for g in source.anchors], [g.items() for g in dest.anchors]) self.assertEqual(len(source), len(dest)) self.assertEqual(len(source.components), len(dest.components)) sourceContours = [] for contour in source: sourceContours.append([]) for point in contour: sourceContours[-1].append( (point.x, point.x, point.segmentType, point.name)) destContours = [] for contour in dest: destContours.append([]) for point in contour: destContours[-1].append( (point.x, point.x, point.segmentType, point.name)) self.assertEqual(sourceContours, destContours) self.assertEqual(source.components[0].baseGlyph, dest.components[0].baseGlyph)
def test_insertGlyph(self): font = Font() layer = font.layers[None] source = Glyph() source.unicodes = [1, 2] source.name = "a" dest = layer.insertGlyph(source, name="nota") self.assertNotEqual(dest, source) self.assertEqual(dest.name, "nota") self.assertEqual(list(layer.unicodeData.items()), [(1, ["nota"]), (2, ["nota"])]) source = Glyph() source.unicodes = [3] source.name = "b" dest = layer.insertGlyph(source) self.assertNotEqual(dest, source) self.assertEqual(dest.name, "b") self.assertEqual(list(layer.unicodeData.items()), [(1, ["nota"]), (2, ["nota"]), (3, ["b"])])
def test_incompatible_glyphs(self, outlines, exception, message): glyphs = [] for i, outline in enumerate(outlines): glyph = Glyph() glyph.name = "glyph%d" % i pen = glyph.getPen() for operator, args in outline: getattr(pen, operator)(*args) glyphs.append(glyph) with pytest.raises(exception) as excinfo: glyphs_to_quadratic(glyphs) assert excinfo.match(message)
def to_glyph(self, name=None, width=None): """ Create a glyph (like from `defcon`) using this pen’s value. *Warning: be sure to call endPath or closePath on your pen or this call will silently do nothing """ bounds = self.bounds() glyph = Glyph() glyph.name = name glyph.width = width or bounds.w sp = glyph.getPen() self.replay(sp) print(glyph._contours) return glyph
def test_copyDataFromGlyph(self): source = Glyph() source.name = "a" source.width = 1 source.height = 2 source.unicodes = [3, 4] source.note = "test image" source.image = dict(fileName="test image", xScale=1, xyScale=1, yxScale=1, yScale=1, xOffset=0, yOffset=0, color=None) source.anchors = [dict(x=100, y=200, name="test anchor")] source.guidelines = [dict(x=10, y=20, name="test guideline")] source.lib = {"foo": "bar"} pen = source.getPointPen() pen.beginPath() pen.addPoint((100, 200), segmentType="line") pen.addPoint((300, 400), segmentType="line") pen.endPath() component = Component() component.base = "b" source.appendComponent(component) dest = Glyph() dest.copyDataFromGlyph(source) self.assertNotEqual(source.name, dest.name) self.assertEqual(source.width, dest.width) self.assertEqual(source.height, dest.height) self.assertEqual(source.unicodes, dest.unicodes) self.assertEqual(source.note, dest.note) self.assertEqual(source.image.items(), dest.image.items()) self.assertEqual([g.items() for g in source.guidelines], [g.items() for g in dest.guidelines]) self.assertEqual([g.items() for g in source.anchors], [g.items() for g in dest.anchors]) self.assertEqual(len(source), len(dest)) self.assertEqual(len(source.components), len(dest.components)) sourceContours = [] for contour in source: sourceContours.append([]) for point in contour: sourceContours[-1].append((point.x, point.x, point.segmentType, point.name)) destContours = [] for contour in dest: destContours.append([]) for point in contour: destContours[-1].append((point.x, point.x, point.segmentType, point.name)) self.assertEqual(sourceContours, destContours) self.assertEqual(source.components[0].baseGlyph, dest.components[0].baseGlyph)
def getGlyphFromDict(glyph_dict): g = Glyph() # Set attributes g.height = glyph_dict.get('height', 0) g.lib = glyph_dict.get('lib', {}) g.name = glyph_dict.get('name', '') g.note = glyph_dict.get('note', None) g.unicode = glyph_dict.get('unicode', None) g.unicodes = glyph_dict.get('unicodes', []) g.width = glyph_dict.get('width', 0) # Draw the outlines with a pen pen = g.getPointPen() for contour in glyph_dict.get('contours', []): pen.beginPath() for point in contour: pen.addPoint( ( point.get('x'), point.get('y') ), segmentType = point.get('type', None), name = point.get('name', None), smooth = point.get('smooth', None), ) pen.endPath() # Add components for component in glyph_dict.get('components', []): c = Component() c.baseGlyph = component.get('ref', '') c.transformation = component.get('transformation', (1, 0, 0, 1, 0, 0)) g.appendComponent(c) # Add anchors for anchor in glyph_dict.get('anchors', []): a = Anchor(anchorDict = anchor) g.appendAnchor(a) # Return the completed glyph object return g
def to_glyph(self, name=None, width=None, allow_blank=False): """ Create a glyph (like from `defcon`) using this pen’s value. *Warning*: if path is unended, closedPath will be called """ from defcon import Glyph if not allow_blank: if self.unended(): self.closePath() bounds = self.bounds() glyph = Glyph() glyph.name = name glyph.width = width or bounds.w try: sp = glyph.getPen() self.replay(sp) except AssertionError: if not allow_blank: print(">>>blank glyph:", glyph.name) return glyph
def createGlyphs(self): self.UFO.newGlyph('.notdef') missing = '' for glf in self.GDB.Master2Search: glfUnicode = int(self.GDB.Master2Unicode[glf], 16) print('') layer = 0 stopAt = 'arAlef.fina.la' glfSrc = self.GDB.Master2Search[glf].split(',') if glf == stopAt: m = 1 log = (glf + ' ' * 50)[0:20] if glf in self.GDB.MAPPING: try: mgName = [self.GDB.MAPPING[glf]] glyph = Glyph() glyph.copyDataFromGlyph(self.srcUFO[mgName[0]]) glyph.name = glf if layer == 0: glyph.unicode = glfUnicode glyph.unicodes = [glfUnicode] else: glyph.unicode = None glyph.anchors = [] glyph.decomposeAllComponents() if layer > 0: currentLayer = self.getLayer(layer) currentLayer.insertGlyph(glyph) else: self.UFO.insertGlyph(glyph) layer += 1 # print(g + ' found :)' + ' L ' + str(layer)) gLog = log + (mgName[0] + ' ' * 50)[0:20] print(gLog + '[' + str(layer) + '] *') except: pass for g in glfSrc: mgName = None gCode = None gLog = log + (g + ' ' * 50)[0:20] try: try: gCode = self.GDB.Prod2Decimal[g] mgName = self.srcUFO.unicodeData[gCode] except: try: mgName = [g] except: pass glyph = Glyph() glyph.copyDataFromGlyph(self.srcUFO[mgName[0]]) glyph.name = glf if layer == 0: glyph.unicode = glfUnicode glyph.unicodes = [glfUnicode] else: glyph.unicode = None glyph.anchors = [] glyph.decomposeAllComponents() if layer > 0: currentLayer = self.getLayer(layer) currentLayer.insertGlyph(glyph) else: self.UFO.insertGlyph(glyph) layer += 1 print(gLog + '[' + str(layer) + ']') except: print(gLog + '[ ]') glyph = None if layer == 0: self.UFO.newGlyph(glf) missing += log + "\n" if len(missing) > 1: print('\n') print('=' * 60) print('Missing glyphes ' + str(len(missing.splitlines()))) print('=' * 60) print(missing) print('=' * 60)