rel
[librarian.git] / src / librarian / covers / widgets / title.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 TitleBox(Widget):
9     font_size = 159 # 45pt
10     leading = 176  # 50pt
11     tracking = 2.385
12
13     def __init__(self, cover, width, height, lines, scale=1):
14         self.width = width
15         self.height = height
16         self.lines = lines
17         self.m = Metric(self, cover.m._scale * scale)
18         super().__init__(cover)
19
20     def setup(self):
21         title_font = PIL.ImageFont.truetype(
22             get_resource('fonts/SourceSans3VF-Roman.ttf'),
23             self.m.font_size,
24             layout_engine=PIL.ImageFont.LAYOUT_BASIC
25         )
26         title_font.set_variation_by_axes([800])
27
28         lines = self.lines or (int(self.height * (176/200) / self.m.leading) - 0)
29         
30         self.tb = TextBox(
31             self.width,
32             self.height,
33             split_words(self.cover.title),
34             title_font,
35             lines,
36             self.m.leading,
37             self.m.tracking,
38             .5, .5
39         )
40         self.margin_top = self.tb.margin_top
41             
42     def build(self, w, h):
43         return self.tb.as_pil_image(self.cover.color_scheme['text'])
44