forked from orsinium-labs/gram-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
240 lines (218 loc) · 7.06 KB
/
generate.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
from __future__ import annotations
from argparse import ArgumentParser
from dataclasses import dataclass
from functools import cached_property
from itertools import count
from pathlib import Path
from textwrap import wrap
from typing import Iterator
import svg
ROOT = Path(__file__).parent
PAGE_BG_COLOR = 'white'
CARD_FRAME_COLOR = 'red'
CARD_TEXT_COLOR = 'black'
CARD_NUMBER_COLOR = 'grey'
@dataclass
class Coord:
page: int
x: int
y: int
@dataclass
class Card:
coord: Coord
number: int
width: int
height: int
font_size: int
text: str
radius: int
stroke_width: int
def render(self) -> svg.G:
line_height = round(self.font_size * 1.2, 2)
lines: list[svg.Element] = []
for line in wrap(self.text, 14):
tspan = svg.TSpan(
text=line,
x=self.width // 2,
dy=line_height,
)
lines.append(tspan)
text_height = len(lines) * line_height
r = self.radius
return svg.G(
transform=[
svg.Translate(x=self.coord.x, y=self.coord.y),
],
elements=[
svg.Path(
fill='none',
stroke=CARD_FRAME_COLOR,
stroke_width=self.stroke_width,
d=[
svg.MoveTo(r, 0),
svg.LineTo(self.width - r, 0),
self._draw_corner(r, r),
svg.LineTo(self.width, self.height - r),
self._draw_corner(-r, r),
svg.LineTo(0 + r, self.height),
self._draw_corner(-r, -r),
svg.LineTo(0, r),
self._draw_corner(r, -r),
],
),
svg.Text(
y=self.height // 2 - text_height // 2,
dominant_baseline='middle',
text_anchor='middle',
fill=CARD_TEXT_COLOR,
font_size=self.font_size,
font_family='Roboto Mono, monospace',
elements=lines,
),
svg.Text(
x=self.width - 5,
y=self.height - 5,
text_anchor='end',
fill=CARD_NUMBER_COLOR,
font_size=self.font_size,
font_family='Roboto Mono, monospace',
text=str(self.number),
),
],
)
def _draw_corner(self, dx: int, dy: int) -> svg.ArcRel:
"""Produce an arc for rounded corner
"""
return svg.ArcRel(
rx=self.radius,
ry=self.radius,
angle=0,
large_arc=False,
sweep=True,
dx=dx, dy=dy,
)
@dataclass
class Generator:
page_width: int
page_height: int
card_width: int
card_height: int
border: int
padding: int
corner_radius: int
stroke_width: int
font_size: int
output: Path
def save_all(self) -> None:
self.output.mkdir(exist_ok=True)
for page_number in self.pages:
out_path = self.output / f'{page_number}.svg'
page_svg = self.generate_page(page_number)
out_path.write_text(str(page_svg))
def generate_page(self, page_number: int) -> svg.SVG:
return svg.SVG(
viewBox=svg.ViewBoxSpec(0, 0, self.page_width, self.page_height),
width=svg.mm(self.page_width),
height=svg.mm(self.page_height),
elements=list(self.iter_cards(page_number)),
)
def iter_cards(self, page_number: int) -> Iterator[svg.Element]:
yield svg.Defs(elements=[
svg.Style(
text='@import url("https://fonts.googleapis.com/css?family=Roboto+Mono:400");',
),
])
yield svg.Rect(
x=0, y=0,
width=self.page_width,
height=self.page_height,
fill=PAGE_BG_COLOR,
)
for card in self.cards:
if card.coord.page == page_number:
yield card.render()
@property
def pages(self) -> Iterator[int]:
prev_page = -1
for card in self.cards:
if card.coord.page != prev_page:
yield card.coord.page
prev_page = card.coord.page
@cached_property
def cards(self) -> list[Card]:
"""List of all cards to render.
"""
cards = []
phrases = (ROOT / 'phrases.txt').read_text().strip().splitlines()
card_number = count(1)
for coord, phrase in zip(self.iter_coords(), phrases):
cards.append(Card(
coord=coord,
number=next(card_number),
text=phrase,
width=self.card_width,
height=self.card_height,
font_size=self.font_size,
radius=self.corner_radius,
stroke_width=self.stroke_width,
))
return cards
def iter_coords(self) -> Iterator[Coord]:
"""Infinite iterator producing coordinates for cards.
Alright, it's capped to 100 pages, but that's just a precaution.
"""
step_x = self.card_width + self.padding
step_y = self.card_height + self.padding
right_stop = self.page_width - step_x - self.border
bottom_stop = self.page_height - step_y - self.border
for page in range(1, 100):
for y in range(self.border, bottom_stop, step_y):
for x in range(self.border, right_stop, step_x):
yield Coord(page=page, x=x, y=y)
def main() -> None:
parser = ArgumentParser()
parser.add_argument(
'--page-width', type=int, default=210,
help='horizontal size of each page',
)
parser.add_argument(
'--page-height', type=int, default=297,
help='vertical size of each page',
)
parser.add_argument(
'--card-width', type=int, default=60,
help='horizontal size of each card',
)
parser.add_argument(
'--card-height', type=int, default=90,
help='vertical size of each card',
)
parser.add_argument(
'--border', type=int, default=5,
help='distance to keep from each page edge',
)
parser.add_argument(
'--padding', type=int, default=2,
help='distance between cards',
)
parser.add_argument(
'--font-size', type=int, default=6,
help='size of the font for text on cards',
)
parser.add_argument(
'--corner-radius', type=int, default=5,
help='radius of the corner for each card frame',
)
parser.add_argument(
'--stroke-width', type=int, default=1,
help='line width for card frames',
)
parser.add_argument(
'--output', type=Path, default=ROOT / 'build',
help='path where to write generated pages',
)
args = parser.parse_args()
generator = Generator(**vars(args))
generator.save_all()
if __name__ == '__main__':
main()