def test_overlapping_start_end_points(self): # https://github.com/googlefonts/fontmake/issues/572 glyph1 = Glyph() pen = glyph1.getPointPen() pen.beginPath() pen.addPoint((0, 651), segmentType="line") pen.addPoint((0, 101), segmentType="line") pen.addPoint((0, 101), segmentType="line") pen.addPoint((0, 651), segmentType="line") pen.endPath() glyph2 = Glyph() pen = glyph2.getPointPen() pen.beginPath() pen.addPoint((1, 651), segmentType="line") pen.addPoint((2, 101), segmentType="line") pen.addPoint((3, 101), segmentType="line") pen.addPoint((4, 651), segmentType="line") pen.endPath() glyphs = [glyph1, glyph2] assert glyphs_to_quadratic(glyphs, reverse_direction=True) assert [[(p.x, p.y) for p in glyph[0]] for glyph in glyphs] == [ [ (0, 651), (0, 651), (0, 101), (0, 101), ], [(1, 651), (4, 651), (3, 101), (2, 101)], ]
def basic_glyph(): glyph = Glyph() pen = glyph.getPointPen() for contour in BASIC_CONTOURS: pen.beginPath() for pt, segmentType, smooth in contour: pen.addPoint(pt, segmentType, smooth) pen.endPath() 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_drawZsWithPointPen(guessSmooth): glyph = Glyph() pen = glyph.getPointPen() assert len(glyph) == 0 drawZsWithPointPen(BASIC_Z_SHAPE, pen, guessSmooth=guessSmooth) assert len(glyph) == 2 assert glyph[0][0].smooth is False assert glyph[1][0].smooth is guessSmooth
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 glyph_to_quadratic(glyph, max_n, max_err, correctDirection=True, verbose=False): """ Convert the glyph outline to TrueType quadratic splines. """ new = Glyph() writerPen = new.getPointPen() cu2quPen = Cu2QuPen(writerPen, max_n, max_err, verbose) if correctDirection: reversePen = ReverseContourPointPen(cu2quPen) glyph.drawPoints(reversePen) else: glyph.drawPoints(cu2quPen) # clear glyph but keep anchors for mark, mkmk features glyph.clearContours() glyph.clearComponents() writerPen = glyph.getPointPen() new.drawPoints(writerPen)
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 test_correct_direction_same_area(self): glyph = Glyph() pen = glyph.getPointPen() pen.beginPath() pen.addPoint((0, 0), segmentType="line") pen.addPoint((0, 50), segmentType="line") pen.addPoint((50, 50), segmentType="line") pen.endPath() pen.beginPath() pen.addPoint((50, 50), segmentType="line") pen.addPoint((50, 100), segmentType="line") pen.addPoint((100, 100), segmentType="line") pen.endPath() try: glyph.correctContourDirection() except Exception as e: self.fail("glyph.correctContourDirection() raised unexpected exception: " + str(e))
def test_contoursToZs_open_contour(): glyph = Glyph() pen = glyph.getPointPen() pen.beginPath() pen.addPoint((0, 0), 'move') pen.addPoint((1, 1), 'line') pen.addPoint((2, 2), 'line') pen.endPath() shape = contoursToZs(glyph) assert shape == [[{ 'x': 0, 'y': 0, 'on': True }, { 'x': 1, 'y': 1, 'on': True }, { 'x': 2, 'y': 2, 'on': True }]]
def getGlyph(self): glyph = Glyph() pointPen = glyph.getPointPen() self.drawPoints(pointPen) return glyph
def test_identifiers(self): glyph = Glyph() pointPen = glyph.getPointPen() pointPen.beginPath(identifier="contour 1") pointPen.addPoint((0, 0), identifier="point 1") pointPen.addPoint((0, 0), identifier="point 2") pointPen.endPath() pointPen.beginPath(identifier="contour 2") pointPen.endPath() pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 1") pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 2") guideline = Guideline() guideline.identifier = "guideline 1" glyph.appendGuideline(guideline) guideline = Guideline() guideline.identifier = "guideline 2" glyph.appendGuideline(guideline) self.assertEqual([contour.identifier for contour in glyph], ["contour 1", "contour 2"]) self.assertEqual([point.identifier for point in glyph[0]], ["point 1", "point 2"]) self.assertEqual( [component.identifier for component in glyph.components], ["component 1", "component 2"]) with self.assertRaises(AssertionError): pointPen.beginPath(identifier="contour 1") pointPen.endPath() pointPen.beginPath() pointPen.addPoint((0, 0)) with self.assertRaises(AssertionError): pointPen.addPoint((0, 0), identifier="point 1") pointPen.endPath() with self.assertRaises(AssertionError): pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 1") g = Guideline() g.identifier = "guideline 1" with self.assertRaises(AssertionError): glyph.appendGuideline(g) self.assertEqual(sorted(glyph.identifiers), [ "component 1", "component 2", "contour 1", "contour 2", "guideline 1", "guideline 2", "point 1", "point 2" ]) glyph.removeContour(glyph[0]) self.assertEqual(sorted(glyph.identifiers), [ "component 1", "component 2", "contour 2", "guideline 1", "guideline 2" ]) glyph.removeComponent(glyph.components[0]) self.assertEqual( sorted(glyph.identifiers), ["component 2", "contour 2", "guideline 1", "guideline 2"]) glyph.removeGuideline(glyph.guidelines[0]) self.assertEqual(sorted(glyph.identifiers), ["component 2", "contour 2", "guideline 2"])
def test_identifiers(self): glyph = Glyph() pointPen = glyph.getPointPen() pointPen.beginPath(identifier="contour 1") pointPen.addPoint((0, 0), identifier="point 1") pointPen.addPoint((0, 0), identifier="point 2") pointPen.endPath() pointPen.beginPath(identifier="contour 2") pointPen.endPath() pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 1") pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 2") guideline = Guideline() guideline.identifier = "guideline 1" glyph.appendGuideline(guideline) guideline = Guideline() guideline.identifier = "guideline 2" glyph.appendGuideline(guideline) self.assertEqual([contour.identifier for contour in glyph], ["contour 1", "contour 2"]) self.assertEqual([point.identifier for point in glyph[0]], ["point 1", "point 2"]) self.assertEqual( [component.identifier for component in glyph.components], ["component 1", "component 2"]) with self.assertRaises(AssertionError): pointPen.beginPath(identifier="contour 1") pointPen.endPath() pointPen.beginPath() pointPen.addPoint((0, 0)) with self.assertRaises(AssertionError): pointPen.addPoint((0, 0), identifier="point 1") pointPen.endPath() with self.assertRaises(AssertionError): pointPen.addComponent("A", (1, 1, 1, 1, 1, 1), identifier="component 1") g = Guideline() g.identifier = "guideline 1" with self.assertRaises(AssertionError): glyph.appendGuideline(g) self.assertEqual( sorted(glyph.identifiers), ["component 1", "component 2", "contour 1", "contour 2", "guideline 1", "guideline 2", "point 1", "point 2"]) glyph.removeContour(glyph[0]) self.assertEqual( sorted(glyph.identifiers), ["component 1", "component 2", "contour 2", "guideline 1", "guideline 2"]) glyph.removeComponent(glyph.components[0]) self.assertEqual( sorted(glyph.identifiers), ["component 2", "contour 2", "guideline 1", "guideline 2"]) glyph.removeGuideline(glyph.guidelines[0]) self.assertEqual( sorted(glyph.identifiers), ["component 2", "contour 2", "guideline 2"])
subjectPaths = contoursToZs(subjectContours) clippingPaths = contoursToZs(clippingContours) result = shapeops.intersection(subjectPaths, clippingPaths, **kwargs) drawZsWithPointPen(result, outPen, guessSmooth=guessSmooth) def xor(subjectContours, clippingContours, outPen, guessSmooth=True, **kwargs): subjectPaths = contoursToZs(subjectContours) clippingPaths = contoursToZs(clippingContours) result = shapeops.xor(subjectPaths, clippingPaths, **kwargs) drawZsWithPointPen(result, outPen, guessSmooth=guessSmooth) if __name__ == "__main__": import sys from defcon import Glyph from ufoLib.glifLib import readGlyphFromString, writeGlyphToString data = sys.stdin.read() glyph = Glyph() readGlyphFromString(data, glyph, glyph.getPointPen()) contours = list(glyph) glyph.clearContours() union(contours, glyph.getPointPen()) output = writeGlyphToString(glyph.name, glyph, glyph.drawPoints) sys.stdout.write(output)