some cleaning
[librarian.git] / librarian / cover.py
index a7fc674..c946d43 100644 (file)
@@ -3,37 +3,78 @@
 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-import Image, ImageFont, ImageDraw
+import Image, ImageFont, ImageDraw, ImageFilter
 from librarian import get_resource
 
 
-def cover(width, height, author, title):
-    def draw_centered_text(text, draw, font, width, pos_y, lineskip):
+def cover(author, title,
+          width, height, background_color, background_img,
+          author_top, author_margin_left, author_margin_right, author_lineskip, author_color, author_font, author_shadow,
+          title_top, title_margin_left, title_margin_right, title_lineskip, title_color, title_font, title_shadow,
+          logo_width, logo_bottom
+          ):
+    def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color):
+        if shadow_color:
+            shadow_img = Image.new('RGBA', img.size)
+            shadow_draw = ImageDraw.Draw(shadow_img)
+        text_img = Image.new('RGBA', img.size)
+        text_draw = ImageDraw.Draw(text_img)
         while text:
             line = text
-            while draw.textsize(line, font=font)[0] > width:
+            while text_draw.textsize(line, font=font)[0] > width:
                 try:
                     line, ext = line.rsplit(' ', 1)
                 except:
                     break
-            draw.text(((img.size[0] - draw.textsize(line, font=font)[0]) / 2, pos_y), line, font=font, fill='#000')
+            pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2
+            if shadow_color:
+                shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color)
+            text_draw.text((pos_x, pos_y), line, font=font, fill=color)
             pos_y += lineskip
             text = text[len(line)+1:]
+        if shadow_color:
+            shadow_img = shadow_img.filter(ImageFilter.BLUR)
+            img.paste(shadow_img, None, shadow_img)
+        img.paste(text_img, None, text_img)
         return pos_y
 
 
-    img = Image.new('RGB', (width, height), (255, 255, 255))
+    img = Image.new('RGB', (width, height), background_color)
 
-    # WL logo
-    logo = Image.open(get_resource('pdf/wl-logo.png'))
-    logo = logo.resize((img.size[0] / 2, logo.size[1] * img.size[0] / 2 / logo.size[0]))
-    img.paste(logo, (width / 4, img.size[1] - logo.size[1]))
+    if background_img:
+        background = Image.open(background_img)
+        img.paste(background)
+        del background
 
-    draw = ImageDraw.Draw(img)
-    author_font = ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), width/20)
-    title_y = draw_centered_text(author, draw, author_font, width*9/10, height/10, width/15) + height/10
+    # WL logo
+    if logo_width:
+        logo = Image.open(get_resource('res/wl-logo.png'))
+        logo = logo.resize((logo_width, logo.size[1] * logo_width / logo.size[0]))
+        img.paste(logo, ((width - logo_width) / 2, img.size[1] - logo.size[1] - logo_bottom))
 
-    title_font = ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), width/15)
-    draw_centered_text(title, draw, title_font, width*9/10, title_y, width/11)
+    title_y = draw_centered_text(author, img, author_font,
+                    author_margin_left, width - author_margin_left - author_margin_right, author_top,
+                    author_lineskip, author_color, author_shadow) + title_top
+    draw_centered_text(title, img, title_font,
+                    title_margin_left, width - title_margin_left - title_margin_right, title_y,
+                    title_lineskip, title_color, title_shadow)
 
     return img
+
+
+def virtualo_cover(author, title):
+    return cover(author, title,
+          600, 730, '#fff', None,
+          73, 20, 20, 40, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30), None,
+          73, 20, 20, 54, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40), None,
+          300, 0
+          )
+
+def asbis_cover(author, title):
+    return cover(author, u"„%s”" % title,
+          800, 800, '#000', '',
+          455, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',
+          0, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',
+          None, None
+          )
+