def test_glyph_dispatcher_new(self): font = Font() font.newGlyph("A") glyph = font["A"] pen = glyph.getPointPen() pen.beginPath() pen.addPoint((0, 0), segmentType="line") pen.addPoint((0, 100), segmentType="line") pen.addPoint((100, 100), segmentType="line") pen.addPoint((100, 0), segmentType="line") pen.endPath() contour = glyph[0] self.assertEqual(contour.getParent(), glyph) self.assertEqual(contour.dispatcher, font.dispatcher) component = Component() glyph.appendComponent(component) self.assertEqual(component.getParent(), glyph) self.assertEqual(component.dispatcher, font.dispatcher) anchor = Anchor() glyph.appendAnchor(anchor) self.assertEqual(anchor.getParent(), glyph) self.assertEqual(anchor.dispatcher, font.dispatcher) guideline = Guideline() glyph.appendGuideline(guideline) self.assertEqual(guideline.getParent(), glyph) self.assertEqual(guideline.dispatcher, font.dispatcher)
def test_glyph_dispatcher_inserted(self): font = Font() font.newGlyph("A") glyph = font["A"] pen = glyph.getPointPen() pen.beginPath() pen.addPoint((0, 0), segmentType="line") pen.addPoint((0, 100), segmentType="line") pen.addPoint((100, 100), segmentType="line") pen.addPoint((100, 0), segmentType="line") pen.endPath() contour = glyph[0] component = Component() glyph.appendComponent(component) anchor = Anchor() glyph.appendAnchor(anchor) guideline = Guideline() glyph.appendGuideline(guideline) sourceGlyph = glyph newFont = Font() insertedGlyph = newFont.insertGlyph(sourceGlyph) contour = insertedGlyph[0] self.assertTrue(contour.getParent(), insertedGlyph) self.assertTrue(contour.dispatcher, newFont.dispatcher) component = insertedGlyph.components[0] self.assertTrue(component.getParent(), insertedGlyph) self.assertTrue(component.dispatcher, newFont.dispatcher) anchor = insertedGlyph.anchors[0] self.assertTrue(anchor.getParent(), insertedGlyph) self.assertTrue(anchor.dispatcher, newFont.dispatcher) guideline = insertedGlyph.guidelines[0] self.assertTrue(guideline.getParent(), insertedGlyph) self.assertTrue(guideline.dispatcher, newFont.dispatcher)
def place_anchors(font, anchors): """anchors is a dict in the form {'glyph_name': {'anchor_name1': 'BOT_LEFT', 'anchor_name2': (320, -20)}}""" placed_count = 0 for gname, anchor_names in anchors.items(): if gname not in font.keys(): continue glyph = font[gname] glyph_pos = get_glyph_pos(glyph.bounds) glyph_anchors = [anchor.name for anchor in glyph.anchors] for anchor_name, anchor_pos in anchor_names.items(): if isinstance(anchor_pos, tuple): pass else: anchor_pos = glyph_pos[anchor_pos] anchor = Anchor() anchor.name = anchor_name anchor.x, anchor.y = anchor_pos if anchor_name not in glyph_anchors: glyph.appendAnchor(anchor) placed_count += 1 if placed_count: font.save() print(os.path.basename(font.path)) print(' %d anchors placed' % placed_count)
def test_appendAnchor(self): glyph = Glyph() glyph.dirty = False anchor = Anchor() glyph.appendAnchor(anchor) self.assertEqual(len(glyph.anchors), 1) self.assertTrue(glyph.dirty) self.assertEqual(anchor.getParent(), glyph)
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