-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrenderer.py
63 lines (50 loc) · 2.06 KB
/
renderer.py
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
import re
class Renderer:
def text_size(self, text, elem):
""" Return a tuple (width, height) of the size the text would be if
rendered.
"""
raise NotImplementedError
def render_box(self, box):
""" Render onto the internal canvas of the renderer the contents of the given box
"""
raise NotImplementedError
import pygame
class PygameRenderer(Renderer):
def __init__(self, width, height):
self.box_counter = 0
self.boxes = []
self.width = width
self.height = height
self.iwidth = width
self.iheight = height
pygame.font.init()
self.font = pygame.font.SysFont('Times New Roman', 16)
def text_size(self, text, elem):
# Todo: make this actually determine the font to use from the CSS of
# the element the text is in. Also honor white-space property
#compress whitespace:
#text = re.sub('\s+', ' ', text)
(width, height) = self.font.size(text)
return (width, self.font.get_linesize())
def renderAll(self):
page = pygame.Surface((self.width, self.height))
page.fill((255,255,255))
for box, img in self.boxes:
page.blit(img, (box.x, box.y))
pygame.draw.rect(page, (0,0,0),
pygame.Rect(0,0, self.iwidth, self.iheight),
1)
return page
def render_box(self, box):
if box.__class__.__name__ == 'TextBox':
#print 'about to render box %d: "%s": %d,%d' % (self.box_counter, box.text, box.x, box.y)
box_render = self.font.render(box.text, True, (0,0,0))#, (255,255,255))
#pygame.image.save(box_render, 'box_dumps/box%d.bmp' % self.box_counter)
self.box_counter += 1
self.width = max(self.width, box.x + box.width)
self.height = max(self.height, box.y + box.height)
self.boxes.append((box, box_render))
else:
for child in box.childBoxes:
self.render_box(child)