786204cf5359e597f3de44969ed1cfc703b48402
[librarian.git] / src / librarian / covers / widgets / author.py
1 import PIL.ImageFont
2 from librarian import get_resource
3 from librarian.cover import Metric
4 from ..utils.textbox import TextBox, split_words
5 from .base import Widget
6
7
8 class AuthorBox(Widget):
9     font_size = 75
10     leading = 92
11
12     def __init__(self, cover, width):
13         self.width = width
14         self.m = Metric(self, cover.m._scale)
15         super().__init__(cover)
16
17     def setup(self):
18         author_font = PIL.ImageFont.truetype(
19             get_resource('fonts/SourceSans3VF-Roman.ttf'),
20             self.m.font_size,
21             layout_engine=PIL.ImageFont.LAYOUT_BASIC
22         )
23         author_font.set_variation_by_axes([600])
24
25         translator_font = PIL.ImageFont.truetype(
26             get_resource('fonts/SourceSans3VF-Roman.ttf'),
27             self.m.font_size,
28             layout_engine=PIL.ImageFont.LAYOUT_BASIC
29         )
30         translator_font.set_variation_by_axes([400])
31
32         authors = [a.readable() for a in self.cover.book_info.authors]
33         translators = [a.readable() for a in self.cover.book_info.translators]
34         if authors and translators:
35             author_str = ', '.join(authors)
36             translator_str = '(tłum. ' + ', '.join(translators) + ')'
37             # just print
38             parts = [author_str, translator_str]
39
40             self.textboxes = [
41                 TextBox(
42                     self.width,
43                     self.m.leading * 2,
44                     [author_str],
45                     author_font,
46                     1,
47                     self.m.leading,
48                     0,
49                     1, 0
50                 ),
51                 TextBox(
52                     self.width,
53                     self.m.leading * 2,
54                     [translator_str],
55                     translator_font,
56                     1,
57                     self.m.leading,
58                     0,
59                     1, 0
60                 )
61             ]
62
63         else:
64             assert authors
65             if len(authors) == 2:
66                 parts = authors
67             elif len(authors) > 2:
68                 parts = [author + ',' for author in authors[:-1]] + [authors[-1]]
69             else:
70                 parts = split_words(authors[0])
71
72             try:
73                 self.textboxes = [
74                     TextBox(
75                         self.width,
76                         self.m.leading * 2,
77                         parts,
78                         author_font,
79                         2,
80                         self.m.leading,
81                         0,
82                         1, 0
83                     )
84                 ]
85             except:
86                 self.textboxes = [
87                     TextBox(
88                         self.width,
89                         self.m.leading * 2,
90                         parts,
91                         author_font,
92                         1,
93                         self.m.leading,
94                         0,
95                         1, 0
96                     )
97                 ]
98         self.margin_top = self.textboxes[0].margin_top
99
100     def build(self, w, h):
101         img = PIL.Image.new('RGBA', (self.width, self.m.leading * 2))
102         offset = 0
103         for i, tb in enumerate(self.textboxes):
104             sub_img = tb.as_pil_image(self.cover.color_scheme['text'])
105             img.paste(sub_img, (0, self.m.leading * i), sub_img)
106         
107         return img