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
13 background_color = '#fff'
17 author_margin_left = 20
18 author_margin_right = 20
25 title_margin_left = 20
26 title_margin_right = 20
50 def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color):
52 shadow_img = Image.new('RGBA', img.size)
53 shadow_draw = ImageDraw.Draw(shadow_img)
54 text_img = Image.new('RGBA', img.size)
55 text_draw = ImageDraw.Draw(text_img)
58 while text_draw.textsize(line, font=font)[0] > width:
60 line, ext = line.rsplit(' ', 1)
63 pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2
65 shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color)
66 text_draw.text((pos_x, pos_y), line, font=font, fill=color)
68 text = text[len(line)+1:]
70 shadow_img = shadow_img.filter(ImageFilter.BLUR)
71 img.paste(shadow_img, None, shadow_img)
72 img.paste(text_img, None, text_img)
76 def __init__(self, author='', title=''):
80 def pretty_author(self):
83 def pretty_title(self):
87 img = Image.new('RGB', (self.width, self.height), self.background_color)
89 if self.background_img:
90 background = Image.open(self.background_img)
91 img.paste(background, None, background)
96 logo = Image.open(get_resource('res/wl-logo.png'))
97 logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
98 img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
100 author_font = self.author_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30)
101 title_y = self.draw_centered_text(self.pretty_author(), img, author_font,
102 self.author_margin_left, self.width - self.author_margin_left - self.author_margin_right, self.author_top,
103 self.author_lineskip, self.author_color, self.author_shadow) + self.title_top
104 title_font = self.title_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40)
105 self.draw_centered_text(self.pretty_title(), img, title_font,
106 self.title_margin_left, self.width - self.title_margin_left - self.title_margin_right, title_y,
107 self.title_lineskip, self.title_color, self.title_shadow)
112 return self.mime_types[self.format]
115 return self.exts[self.format]
117 def save(self, *args, **kwargs):
118 return self.image().save(format=self.format, *args, **kwargs)
122 class VirtualoCover(Cover):
131 class PrestigioCover(Cover):
134 background_img = get_resource('res/cover-prestigio.png')
137 author_margin_left = 118
138 author_margin_right = 62
140 author_color = '#fff'
141 author_shadow = '#000'
142 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
145 title_margin_left = 118
146 title_margin_right = 62
149 title_shadow = '#000'
150 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
152 def pretty_title(self):
153 return u"„%s”" % self.title
156 class BookotekaCover(Cover):
159 background_img = get_resource('res/cover-bookoteka.png')
162 author_margin_left = 307
163 author_margin_right = 233
164 author_lineskip = 156
165 author_color = '#d9d919'
166 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
169 title_margin_left = 307
170 title_margin_right = 233
172 title_color = '#d9d919'
173 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)