def testRoundTrip(self): glyph = _Glyph() glyph.name = "a" glyph.unicodes = [0x0061] s1 = writeGlyphToString(glyph.name, glyph) glyph2 = _Glyph() readGlyphFromString(s1, glyph2) self.assertEqual(glyph.__dict__, glyph2.__dict__) s2 = writeGlyphToString(glyph2.name, glyph2) self.assertEqual(s1, s2)
def pyToGLIF(self, py): py = stripText(py) glyph = Glyph() exec(py, {"glyph" : glyph, "pointPen" : glyph}) glif = writeGlyphToString(glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=1) glif = "\n".join(glif.splitlines()[1:]) return glif
def svg2glif(svg_file, 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(svg_file, 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 pyToGLIF(self, py): py = stripText(py) glyph = Glyph() exec py in {"glyph" : glyph, "pointPen" : glyph} glif = writeGlyphToString(glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=1) glif = "\n".join(glif.splitlines()[1:]) return glif
def pyToGLIF(self, py): py = stripText(py) glyph = Glyph() exec(py, {"glyph": glyph, "pointPen": glyph}) glif = writeGlyphToString(glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=1, validate=True) # discard the first line containing the xml declaration return "\n".join(islice(glif.splitlines(), 1, None))
def svg2glif(path, name, width=0, height=0, unicodes=None, version=2, transform=(1, 0, 0, 1, 0, 0)): glyph = SimpleNamespace(width=width, height=height, unicodes=unicodes) def drawPoints(pointPen): pen = TransformPen(SegmentToPointPen(pointPen), transform) parse_path(path, pen) return writeGlyphToString(name, glyphObject=glyph, drawPointsFunc=drawPoints, formatVersion=version)
def svg2glif(svg, name, width=0, height=0, unicodes=None, transform=None, version=1): """ Convert an SVG outline to a UFO glyph, and assign the given 'name', advance 'width' and 'height' (int), 'unicodes' (list of int) to the generated glyph. 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). """ add_info = get_additional_info_from_path_id(svg) glyph = SVGOutline.fromstring(svg, transform=transform) glyph.name = add_info[0] glyph.width = width glyph.height = height glyph.unicodes = unicodes or [] # glif_string = writeGlyphToString(glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=version) # if add_info != "NONE": # tree = ET.fromstring(glif_string) # _adv = ET.Element("advance", width=add_info[2]) tree.insert(0, _adv) # if add_info[1] != "NONE": _uni = ET.Element("unicode", hex=add_info[1]) tree.insert(1, _uni) # xmlstr = ET.tostring(tree, encoding='utf8', method='xml').decode("utf-8") # else: # xmlstr = glif_string # return xmlstr
def testXmlDeclaration(self): s = writeGlyphToString("a", _Glyph()) self.assertTrue(s.startswith(_XML_DECLARATION.decode("utf-8")))
def _dumpToGLIF(self, glyphFormatVersion): glyph = self.naked() return writeGlyphToString(glyphName=glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=glyphFormatVersion)
def PlistFactory(glyph, font): return writeGlyphToString(glyph.name, glyph, glyph.drawPoints)
def _writeGlyphToString(self, glyphFormatVersion): glyph = self.naked() return writeGlyphToString(glyph.name, glyph, glyph.drawPoints, glyphFormatVersion)
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)