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 class TextBox(object):
11 """Creates an Image with a series of centered strings."""
17 def __init__(self, max_width, max_height, padding_x=None, padding_y=None):
19 padding_x = self.SHADOW_X + self.SHADOW_BLUR
21 padding_y = self.SHADOW_Y + self.SHADOW_BLUR
23 self.max_width = max_width
24 self.max_text_width = max_width - 2 * padding_x
25 self.padding_y = padding_y
26 self.height = padding_y
27 self.img = Image.new('RGBA', (max_width, max_height))
28 self.draw = ImageDraw.Draw(self.img)
29 self.shadow_img = None
30 self.shadow_draw = None
32 def skip(self, height):
33 """Skips some vertical space."""
36 def text(self, text, color='#000', font=None, line_height=20,
38 """Writes some centered text."""
40 if not self.shadow_img:
41 self.shadow_img = Image.new('RGBA', self.img.size)
42 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
45 line_width = self.draw.textsize(line, font=font)[0]
46 while line_width > self.max_text_width:
47 parts = line.rsplit(' ', 1)
49 line_width = self.max_text_width
52 line_width = self.draw.textsize(line, font=font)[0]
53 line = line.strip() + ' '
55 pos_x = (self.max_width - line_width) / 2
58 self.shadow_draw.text(
59 (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
60 line, font=font, fill=shadow_color
63 self.draw.text((pos_x, self.height), line, font=font, fill=color)
64 self.height += line_height
66 text = text[len(line):]
69 """Creates the actual Image object."""
70 image = Image.new('RGBA', (self.max_width,
71 self.height + self.padding_y))
73 shadow = self.shadow_img.filter(ImageFilter.BLUR)
74 image.paste(shadow, (0, 0), shadow)
75 image.paste(self.img, (0, 0), self.img)
77 image.paste(self.img, (0, 0))
82 """Abstract base class for cover images generator."""
85 background_color = '#fff'
89 author_margin_left = 20
90 author_margin_right = 20
97 title_margin_left = 20
98 title_margin_right = 20
106 uses_dc_cover = False
116 'JPEG': 'image/jpeg',
120 def __init__(self, book_info):
121 self.author = ", ".join(auth.readable() for auth in book_info.authors)
122 self.title = book_info.title
124 def pretty_author(self):
125 """Allows for decorating author's name."""
128 def pretty_title(self):
129 """Allows for decorating title."""
133 img = Image.new('RGB', (self.width, self.height), self.background_color)
135 if self.background_img:
136 background = Image.open(self.background_img)
137 img.paste(background, None, background)
142 logo = Image.open(get_resource('res/wl-logo.png'))
143 logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
144 img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
146 top = self.author_top
148 self.width - self.author_margin_left - self.author_margin_right,
151 author_font = self.author_font or ImageFont.truetype(
152 get_resource('fonts/DejaVuSerif.ttf'), 30)
153 tbox.text(self.pretty_author(), self.author_color, author_font,
154 self.author_lineskip, self.author_shadow)
155 text_img = tbox.image()
156 img.paste(text_img, (self.author_margin_left, top), text_img)
158 top += text_img.size[1] + self.title_top
160 self.width - self.title_margin_left - self.title_margin_right,
163 title_font = self.author_font or ImageFont.truetype(
164 get_resource('fonts/DejaVuSerif.ttf'), 40)
165 tbox.text(self.pretty_title(), self.title_color, title_font,
166 self.title_lineskip, self.title_shadow)
167 text_img = tbox.image()
168 img.paste(text_img, (self.title_margin_left, top), text_img)
173 return self.mime_types[self.format]
176 return self.exts[self.format]
178 def save(self, *args, **kwargs):
179 return self.image().save(format=self.format, *args, **kwargs)
182 class WLCover(Cover):
183 """Default Wolne Lektury cover generator."""
185 author_font = ImageFont.truetype(
186 get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
188 title_font = ImageFont.truetype(
189 get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
191 title_box_width = 350
193 background_color = '#444'
194 author_color = '#444'
195 default_background = get_resource('res/cover.png')
199 u'Starożytność': '#9e3610',
200 u'Średniowiecze': '#564c09',
201 u'Renesans': '#8ca629',
203 u'Oświecenie': '#f2802e',
204 u'Romantyzm': '#db4b16',
205 u'Pozytywizm': '#961060',
206 u'Modernizm': '#7784e0',
207 u'Dwudziestolecie międzywojenne': '#3044cf',
208 u'Współczesność': '#06393d',
211 def __init__(self, book_info):
212 super(WLCover, self).__init__(book_info)
213 self.kind = book_info.kind
214 self.epoch = book_info.epoch
215 if book_info.cover_url:
216 from urllib2 import urlopen
217 from StringIO import StringIO
219 bg_src = urlopen(book_info.cover_url)
220 self.background_img = StringIO(bg_src.read())
223 self.background_img = self.default_background
225 def pretty_author(self):
226 return self.author.upper()
229 img = Image.new('RGB', (self.width, self.height), self.background_color)
230 draw = ImageDraw.Draw(img)
232 if self.epoch in self.epoch_colors:
233 epoch_color = self.epoch_colors[self.epoch]
236 draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
238 if self.background_img:
239 src = Image.open(self.background_img)
240 trg_size = (self.width - self.bar_width, self.height)
241 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
244 src.size[1] * trg_size[0] / src.size[0]
246 cut = (resized[1] - trg_size[1]) / 2
247 src = src.resize(resized)
248 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
251 src.size[0] * trg_size[1] / src.size[1],
254 cut = (resized[0] - trg_size[0]) / 2
255 src = src.resize(resized)
256 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
258 img.paste(src, (self.bar_width, 0))
261 box = TextBox(self.title_box_width, self.height, padding_y=20)
262 box.text(self.pretty_author(),
263 font=self.author_font,
264 line_height=self.author_lineskip,
265 color=self.author_color,
266 shadow_color=self.author_shadow,
270 box.draw.line((75, box.height, 275, box.height),
271 fill=self.author_color, width=2)
274 box.text(self.pretty_title(),
275 line_height=self.title_lineskip,
276 font=self.title_font,
278 shadow_color=self.title_shadow,
280 box_img = box.image()
282 if self.kind == 'Liryka':
285 elif self.kind == 'Epika':
287 box_top = self.height - 100 - box_img.size[1]
290 box_top = (self.height - box_img.size[1]) / 2
292 box_left = self.bar_width + (self.width - self.bar_width -
294 draw.rectangle((box_left, box_top,
295 box_left + box_img.size[0], box_top + box_img.size[1]),
297 img.paste(box_img, (box_left, box_top), box_img)
303 class VirtualoCover(Cover):
312 class PrestigioCover(Cover):
315 background_img = get_resource('res/cover-prestigio.png')
318 author_margin_left = 118
319 author_margin_right = 62
321 author_color = '#fff'
322 author_shadow = '#000'
323 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
326 title_margin_left = 118
327 title_margin_right = 62
330 title_shadow = '#000'
331 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
333 def pretty_title(self):
334 return u"„%s”" % self.title
337 class BookotekaCover(Cover):
340 background_img = get_resource('res/cover-bookoteka.png')
343 author_margin_left = 307
344 author_margin_right = 233
345 author_lineskip = 156
346 author_color = '#d9d919'
347 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
350 title_margin_left = 307
351 title_margin_right = 233
353 title_color = '#d9d919'
354 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
359 class GandalfCover(Cover):
362 background_img = get_resource('res/cover-gandalf.png')
363 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
364 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)