wip change fb2 api
[librarian.git] / src / librarian / covers / marquise.py
index fb84a79..c56ae7a 100644 (file)
@@ -1,10 +1,13 @@
+# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
 import PIL.Image
 from librarian.cover import Cover, Metric
 from .utils.color import algo_contrast_or_hue, luminance, is_very_bright
 from .utils.textbox import DoesNotFit
 from .widgets.author import AuthorBox
 from .widgets.background import Background
-from .widgets.image import WLLogo, Label
+from .widgets.image import WLLogo, Label, LogoSticker
 from .widgets.marquise import Marquise
 from .widgets.title import TitleBox
 
@@ -13,11 +16,16 @@ class MarquiseCover(Cover):
     additional_logos = []
     square_variant = False
 
+    background_color = '#000'
     width = 2100
     height = 2970
     margin = 100
     logo_h = 177
-    author_width = 1300
+    author_width = 1400
+    sticker_size = 400
+    sticker_padding_x = 30
+    sticker_padding_y = 50
+    sticker_radius = 10
 
     title_box_top = 262
 
@@ -81,71 +89,90 @@ class MarquiseCover(Cover):
     def image(self):
         img = PIL.Image.new('RGB', (self.m.width, self.m.height), self.background_color)
         
-        bg = Background(self)
-
-        if self.square_variant:
-            layout_options = [
-                (self.m.marquise_small, 1),
-                (self.m.marquise_big, 2),
-                (self.m.marquise_big, 3),
-                (self.m.marquise_big, None),
-            ]
+        if self.predesigned:
+            bg = Background(self, crop_to_square=False)
+            bg.apply(
+                img,
+                0, 0,
+                self.m.width, self.m.height
+            )
         else:
-            layout_options = [
-                (self.m.marquise_small, 2),
-                (self.m.marquise_small, 1),
-                (self.m.marquise_big, 3),
-                (self.m.marquise_xl, 4),
-                (self.m.marquise_xl, None),
-            ]
-
-        for marquise_h, lines in layout_options:
-            title_box_height = marquise_h - self.m.title_box_top - self.m.margin
-            try:
-                title_box = TitleBox(
-                    self,
-                    self.m.width - 2 * self.m.margin,
-                    title_box_height,
-                    lines,
-                    force=lines is None
-                )
-            except DoesNotFit:
-                continue
+            bg = Background(self)
+            if self.square_variant:
+                layout_options = [
+                    (self.m.marquise_small, 1),
+                    (self.m.marquise_big, 2),
+                    (self.m.marquise_big, 3),
+                    (self.m.marquise_big, None),
+                ]
             else:
-                break
-
-        self.marquise_height = marquise_h
-        marquise = Marquise(self, marquise_h)
-
-        bg.apply(
-            img,
-            0, marquise.edge_top,
-            self.m.width, self.m.height - marquise.edge_top
-        )
-        self.set_color_scheme_from(
-            img.crop((
+                layout_options = [
+                    (self.m.marquise_small, 2),
+                    (self.m.marquise_small, 1),
+                    (self.m.marquise_big, 3),
+                    (self.m.marquise_xl, 4),
+                    (self.m.marquise_xl, None),
+                ]
+
+            # Trying all the layout options with decreasing scale.
+            title_box = None
+            title_scale = 1
+            while title_box is None:
+                for marquise_h, lines in layout_options:
+                    title_box_height = marquise_h - self.m.title_box_top - self.m.margin
+                    try:
+                        title_box = TitleBox(
+                            self,
+                            self.m.width - 2 * self.m.margin,
+                            title_box_height,
+                            lines,
+                            scale=title_scale
+                        )
+                    except DoesNotFit:
+                        continue
+                    else:
+                        break
+                title_scale *= .99
+
+            self.marquise_height = marquise_h
+            marquise = Marquise(self, self.marquise_height)
+
+            bg.apply(
+                img,
                 0, marquise.edge_top,
-                self.m.width, marquise.edge_top + (
-                    self.m.height - marquise.edge_top
-                ) / 4
-            ))
-        )
-
-        marquise.apply(
-            img, 0, 0, self.m.width
-        )
-        title_box.apply(
-            img,
-            marquise.title_box_position[0],
-            marquise.title_box_position[1],
-        )
-
-        AuthorBox(self, self.m.author_width).apply(
-            img, self.m.width - self.m.margin - self.m.author_width, self.m.margin
-        )
-        WLLogo(self).apply(img, self.m.margin, self.m.margin, None, self.m.logo_h)
-
-                              
+                self.m.width, self.m.height - marquise.edge_top
+            )
+            self.set_color_scheme_from(
+                img.crop((
+                    0, marquise.edge_top,
+                    self.m.width, marquise.edge_top + (
+                        self.m.height - marquise.edge_top
+                    ) / 4
+                ))
+            )
+
+            marquise.apply(
+                img, 0, 0, self.m.width
+            )
+            title_box.apply(
+                img,
+                marquise.title_box_position[0],
+                marquise.title_box_position[1],
+            )
+
+            AuthorBox(self, self.m.author_width).apply(
+                img, self.m.width - self.m.margin - self.m.author_width, self.m.margin
+            )
+            WLLogo(self).apply(img, self.m.margin, self.m.margin, None, self.m.logo_h)
+
+
+        if self.cover_logo:
+            LogoSticker(self, self.cover_logo).apply(
+                img,
+                self.m.width - self.m.margin - self.m.sticker_size,
+                self.m.height - self.m.margin,
+                self.m.sticker_size, self.m.sticker_size, self.m.sticker_padding_x, self.m.sticker_padding_y, self.m.sticker_radius
+            )
         for logo in self.additional_logos:
             LogoSticker(self, logo).apply(img, 0, 0)
 
@@ -162,13 +189,13 @@ class LabelMarquiseCover(MarquiseCover):
     
     def image(self):
         img = super().image()
-
-        Label(self).apply(
-            img,
-            self.m.label_left,
-            self.marquise_height - self.m.label_top,
-            self.m.label_w,
-            self.m.label_h
-        )
+        if not self.predesigned:
+            Label(self).apply(
+                img,
+                self.m.label_left,
+                self.marquise_height - self.label_top,
+                self.m.label_w,
+                self.m.label_h
+            )
 
         return img