forked from ep1cman/DnD-Card-Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
card_item.py
108 lines (89 loc) · 3.2 KB
/
card_item.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
import pathlib
from reportlab.lib.units import mm
from reportlab.platypus import Paragraph
from reportlab.platypus.flowables import Spacer
from card import CardLayout, Border, SmallCard
ASSET_DIR = pathlib.Path(__file__).parent.resolve() / "assets"
class ItemCardLayout(CardLayout):
def __init__(
self,
title,
subtitle,
category,
description,
subcategory=None,
image_path=ASSET_DIR / "placeholder_item.png",
**kwargs,
):
super().__init__(
title=title, subtitle=subtitle, image_path=image_path, **kwargs
)
self.category = category
self.subcategory = subcategory
self.description = description
def _draw_back(self, canvas, x, y):
super()._draw_back(canvas, x, y)
canvas.setFillColor("white")
self.fonts.set_font(canvas, "category")
left_of_category_text = x + self.border_front[Border.LEFT]
width_of_category_text = y
canvas.drawString(
x + self.border_back[Border.LEFT],
y + self.border_back[Border.TOP],
self.category,
)
if self.subcategory is not None:
self.fonts.set_font(canvas, "subcategory")
canvas.drawString(
left_of_category_text + width_of_category_text + 1 * mm,
self.category_bottom,
"({})".format(self.subcategory),
)
def fill_frames(self, canvas, x, y):
# Title
self.elements.append(self._get_title_paragraph())
# Subtitle
self.elements.append(
Paragraph(
self.subtitle,
self.fonts.paragraph_styles["subtitle"],
)
)
# Add a space before text
self.elements.append(Spacer(1 * mm, 1 * mm))
if type(self.description) == str:
self.elements.append(
Paragraph(self.description, self.fonts.paragraph_styles["text"])
)
return
if type(self.description) != list:
raise ValueError(
f"Item `{self.title}` description should be a `str` or `list`"
)
for entry in self.description:
if type(entry) == str:
self.elements.append(
Paragraph(entry, self.fonts.paragraph_styles["text"])
)
if type(entry) == dict:
for title, description in entry.items():
text = f"<i><b>{title}.</b></i>"
if description is not None:
text += f" {description}"
self.elements.append(
Paragraph(
text,
self.fonts.paragraph_styles["text"],
)
)
# TODO: Tables
def _get_title_paragraph(self):
return Paragraph(
self.title,
self.fonts.paragraph_styles["title"],
)
class ItemCardSmall(SmallCard, ItemCardLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# category is centered in the footer
self.category_bottom = 3.5 * mm + self.bleed