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