Bidi text in titles.
[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         title_font_2 = PIL.ImageFont.truetype(
31             get_resource('fonts/OpenSans-VariableFont_wdth,wght.ttf'),
32             self.m.font_size,
33             layout_engine=PIL.ImageFont.Layout.BASIC
34         )
35         title_font_2.set_variation_by_axes([700, 700])
36         font_fallbacks = {
37             ('\u0590', '\u05FF'): title_font_2,
38         }
39
40         lines = self.lines or (int(self.height * (176/200) / self.m.leading) - 0)
41         
42         self.tb = TextBox(
43             self.width,
44             self.height,
45             split_words(self.cover.title),
46             title_font,
47             lines,
48             self.m.leading,
49             self.m.tracking,
50             .5, .5,
51             font_fallbacks=font_fallbacks
52         )
53         self.margin_top = self.tb.margin_top
54             
55     def build(self, w, h):
56         return self.tb.as_pil_image(self.cover.color_scheme['text'])
57