forked from donkirkby/donimoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
donimoes.py
328 lines (300 loc) · 12 KB
/
donimoes.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from argparse import ArgumentParser, FileType, ArgumentDefaultsHelpFormatter
from functools import partial
from pathlib import Path
from subprocess import call
from PIL import Image
from reportlab.lib import pagesizes
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.platypus.flowables import Spacer, KeepTogether, ListFlowable
from reportlab.lib.styles import getSampleStyleSheet, ListStyle
from reportlab.lib.units import inch
# noinspection PyUnresolvedReferences
from reportlab.rl_config import defaultPageSize
from diagram import draw_diagram, draw_fuji
from diagram_differ import diagram_to_image, DiagramDiffer
from domino_puzzle import Board
from dominosa import DominosaBoard
from footer import FooterCanvas
from book_parser import parse, Styles
from svg_diagram import SvgDiagram
PAGE_HEIGHT = defaultPageSize[1]
PAGE_WIDTH = defaultPageSize[0]
def parse_args():
default_markdown = str(Path(__file__).parent / 'docs' / 'rules.md')
# noinspection PyTypeChecker
parser = ArgumentParser(description='Convert rules markdown into a PDF.',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--booklet',
action='store_true',
help='Reorder pages for printing as a folded booklet.')
parser.add_argument('markdown',
type=FileType(),
nargs='?',
default=default_markdown,
help='markdown source file to convert')
return parser.parse_args()
class Diagram:
MAX_COLUMN_COUNT = 14
def __init__(self,
page_width,
page_height,
board_state,
show_path=False,
board_class=Board):
self.page_width = page_width
self.page_height = page_height
self.board_state = board_state
self.show_path = show_path
self.board_class = board_class
sections = board_state.split('\n---\n')
lines = sections[0].splitlines(True)
self.row_count = (len(lines) + 1)/2
self.col_count = max(map(len, lines))/2
self.cell_size = self.page_width / Diagram.MAX_COLUMN_COUNT
self.width = self.cell_size * self.col_count
self.height = self.cell_size * self.row_count
self.resize()
def resize(self):
pass
def build(self) -> SvgDiagram:
diagram = SvgDiagram(self.width, self.height)
t = diagram.turtle
t.up()
t.back(self.width/2)
t.left(90)
t.forward(self.height/2)
t.right(90)
t.down()
self.draw_background(t)
self.draw_foreground(t)
return diagram
def draw_foreground(self, t):
try:
draw_diagram(t,
self.board_state,
self.cell_size,
show_path=self.show_path,
board_class=self.board_class)
except Exception:
print(self.board_state)
raise
def draw_background(self, t):
""" Draw background items to appear behind the regular dominoes. """
pass
class FujisanDiagram(Diagram):
def draw_background(self, t):
draw_fuji(t, self.col_count, self.cell_size)
def resize(self):
self.height += self.cell_size
class DiagramWriter:
def __init__(self, target_folder: Path, images_folder: Path):
self.diagram_count = 0
self.target_folder = target_folder
self.images_folder = images_folder
self.diagram_differ = DiagramDiffer()
self.diagram_differ.tolerance = 10
def add_diagram(self, diagram: Diagram) -> Path:
self.diagram_count += 1
svg_diagram = diagram.build()
image = diagram_to_image(svg_diagram)
file_name = f'diagram{self.diagram_count}.png'
target_path = self.images_folder / file_name
relative_path = target_path.relative_to(self.target_folder)
try:
old_image = Image.open(target_path)
if self.diagram_differ.compare_pngs(old_image, image) is None:
return relative_path
except IOError:
pass
with target_path.open('wb') as f:
image.save(f, 'png')
return relative_path
def main():
args = parse_args()
markdown_path = Path(args.markdown.name)
rules_stem = markdown_path.stem
pdf_stem = 'donimoes' if rules_stem == 'rules' else rules_stem
source_path = Path(__file__).parent
pdf_path = source_path / 'docs' / (pdf_stem + '.pdf')
merged_path = pdf_path.parent / (rules_stem + '.md')
images_path = pdf_path.parent / 'images' / rules_stem
images_path.mkdir(parents=True, exist_ok=True)
fonts_path = source_path / 'fonts'
fredoka_file = fonts_path / 'Fredoka_One' / 'FredokaOne-Regular.ttf'
raleway_file = fonts_path / 'Raleway' / 'static' / 'Raleway-Regular.ttf'
pdfmetrics.registerFont(TTFont("Fredoka", fredoka_file))
pdfmetrics.registerFont(TTFont("Raleway", raleway_file))
with args.markdown:
states = parse(args.markdown.read())
diagram_writer = DiagramWriter(pdf_path.parent, images_path)
if args.booklet:
page_size = (4.25*inch, 6.875*inch)
vertical_margin = 0.25*inch
side_margin = 0.5*inch
else:
page_size = pagesizes.letter
vertical_margin = 0.625*inch
side_margin = inch
doc = SimpleDocTemplate(str(pdf_path),
author='Don Kirkby',
pagesize=page_size,
leftMargin=side_margin,
rightMargin=side_margin,
topMargin=vertical_margin,
bottomMargin=vertical_margin)
styles = getSampleStyleSheet()
for style in styles.byName.values():
if hasattr(style, 'fontSize'):
if style.name.startswith('Heading'):
scale = 1.5
style.fontName = 'Fredoka'
else:
scale = 2
style.fontName = 'Raleway'
if False and args.booklet:
style.fontSize *= scale
style.leading *= scale
paragraph_style = styles[Styles.Normal]
numbered_list_style = ListStyle('default_list',
bulletFontName='Raleway',
bulletFontSize=paragraph_style.fontSize,
leftIndent=paragraph_style.fontSize*1.5,
bulletFormat='%s.')
bulleted_list_style = ListStyle('default_list',
bulletFontName='Raleway',
bulletFontSize=paragraph_style.fontSize,
leftIndent=paragraph_style.fontSize*1.5)
story = []
group = []
bulleted = []
headings = []
first_bullet = None
image_width = 800
image_height = 600
for state in states:
if state.style == Styles.Metadata:
doc.title = state.text
continue
elif state.style == Styles.Diagram:
if 'Fujisan' in headings or 'Fujisan Problems' in headings:
flowable = FujisanDiagram(doc.width,
doc.height,
state.text).build().to_reportlab()
state.image_path = diagram_writer.add_diagram(FujisanDiagram(
image_width,
image_height,
state.text))
elif 'Dominosa' in headings:
flowable = Diagram(
doc.width,
doc.height,
state.text,
board_class=DominosaBoard).build().to_reportlab()
state.image_path = diagram_writer.add_diagram(Diagram(
image_width,
image_height,
state.text,
board_class=DominosaBoard))
else:
if 'Mountains and Valleys' in headings:
diagram_width = (len(state.text.splitlines()[0]) + 1)//2
show_path = diagram_width == 6
else:
show_path = False
flowable = Diagram(doc.width,
doc.height,
state.text,
show_path).build().to_reportlab()
state.image_path = diagram_writer.add_diagram(Diagram(
image_width,
image_height,
state.text,
show_path))
else:
flowable = Paragraph(state.text,
styles[state.style])
if state.style.startswith(Styles.Heading):
if bulleted:
create_list_flowable(bulleted,
group,
story,
first_bullet,
bulleted_list_style,
numbered_list_style)
group = []
bulleted = []
first_bullet = None
group.append(flowable)
heading_level = int(state.style[-1])
headings = headings[:heading_level]
while len(headings) < heading_level:
headings.append(None)
headings[heading_level - 1] = state.text
elif state.bullet:
bulleted.append(flowable)
first_bullet = first_bullet or state.bullet
else:
if bulleted:
create_list_flowable(bulleted,
group,
story,
first_bullet,
bulleted_list_style,
numbered_list_style)
group = []
bulleted = []
first_bullet = None
story.append(Spacer(1, 0.055*inch))
if not group:
story.append(flowable)
else:
group.append(flowable)
story.append(KeepTogether(group))
group = []
story.append(Spacer(1, 0.055*inch))
if bulleted:
create_list_flowable(bulleted,
group,
story,
first_bullet,
bulleted_list_style,
numbered_list_style)
doc.build(story, canvasmaker=partial(FooterCanvas,
font_name='Raleway',
is_booklet=args.booklet))
with merged_path.open('w') as merged_file:
for state in states:
state.write_markdown(merged_file)
call(["evince", pdf_path])
def create_list_flowable(bulleted,
group,
story,
first_bullet,
bulleted_list_style,
numbered_list_style):
if first_bullet == '*':
bullet_type = 'bullet'
first_bullet = None
list_style = bulleted_list_style
else:
bullet_type = '1'
list_style = numbered_list_style
group.append(ListFlowable(bulleted[:1],
style=list_style,
bulletType=bullet_type,
start=first_bullet))
story.append(KeepTogether(group))
bulleted = bulleted[1:]
if bulleted:
if first_bullet is not None:
next_bullet = int(first_bullet) + 1
else:
next_bullet = first_bullet
story.append(ListFlowable(bulleted,
style=list_style,
bulletType=bullet_type,
start=next_bullet))
if __name__ == '__main__':
main()