forked from ep1cman/DnD-Card-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardGenerator.py
820 lines (695 loc) · 30.5 KB
/
CardGenerator.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
import os
import math
import yaml
import sys
import argparse
from enum import Enum, IntEnum
from abc import ABC
from PIL import Image
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.ttfonts import TTFError
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
from reportlab.platypus import Frame, Paragraph, Table, TableStyle
from reportlab.platypus.flowables import Flowable
from reportlab.lib.styles import ParagraphStyle, StyleSheet1
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.fonts import addMapping
from reportlab.platypus.doctemplate import LayoutError
from svglib.svglib import svg2rlg
# Returns the best orientation for the given image aspect ration
def best_orientation(image_path, card_width, card_height):
image = Image.open(image_path)
image_width, image_height = image.size
if (image_width > image_height) == (card_width> card_height):
return Orientation.NORMAL
else:
return Orientation.TURN90
#TODO: Clean up the font object, it seems a bit crude
#TODO: Also manage colours
class Fonts(object):
styles = {}
#Scaling factor between the font size and its actual height in mm
FONT_SCALE = None
def __init__(self):
self.paragraph_styles = StyleSheet1()
class FreeFonts(Fonts):
FONT_SCALE = 1.41
styles = {
"title": ('Universal Serif', 2.5*mm, "black"),
"subtitle": ("ScalySans", 1.5*mm, "white"),
"challenge": ('Universal Serif', 2.25*mm, "black"),
"heading": ("ScalySansBold", 1.5*mm, "black"),
"text": ("ScalySans", 1.5*mm, "black"),
"artist": ("ScalySans", 1.25*mm, "white"),
"modifier_title": ( "Universal Serif", 1.5*mm, "black"),
}
def set_font(self, canvas, section):
canvas.setFont(self.styles[section][0],
self.styles[section][1]*self.FONT_SCALE)
return self.styles[section][1]
def __init__(self):
super().__init__()
pdfmetrics.registerFont(TTFont('Universal Serif',
os.path.join('fonts',
'Universal Serif.ttf')))
pdfmetrics.registerFont(TTFont('ScalySans',
os.path.join('fonts',
'ScalySans.ttf')))
pdfmetrics.registerFont(TTFont('ScalySansItalic',
os.path.join('fonts',
'ScalySans-Italic.ttf')))
pdfmetrics.registerFont(TTFont('ScalySansBold',
os.path.join('fonts',
'ScalySans-Bold.ttf')))
pdfmetrics.registerFont(TTFont('ScalySansBoldItalic',
os.path.join('fonts',
'ScalySans-BoldItalic.ttf')))
addMapping('ScalySans', 0, 0, 'ScalySans') #normal
addMapping('ScalySans', 0, 1, 'ScalySansItalic') #italic
addMapping('ScalySans', 1, 0, 'ScalySansBold') #bold
addMapping('ScalySans', 1, 1, 'ScalySansBoldItalic') #italic and bold
self.paragraph_styles.add(ParagraphStyle(
name='text',
fontName=self.styles["text"][0],
fontSize=self.styles["text"][1]*self.FONT_SCALE,
leading=self.styles["text"][1]*self.FONT_SCALE + 0.5*mm,
spaceBefore=1*mm
)),
self.paragraph_styles.add(ParagraphStyle(
name='modifier',
fontName=self.styles["text"][0],
fontSize=self.styles["text"][1]*self.FONT_SCALE,
leading=self.styles["text"][1]*self.FONT_SCALE + 0.5*mm,
alignment=TA_CENTER,
))
self.paragraph_styles.add(ParagraphStyle(
name='action_title',
fontName=self.styles["modifier_title"][0],
fontSize=self.styles["modifier_title"][1]*self.FONT_SCALE,
leading=self.styles["modifier_title"][1]*self.FONT_SCALE + 0.5*mm,
spaceBefore=1*mm
))
self.paragraph_styles.add(ParagraphStyle(
name='modifier_title',
fontName=self.styles["modifier_title"][0],
fontSize=self.styles["modifier_title"][1]*self.FONT_SCALE,
leading=self.styles["modifier_title"][1]*self.FONT_SCALE + 0.5*mm,
alignment=TA_CENTER,
))
class AccurateFonts(Fonts):
FONT_SCALE = 1.41
styles = {
"title": ('ModestoExpanded', 2.5*mm, "black"),
"subtitle": ("ModestoTextLight", 1.5*mm, "white"),
"challenge": ('ModestoExpanded', 2.25*mm, "black"),
"heading": ("ModestoTextBold", 1.5*mm, "black"),
"text": ("ModestoTextLight", 1.5*mm, "black"),
"artist": ("ModestoTextLight", 1.25*mm, "white"),
"modifier_title": ( "ModestoExpanded", 1.5*mm, "black"),
}
def set_font(self, canvas, section):
canvas.setFont(self.styles[section][0],
self.styles[section][1]*self.FONT_SCALE)
return self.styles[section][1]
def __init__(self):
super().__init__()
pdfmetrics.registerFont(TTFont('ModestoExpanded',
os.path.join('fonts',
'ModestoExpanded-Regular.ttf')))
pdfmetrics.registerFont(TTFont('ModestoTextLight',
os.path.join('fonts',
'ModestoText-Light.ttf')))
pdfmetrics.registerFont(TTFont('ModestoTextLightItalic',
os.path.join('fonts',
'ModestoText-LightItalic.ttf')))
pdfmetrics.registerFont(TTFont('ModestoTextBold',
os.path.join('fonts',
'ModestoText-Bold.ttf')))
pdfmetrics.registerFont(TTFont('ModestoTextBoldItalic',
os.path.join('fonts',
'ModestoText-BoldItalic.ttf')))
addMapping('ModestoTextLight', 0, 0, 'ModestoTextLight') #normal
addMapping('ModestoTextLight', 0, 1, 'ModestoTextLightItalic') #italic
addMapping('ModestoTextLight', 1, 0, 'ModestoTextBold') #bold
addMapping('ModestoTextLight', 1, 1, 'ModestoTextBoldItalic') #italic and bold
self.paragraph_styles.add(ParagraphStyle(
name='text',
fontName=self.styles["text"][0],
fontSize=self.styles["text"][1]*self.FONT_SCALE,
leading=self.styles["text"][1]*self.FONT_SCALE + 0.5*mm,
spaceBefore=1*mm
)),
self.paragraph_styles.add(ParagraphStyle(
name='modifier',
fontName=self.styles["text"][0],
fontSize=self.styles["text"][1]*self.FONT_SCALE,
leading=self.styles["text"][1]*self.FONT_SCALE + 0.5*mm,
alignment=TA_CENTER,
))
self.paragraph_styles.add(ParagraphStyle(
name='action_title',
fontName=self.styles["modifier_title"][0],
fontSize=self.styles["modifier_title"][1]*self.FONT_SCALE,
leading=self.styles["modifier_title"][1]*self.FONT_SCALE + 0.5*mm,
spaceBefore=1*mm
))
self.paragraph_styles.add(ParagraphStyle(
name='modifier_title',
fontName=self.styles["modifier_title"][0],
fontSize=self.styles["modifier_title"][1]*self.FONT_SCALE,
leading=self.styles["modifier_title"][1]*self.FONT_SCALE + 0.5*mm,
alignment=TA_CENTER,
))
# Draws a line across the frame, unless it is at the top of the frame, in which
# case nothing is drawn
class LineDivider(Flowable):
def __init__(self, xoffset=0, width=None, fill_color="red",
line_height=0.25*mm, spacing=1*mm):
self.xoffset = xoffset
self.width = width
self.fill_color = fill_color
self.spacing = spacing
self.line_height = line_height
self.height = self.line_height + self.spacing
def _at_top(self):
at_top = False
frame = getattr(self,'_frame',None)
if frame:
at_top = getattr(frame,'_atTop',None)
return at_top
def wrap(self, *args):
if self._at_top():
return (0,0)
else:
return (self.width, self.height)
def draw(self):
if not self._at_top():
canvas = self.canv
canvas.setFillColor(self.fill_color)
canvas.setFillColor("red")
canvas.rect(self.xoffset, 0,
self.width, self.line_height, stroke=0, fill=1)
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()
class Orientation(Enum):
NORMAL = 1
TURN90 = 2
class Border(IntEnum):
LEFT = 0
RIGHT = 1
BOTTOM = 2
TOP = 3
class Modifier(object):
@classmethod
def from_ability_score(cls, value):
if value < 1 or value > 30:
raise ValueError("Ability scores must be between 1 and 30 inclusive")
return cls(math.floor((value-10)/2))
def __init__(self, value):
value = int(value)
if value < -5 or value > 10:
raise ValueError("Ability score modifiers must be between -5 and +10 inclusive")
self.value = value
def __str__(self):
if self.value <= 0:
return "+{}".format(self.value)
else:
return "-{}".format(self.value)
class TemplateTooSmall(Exception):
pass
class CardLayout(ABC):
CARD_CORNER_DIAMETER = 3*mm
BACKGROUND_CORNER_DIAMETER = 2*mm
LOGO_WIDTH = 42*mm
STANDARD_BORDER = 2.5*mm
TEXT_MARGIN = 2*mm
BASE_WIDTH = 63*mm
BASE_HEIGHT = 89*mm
BORDER_COLOR = "red"
TITLE_BAR_HEIGHT = 4.8*mm
# These must be set by sub classes
WIDTH = None
HEIGHT = None
BORDER_FRONT = ()
BORDER_BACK = ()
def __init__(self, title, subtitle, artist, image_path,
background="background.png", border_color="red",
fonts=FreeFonts()):
self.frames = []
self.FRONT_MARGINS = tuple([x + 1*mm for x in self.BORDER_FRONT])
self.title = title
self.subtitle = subtitle
self.artist = artist
self.fonts = fonts
self.background_image_path = background
self.front_image_path = os.path.abspath(image_path)
# Figure out front orientation
self.front_orientation = best_orientation(self.front_image_path,
self.WIDTH, self.HEIGHT)
self.elements = []
def set_size(self, canvas):
canvas.setPageSize((self.WIDTH*2, self.HEIGHT))
def draw(self, canvas):
self.set_size(canvas)
self._draw_front(canvas)
self._draw_back(canvas)
self.fill_frames(canvas)
self._draw_frames(canvas)
def fill_frames(self, canvas):
pass
def _draw_frames(self, canvas):
frames = iter(self.frames)
try:
current_frame = next(frames)
except StopIteration:
return
# Draw the elements
while len(self.elements) > 0:
element = self.elements[0]
try:
if type(element) == LineDivider:
available_height = current_frame._y - current_frame._y1p - self.elements[1].getSpaceBefore()
available_width = current_frame._getAvailableWidth()
_, line_height = element.wrap(available_width, 0xffffffff)
_, next_height = self.elements[1].wrap(available_width, 0xffffffff)
# Dont draw it if it will be the last thing on the frame
if available_height < line_height + next_height:
try:
current_frame = next(frames)
except StopIteration:
raise LayoutError()
del self.elements[0]
continue
result = current_frame.add(element, canvas)
#current_frame.drawBoundary(canvas)
# Could not draw into current frame
if result == 0:
raise LayoutError()
del self.elements[0]
# Frame is full, get next frame
except LayoutError:
try:
current_frame = next(frames)
# No more frames
except StopIteration:
break
# If there are undrawn elements, raise an error
if len(self.elements) > 0:
raise TemplateTooSmall("Template too small")
def _draw_front(self, canvas):
canvas.saveState()
# Set card orientation
if self.front_orientation == Orientation.TURN90:
canvas.rotate(90)
canvas.translate(0, -self.WIDTH)
width = self.HEIGHT
height = self.WIDTH
else:
width = self.WIDTH
height = self.HEIGHT
# Draw red border
self._draw_single_border(canvas, 0, width, height)
# Parchment background
self._draw_single_background(canvas, 0, self.BORDER_FRONT, width, height)
# D&D logo
dnd_logo = svg2rlg("logo.svg")
if dnd_logo is not None:
factor = self.LOGO_WIDTH/dnd_logo.width
dnd_logo.width *= factor
dnd_logo.height *= factor
dnd_logo.scale(factor, factor)
logo_margin = (self.BORDER_FRONT[Border.TOP]-dnd_logo.height)/2
renderPDF.draw(dnd_logo, canvas,
(width-self.LOGO_WIDTH)/2,
height - self.BORDER_FRONT[Border.TOP] + logo_margin,
)
# Titles
canvas.setFillColor("black")
title_height = self.fonts.set_font(canvas, "title")
canvas.drawCentredString(width * 0.5, self.FRONT_MARGINS[Border.BOTTOM], self.title.upper())
# Artist
if self.artist:
canvas.setFillColor("white")
artist_font_height = self.fonts.set_font(canvas, "artist")
canvas.drawCentredString(width/2,
self.BORDER_FRONT[Border.BOTTOM] - artist_font_height - 1*mm,
"Artist: {}".format(self.artist))
# Image
image_bottom = self.FRONT_MARGINS[Border.BOTTOM] + title_height + 1*mm
canvas.drawImage(self.front_image_path,
self.FRONT_MARGINS[Border.LEFT], image_bottom,
width=width \
- self.FRONT_MARGINS[Border.LEFT] \
- self.FRONT_MARGINS[Border.RIGHT],
height=height \
- image_bottom \
- self.FRONT_MARGINS[Border.TOP],
preserveAspectRatio=True, mask='auto')
canvas.restoreState()
def _draw_back(self, canvas):
# Draw red border
self._draw_single_border(canvas, self.WIDTH, self.WIDTH, self.HEIGHT)
# Parchment background
self._draw_single_background(canvas, self.WIDTH,
self.BORDER_BACK,
self.WIDTH,
self.HEIGHT)
# Title
canvas.setFillColor("black")
title_font_height = self.fonts.set_font(canvas, "title")
title_line_bottom = self.HEIGHT - self.BORDER_BACK[Border.TOP] - self.TITLE_BAR_HEIGHT
title_bottom = title_line_bottom + (self.TITLE_BAR_HEIGHT-title_font_height)/2
canvas.drawCentredString(self.WIDTH + self.BASE_WIDTH/2,
title_bottom, self.title.upper())
# Subtitle
subtitle_line_bottom = title_line_bottom - self.STANDARD_BORDER
canvas.setFillColor(self.BORDER_COLOR)
canvas.rect(self.WIDTH, subtitle_line_bottom, self.BASE_WIDTH,
self.STANDARD_BORDER, stroke=0, fill=1)
canvas.setFillColor("white")
subtitle_font_height = self.fonts.set_font(canvas, "subtitle")
subtitle_bottom = subtitle_line_bottom + (self.STANDARD_BORDER-subtitle_font_height)/2
canvas.drawCentredString(self.WIDTH + self.BASE_WIDTH/2, subtitle_bottom, self.subtitle)
def _draw_single_border(self, canvas, x, width, height):
canvas.saveState()
if type(self.BORDER_COLOR) == str:
canvas.setFillColor(self.BORDER_COLOR)
else:
canvas.setFillColorRGB(self.BORDER_COLOR[0]/255,
self.BORDER_COLOR[1]/255,
self.BORDER_COLOR[2]/255)
canvas.roundRect(x, 0, width, height,
self.CARD_CORNER_DIAMETER, stroke=0, fill=1)
canvas.restoreState()
def _draw_single_background(self, canvas, x, margins, width, height):
canvas.saveState()
clipping_mask = canvas.beginPath()
clipping_mask.roundRect(x + margins[Border.LEFT],
margins[Border.BOTTOM],
width - margins[Border.RIGHT] - margins[Border.LEFT],
height - margins[Border.TOP] - margins[Border.BOTTOM],
self.BACKGROUND_CORNER_DIAMETER)
canvas.clipPath(clipping_mask, stroke = 0, fill = 0)
# get optimum background orientation
background_orientation = best_orientation(self.background_image_path,
width, height)
if background_orientation == Orientation.TURN90:
canvas.rotate(90)
canvas.translate(0, -self.WIDTH*2)
canvas.drawImage(self.background_image_path, 0, 0, width=height,
height=width, mask=None)
else:
canvas.drawImage(self.background_image_path, x, 0, width=width,
height=height, mask=None)
canvas.restoreState()
class SmallCard(CardLayout):
WIDTH = CardLayout.BASE_WIDTH
HEIGHT = CardLayout.BASE_HEIGHT
BORDER_FRONT = (
CardLayout.STANDARD_BORDER,
CardLayout.STANDARD_BORDER,
7*mm,
7*mm
)
BORDER_BACK = (
CardLayout.STANDARD_BORDER,
CardLayout.STANDARD_BORDER,
9.2*mm,
1.7*mm
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
frame = Frame(
self.WIDTH + self.BORDER_BACK[Border.LEFT],
self.BORDER_BACK[Border.BOTTOM],
self.BASE_WIDTH - self.BORDER_BACK[Border.LEFT] - self.BORDER_BACK[Border.RIGHT],
self.HEIGHT - self.BORDER_BACK[Border.TOP] - self.TITLE_BAR_HEIGHT - self.STANDARD_BORDER - self.BORDER_BACK[Border.BOTTOM],
leftPadding = self.TEXT_MARGIN,
bottomPadding = self.TEXT_MARGIN,
rightPadding = self.TEXT_MARGIN,
topPadding = 1*mm,
showBoundary=True,
)
self.frames.append(frame)
class LargeCard(CardLayout):
WIDTH = CardLayout.BASE_WIDTH * 2
HEIGHT = CardLayout.BASE_HEIGHT
BORDER_FRONT = (3.5*mm, 3.5*mm, 7*mm, 9*mm)
BORDER_BACK = (4*mm, 4*mm, 8.5*mm, 3*mm)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
left_frame = Frame(
self.WIDTH + self.BORDER_BACK[Border.LEFT],
self.BORDER_BACK[Border.BOTTOM],
self.BASE_WIDTH - self.BORDER_BACK[Border.LEFT] - self.STANDARD_BORDER/2,
self.HEIGHT - self.BORDER_BACK[Border.TOP] - self.TITLE_BAR_HEIGHT - self.STANDARD_BORDER - self.BORDER_BACK[Border.BOTTOM],
leftPadding = self.TEXT_MARGIN,
bottomPadding = self.TEXT_MARGIN,
rightPadding = self.TEXT_MARGIN,
topPadding = 1*mm
)
right_frame = Frame(
self.WIDTH*1.5 + self.STANDARD_BORDER/2,
self.BORDER_BACK[Border.BOTTOM],
self.BASE_WIDTH - self.BORDER_BACK[Border.LEFT] - self.STANDARD_BORDER/2,
self.HEIGHT - self.BORDER_BACK[Border.BOTTOM] - self.BORDER_BACK[Border.TOP],
leftPadding = self.TEXT_MARGIN,
bottomPadding = self.TEXT_MARGIN,
rightPadding = self.TEXT_MARGIN,
topPadding = 1*mm,
showBoundary=True
)
self.frames.append(left_frame)
self.frames.append(right_frame)
def draw(self, canvas):
super().draw(canvas)
canvas.setFillColor(self.BORDER_COLOR)
canvas.rect(self.WIDTH*1.5 - self.STANDARD_BORDER/2, 0,
self.STANDARD_BORDER, self.HEIGHT, stroke=0,
fill=1)
class MonsterCardLayout(CardLayout):
# These must be set by subclasses
CHALLENGE_BOTTOM = None
SOURCE_LOCATION = []
def __init__(self, title, subtitle, artist, image_path,
armor_class, max_hit_points, speed, strength, dexterity,
constitution, intelligence, wisdom, charisma, challenge_rating,
experience_points, source, attributes=None, abilities=None,
actions=None, reactions=None, *args, **kwargs):
super().__init__(title, subtitle, artist, image_path, *args, **kwargs)
self.armor_class = armor_class
self.max_hit_points = max_hit_points
self.speed = speed
# Modifieres
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.challenge_rating = challenge_rating
if int(experience_points) < 0:
raise ValueError("Experience points must be positive")
self.experience_points = "{:,}".format(int(experience_points))
self.source = source
def _draw_back(self, canvas):
super()._draw_back(canvas)
# Challenge
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):
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]
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.BASE_WIDTH - self.BORDER_BACK[Border.LEFT]
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>{}:</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)
class MonsterCardSmall(SmallCard, MonsterCardLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.CHALLENGE_BOTTOM = 5.5*mm
self.SOURCE_LOCATION = (self.WIDTH + self.BORDER_BACK[Border.LEFT], 3*mm)
class MonsterCardLarge(LargeCard, MonsterCardLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.CHALLENGE_BOTTOM = (self.BORDER_BACK[Border.BOTTOM] - self.fonts.styles["challenge"][1])/2
self.SOURCE_LOCATION = (self.WIDTH*1.5 + self.STANDARD_BORDER/2, self.CHALLENGE_BOTTOM)
class CardGenerator(ABC):
sizes = [] # Set by subclass
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def draw(self, canvas):
# TODO: Find a way to clear the page not just draw over it
for size in self.sizes:
try:
card_layout = size(*self._args, **self._kwargs)
card_layout.draw(canvas)
break
except TemplateTooSmall:
pass
class MonsterCard(CardGenerator):
sizes = [MonsterCardSmall, MonsterCardLarge]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate D&D cards.")
parser.add_argument("-t", "--type", help="What type of cards to generate",
action="store", default="monster", choices=["monster"],
dest="type")
parser.add_argument("-o", "--out", help="Output file path",
action="store", default="cards.pdf", dest="output_path",
metavar="output_path")
parser.add_argument("input", help="Path to input YAML file",
action="store")
parser.add_argument("-f", "--fonts", help="What fonts to use when generating cards",
action="store", default="free", choices=["free", "accurate"],
dest="fonts")
args = parser.parse_args()
fonts = None
if args.fonts == "accurate":
try:
fonts = AccurateFonts()
except TTFError:
raise Exception("Failed to load accurate fonts, are you sure you used the correct file names?")
else:
fonts = FreeFonts()
canvas = canvas.Canvas(args.output_path,
pagesize=(SmallCard.WIDTH*4, SmallCard.HEIGHT))
with open(args.input, 'r') as stream:
try:
entries = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
exit()
for entry in entries:
if args.type == "monster":
card = MonsterCard(
entry["title"],
entry["subtitle"],
entry.get("artist", None),
entry["image_path"],
entry["armor_class"],
entry["max_hit_points"],
entry["speed"],
entry["strength"],
entry["dexterity"],
entry["constitution"],
entry["intelligence"],
entry["wisdom"],
entry["charisma"],
entry["challenge_rating"],
entry["experience_points"],
entry["source"],
entry["attributes"],
entry.get("abilities", None),
entry.get("actions", None),
entry.get("reactions", None),
fonts=fonts
)
card.draw(canvas)
canvas.showPage()
canvas.save()