20cca6640910721ee78a9d44b5d4c8d17c8bc544
[librarian.git] / librarian / cover.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import Image, ImageFont, ImageDraw, ImageFilter
7 from librarian import get_resource
8
9
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
15           ):
16     def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color):
17         if 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)
22         while text:
23             line = text
24             while text_draw.textsize(line, font=font)[0] > width:
25                 try:
26                     line, ext = line.rsplit(' ', 1)
27                 except:
28                     break
29             pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2
30             if shadow_color:
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)
33             pos_y += lineskip
34             text = text[len(line)+1:]
35         if shadow_color:
36             shadow_img = shadow_img.filter(ImageFilter.BLUR)
37             img.paste(shadow_img, None, shadow_img)
38         img.paste(text_img, None, text_img)
39         return pos_y
40
41
42     img = Image.new('RGB', (width, height), background_color)
43
44     if background_img:
45         background = Image.open(background_img)
46         img.paste(background, None, background)
47         del background
48
49     # WL logo
50     if logo_width:
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))
54
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)
61
62     return img
63
64
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,
70           300, 0
71           )
72
73 def asbis_cover(author, title):
74     return cover(author, u"„%s”" % title,
75           580, 783, '#fff', get_resource('res/cover-prestigio.png'),
76           446, 118, 62, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',
77           0, 118, 62, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000',
78           None, None
79           )
80