Exemplo n.º 1
0
    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)],
        ]
Exemplo n.º 2
0
    def __init__(self,
                 glyphSet,
                 offset=10,
                 contrast=0,
                 contrastAngle=0,
                 connection="square",
                 cap="round",
                 miterLimit=None,
                 closeOpenPaths=True,
                 optimizeCurve=False,
                 preserveComponents=False,
                 filterDoubles=True,
                 alwaysConnect=False):
        BasePen.__init__(self, glyphSet)

        self.offset = abs(offset)
        self.contrast = abs(contrast)
        self.contrastAngle = contrastAngle
        self._inputmiterLimit = miterLimit
        if miterLimit is None:
            miterLimit = self.offset * 2
        self.miterLimit = abs(miterLimit)

        self.closeOpenPaths = closeOpenPaths
        self.optimizeCurve = optimizeCurve

        self.connectionCallback = getattr(
            self, "connection%s" % (connection.title()))
        self.capCallback = getattr(self, "cap%s" % (cap.title()))

        self.originalGlyph = Glyph()
        self.originalPen = self.originalGlyph.getPen()

        self.outerGlyph = Glyph()
        self.outerPen = self.outerGlyph.getPen()
        self.outerCurrentPoint = None
        self.outerFirstPoint = None
        self.outerPrevPoint = None

        self.innerGlyph = Glyph()
        self.innerPen = self.innerGlyph.getPen()
        self.innerCurrentPoint = None
        self.innerFirstPoint = None
        self.innerPrevPoint = None

        self.prevPoint = None
        self.firstPoint = None
        self.firstAngle = None
        self.prevAngle = None

        self.shouldHandleMove = True

        self.preserveComponents = preserveComponents
        self.components = []

        self.filterDoubles = filterDoubles
        self.alwaysConnect = alwaysConnect
        self.drawSettings()
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
 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"])
Exemplo n.º 5
0
 def test_already_quadratic(self):
     glyph = Glyph()
     pen = glyph.getPen()
     pen.moveTo((0, 0))
     pen.qCurveTo((1, 1), (2, 2))
     pen.closePath()
     assert not glyph_to_quadratic(glyph)
Exemplo n.º 6
0
 def test_appendGuideline(self):
     glyph = Glyph()
     glyph.dirty = False
     guideline = Guideline()
     glyph.appendGuideline(guideline)
     self.assertEqual(len(glyph.guidelines), 1)
     self.assertTrue(glyph.dirty)
     self.assertEqual(guideline.getParent(), glyph)
Exemplo n.º 7
0
    def __init__(self,
                 glyphSet,
                 offset=10,
                 connection="square",
                 cap="round",
                 miterLimit=None,
                 closeOpenPaths=True):
        BasePen.__init__(self, glyphSet)

        self.offset = abs(offset)
        self._inputmiterLimit = miterLimit
        if miterLimit is None:
            miterLimit = self.offset
        self.miterLimit = abs(miterLimit)

        self.closeOpenPaths = closeOpenPaths

        self.connectionCallback = getattr(
            self,
            "connection%s" % (connection[0].capitalize() + connection[1:]))
        self.capCallback = getattr(self,
                                   "cap%s" % (cap[0].capitalize() + cap[1:]))

        self.originalGlyph = Glyph()
        self.originalPen = self.originalGlyph.getPen()

        self.outerGlyph = Glyph()
        self.outerPen = self.outerGlyph.getPen()
        self.outerCurrentPoint = None
        self.outerFirstPoint = None
        self.outerPrevPoint = None

        self.innerGlyph = Glyph()
        self.innerPen = self.innerGlyph.getPen()
        self.innerCurrentPoint = None
        self.innerFirstPoint = None
        self.innerPrevPoint = None

        self.prevPoint = None
        self.firstPoint = None
        self.firstAngle = None
        self.prevAngle = None

        self.shouldHandleMove = True

        self.drawSettings()
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 def test_appendContour(self):
     glyph = Glyph()
     glyph.dirty = False
     contour = Contour()
     glyph.appendContour(contour)
     self.assertEqual(len(glyph), 1)
     self.assertTrue(glyph.dirty)
     self.assertEqual(contour.getParent(), glyph)
Exemplo n.º 10
0
 def test_appendComponent(self):
     glyph = Glyph()
     glyph.dirty = False
     component = Component()
     glyph.appendComponent(component)
     self.assertEqual(len(glyph.components), 1)
     self.assertTrue(glyph.dirty)
     self.assertEqual(component.getParent(), glyph)
Exemplo n.º 11
0
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
Exemplo n.º 12
0
 def test_ignore_components(self):
     glyph = Glyph()
     pen = glyph.getPen()
     pen.addComponent('a', (1, 0, 0, 1, 0, 0))
     pen.moveTo((0, 0))
     pen.curveTo((1, 1), (2, 2), (3, 3))
     pen.closePath()
     assert glyph_to_quadratic(glyph)
     assert len(glyph.components) == 1
