1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3from fontTools.pens.basePen import BasePen 4from reportlab.graphics.shapes import Path 5 6 7__all__ = ["ReportLabPen"] 8 9 10class ReportLabPen(BasePen): 11 12 """A pen for drawing onto a reportlab.graphics.shapes.Path object.""" 13 14 def __init__(self, glyphSet, path=None): 15 BasePen.__init__(self, glyphSet) 16 if path is None: 17 path = Path() 18 self.path = path 19 20 def _moveTo(self, p): 21 (x,y) = p 22 self.path.moveTo(x,y) 23 24 def _lineTo(self, p): 25 (x,y) = p 26 self.path.lineTo(x,y) 27 28 def _curveToOne(self, p1, p2, p3): 29 (x1,y1) = p1 30 (x2,y2) = p2 31 (x3,y3) = p3 32 self.path.curveTo(x1, y1, x2, y2, x3, y3) 33 34 def _closePath(self): 35 self.path.closePath() 36 37 38if __name__=="__main__": 39 import sys 40 if len(sys.argv) < 3: 41 print("Usage: reportLabPen.py <OTF/TTF font> <glyphname> [<image file to create>]") 42 print(" If no image file name is created, by default <glyphname>.png is created.") 43 print(" example: reportLabPen.py Arial.TTF R test.png") 44 print(" (The file format will be PNG, regardless of the image file name supplied)") 45 sys.exit(0) 46 47 from fontTools.ttLib import TTFont 48 from reportlab.lib import colors 49 50 path = sys.argv[1] 51 glyphName = sys.argv[2] 52 if (len(sys.argv) > 3): 53 imageFile = sys.argv[3] 54 else: 55 imageFile = "%s.png" % glyphName 56 57 font = TTFont(path) # it would work just as well with fontTools.t1Lib.T1Font 58 gs = font.getGlyphSet() 59 pen = ReportLabPen(gs, Path(fillColor=colors.red, strokeWidth=5)) 60 g = gs[glyphName] 61 g.draw(pen) 62 63 w, h = g.width, 1000 64 from reportlab.graphics import renderPM 65 from reportlab.graphics.shapes import Group, Drawing, scale 66 67 # Everything is wrapped in a group to allow transformations. 68 g = Group(pen.path) 69 g.translate(0, 200) 70 g.scale(0.3, 0.3) 71 72 d = Drawing(w, h) 73 d.add(g) 74 75 renderPM.drawToFile(d, imageFile, fmt="PNG") 76