2 from librarian import get_resource
3 from librarian.cover import Metric
4 from ..utils.textbox import TextBox, DoesNotFit, split_words
5 from .base import Widget
8 class AuthorBox(Widget):
12 def __init__(self, cover, width):
14 self.m = Metric(self, cover.m._scale)
15 super().__init__(cover)
18 author_font = PIL.ImageFont.truetype(
19 get_resource('fonts/SourceSans3VF-Roman.ttf'),
21 layout_engine=PIL.ImageFont.LAYOUT_BASIC
23 author_font.set_variation_by_axes([600])
25 translator_font = PIL.ImageFont.truetype(
26 get_resource('fonts/SourceSans3VF-Roman.ttf'),
28 layout_engine=PIL.ImageFont.LAYOUT_BASIC
30 translator_font.set_variation_by_axes([400])
32 authors = [a.readable() for a in self.cover.book_info.authors]
33 translators = [a.readable() for a in self.cover.book_info.translators]
35 if authors and translators:
38 authors_shortened = False
40 while author_box is None:
41 author_str = ', '.join(authors)
44 author_str += ' i in.'
58 authors_shortened = True
60 translators_shortened = False
62 while translator_box is None:
63 translator_str = '(tłum. ' + ', '.join(translators)
64 if translators_shortened:
65 translator_str += ' i in.'
68 translator_box = TextBox(
80 translators_shortened = True
90 while author_box is None:
91 if not shortened and len(authors) == 2:
93 elif len(authors) > 2:
94 parts = [author + ',' for author in authors[:-1]] + [authors[-1]]
98 parts = split_words(authors[0])
100 parts.append('i in.')
104 # Author in two lines.
105 author_box = TextBox(
116 # author in one line.
117 author_box = TextBox(
131 self.textboxes = [author_box]
133 self.margin_top = self.textboxes[0].margin_top
135 def build(self, w, h):
136 img = PIL.Image.new('RGBA', (self.width, self.m.leading * 2))
138 for i, tb in enumerate(self.textboxes):
139 sub_img = tb.as_pil_image(self.cover.color_scheme['text'])
140 img.paste(sub_img, (0, self.m.leading * i), sub_img)