about summary refs log tree commit diff homepage
path: root/utils/hacks/TreeGraphs/Graphics/Canvas/__init__.py
blob: 664ac66898a9aa653d09bda038718e3ebfc1684a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# ===-- __init__.py -------------------------------------------------------===##
# 
#                      The KLEE Symbolic Virtual Machine
# 
#  This file is distributed under the University of Illinois Open Source
#  License. See LICENSE.TXT for details.
# 
# ===----------------------------------------------------------------------===##


from __future__ import division

###

import math, os, random

from Graphics.Geometry import vec2

from reportlab.pdfgen import canvas
#from reportlab.graphics import shapes
from reportlab.pdfbase import pdfmetrics

"""
class Canvas:
	def startDrawing(self, (w,h)):
	def endDrawing(self):
		
	def getAspect(self):
		
	def setColor(self, r, g, b):
	def setLineWidth(self, width):
	def drawOutlineBox(self, x0, y0, x1, y1):
	def drawFilledBox(self, x0, y0, x1, y1):
	def drawFilledPolygon(self, pts):
	def drawOutlineCircle(self, x, y, r):
	def startDrawPoints(self):
	def endDrawPoints(self):
	def drawPoint(self, x, y):
	def setPointSize(self, size):

	def drawLine(self, a, b):
	def drawLines(self, ptPairs):
	def drawLineStrip(self, pts):

	def pushTransform(self):
	def popTransform(self):
	def translate(self, x, y):
	def scale(self, x, y):
	def setFontSize(self, size):
	def drawString(self, (x, y), text):
"""

class BaseCanvas:
	def drawPoints(self, pts):
		self.startDrawPoints()
		for pt in pts:
			self.drawPoint(pt)
		self.endDrawPoints()
		
	def drawStringCentered(self, boxLL, boxUR, text):
		ll,ur = self.getStringBBox(text)
		stringSize = vec2.sub(ur,ll)
		boxSize = vec2.sub(boxUR,boxLL)
		deltaSize = vec2.sub(boxSize, stringSize)
		halfDeltaSize = vec2.mulN(deltaSize, .5)
		
		self.drawString(vec2.add(boxLL,halfDeltaSize), text)

	def getStringSize(self, string):
		ll,ur = self.getStringBBox(string)
		return vec2.sub(ur,ll)
	
