-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
158 lines (126 loc) · 5.04 KB
/
render.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
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
import re
import copy
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
from reportlab.lib.pagesizes import letter, inch
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table, TableStyle
pdfmetrics.registerFont(TTFont('TheSansReg', 'resources/TheSans-Regular.ttf'))
pdfmetrics.registerFont(TTFont('TheSansRC', 'resources/TheSans-RegularCaps.ttf'))
pdfmetrics.registerFont(TTFont('TheSansBP', 'resources/TheSans-BoldPlain.ttf'))
pdfmetrics.registerFont(TTFont('TheSansBC', 'resources/TheSans-BoldCaps.ttf'))
base_style = getSampleStyleSheet().get('Normal')
base_style.textColor = colors.black
def _get_style(font, size, align):
style = copy.deepcopy(base_style)
style.fontName = font
style.fontSize = float(size)
style.leading = float(size) * (14.64 / 12.96)
style.alignment = align
style.spaceAfter = 0.15 * inch
return style
def _get_icon(width, height):
icon = Image('resources/icon_plottwist2.png')
icon.drawWidth = width
icon.drawHeight = height
return icon
def _rewrap(text, num_font=None):
text = re.sub(r'<\s*br\s*>', r'<br/>', text, flags=re.IGNORECASE)
if num_font is not None:
text = re.sub(r'(\d+)', r'<font name="{}">\1</font>'.format(num_font), text)
return text
def render_plot(data):
doc = SimpleDocTemplate(
'plot.pdf',
pagesize=letter,
leftMargin=0.293 * inch,
rightMargin=0.293 * inch,
topMargin=0.188 * inch,
bottomMargin=0)
icon = _get_icon(0.556 * inch, 0.233 * inch)
def layout(data):
if data['size']:
style = _get_style('TheSansBP', data['size'], TA_CENTER)
else:
style = _get_style('TheSansBP', 12.96, TA_CENTER)
inside_table = Table(
[
[Paragraph(_rewrap(data['text'], num_font='TheSansBC'), style)], [icon]
],
colWidths=(1.97 * inch),
rowHeights=(1.844 * inch, 0.233 * inch))
inside_table.setStyle(TableStyle([
# Global
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 0.15 * inch),
('RIGHTPADDING', (0, 0), (-1, -1), 0.15 * inch),
('TOPPADDING', (0, 0), (-1, -1), 0.117 * inch),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
# Logo
('VALIGN', (0, 1), (0, 1), 'BOTTOM'),
('BOTTOMPADDING', (0, 1), (0, 1), 0.063 * inch),
]))
return inside_table
cells = [layout(x) for x in data]
rows = [cells[x:x + 4] for x in xrange(0, len(cells), 4)]
outside_table = Table(rows)
outside_table.setStyle(TableStyle([
('LEFTPADDING', (0, 0), (-1, -1), 0),
('RIGHTPADDING', (0, 0), (-1, -1), 0),
('TOPPADDING', (0, 0), (-1, -1), 0),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
('GRID', (0, 0), (-1, -1), 0.02 * inch, (0.341, 0.341, 0.341))
]))
doc.build([outside_table])
def render_trope(data):
doc = SimpleDocTemplate(
'trope.pdf',
pagesize=letter,
leftMargin=0.418 * inch,
rightMargin=0.418 * inch,
topMargin=0.155 * inch,
bottomMargin=0)
icon = _get_icon(0.712 * inch, 0.299 * inch)
def layout(data):
line_top = Paragraph(
_rewrap(data['top'], num_font='TheSansBC'),
_get_style('TheSansBP', 13.92, TA_CENTER))
line_mid = Paragraph(
_rewrap(data['mid'], num_font='TheSansRC'),
_get_style('TheSansReg', 9.77, TA_JUSTIFY))
line_bot = Paragraph(
_rewrap(data['bot'], num_font='TheSansBC'),
_get_style('TheSansBP', 9.77, TA_JUSTIFY))
inside_table = Table(
[
[[line_top, line_mid, line_bot]], [icon]
],
colWidths=(2.5 * inch),
rowHeights=(2.333 * inch, 0.299 * inch))
inside_table.setStyle(TableStyle([
# Global
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 0.10 * inch),
('RIGHTPADDING', (0, 0), (-1, -1), 0.10 * inch),
('TOPPADDING', (0, 0), (-1, -1), 0.117 * inch),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
# Logo
('VALIGN', (0, 1), (0, 1), 'BOTTOM'),
('BOTTOMPADDING', (0, 1), (0, 1), 0.101 * inch),
]))
return inside_table
cells = [layout(x) for x in data]
rows = [cells[x:x + 3] for x in xrange(0, len(cells), 3)]
outside_table = Table(rows)
outside_table.setStyle(TableStyle([
('LEFTPADDING', (0, 0), (-1, -1), 0),
('RIGHTPADDING', (0, 0), (-1, -1), 0),
('TOPPADDING', (0, 0), (-1, -1), 0),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
('GRID', (0, 0), (-1, -1), 0.02 * inch, (0.341, 0.341, 0.341))
]))
doc.build([outside_table])