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)
121 class WLCover(Cover):
122 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 40)
123 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
129 class VirtualoCover(Cover):
138 class PrestigioCover(Cover):
141 background_img = get_resource('res/cover-prestigio.png')
144 author_margin_left = 118
145 author_margin_right = 62
147 author_color = '#fff'
148 author_shadow = '#000'
149 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
152 title_margin_left = 118
153 title_margin_right = 62
156 title_shadow = '#000'
157 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
159 def pretty_title(self):
160 return u"„%s”" % self.title
163 class BookotekaCover(Cover):
166 background_img = get_resource('res/cover-bookoteka.png')
169 author_margin_left = 307
170 author_margin_right = 233
171 author_lineskip = 156
172 author_color = '#d9d919'
173 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
176 title_margin_left = 307
177 title_margin_right = 233
179 title_color = '#d9d919'
180 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
185 class GandalfCover(Cover):
188 background_img = get_resource('res/cover-gandalf.png')
189 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
190 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)