Version 2.2
[librarian.git] / src / librarian / cover.py
index b5996ae..8f7b656 100644 (file)
@@ -68,6 +68,10 @@ class TextBox(object):
                     line_width = self.max_text_width
                     break
                 line = parts[0]
                     line_width = self.max_text_width
                     break
                 line = parts[0]
+
+                if line[-2] == ' ':
+                    line = line[:-2]
+
                 line_width = self.draw.textsize(line, font=font)[0]
             line = line.strip() + ' '
 
                 line_width = self.draw.textsize(line, font=font)[0]
             line = line.strip() + ' '
 
@@ -191,7 +195,8 @@ class Cover(object):
             )
 
         author_font = ImageFont.truetype(
             )
 
         author_font = ImageFont.truetype(
-            self.author_font_ttf, metr.author_font_size)
+            self.author_font_ttf, metr.author_font_size,
+            layout_engine=ImageFont.LAYOUT_BASIC)
         for pa in self.pretty_authors():
             tbox.text(pa, self.author_color, author_font, metr.author_lineskip,
                       self.author_shadow)
         for pa in self.pretty_authors():
             tbox.text(pa, self.author_color, author_font, metr.author_lineskip,
                       self.author_shadow)
@@ -204,7 +209,8 @@ class Cover(object):
             metr.height - top,
             )
         title_font = ImageFont.truetype(
             metr.height - top,
             )
         title_font = ImageFont.truetype(
-            self.title_font_ttf, metr.title_font_size)
+            self.title_font_ttf, metr.title_font_size,
+            layout_engine=ImageFont.LAYOUT_BASIC)
         tbox.text(self.pretty_title(), self.title_color, title_font,
                   metr.title_lineskip, self.title_shadow)
         text_img = tbox.image()
         tbox.text(self.pretty_title(), self.title_color, title_font,
                   metr.title_lineskip, self.title_shadow)
         text_img = tbox.image()
@@ -335,7 +341,8 @@ class WLCover(Cover):
         box = TextBox(metr.title_box_width, metr.height,
                       padding_y=metr.box_padding_y)
         author_font = ImageFont.truetype(
         box = TextBox(metr.title_box_width, metr.height,
                       padding_y=metr.box_padding_y)
         author_font = ImageFont.truetype(
-            self.author_font_ttf, metr.author_font_size)
+            self.author_font_ttf, metr.author_font_size,
+            layout_engine=ImageFont.LAYOUT_BASIC)
         for pa in self.pretty_authors():
             box.text(pa, font=author_font, line_height=metr.author_lineskip,
                      color=self.author_color, shadow_color=self.author_shadow)
         for pa in self.pretty_authors():
             box.text(pa, font=author_font, line_height=metr.author_lineskip,
                      color=self.author_color, shadow_color=self.author_shadow)
@@ -349,7 +356,8 @@ class WLCover(Cover):
 
         # Write title.
         title_font = ImageFont.truetype(
 
         # Write title.
         title_font = ImageFont.truetype(
-            self.title_font_ttf, metr.title_font_size)
+            self.title_font_ttf, metr.title_font_size,
+            layout_engine=ImageFont.LAYOUT_BASIC)
         box.text(self.pretty_title(),
                  line_height=metr.title_lineskip,
                  font=title_font,
         box.text(self.pretty_title(),
                  line_height=metr.title_lineskip,
                  font=title_font,
@@ -359,10 +367,10 @@ class WLCover(Cover):
         box_img = box.image()
 
         # Find box position.
         box_img = box.image()
 
         # Find box position.
-        if self.box_position == 'top':
-            box_top = metr.box_top_margin
-        elif self.box_position == 'bottom':
+        if self.box_position == 'bottom' or box_img.size[1] + metr.box_top_margin + metr.box_bottom_margin > metr.height:
             box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
             box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
+        elif self.box_position == 'top':
+            box_top = metr.box_top_margin
         else:   # Middle.
             box_top = (metr.height - box_img.size[1]) // 2
 
         else:   # Middle.
             box_top = (metr.height - box_img.size[1]) // 2
 
@@ -466,6 +474,7 @@ class WLNoBoxCover(WLCover):
 class LogoWLCover(WLCover):
     gradient_height = 90
     gradient_logo_height = 60
 class LogoWLCover(WLCover):
     gradient_height = 90
     gradient_logo_height = 60
+    gradient_logo_max_width = 1000
     gradient_logo_margin_right = 30
     gradient_logo_spacing = 40
     gradient_color = '#000'
     gradient_logo_margin_right = 30
     gradient_logo_spacing = 40
     gradient_color = '#000'
@@ -474,6 +483,8 @@ class LogoWLCover(WLCover):
         'res/wl-logo-white.png',
         'res/fnp-logo-white.png',
     ]
         'res/wl-logo-white.png',
         'res/fnp-logo-white.png',
     ]
