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