Some prelim work on builder api.
[librarian.git] / src / librarian / covers / widgets / title.py
1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import PIL.ImageFont
5 from librarian import get_resource
6 from librarian.cover import Metric
7 from ..utils.textbox import TextBox, split_words
8 from .base import Widget
9
10
11 class TitleBox(Widget):
12     font_size = 159 # 45pt
13     leading = 176  # 50pt
14     tracking = 2.385
15
16     def __init__(self, cover, width, height, lines, scale=1):
17         self.width = width
18         self.height = height
19         self.lines = lines
20         self.m = Metric(self, cover.m._scale * scale)
21         super().__init__(cover)
22
23     def setup(self):
24         title_font = PIL.ImageFont.truetype(
25             get_resource('fonts/SourceSans3VF-Roman.ttf'),
26             self.m.font_size,
27             layout_engine=PIL.ImageFont.Layout.BASIC
28         )
29         title_font.set_variation_by_axes([800])
30
31         lines = self.lines or (int(self.height * (176/200) / self.m.leading) - 0)
32         
33         self.tb = TextBox(
34             self.width,
35             self.height,
36             split_words(self.cover.title),
37             title_font,
38             lines,
39             self.m.leading,
40             self.m.tracking,
41             .5, .5
42         )
43         self.margin_top = self.tb.margin_top
44             
45     def build(self, w, h):
46         return self.tb.as_pil_image(self.cover.color_scheme['text'])
47