Exemplo n.º 13
0
 def test_extract_scaled_glyph_as_Defcon_Glyph(self):
     """Test scaled glyph retrieval as a Defcon glyph."""
     from defcon import Glyph
     for testFont in [self.smallFont, self.stemedSmallFont]:
         scaledGlyph = Glyph()
         for glyphName in self.glyphNames:
             testFont.extractGlyph(glyphName, scaledGlyph)
             self.assertIsInstance(scaledGlyph, Glyph)
             self.assertEqual(scaledGlyph.name, glyphName)
Exemplo n.º 14
0
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
Exemplo n.º 15
0
 def test_glyph(self):
     self.assertIsNone(self.contour.glyph)
     self.contour = Contour(self.glyph)
     self.assertEqual(self.contour.glyph, self.glyph)
     glyph = Glyph()
     self.contour = Contour()
     self.contour.glyph = glyph
     self.assertEqual(self.contour.glyph, glyph)
     with self.assertRaises(AssertionError):
         self.contour.glyph = self.glyph
Exemplo n.º 16
0
 def test_open_paths(self):
     glyph = Glyph()
     pen = glyph.getPen()
     pen.moveTo((0, 0))
     pen.lineTo((1, 1))
     pen.curveTo((2, 2), (3, 3), (4, 4))
     pen.endPath()
     assert glyph_to_quadratic(glyph)
     # open contour is still open
     assert glyph[-1][0].segmentType == "move"
Exemplo n.º 17
0
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
Exemplo n.º 18
0
def test_remove_tiny_sub_paths_large_contour():
    g = Glyph()
    p = g.getPen()
    p.moveTo((100, 100))
    p.lineTo((200, 200))
    p.lineTo((0, 100))
    p.closePath()
    assert len(g[0]) == 3
    assert g.bounds == (0, 100, 200, 200)
    bg = BooleanGlyph(g)
    assert remove_tiny_sub_paths(bg, 25, []) == []
Exemplo n.º 19
0
 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"])])
Exemplo n.º 20
0
    def find_shape_diffs(self):
        """Report differences in glyph shapes, using BooleanOperations."""

        self.build_names()

        area_pen = GlyphAreaPen(None)
        pen = PointToSegmentPen(area_pen)
        mismatched = {}
        for name in self.names:
            glyph_a = Glyph()
            glyph_b = Glyph()
            self.glyph_set_a[name].draw(Qu2CuPen(glyph_a.getPen(), self.glyph_set_a))
            self.glyph_set_b[name].draw(Qu2CuPen(glyph_b.getPen(), self.glyph_set_b))
            booleanOperations.xor(list(glyph_a), list(glyph_b), pen)
            area = abs(area_pen.pop())
            if area:
                mismatched[name] = area

        stats = self.stats["compared"]
        for name, area in mismatched.items():
            stats.append((area, name, self.basepath))
Exemplo n.º 21
0
 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)
Exemplo n.º 22
0
 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
Exemplo n.º 23
0
def test_remove_tiny_sub_paths_small_contour():
    g = Glyph()
    p = g.getPen()
    p.moveTo((1, 1))
    p.lineTo((2, 2))
    p.lineTo((0, 1))
    p.closePath()
    assert len(g[0]) == 3
    assert g.bounds == (0, 1, 2, 2)
    bg = BooleanGlyph(g)
    assert remove_tiny_sub_paths(bg, 25, []) == \
        ['Contour 0 is too small: bounding box is less than minimum area. '
         'Start point: ((1, 1)).']
Exemplo n.º 24
0
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
Exemplo n.º 25
0
 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))
Exemplo n.º 26
0
 def test_identifier(self):
     glyph = Glyph()
     contour = Contour()
     glyph.appendContour(contour)
     contour.identifier = "contour 1"
     self.assertEqual(contour.identifier, "contour 1")
     self.assertEqual(sorted(glyph.identifiers), ["contour 1"])
     contour = Contour()
     glyph.appendContour(contour)
     with self.assertRaises(AssertionError):
         contour.identifier = "contour 1"
     contour.identifier = "contour 2"
     self.assertEqual(sorted(glyph.identifiers), ["contour 1", "contour 2"])
     contour.identifier = "not contour 2 anymore"
     self.assertEqual(contour.identifier, "contour 2")
     self.assertEqual(sorted(glyph.identifiers), ["contour 1", "contour 2"])
     contour.identifier = None
     self.assertEqual(contour.identifier, "contour 2")
     self.assertEqual(sorted(glyph.identifiers), ["contour 1", "contour 2"])
Exemplo n.º 27
0
 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
Exemplo n.º 28
0
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
Exemplo n.º 30
0
    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"])