Beispiel #1
0
def svg2glif(svg,
             name,
             width=0,
             height=0,
             unicodes=None,
             transform=None,
             version=2):
    """ Convert an SVG outline to a UFO glyph with given 'name', advance
	'width' and 'height' (int), and 'unicodes' (list of int).
	Return the resulting string in GLIF format (default: version 2).
	If 'transform' is provided, apply a transformation matrix before the
	conversion (must be tuple of 6 floats, or a FontTools Transform object).
	"""
    glyph = SimpleNamespace(width=width, height=height, unicodes=unicodes)
    outline = SVGPath.fromstring(svg, transform=transform)

    # writeGlyphToString takes a callable (usually a glyph's drawPoints
    # method) that accepts a PointPen, however SVGPath currently only has
    # a draw method that accepts a segment pen. We need to wrap the call
    # with a converter pen.
    def drawPoints(pointPen):
        pen = SegmentToPointPen(pointPen)
        outline.draw(pen)

    return writeGlyphToString(name,
                              glyphObject=glyph,
                              drawPointsFunc=drawPoints,
                              formatVersion=version)
 def dropEvent(self, event):
     mimeData = event.mimeData()
     if mimeData.hasUrls():
         paths = mimeData.urls()
         # pick just one image
         path = paths[0].toLocalFile()
         fileName = os.path.basename(path)
         with open(path, "rb") as imgFile:
             data = imgFile.read()
         ext = os.path.splitext(path)[1][1:]
         # TODO: make sure we cleanup properly when replacing an image with
         # another
         if ext.lower() == "glif":
             otherGlyph = self._glyph.__class__()
             try:
                 readGlyphFromString(data, otherGlyph,
                                     otherGlyph.getPointPen())
             except Exception as e:
                 errorReports.showCriticalException(e)
                 return
             self._glyph.beginUndoGroup()
             otherGlyph.drawPoints(self._glyph.getPointPen())
             self._glyph.endUndoGroup()
             return
         if ext.lower() == "svg":
             try:
                 svgPath = SVGPath.fromstring(data)
             except Exception as e:
                 errorReports.showCriticalException(e)
                 return
             self._glyph.beginUndoGroup()
             svgPath.draw(self._glyph.getPen())
             self._glyph.endUndoGroup()
             return
         if ext.lower() != "png":
             # convert
             img = QImage(path)
             data = QByteArray()
             buffer = QBuffer(data)
             buffer.open(QIODevice.WriteOnly)
             img.save(buffer, "PNG")
             # format
             data = bytearray(data)
             fileName = "%s.png" % os.path.splitext(fileName)[0]
         imageSet = self._glyph.font.images
         try:
             imageSet[fileName] = data
         except Exception as e:
             errorReports.showCriticalException(e)
             return
         image = self._glyph.instantiateImage()
         image.fileName = fileName
         self._glyph.image = image
         event.setAccepted(True)
     else:
         super().dropEvent(event)
Beispiel #3
0
    def test_transform(self):
        pen = RecordingPen()
        svg = SVGPath.fromstring(SVG_DATA,
                                 transform=(1.0, 0, 0, -1.0, 0, 1000))
        svg.draw(pen)

        assert pen.value == [
            ("moveTo", ((100.0, 900.0), )), ("lineTo", ((300.0, 900.0), )),
            ("lineTo", ((200.0, 700.0), )), ("lineTo", ((100.0, 900.0), )),
            ("closePath", ()), ("moveTo", ((100.0, 800.0), )),
            ("curveTo", ((100.0, 900.0), (250.0, 900.0), (250.0, 800.0))),
            ("curveTo", ((250.0, 700.0), (400.0, 700.0), (400.0, 800.0))),
            ("endPath", ())
        ]
Beispiel #4
0
    def test_transform(self):
        pen = RecordingPen()
        svg = SVGPath.fromstring(SVG_DATA,
                                 transform=(1.0, 0, 0, -1.0, 0, 1000))
        svg.draw(pen)

        assert pen.value == [
            ("moveTo", ((100.0, 900.0),)),
            ("lineTo", ((300.0, 900.0),)),
            ("lineTo", ((200.0, 700.0),)),
            ("lineTo", ((100.0, 900.0),)),
            ("closePath", ()),
            ("moveTo", ((100.0, 800.0),)),
            ("curveTo", ((100.0, 900.0),
                         (250.0, 900.0),
                         (250.0, 800.0))),
            ("curveTo", ((250.0, 700.0),
                         (400.0, 700.0),
                         (400.0, 800.0))),
            ("endPath", ())
        ]
