-
Notifications
You must be signed in to change notification settings - Fork 0
/
superbatalo.py
executable file
·145 lines (106 loc) · 3.85 KB
/
superbatalo.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
#!/usr/bin/env python3
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
gi.require_version('Pango', '1.0')
from gi.repository import Pango
gi.require_version('PangoCairo', '1.0')
from gi.repository import PangoCairo
import cairo
import math
POINTS_PER_MM = 2.8346457
PAGE_WIDTH = 210
PAGE_HEIGHT = 297
# The margin from the edge of the page to the start of the cards
MARGIN = 15
# The margin inside the card for the text
INNER_MARGIN = 2.5
N_COLUMNS = 4
N_ROWS = 5
CARDS_PER_PAGE = N_COLUMNS * N_ROWS
CARD_WIDTH = (PAGE_WIDTH - MARGIN * 2) / N_COLUMNS
CARD_HEIGHT = (PAGE_HEIGHT - MARGIN * 2) / N_ROWS
def start_page(cr):
for i in range(N_COLUMNS + 1):
cr.move_to(i * CARD_WIDTH + MARGIN, MARGIN)
cr.rel_line_to(0, PAGE_HEIGHT - MARGIN * 2)
for i in range(N_ROWS + 1):
cr.move_to(MARGIN, i * CARD_HEIGHT + MARGIN)
cr.rel_line_to(PAGE_WIDTH - MARGIN * 2, 0)
cr.stroke()
def render_paragraph(cr, x, y, width, height, text):
font_size = 12
while True:
layout = PangoCairo.create_layout(cr)
fd = Pango.FontDescription.from_string("Sans {}".format(font_size))
layout.set_font_description(fd)
layout.set_width(width * POINTS_PER_MM * Pango.SCALE)
layout.set_text(text, -1)
(ink_rect, logical_rect) = layout.get_pixel_extents()
if (ink_rect.height / POINTS_PER_MM <= height and
ink_rect.width / POINTS_PER_MM) <= width:
break
font_size *= 0.75
cr.save()
cr.move_to(x, y)
# Remove the mm scale
cr.scale(1.0 / POINTS_PER_MM, 1.0 / POINTS_PER_MM)
PangoCairo.show_layout(cr, layout)
cr.restore()
return logical_rect.height / POINTS_PER_MM
def draw_card(cr, x, y, text, logo):
logo_dim = logo.get_dimensions()
logo_scale = (CARD_WIDTH - INNER_MARGIN * 2) / logo_dim.width
cr.save()
cr.translate(MARGIN + x * CARD_WIDTH + INNER_MARGIN,
MARGIN + (y + 1) * CARD_HEIGHT -
logo_dim.height * logo_scale -
INNER_MARGIN)
cr.scale(logo_scale, logo_scale)
logo.render_cairo(cr)
cr.restore()
paragraph_height = (CARD_HEIGHT -
INNER_MARGIN * 2 -
logo_dim.height * logo_scale)
render_paragraph(cr,
MARGIN + x * CARD_WIDTH + INNER_MARGIN,
MARGIN + y * CARD_HEIGHT + INNER_MARGIN,
CARD_WIDTH - INNER_MARGIN * 2,
paragraph_height,
text)
class Renderer:
def __init__(self):
surface = cairo.PDFSurface("superbatalo.pdf",
PAGE_WIDTH * POINTS_PER_MM,
PAGE_HEIGHT * POINTS_PER_MM)
self.cr = cairo.Context(surface)
# Use mm for the units from now on
self.cr.scale(POINTS_PER_MM, POINTS_PER_MM)
# Use ½mm line width
self.cr.set_line_width(0.5)
self.card_num = 0
def render(self, text, logo):
in_page = self.card_num % CARDS_PER_PAGE
if in_page == 0:
start_page(self.cr)
draw_card(self.cr,
in_page % N_COLUMNS,
in_page // N_COLUMNS,
text,
logo)
self.card_num += 1
if self.card_num % CARDS_PER_PAGE == 0:
self.cr.show_page()
def render_file(renderer, filename, logo_file):
logo = Rsvg.Handle.new_from_file(logo_file)
with open(filename, 'rt') as f:
card_num = 0
for line in f:
line = line.strip()
if line.startswith('#') or len(line) <= 0:
continue
renderer.render(line, logo)
renderer = Renderer()
render_file(renderer, "roloj.txt", "logo-rolo.svg")
render_file(renderer, "konkursoj.txt", "logo-konkurso.svg")
render_file(renderer, "povoj.txt", "logo-povo.svg")