This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
forked from snerror/DnD-Card-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_monster.py
369 lines (329 loc) · 11.5 KB
/
card_monster.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import math
import yaml
import argparse
import pathlib
import itertools
from copy import copy
from abc import ABC
from reportlab.pdfbase.ttfonts import TTFError
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.platypus.flowables import Flowable, Spacer
from fonts import FreeFonts, AccurateFonts
from card import (
CardLayout,
SmallCard,
LargeCard,
EpicCard,
SuperEpicCard,
TemplateTooSmall,
Border,
LineDivider,
)
from card_item import ItemCardLayout
ASSET_DIR = pathlib.Path(__file__).parent.resolve() / "assets"
class MonsterCardLayout(CardLayout):
def __init__(
self,
armor_class,
max_hit_points,
speed,
strength,
dexterity,
constitution,
intelligence,
wisdom,
charisma,
challenge_rating,
experience_points,
source,
attributes,
abilities=None,
actions=None,
reactions=None,
legendary=None,
image_path=ASSET_DIR / "placeholder_monster.png",
**kwargs,
):
super().__init__(image_path=image_path, **kwargs)
self.armor_class = armor_class
self.max_hit_points = max_hit_points
self.speed = speed
self.strength = strength
self.dexterity = dexterity
self.constitution = constitution
self.intelligence = intelligence
self.wisdom = wisdom
self.charisma = charisma
self.attributes = attributes
self.abilities = abilities
self.actions = actions
self.reactions = reactions
self.legendary = legendary
self.challenge_rating = challenge_rating
self.experience_points = experience_points
self.source = source
def _draw_back(self, canvas):
super()._draw_back(canvas)
# Challenge
canvas.setFillColor("white")
self.fonts.set_font(canvas, "challenge")
canvas.drawString(
self.width + self.border_front[Border.LEFT],
self.challenge_bottom,
"Challenge {} ({} XP)".format(
self.challenge_rating, self.experience_points
),
)
### Source
self.fonts.set_font(canvas, "text")
canvas.drawString(*self.source_location, self.source)
def fill_frames(self, canvas):
# Title font scaling
custom_scale = (
min(1.0, 20 / len(self.title)) if isinstance(self, SmallCard) else 1.0
)
original_font_size = self.fonts.styles["title"][1] * self.fonts.FONT_SCALE
font_size = original_font_size * custom_scale
spacer_height = (original_font_size - font_size + 0.5 * mm) / 2
style = copy(self.fonts.paragraph_styles["title"])
style.fontSize = font_size
style.leading = font_size + spacer_height
# Title
self.elements.append(Spacer(1 * mm, spacer_height))
self.elements.append(
Paragraph(
self.title,
style,
)
)
# Subtitle
self.elements.append(
Paragraph(
self.subtitle,
self.fonts.paragraph_styles["subtitle"],
)
)
top_stats = [
[
Paragraph(
"<b>AC:</b> {}<br/><b>Speed:</b> {}".format(
self.armor_class, self.speed
),
self.fonts.paragraph_styles["text"],
),
Paragraph(
"<b>HP:</b> {}".format(self.max_hit_points),
self.fonts.paragraph_styles["text"],
),
]
]
ts = TableStyle(
[
("BOTTOMPADDING", (0, 0), (-1, -1), 0),
("TOPPADDING", (0, 0), (-1, -1), 0),
("LEFTPADDING", (0, 0), (-1, -1), 0),
("RIGHTPADDING", (0, 0), (-1, -1), 0),
("VALIGN", (0, 0), (-1, -1), "TOP"),
]
)
t = Table(top_stats, style=ts, spaceBefore=1 * mm)
self.elements.append(t)
# Modifiers
abilities = ["STR", "DEX", "CON", "INT", "WIS", "CHA"]
modifiers = [
self.strength,
self.dexterity,
self.constitution,
self.intelligence,
self.wisdom,
self.charisma,
]
# if modifiers are (int), e.g. 13, then automatically reformat as "13 (+1)"
modifiers = [
(m if isinstance(m, str) else "%d (%+d)" % (m, math.floor((m - 10) / 2)))
for m in modifiers
]
modifier_table_data = [
[
Paragraph(a, self.fonts.paragraph_styles["modifier_title"])
for a in abilities
],
[Paragraph(m, self.fonts.paragraph_styles["modifier"]) for m in modifiers],
]
t = Table(
modifier_table_data,
[self.BASE_WIDTH / (len(abilities) + 1)] * 5,
style=ts,
spaceBefore=1 * mm,
)
self.elements.append(t)
# Divider 1
line_width = self.frames[0]._width
self.elements.append(
LineDivider(
width=line_width,
xoffset=-self.TEXT_MARGIN,
fill_color=self.border_color,
)
)
# Attributes
# TODO: Handle list attributes
text = ""
for heading, body in (self.attributes or {}).items():
text += "<b>{}:</b> {}<br/>".format(heading, body)
self.elements.append(Paragraph(text, self.fonts.paragraph_styles["text"]))
# Abilities
for heading, body in (self.abilities or {}).items():
paragraph = Paragraph(
"<i><b>{}.</b></i> {}".format(heading, body),
self.fonts.paragraph_styles["text"],
)
self.elements.append(paragraph)
# Divider 2
self.elements.append(
LineDivider(
width=line_width,
xoffset=-self.TEXT_MARGIN,
fill_color=self.border_color,
)
)
# Actions
title = Paragraph("ACTIONS", self.fonts.paragraph_styles["action_title"])
first_action = True
for heading, body in (self.actions or {}).items():
paragraph = Paragraph(
"<i><b>{}.</b></i> {}".format(heading, body),
self.fonts.paragraph_styles["text"],
)
if first_action:
element = KeepTogether([title, paragraph])
first_action = False
else:
element = paragraph
self.elements.append(element)
if self.reactions is not None:
# Divider 3
self.elements.append(
LineDivider(
width=line_width,
xoffset=-self.TEXT_MARGIN,
fill_color=self.border_color,
)
)
title = Paragraph("REACTIONS", self.fonts.paragraph_styles["action_title"])
first_reaction = True
for heading, body in (self.reactions or {}).items():
paragraph = Paragraph(
"<i><b>{}.</b></i> {}".format(heading, body),
self.fonts.paragraph_styles["text"],
)
if first_reaction:
element = KeepTogether([title, paragraph])
first_reaction = False
else:
element = paragraph
self.elements.append(element)
if self.legendary is not None:
self.elements.append(
LineDivider(
width=line_width,
xoffset=-self.TEXT_MARGIN,
fill_color=self.border_color,
)
)
title = Paragraph(
"LEGENDARY ACTIONS", self.fonts.paragraph_styles["action_title"]
)
first_legendary = True
for entry in self.legendary or []:
if type(entry) == str:
paragraph = Paragraph(
entry,
self.fonts.paragraph_styles["text"],
)
elif type(entry) == dict:
paragraph = Paragraph(
"<i><b>{}.</b></i> {}".format(*list(entry.items())[0]),
self.fonts.paragraph_styles["legendary_action"],
)
else:
TypeError(
'Legendary action cannot be type "{}"'.format(type(entry))
)
if first_legendary:
element = KeepTogether([title, paragraph])
first_legendary = False
else:
element = paragraph
self.elements.append(element)
def _get_title_paragraph(self):
# Title font scaling
custom_scale = (
min(1.0, 20 / len(self.title)) if isinstance(self, SmallCard) else 1.0
)
original_font_size = self.fonts.styles["title"][1] * self.fonts.FONT_SCALE
font_size = original_font_size * custom_scale
style = copy(self.fonts.paragraph_styles["title"])
style.fontSize = font_size
style.leading = font_size
# Title
return Paragraph(
self.title,
style,
)
class MonsterCardSmall(SmallCard, MonsterCardLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.challenge_bottom = 5.5 * mm + self.bleed
self.source_location = (
self.width + self.border_back[Border.LEFT],
3 * mm + self.bleed,
)
class MonsterCardLarge(LargeCard, MonsterCardLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.challenge_bottom = (
self.border_back[Border.BOTTOM]
- self.bleed
- self.fonts.styles["challenge"][1]
) / 2 + self.bleed
self.source_location = (
self.width * 1.5 + self.STANDARD_BORDER / 2,
self.challenge_bottom,
)
class MonsterCardEpic(EpicCard, MonsterCardLarge):
pass
class MonsterCardSuperEpic(SuperEpicCard, MonsterCardLarge):
pass
class KeepTogether(Flowable):
def __init__(self, flowables):
self.flowables = flowables
self._available_height = None
self._available_width = None
def wrap(self, aW, aH):
self._available_width = aW
self._available_height = aH
height = 0
width = 0
for flowable in self.flowables:
w, h = flowable.wrap(aW, 0xFFFFFFFF)
height += flowable.getSpaceBefore()
height += h
height += flowable.getSpaceAfter()
if w > width:
width = w
return width, height
def drawOn(self, canvas, x, y, _sW=0):
y -= self.flowables[0].getSpaceBefore()
for flowable in self.flowables[::-1]:
y += flowable.getSpaceBefore()
width, height = flowable.wrap(self._available_width, self._available_height)
flowable.drawOn(canvas, x, y, _sW=_sW)
y += height
y += flowable.getSpaceAfter()
self._available_height -= (
flowable.getSpaceBefore() + height + flowable.getSpaceBefore()
)