class PdfCanvas(BaseCanvas):
	def __init__(self, name, basePos=(300,400), baseScale=(250,250), pageSize=None):
		self._font = 'Times-Roman'
		self.c = canvas.Canvas(name, pagesize=pageSize)
		self.pointSize = 1
		self.scaleX = self.scaleY = 1
		self.state = []
		self.basePos = tuple(basePos)
		self.baseScale = tuple(baseScale)
		self.lastFontSizeSet = None
		
		self.kLineScaleFactor = 1.95
		
	def getAspect(self):
		return 1.0,1.0
		
	def startDrawing(self):
		self.pointSize = 1
		self.scaleX = self.scaleY = 1
		
		self.translate((self.basePos[0] + self.baseScale[0], self.basePos[1] + self.baseScale[1]))
		self.scale(self.baseScale)

		self.setColor(0,0,0)
		self.setLineWidth(1)
		self.setPointSize(1)

	def finishPage(self):
		self.c.showPage()

		self.pointSize = 1
		self.scaleX = self.scaleY = 1
		
		self.translate((self.basePos[0] + self.baseScale[0], self.basePos[1] + self.baseScale[1]))
		self.scale(self.baseScale)

		self.setColor(0,0,0)
		self.setLineWidth(1)
		self.setPointSize(1)
		
		if self.lastFontSizeSet is not None:
			self.setFontSize(self.lastFontSizeSet)
			
	def endDrawing(self):
		self.c.showPage()
		self.c.save()
		
	def setColor(self, r, g, b):
		self.c.setStrokeColorRGB(r,g,b)
		self.c.setFillColorRGB(r,g,b)
	def setLineWidth(self, width):
		avgScale = (self.scaleX+self.scaleY)/2
		self.c.setLineWidth(width/(self.kLineScaleFactor*avgScale))
	def setPointSize(self, size):
		avgScale = (self.scaleX+self.scaleY)/2
		self.pointSize = size/(4*avgScale)

	def drawOutlineBox(self, (x0, y0), (x1, y1)):
		self.c.rect(x0, y0, x1-x0, y1-y0, stroke=1, fill=0)
	def drawFilledBox(self, (x0, y0), (x1, y1)):
		self.c.rect(x0, y0, x1-x0, y1-y0, stroke=0, fill=1)
	def drawOutlineCircle(self, (x, y), r):
		self.c.circle(x, y, r, stroke=1, fill=0)
	def drawFilledCircle(self, (x, y), r):
		self.c.circle(x, y, r, stroke=0, fill=1)
	def drawFilledPolygon(self, pts):
		p = self.c.beginPath()
		p.moveTo(*pts[-1])
		for x,y in pts:
			p.lineTo(x,y)
		self.c.drawPath(p, fill=1, stroke=0)
	def drawOutlinePolygon(self, pts):
		p = self.c.beginPath()
		p.moveTo(* pts[0])
		for x,y in pts[1:]:
			p.lineTo(x,y)
		p.close()
		self.c.drawPath(p, fill=0, stroke=1)
	def startDrawPoints(self):
		pass
	def endDrawPoints(self):
		pass
	def drawPoint(self, (x, y)):
		self.c.circle(x, y, self.pointSize, stroke=0, fill=1)

	def drawLine(self, a, b):
		self.drawLines([(a,b)])
	def drawLines(self, ptPairs):
		p = self.c.beginPath()
		for a,b in ptPairs:
			p.moveTo(a[0],a[1])
			p.lineTo(b[0],b[1])
		self.c.drawPath(p)
	def drawLineStrip(self, pts):
		p = self.c.beginPath()
		p.moveTo(pts[0][0],pts[0][1])
		for pt in pts[1:]:
			p.lineTo(pt[0],pt[1])
		self.c.drawPath(p)
	def drawBezier(self, (p0,p1,p2,p3)):
		self.c.bezier(*(p0+p1+p2+p3))
		
	def pushTransform(self):
		self.state.append( (self.scaleX,self.scaleY) )
		self.c.saveState()
	def popTransform(self):
		self.c.restoreState()
		self.scaleX,self.scaleY = self.state.pop()
	def translate(self, (x, y)):
		self.c.translate(x, y)
	def rotate(self, angle):
		self.c.rotate(angle*180/math.pi)
	def scale(self, (x, y)):
		self.scaleX *= x
		self.scaleY *= y
		self.c.scale(x, y)

	def setFont(self, fontName):
		self._font = {"Symbol":"Symbol",
					 "Times":"Times-Roman"}.get(fontName,fontName)
	def setFontSize(self, size):
		self.lastFontSizeSet = size
		avgScale = (self.scaleX+self.scaleY)/2
		self.fontSize = size/(2*avgScale)
		self.c.setFont(self._font, self.fontSize)
#		self.c.setFont("Times-Roman", size/(2*avgScale))
	def drawString(self, (x, y), text):
		self.c.drawString(x, y, text)
		
	def drawOutlineString(self, (x,y), text):
		t = self.c.beginText(x, y)
		t.setTextRenderMode(1)
		t.textLine(text)
		t.setTextRenderMode(0)
		self.c.drawText(t)

	def getStringBBox(self, text):
		font = pdfmetrics.getFont(self._font)
		width = pdfmetrics.stringWidth(text, self._font, self.fontSize)
		ll = (0,0)
		ur = (width, (1.0 - font.face.ascent/2048.)*self.fontSize)
		ur = (width, (1.0 - font.face.ascent/2048.)*self.fontSize)
		return ll,ur