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