1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 import Image, ImageFont, ImageDraw, ImageFilter
7 from librarian import get_resource
10 def cover(author, title,
11 width, height, background_color, background_img,
12 author_top, author_margin_left, author_margin_right, author_lineskip, author_color, author_font, author_shadow,
13 title_top, title_margin_left, title_margin_right, title_lineskip, title_color, title_font, title_shadow,
14 logo_width, logo_bottom
16 def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color):
18 shadow_img = Image.new('RGBA', img.size)
19 shadow_draw = ImageDraw.Draw(shadow_img)
20 text_img = Image.new('RGBA', img.size)
21 text_draw = ImageDraw.Draw(text_img)
24 while text_draw.textsize(line, font=font)[0] > width:
26 line, ext = line.rsplit(' ', 1)
29 pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2
31 shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color)
32 text_draw.text((pos_x, pos_y), line, font=font, fill=color)
34 text = text[len(line)+1:]
36 shadow_img = shadow_img.filter(ImageFilter.BLUR)
37 img.paste(shadow_img, None, shadow_img)
38 img.paste(text_img, None, text_img)
42 img = Image.new('RGB', (width, height), background_color)
45 background = Image.open(background_img)
51 logo = Image.open(get_resource('res/wl-logo.png'))
52 logo = logo.resize((logo_width, logo.size[1] * logo_width / logo.size[0]))
53 img.paste(logo, ((width - logo_width) / 2, img.size[1] - logo.size[1] - logo_bottom))
55 title_y = draw_centered_text(author, img, author_font,
56 author_margin_left, width - author_margin_left - author_margin_right, author_top,
57 author_lineskip, author_color, author_shadow) + title_top
58 draw_centered_text(title, img, title_font,
59 title_margin_left, width - title_margin_left - title_margin_right, title_y,
60 title_lineskip, title_color, title_shadow)
65 def virtualo_cover(author, title):
66 return cover(author, title,
67 600, 730, '#fff', None,
68 73, 20, 20, 40, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30), None,
69 73, 20, 20, 54, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40), None,
73 def asbis_cover(author, title):
74 return cover(author, u"„%s”" % title,
76 455, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',
77 0, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',