Exemplo n.º 1
0
def scaleGlyph(font, glyph, scale):
    """Scales the glyph, but keeps it centered around its original bounding
    box."""
    from fontTools.pens.recordingPen import RecordingPointPen
    from fontTools.pens.transformPen import TransformPointPen
    from fontTools.misc.transform import Identity

    width = glyph.width
    bbox = glyph.getBounds(font)
    x = (bbox.xMin + bbox.xMax) / 2
    y = (bbox.yMin + bbox.yMax) / 2
    matrix = Identity
    matrix = matrix.translate(-x * scale + x, -y * scale + y)
    matrix = matrix.scale(scale)

    rec = RecordingPointPen()
    glyph.drawPoints(rec)
    glyph.clearContours()
    glyph.clearComponents()

    pen = TransformPointPen(glyph.getPointPen(), matrix)
    rec.replay(pen)

    if width == 0:
        glyph.width = width

    return matrix
Exemplo n.º 2
0
    def filter(self, glyph):
        matrix = self.context.matrix
        if matrix == Identity or not (glyph or glyph.components or glyph.anchors):
            return False  # nothing to do

        modified = self.context.modified
        glyphSet = self.context.glyphSet
        for component in glyph.components:
            base_name = component.baseGlyph
            if base_name in modified:
                continue
            base_glyph = glyphSet[base_name]
            if self.include(base_glyph) and self.filter(base_glyph):
                # base glyph is included but was not transformed yet; we
                # call filter recursively until all the included bases are
                # transformed, or there are no more components
                modified.add(base_name)

        rec = RecordingPointPen()
        glyph.drawPoints(rec)
        glyph.clearContours()
        glyph.clearComponents()

        outpen = glyph.getPointPen()
        filterpen = TransformPointPen(outpen, matrix, modified)
        rec.replay(filterpen)

        # anchors are not drawn through the pen API,
        # must be transformed separately
        for a in glyph.anchors:
            a.x, a.y = matrix.transformPoint((a.x, a.y))

        return True
Exemplo n.º 3
0
    def test_record_and_replay(self):
        pen = RecordingPointPen()
        glyph = _TestGlyph()
        glyph.drawPoints(pen)
        pen.addComponent("a", (2, 0, 0, 2, -10, 5))

        assert pen.value == [
            ("beginPath", (), {
                "identifier": "abc"
            }),
            ("addPoint", ((0.0, 0.0), "line", False, "start"), {
                "identifier": "0000"
            }),
            ("addPoint", ((0.0, 100.0), "line", False, None), {
                "identifier": "0001"
            }),
            ("addPoint", ((50.0, 75.0), None, False, None), {
                "identifier": "0002"
            }),
            ("addPoint", ((60.0, 50.0), None, False, None), {
                "identifier": "0003"
            }),
            ("addPoint", ((50.0, 0.0), "curve", True, "last"), {
                "identifier": "0004"
            }),
            ("endPath", (), {}),
            ("addComponent", ("a", (2, 0, 0, 2, -10, 5)), {}),
        ]

        pen2 = RecordingPointPen()
        pen.replay(pen2)

        assert pen2.value == pen.value