Beispiel #5
0
    def potrace(self,
                rect,
                poargs=[],
                invert=True,
                pen_class=None,
                context=None):
        import skia
        from PIL import Image
        from pathlib import Path
        from subprocess import run
        from fontTools.svgLib import SVGPath

        pc, ctx = self._get_renderer_state(pen_class, context)

        img = pc.Precompose(self, rect, context=ctx)
        pilimg = Image.fromarray(
            img.convert(alphaType=skia.kUnpremul_AlphaType))
        binpo = Path("bin/potrace")
        if not binpo.exists():
            binpo = Path(__file__).parent.parent.parent / "bin/potrace"

        with tempfile.NamedTemporaryFile(prefix="coldtype_tmp",
                                         suffix=".bmp") as tmp_bmp:
            pilimg.save(tmp_bmp.name)
            rargs = [str(binpo), "-s"]
            if invert:
                rargs.append("--invert")
            rargs.extend([str(x) for x in poargs])
            rargs.extend(["-o", "-", "--", tmp_bmp.name])
            if False:
                print(">>>", " ".join(rargs))
            result = run(rargs, capture_output=True)
            t = Transform()
            t = t.scale(0.1, 0.1)
            svgp = SVGPath.fromstring(result.stdout, transform=t)
            dp = self.single_pen_class()
            svgp.draw(dp)
            return dp.f(0)
Beispiel #6
0
def svg2glif(svg, name, width=0, height=0, unicodes=None, transform=None,
             version=2):
    """ Convert an SVG outline to a UFO glyph with given 'name', advance
    'width' and 'height' (int), and 'unicodes' (list of int).
    Return the resulting string in GLIF format (default: version 2).
    If 'transform' is provided, apply a transformation matrix before the
    conversion (must be tuple of 6 floats, or a FontTools Transform object).
    """
    glyph = SimpleNamespace(width=width, height=height, unicodes=unicodes)
    outline = SVGPath.fromstring(svg, transform=transform)

    # writeGlyphToString takes a callable (usually a glyph's drawPoints
    # method) that accepts a PointPen, however SVGPath currently only has
    # a draw method that accepts a segment pen. We need to wrap the call
    # with a converter pen.
    def drawPoints(pointPen):
        pen = SegmentToPointPen(pointPen)
        outline.draw(pen)

    return writeGlyphToString(name,
                              glyphObject=glyph,
                              drawPointsFunc=drawPoints,
                              formatVersion=version)
Beispiel #7
0
    def test_fromstring(self):
        pen = RecordingPen()
        svg = SVGPath.fromstring(SVG_DATA)
        svg.draw(pen)

        assert pen.value == EXPECTED_PEN_COMMANDS
Beispiel #8
0
 def paste(self):
     isGlyphTab = self.isGlyphTab()
     widget = self.stackWidget.currentWidget()
     if isGlyphTab:
         glyphs = (widget.activeGlyph(),)
     else:
         selection = self.glyphCellView.selection()
         glyphs = widget.glyphsForIndexes(selection)
     clipboard = QApplication.clipboard()
     mimeData = clipboard.mimeData()
     if mimeData.hasFormat("application/x-trufont-glyph-data"):
         data = pickle.loads(mimeData.data("application/x-trufont-glyph-data"))
         if len(data) == len(glyphs):
             for pickled, glyph in zip(data, glyphs):
                 if isGlyphTab:
                     pasteGlyph = glyph.__class__()
                     pasteGlyph.deserialize(pickled)
                     # TODO: if we serialize selected state, we don't need
                     # to do this
                     pasteGlyph.selected = True
                     if (
                         len(pasteGlyph)
                         or len(pasteGlyph.components)
                         or len(pasteGlyph.anchors)
                     ):
                         glyph.beginUndoGroup()
                         glyph.holdNotifications()
                         count = len(glyph)
                         pen = glyph.getPointPen()
                         # contours, components
                         pasteGlyph.drawPoints(pen)
                         for contour in glyph[count:]:
                             contour.selected = True
                         # anchors
                         for anchor in pasteGlyph.anchors:
                             glyph.appendAnchor(dict(anchor))
                         # guidelines
                         for guideline in pasteGlyph.guidelines:
                             glyph.appendGuideline(dict(guideline))
                         glyph.releaseHeldNotifications()
                         glyph.endUndoGroup()
                 else:
                     glyph.deserialize(pickled)
         return
     if mimeData.hasFormat("image/svg+xml"):
         if len(glyphs) == 1:
             glyph = glyphs[0]
             try:
                 svgPath = SVGPath.fromstring(mimeData.data("image/svg+xml"))
             except Exception:
                 pass
             else:
                 glyph.beginUndoGroup()
                 if not isGlyphTab:
                     glyph.clear()
                 svgPath.draw(glyph.getPen())
                 glyph.endUndoGroup()
                 return
     if mimeData.hasText():
         if len(glyphs) == 1:
             glyph = glyphs[0]
             otherGlyph = glyph.__class__()
             text = mimeData.text()
             try:
                 readGlyphFromString(text, otherGlyph, otherGlyph.getPointPen())
             except Exception:
                 try:
                     svgPath = SVGPath.fromstring(text)
                     svgPath.draw(otherGlyph.getPen())
                 except Exception:
                     return
             glyph.beginUndoGroup()
             if not isGlyphTab:
                 glyph.clear()
             otherGlyph.drawPoints(glyph.getPointPen())
             glyph.endUndoGroup()
Beispiel #9
0
    def test_fromstring(self):
        pen = RecordingPen()
        svg = SVGPath.fromstring(SVG_DATA)
        svg.draw(pen)

        assert pen.value == EXPECTED_PEN_COMMANDS