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