forked from tealefristoe/nsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_drawer.py
870 lines (765 loc) · 26.4 KB
/
card_drawer.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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# CardDrawer
# A class to draw cards for printing purposes.
import constants
import datetime
import pygame
import os
import os.path
import string
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
class CardDrawer:
pygame_init = False
window_surface = None
ot = None
templates = {}
start_x = 0
start_y = 0
window_width_inches = 8.5
window_height_inches = 11
resolution = 300.0
window_width = int(window_width_inches * resolution)
window_height = int(window_height_inches * resolution)
text_margin = 8
normal_margin = 15
line_width = 3
quarter_inch = resolution / 8
raw_card_width = 2.5
raw_card_height = 3.5
raw_safe_margin = .125
card_width = raw_card_width * resolution
card_height = raw_card_height * resolution
safe_margin = raw_safe_margin * resolution
out_dir = ""
pdf_file = ""
inline_images = {}
full_images = {}
background_images = {}
text_height = 0
space_image = None
space_width = 0
sheets = 0
drawn_cards = []
def __init__(self, templates={}, fonts={}, output_target=None):
if not self.pygame_init:
self.initPygame()
self.ot = output_target
if self.ot == None:
self.ot = constants.OT_HOME
self.templates = templates
if not fonts:
self.fonts_info = {"name":"default", "font":"garamond", "size":64}
else:
self.fonts_info = fonts
self.initialize()
# drawCards
# Draws a bunch of cards
def drawCards(self, cards=[]):
self.changeSize(self.card_width, self.card_height)
for c in cards:
if c["name"] not in self.drawn_cards:
self.window_surface.fill(constants.WHITE)
self.drawCard(c)
self.drawn_cards += [c["name"]]
# drawSheet
# Draws a sheet of cards
# Returns the cards not included
def drawSheet(self, cards=[], input_dir=None, raw_card_names=False):
if not input_dir:
input_dir = self.out_dir
self.changeSize(self.window_width, self.window_height)
self.window_surface.fill(constants.WHITE)
# Determine best orientation for cards
printable_height = self.window_height - .75 * self.resolution
printable_width = self.window_width - .75 * self.resolution
normal_row_cards = int(printable_height / self.card_height)
normal_col_cards = int(printable_width / self.card_width)
normal_card_count = normal_row_cards * normal_col_cards
rotated_row_cards = int(printable_height / self.card_width)
rotated_col_cards = int(printable_width / self.card_height)
rotated_card_count = rotated_row_cards * rotated_col_cards
if normal_card_count > rotated_card_count:
row_cards = normal_row_cards
col_cards = normal_col_cards
card_dy = self.card_height
card_dx = self.card_width
rotate_card = False
else:
row_cards = rotated_row_cards
col_cards = rotated_col_cards
card_dy = self.card_width
card_dx = self.card_height
rotate_card = True
start_x = 0
x = start_x
cards_r = 0
start_y = 0
y = start_y
cards_c = 0
card_count = 0
seen_cards = []
for c in cards:
if raw_card_names:
image = pygame.image.load(os.path.join(input_dir, c))
else:
image = pygame.image.load(os.path.join(input_dir, self.convertName(c["name"]) + ".png"))
if rotate_card:
image = pygame.transform.rotate(image, 90)
self.window_surface.blit(image, (x, y))
seen_cards += [c]
y += card_dy
cards_r += 1
if cards_r == row_cards:
y = start_y
cards_r = 0
x += card_dx
cards_c += 1
card_count += 1
if cards_c == col_cards:
break
pygame.display.update()
if self.sheets in range(0, 10):
num_string = "0" + str(self.sheets)
else:
num_string = str(self.sheets)
self.printToFile(os.path.join(self.out_dir, 'sheet' + num_string + '.png'))
self.sheets += 1
return cards[card_count:]
def convertName(self, name):
name = "_".join(name.split(" "))
name = "".join(name.split("?"))
name = "".join(name.split("&"))
return name
# getText
# Returns the text for a block based on a particular card.
def getText(self, card=None, block=None):
words = block["content"].split(" ")
replaced_words = []
for word in words:
if len(word) > 0:
# Handle special characters
if word[0] == "\\":
code = word[1:]
if code in card.keys():
replaced_words += [card[code]]
else:
replaced_words += [word]
else:
replaced_words += [word]
return " ".join(replaced_words)
# getBlock
# Returns a particular block from a template, or None of the block isn't in the template.
def getBlock(self, block_name, template):
for block in template.blocks:
if block_name == block["name"]:
return block
return None
# drawBlock
# Draws a template block for the given card, returning a link to the file.
def drawBlock(self, card=None, block=None, template=None):
print(" Drawing block: " + block["name"])
bg_image = None
if "background_image" in block.keys():
bg_name = block["background_image"]
bg_image = self.getBackground(bg_name)
"""
# Code that doesn't work for relative block width
if "width" not in block.keys():
if bg_image:
block["actual_width"] = bg_image.get_width()
else:
block["actual_width"] = self.safe_card_width
else:
other_block = self.getBlock(block["width"], template)
if other_block:
if "actual_width" in other_block.keys():
block["actual_width"] = other_block["actual_width"]
else:
print("!!! Could not use width of " + other_block["name"] + " for block " + block["name"])
block["actual_width"] = self.safe_card_width
else:
block["actual_width"] = block["width"]
self.block_width = block["actual_width"]
"""
if "width" not in block.keys():
if bg_image:
self.block_width = bg_image.get_width()
else:
self.block_width = self.safe_card_width
else:
self.block_width = block["width"]
if "height" not in block.keys():
if bg_image:
self.block_height = bg_image.get_height()
else:
self.block_height = self.safe_card_height
else:
self.block_height = block["height"]
if "align_x" not in block.keys():
self.align_x = "left"
else:
self.align_x = block["align_x"]
if "align_y" not in block.keys():
self.align_y = "top"
else:
self.align_y = block["align_y"]
if "rotation" not in block.keys():
self.block_rotation = 0
elif block["rotation"] == 180:
self.block_rotation = 180
else:
self.block_rotation = 0
# Determine position on card for block location reference
default_pos_x = self.start_x + self.safe_margin
if "pos_x" not in block.keys():
self.pos_x = default_pos_x
else:
pos_x = block["pos_x"]
if pos_x == "flush_left":
self.pos_x = self.start_x
elif pos_x == "left":
self.pos_x = self.start_x + self.safe_margin
elif pos_x == "center":
self.pos_x = int(self.card_width / 2) + self.start_x
elif pos_x == "right":
self.pos_x = self.start_x + self.card_width - self.safe_margin
elif pos_x == "flush_right":
self.pos_x = self.start_x + self.card_width
else:
self.pos_x = pos_x
default_pos_y = self.start_y + self.safe_margin
if "pos_y" not in block.keys():
self.pos_y = default_pos_y
else:
pos_y = block["pos_y"]
if pos_y == "flush_top":
self.pos_y = self.start_y
elif pos_y == "top":
self.pos_y = self.start_y + self.safe_margin
elif pos_y == "center":
self.pos_y = int(self.card_height / 2) + self.start_y
elif pos_y == "bottom":
self.pos_y = self.start_y + self.card_height - self.safe_margin
elif pos_y == "flush_bottom":
self.pos_y = self.start_y + self.card_height
else:
self.pos_y = pos_y
# Determine anchor point
default_ax = self.pos_x
if "anchor_x" not in block.keys():
pos_x = default_ax
else:
ax = block["anchor_x"]
if ax == "left":
pos_x = self.pos_x
elif ax == "center":
pos_x = self.pos_x - int(self.block_width / 2)
elif ax == "right":
pos_x = self.pos_x - self.block_width
else:
pos_x = self.pos_x - ax
default_ay = self.pos_y
if "anchor_y" not in block.keys():
pos_y = default_ay
else:
ay = block["anchor_y"]
if ay == "top":
pos_y = self.pos_y
elif ay == "center":
pos_y = self.pos_y - int(self.block_height / 2)
elif ay == "bottom":
pos_y = self.pos_y - self.block_height
else:
pos_y = self.pos_y - ay
if "color" in block.keys():
if "actual_color" not in block.keys():
block["actual_color"] = self.getColor(block["color"])
color = block["actual_color"]
else:
color = template["actual_color"]
if "background_color" in block.keys():
background_color = self.getColor(block["background_color"])
pygame.draw.rect(self.window_surface, background_color, pygame.Rect(pos_x, pos_y, self.block_width, self.block_height))
# Render background image
if bg_image:
if self.block_rotation == 180:
bg_image = pygame.transform.rotate(bg_image, 180)
bg_x = pos_x + (self.block_width - bg_image.get_width()) / 2
bg_y = pos_y + (self.block_height - bg_image.get_height()) / 2
self.window_surface.blit(bg_image, (bg_x, bg_y))
# Determine font
font = "default"
if "font" in template.keys():
if template["font"] in self.fonts.keys():
font = template["font"]
else:
print("!!! Could not find font for " + template["name"] + " template: " + template["font"])
if "font" in block.keys():
if block["font"] in self.fonts.keys():
font = block["font"]
else:
print("!!! Could not find font for " + block["name"] + " block: " + block["font"])
# Render content of block
text = self.getText(card, block)
self.renderText(text, start_x=pos_x, start_y=pos_y, color=color, width=self.block_width, height=self.block_height, font=font, align=self.align_x, v_align=self.align_y, rotation=self.block_rotation, card=card)
# drawCard
# Draws a card for printing
def drawCard(self, card=None):
if not card:
return
# Check card
if "name" not in card.keys():
print("!!! No name found for " + str(card))
return
print("Drawing card: " + card["name"])
if "type" not in card.keys():
print("!!! No type found for " + str(card))
return
# Find template
template_name = card['type']
if template_name not in self.templates.keys():
print("!!! Could not find template " + template_name)
return
template = self.templates[template_name]
if "blocks" not in template.keys():
print("!!! No blocks for template " + template_name)
return
blocks = template["blocks"]
self.setDimensions(template)
# Draw card edge
pygame.draw.rect(self.window_surface, constants.LIGHT_GREY, pygame.Rect(self.start_x, self.start_y, self.card_width, self.card_height))
# Draw inner border
if "border_color" in template.keys() or "border_color" in card.keys():
if "border_color" in card.keys():
border_color = self.getColor(card["border_color"])
else:
border_color = self.getColor(template["border_color"])
border_x = self.start_x + self.line_width
border_y = self.start_y + self.line_width
border_width = self.card_width - 2 * self.line_width
border_height = self.card_height - 2 * self.line_width
pygame.draw.rect(self.window_surface, border_color, pygame.Rect(border_x, border_y, border_width, border_height))
body_x = border_x + self.safe_margin - self.line_width
body_y = border_y + self.safe_margin - self.line_width
body_width = border_width - 2 * (self.safe_margin - self.line_width)
body_height = border_height - 2 * (self.safe_margin - self.line_width)
else:
body_x = self.start_x + self.line_width
body_y = self.start_y + self.line_width
body_width = self.card_width - 2 * self.line_width
body_height = self.card_height - 2 * self.line_width
# Draw card body
if "background_color" in template.keys():
background_color = self.getColor(template["background_color"])
else:
background_color = constants.WHITE
pygame.draw.rect(self.window_surface, background_color, pygame.Rect(body_x, body_y, body_width, body_height))
# Draw background image
bg_image_name = None
if "background_image" in card.keys():
bg_image_name = card["background_image"]
elif "background_image" in template.keys():
bg_image_name = template["background_image"]
if bg_image_name:
image = self.getBackground(bg_image_name)
if image:
bg_x = self.start_x + (self.card_width - image.get_width()) / 2
bg_y = self.start_y + (self.card_height - image.get_height()) / 2
self.window_surface.blit(image, (bg_x, bg_y))
# Determine default color
if "actual_color" not in template.keys():
if "color" in template.keys():
template["actual_color"] = self.getColor(template["color"])
else:
template["actual_color"] = constants.ACTUAL_BLACK
# Draw each block
for block in blocks:
if "content" not in block.keys():
print("!!! No content defined for " + str(block))
block["content"] = ""
block["image_path"] = self.drawBlock(card, block, template)
# Print card
pygame.display.update()
self.printToFile(os.path.join(self.out_dir, self.convertName(card["name"]) + '.png'))
print("")
def drawLine(self, x=0, y=0, length=0, horizontal=True, color=None, width=-1):
if not color:
color = constants.ACTUAL_BLACK
if width < 0:
width = self.line_width
if horizontal:
pygame.draw.rect(self.window_surface, color, pygame.Rect(x, y, length, width))
else:
pygame.draw.rect(self.window_surface, color, pygame.Rect(x, y, width, length))
# renderText
# Renders the given text, handling multiple lines, special characters,
# and images.
# Returns the height of the text block.
def renderText(self, text, start_x=0, start_y=0, color=None, width=None, height=0, actually_render=True, draw_images=True, font=None, line_space=None, align="left", v_align="top", rotation=0, allow_styling=True, card=None):
# Setup defaults
if color == None:
color = constants.ACTUAL_BLACK
default_color = color
if font == None:
font = 'rules'
default_font = font
# Define space images
space_image = self.fonts[font].render(" ", True, constants.ACTUAL_BLACK)
if line_space == None:
line_space = space_image.get_height() / 32
# Initialize variables
x = start_x
images = []
cur_width = 0
text_buffer = self.normal_margin
total_height = 0
# basic_height = self.fonts[font].size(" ")[1]
# basic_height = self.safe_margin
basic_height = 0
cur_height = basic_height
line_padding = self.safe_margin
blank_line = True
words = text.split(" ")
rule_count = 0
lines = []
# Handle actual rendering
for word in words:
# Initialize word specific variables
y_offset = text_buffer
keyword = False
revert_bold = False
revert_italic = False
render_rule = False
modified_word = ""
# Make sure we have an actual word
if len(word) > 0:
# Handle special characters
if word[0] == "\\":
code = word[1:]
codes = code.split(":")
if len(codes) == 2:
command = codes[0]
code = codes[1]
if command == "img":
if not card:
print("!!! Card not included for text render!")
code = None
elif code in card.keys():
if card[code][0] == "\\":
code = card[code][1:]
if command == "font":
if code == "default":
font = default_font
continue
elif code not in self.fonts.keys():
print("!!! Attempt to use an unknown font: " + code)
code = None
else:
font = code
continue
if command == "color":
if code == "default":
color = default_color
continue
else:
color = self.getColor(code)
continue
# End of Line and Line Padding
if code == "n" or code == "lp":
lines += [{"width":cur_width, "height":cur_height, "align":align, "images":images}]
images = []
x = start_x
total_height += cur_height
cur_height = basic_height
cur_width = 0
if code == "lp":
lines += [{"width":0, "height":line_padding, "align":align, "images":[]}]
total_height += line_padding
continue
# Bold
elif code == "b":
if allow_styling:
self.fonts[font].set_bold(not self.fonts[font].get_bold())
continue
# Italic
elif code == "i":
if allow_styling:
self.fonts[font].set_italic(not self.fonts[font].get_italic())
continue
# Inline
elif code in self.inline_images.keys():
image = self.getInline(code)
y_offset = 0
# Full
elif code in self.full_images.keys():
image = self.getFull(code)
image_x = start_x + (width - image.get_width()) / 2
if cur_width != 0:
lines += [{"width":cur_width, "height":cur_height, "align":align, "images":images}]
images = []
lines += [{"width":image.get_width(), "height":image.get_height(), "align":"center", "images":[image]}]
x = start_x
if cur_width == 0:
total_height += image.get_height() + self.normal_margin
else:
total_height += cur_height + image.get_height() + self.normal_margin
cur_height = basic_height
cur_width = 0
continue
else:
print("!!! Unknown command: " + word)
continue
# Handle writing a word
else:
image = self.fonts[font].render(word, True, color)
image_width = image.get_width()
image_height = image.get_height()
# Handle line wrapping
if width:
if cur_width + image_width > width:
lines += [{"width":cur_width, "height":cur_height, "align":align, "images":images}]
images = []
x = start_x
total_height += cur_height
cur_height = basic_height
cur_width = 0
# Draw image
total_width = image_width
if cur_width > 0:
images += [space_image]
# total_width += self.space_width
total_width += space_image.get_width()
images += [image]
# Update position for next image
x += total_width
cur_width += total_width
if image_height > cur_height:
cur_height = image_height
# Draw remaining images after processing the last word
last_align = align
if align == "justified":
last_align = "left"
lines += [{"width":cur_width, "height":cur_height, "align":last_align, "images":images}]
if cur_width > 0:
total_height += cur_height
# Actually render images, one line at a time.
if actually_render:
# Determine y based on vertical alignment
y = start_y
if v_align == "center" or v_align == "bottom":
if v_align == "center":
y = y + (height - (total_height + (len(lines) - 1) * line_space)) / 2
if v_align == "bottom":
y = y + height - (total_height + (len(lines) - 1) * line_space)
if rotation == 180:
lines.reverse()
fs_y = y
for line in lines:
draw_x = self.determineStartX(start_x, line["width"], line["align"], width)
if rotation == 180:
line["images"].reverse()
self.renderImages(line["images"], (draw_x, y), width=width, space_image=space_image, align=line["align"], rotation=rotation)
y += line["height"] + line_space
if self.fonts[font].get_bold():
print("!!! Font left bold: " + text)
if self.fonts[font].get_italic():
print("!!! Font left italic: " + text)
return total_height
# determineStartX
# Returns the x value of where a line should start to be rendered
def determineStartX(self, start_x, cur_width, align, width):
if align == "center":
return start_x + (width - cur_width) / 2
elif align == "right":
return start_x + width - cur_width
else:
return start_x
# renderImages
# Render a collection of images starting at (x, y), centered vertically.
def renderImages(self, images, (x, y), width=0, space_image=None, align="left", rotation=0):
total_height = 0
cur_x = x
cur_y = y
# Handle justify spaces
if align == "justify":
image_width = 0
image_count = 0
for image in images:
if image != space_image:
image_width += image.get_width()
image_count += 1
space_width = (width - image_width) / (image_count - 1)
# Find the total height
for image in images:
if image != space_image and image.get_height() > total_height:
total_height = image.get_height()
# Render images
for image in images:
if align != "justify" or image != space_image:
if rotation == 180:
image = pygame.transform.rotate(image, 180)
self.window_surface.blit(image, (cur_x, cur_y + (total_height - image.get_height()) / 2))
cur_x += image.get_width()
if align == "justify":
cur_x += space_width
# trim
# Remove unwanted details from a word.
# Removes case and end punctuation.
def trim(self, word):
modified_word = word.lower()
if modified_word[-1] in string.punctuation:
modified_word = modified_word[:-1]
return modified_word
# setDimensions
# Sets the size of the card based on the given template
def setDimensions(self, template):
if "size" in template.keys():
size = template["size"]
if size == "poker":
self.raw_card_width = constants.POKER_WIDTH
self.raw_card_height = constants.POKER_HEIGHT
elif size == "horizontal_poker":
self.raw_card_height = constants.POKER_WIDTH
self.raw_card_width = constants.POKER_HEIGHT
elif size == "mini":
self.raw_card_width = constants.MINI_WIDTH
self.raw_card_height = constants.MINI_HEIGHT
else:
self.raw_card_width = constants.POKER_WIDTH
self.raw_card_height = constants.POKER_HEIGHT
else:
self.raw_card_width = constants.POKER_WIDTH
self.raw_card_height = constants.POKER_HEIGHT
self.card_width = self.raw_card_width * self.resolution
self.card_height = self.raw_card_height * self.resolution
self.safe_card_width = self.card_width - 2 * self.safe_margin
self.safe_card_height = self.card_height - 2 * self.safe_margin
self.changeSize(self.card_width, self.card_height)
# writePDF
# Compiles all of the pngs into a single pdf for printing!
def writePDF(self, back=None):
if back != None:
back = back + constants.SHEET_BACK_POSTFIX
pdf_resolution = 72.0
resolution_converstion = self.resolution / pdf_resolution
pdf_margin = .5
page_size = (resolution_converstion * (letter[0] - .9 * pdf_margin * pdf_resolution), resolution_converstion * (letter[1] - .9 * pdf_margin * pdf_resolution))
pdf_x_offset = pdf_margin * pdf_resolution
pdf_y_offset = -4 * pdf_margin * pdf_resolution
back_pdf_y_offset = pdf_margin * pdf_resolution
c = canvas.Canvas(os.path.join(self.out_dir, 'cards.pdf'), pagesize=page_size)
pngs = os.listdir(self.out_dir)
for f in pngs:
if f[-4:] == '.png' and f[:5] == "sheet":
c.drawImage(os.path.join(self.out_dir, f), pdf_x_offset, pdf_y_offset)
c.showPage()
if back != None:
c.drawImage(os.path.join(constants.ART_DIR, back), 0, back_pdf_y_offset)
c.showPage()
c.save()
def getInline(self, image='none'):
if image not in self.inline_images.keys():
print("!!! Unknown image: " + image)
image = 'none'
image = self.inline_images[image]
return image
def getFull(self, image='none'):
if image not in self.full_images.keys():
print("!!! Unknown image: " + image)
image = 'none'
image = self.full_images[image]
return image
def getBackground(self, image='none'):
if image not in self.background_images.keys():
print("!!! Unknown background image: " + image)
image = 'none'
return None
image = self.background_images[image]
return image
# getColor
# Returns a color based on the color word.
def getColor(self, color):
if color in constants.COLOR_MAP.keys():
return constants.COLOR_MAP[color]
if color[0] == "(":
color = color[1:]
if color[-1] == ")":
color = color[:-1]
colors = color.split(", ")
if len(colors) == 1:
colors = colors[0].split(",")
if len(colors) != 3:
print("!!! Illegal color: " + str(colors))
return constants.WHITE
red = self.toInt(colors[0])
green = self.toInt(colors[1])
blue = self.toInt(colors[2])
return (red, green, blue)
# toInt
# Returns the integer value of a string, whether in base 10 or 16.
def toInt(self, string):
if len(string) < 3 or string[0:2] != "0x":
return int(string)
else:
return int(string, 16)
# printToFile
# Prints the sheet to a file so it can be printed.
def printToFile(self, path):
pygame.image.save(self.window_surface, path)
# initialize
# Sets up the images used in the game and the constants used for drawing.
def initialize(self):
# Setup Images
inline_files = os.listdir(constants.INLINE_DIR)
for inline_file in inline_files:
if inline_file[0] == "." or inline_file[-1] == "~":
continue
image_name = '.'.join(inline_file.split('.')[:-1])
self.inline_images[image_name] = pygame.image.load(os.path.join(constants.INLINE_DIR, inline_file))
full_files = os.listdir(constants.FULL_DIR)
for full_file in full_files:
if full_file[0] == "." or full_file[-1] == "~":
continue
image_name = '.'.join(full_file.split('.')[:-1])
self.full_images[image_name] = pygame.image.load(os.path.join(constants.FULL_DIR, full_file))
background_files = os.listdir(constants.BACKGROUND_DIR)
for background_file in background_files:
if background_file[0] == "." or background_file[-1] == "~":
continue
image_name = '.'.join(background_file.split('.')[:-1])
self.background_images[image_name] = pygame.image.load(os.path.join(constants.BACKGROUND_DIR, background_file))
# Update sizes based on output target
if self.ot == constants.OT_TGC:
self.old_resolution = self.resolution
self.resolution = 300.0
self.resolution_scale = self.resolution / self.old_resolution
self.window_width = int(self.window_width_inches * self.resolution)
self.window_height = int(self.window_height_inches * self.resolution)
self.text_margin = int(self.text_margin * self.resolution_scale)
self.normal_margin = int(self.normal_margin * self.resolution_scale)
self.line_width = int(self.line_width * self.resolution_scale)
self.card_width = self.raw_card_width * self.resolution
self.card_height = self.raw_card_height * self.resolution
self.changeSize(self.card_width, self.card_height)
for f in self.fonts:
f['size'] *= int(self.resolution_scale)
# Setup Fonts
self.fonts = {}
for f in self.fonts_info:
self.fonts[f["name"]] = pygame.font.Font(pygame.font.match_font(f["font"]), f["size"])
# Setup Basic Strings
text = " "
size = self.fonts['default'].size(text)
self.text_height = size[1]
self.space_width = size[0]
self.space_image = self.fonts['default'].render(text, True, constants.ACTUAL_BLACK)
# Make output folder
n = datetime.datetime.today()
self.out_dir = os.path.join(constants.OUTPUT_DIR, str(n.year) + '_' + str(n.month) + '_' + str(n.day) + '_' + str(n.hour) + '_' + str(n.minute) + '_' + str(n.second))
os.makedirs(self.out_dir)
def changeSize(self, width, height):
self.window_surface = pygame.display.set_mode((int(width), int(height)), 0, 32)
def initPygame(self):
pygame.init()
self.changeSize(self.card_width, self.card_height)
self.pygame_init = True