0d64e9c9a9c936b734a0162fb35a0b3f2d18f083
[librarian.git] / src / librarian / covers / marquise.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.Image
5 from librarian.cover import Cover, Metric
6 from .utils.color import algo_contrast_or_hue, luminance, is_very_bright
7 from .utils.textbox import DoesNotFit
8 from .widgets.author import AuthorBox
9 from .widgets.background import Background
10 from .widgets.image import WLLogo, Label, LogoSticker
11 from .widgets.marquise import Marquise
12 from .widgets.title import TitleBox
13
14
15 class MarquiseCover(Cover):
16     additional_logos = []
17     square_variant = False
18
19     width = 2100
20     height = 2970
21     margin = 100
22     logo_h = 177
23     author_width = 1300
24     sticker_size = 400
25     sticker_padding_x = 30
26     sticker_padding_y = 50
27     sticker_radius = 10
28
29     title_box_top = 262
30
31     color_schemes = [
32         {
33             'rgb': (0xc3, 0x27, 0x21),
34             'text': '#000',
35         },
36         {
37             'rgb': (0xa0, 0xbf, 0x38),
38             'text': '#000',
39         },
40         {
41             'rgb': (0xed, 0xc0, 0x16),
42             'text': '#000',
43         },
44         {
45             'rgb': (0x47, 0x66, 0x75),
46             'text': '#fff',
47         }
48     ]
49     for c in color_schemes:
50         c['luminance'] = luminance(c['rgb'])
51         cim = PIL.Image.new('RGB', (1, 1))
52         cim.putpixel((0, 0), c['rgb'])
53         cim.convert('HSV')
54         c['hsv'] = cim.getpixel((0, 0))
55
56     
57     def set_size(self, final_width, final_height):
58         if final_width is None:
59             self.m = Metric(self, 1)
60         else:
61             if final_width > self.width:
62                 self.m = Metric(self, final_width / self.width)
63             else:
64                 self.m = Metric(self, 1)
65                 self.scale_after = final_width / self.width
66             
67         if final_height is not None:
68             self.height = round(final_height / self.scale_after / self.m._scale)
69
70         self.square_variant = self.height / self.width < 250 / 210
71
72         marquise_square_small = int(self.width / 2) - 300
73         marquise_square_big = int(self.width / 2) - 100
74         marquise_a4_small = 2970 - self.width
75         marquise_a4_big = marquise_a4_small + 100
76         
77         self.marquise_small = int(round(marquise_square_small + (marquise_a4_small - marquise_square_small) * (self.height - self.width) / (2970 - 2100)))
78         self.marquise_big = int(round(marquise_square_big + (marquise_a4_big - marquise_square_big) * (self.height - self.width) / (2970 - 2100)))
79         self.marquise_xl = self.marquise_big + 200
80
81         if self.marquise_small > self.marquise_big:
82             self.marquise_small = self.marquise_big
83
84     def set_color_scheme_from(self, img):
85         self.color_scheme = algo_contrast_or_hue(img, self.color_schemes)
86         self.is_very_bright = is_very_bright(img)
87
88     def image(self):
89         img = PIL.Image.new('RGB', (self.m.width, self.m.height), self.background_color)
90         
91         if self.predesigned:
92             bg = Background(self, crop_to_square=False)
93             bg.apply(
94                 img,
95                 0, 0,
96                 self.m.width, self.m.height
97             )
98         else:
99             bg = Background(self)
100             if self.square_variant:
101                 layout_options = [
102                     (self.m.marquise_small, 1),
103                     (self.m.marquise_big, 2),
104                     (self.m.marquise_big, 3),
105                     (self.m.marquise_big, None),
106                 ]
107             else:
108                 layout_options = [
109                     (self.m.marquise_small, 2),
110                     (self.m.marquise_small, 1),
111                     (self.m.marquise_big, 3),
112                     (self.m.marquise_xl, 4),
113                     (self.m.marquise_xl, None),
114                 ]
115
116             # Trying all the layout options with decreasing scale.
117             title_box = None
118             title_scale = 1
119             while title_box is None:
120                 for marquise_h, lines in layout_options:
121                     title_box_height = marquise_h - self.m.title_box_top - self.m.margin
122                     try:
123                         title_box = TitleBox(
124                             self,
125                             self.m.width - 2 * self.m.margin,
126                             title_box_height,
127                             lines,
128                             scale=title_scale
129                         )
130                     except DoesNotFit:
131                         continue
132                     else:
133                         break
134                 title_scale *= .99
135
136             self.marquise_height = marquise_h
137             marquise = Marquise(self, self.marquise_height)
138
139             bg.apply(
140                 img,
141                 0, marquise.edge_top,
142                 self.m.width, self.m.height - marquise.edge_top
143             )
144             self.set_color_scheme_from(
145                 img.crop((
146                     0, marquise.edge_top,
147                     self.m.width, marquise.edge_top + (
148                         self.m.height - marquise.edge_top
149                     ) / 4
150                 ))
151             )
152
153             marquise.apply(
154                 img, 0, 0, self.m.width
155             )
156             title_box.apply(
157                 img,
158                 marquise.title_box_position[0],
159                 marquise.title_box_position[1],
160             )
161
162             AuthorBox(self, self.m.author_width).apply(
163                 img, self.m.width - self.m.margin - self.m.author_width, self.m.margin
164             )
165             WLLogo(self).apply(img, self.m.margin, self.m.margin, None, self.m.logo_h)
166
167
168         if self.cover_logo:
169             LogoSticker(self, self.cover_logo).apply(
170                 img,
171                 self.m.width - self.m.margin - self.m.sticker_size,
172                 self.m.height - self.m.margin,
173                 self.m.sticker_size, self.m.sticker_size, self.m.sticker_padding_x, self.m.sticker_padding_y, self.m.sticker_radius
174             )
175         for logo in self.additional_logos:
176             LogoSticker(self, logo).apply(img, 0, 0)
177
178
179         return img
180
181
182     
183 class LabelMarquiseCover(MarquiseCover):
184     label_left = 95
185     label_top = 105
186     label_w = 710
187     label_h = 710
188     
189     def image(self):
190         img = super().image()
191         if not self.predesigned:
192             Label(self).apply(
193                 img,
194                 self.m.label_left,
195                 self.marquise_height - self.label_top,
196                 self.m.label_w,
197                 self.m.label_h
198             )
199
200         return img