+    annotation = None
+    annotation_height = 10
 
     def __init__(self, book_info, *args, **kwargs):
         super(LogoWLCover, self).__init__(book_info, *args, **kwargs)
 
     def __init__(self, book_info, *args, **kwargs):
         super(LogoWLCover, self).__init__(book_info, *args, **kwargs)
@@ -533,7 +544,10 @@ class LogoWLCover(WLCover):
             - 2 * metr.gradient_logo_margin_right
         )
         widths = [
             - 2 * metr.gradient_logo_margin_right
         )
         widths = [
-            logo.size[0] * metr.gradient_logo_height / logo.size[1]
+            min(
+                metr.gradient_logo_max_width,
+                logo.size[0] * metr.gradient_logo_height / logo.size[1]
+            )
             for logo in logos]
         taken_space = (
             sum(widths)
             for logo in logos]
         taken_space = (
             sum(widths)
@@ -549,13 +563,36 @@ class LogoWLCover(WLCover):
             logo = logo.resize(
                 (
                     int(round(widths[i] * logo_scale)),
             logo = logo.resize(
                 (
                     int(round(widths[i] * logo_scale)),
-                    int(round(metr.gradient_logo_height * logo_scale))
+                    int(round(
+                        logo.size[1] * widths[i] / logo.size[0] * logo_scale
+                    ))
                 ),
                 Image.ANTIALIAS)
             cursor -= logo.size[0]
                 ),
                 Image.ANTIALIAS)
             cursor -= logo.size[0]
-            img.paste(logo, (cursor, logo_top), mask=logo)
+            img.paste(
+                logo,
+                (
+                    cursor,
+                    int(round(logo_top + (metr.gradient_logo_height - logo.size[1]) * logo_scale / 2))
+                ),
+                mask=logo
+            )
             cursor -= int(round(metr.gradient_logo_spacing * logo_scale))
 
             cursor -= int(round(metr.gradient_logo_spacing * logo_scale))
 
+        if self.annotation:
+            img2 = Image.new('RGBA', (metr.height, metr.height), color=None)
+            draw = ImageDraw.Draw(img2)
+            author_font = ImageFont.truetype(
+                self.author_font_ttf, metr.annotation_height,
+                layout_engine=ImageFont.LAYOUT_BASIC)
+            draw.text((self.annotation_height, self.annotation_height), self.annotation, font=author_font, fill='#FFFFFF')
+            img2.show()
+            img2 = img2.rotate(90)
+            img2.show()
+            img.putalpha(0)
+            img.alpha_composite(img2, (0, 0))
+            img = img.convert('RGB')
+
         return img
 
 
         return img
 
 
@@ -672,11 +709,22 @@ class AtriumCover(LogoWLCover):
     ]
 
 
     ]
 
 
+class BNCover(LogoWLCover):
+    gradient_logos = [
+        'res/dofinansowano.png',
+        'res/MKIDN.jpg',
+        'res/BN.png',
+        'res/wl-logo-white.png',
+    ]
+#    annotation = 'Zadanie „Udostępnienie publikacji w formatach cyfrowych” w ramach Narodowego Programu Rozwoju Czytelnictwa. Dofinansowano ze środków Ministra Kultury, Dziedzictwa Narodowego i Sportu.'
+
+
 COVER_CLASSES = {
     'default': LogoWLCover,
     'kmlu': KMLUCover,
     'mpw': MPWCover,
     'atrium': AtriumCover,
 COVER_CLASSES = {
     'default': LogoWLCover,
     'kmlu': KMLUCover,
     'mpw': MPWCover,
     'atrium': AtriumCover,
+    'bn': BNCover,
 }
 
 
 }