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,
37 shadow_color=None, shortener=None):
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 for line in shortener(text):
46 if text_draw.textsize(line, font=font)[0] <= self.max_text_width:
51 line_width = self.draw.textsize(line, font=font)[0]
52 while line_width > self.max_text_width:
53 parts = line.rsplit(' ', 1)
55 line_width = self.max_text_width
58 line_width = self.draw.textsize(line, font=font)[0]
60 line = line.strip() + ' '
62 pos_x = (self.max_width - line_width) / 2
65 self.shadow_draw.text(
66 (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
67 line, font=font, fill=shadow_color
70 self.draw.text((pos_x, self.height), line, font=font, fill=color)
71 self.height += line_height
73 text = text[len(line):]
76 def person_shortener(text):
79 n_chunks = len(chunks)
80 # make initials from given names, starting from last
81 for i in range(n_chunks - 2, -1, -1):
82 chunks[i] = chunks[i][0] + '.'
83 yield " ".join(chunks)
84 # remove given names initials, starting from last
85 while len(chunks) > 2:
87 yield " ".join(chunks)
90 def title_shortener(text):
93 n_chunks = len(chunks)
94 # remove words, starting from last one
95 while len(chunks) > 1:
97 yield " ".join(chunks) + u'…'
100 """Creates the actual Image object."""
101 image = Image.new('RGBA', (self.max_width,
102 self.height + self.padding_y))
104 shadow = self.shadow_img.filter(ImageFilter.BLUR)
105 image.paste(shadow, (0, 0), shadow)
106 image.paste(self.img, (0, 0), self.img)
108 image.paste(self.img, (0, 0))
113 """Abstract base class for cover images generator."""
116 background_color = '#fff'
117 background_img = None
121 author_margin_left = 20
122 author_margin_right = 20
124 author_color = '#000'
131 title_margin_left = 20
132 title_margin_right = 20
141 uses_dc_cover = False
151 'JPEG': 'image/jpeg',
155 def __init__(self, book_info):
156 self.author = ", ".join(auth.readable() for auth in book_info.authors)
157 self.title = book_info.title
159 def pretty_author(self):
160 """Allows for decorating author's name."""
163 def pretty_title(self):
164 """Allows for decorating title."""
168 img = Image.new('RGB', (self.width, self.height), self.background_color)
170 if self.background_img:
171 background = Image.open(self.background_img)
173 img.paste(background, None, background)
174 except ValueError, e:
175 img.paste(background)
180 logo = Image.open(get_resource('res/wl-logo.png'))
181 logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
182 img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
184 top = self.author_top
186 self.width - self.author_margin_left - self.author_margin_right,
189 author_font = self.author_font or ImageFont.truetype(
190 get_resource('fonts/DejaVuSerif.ttf'), 30)
191 author_shortener = None if self.author_wrap else TextBox.person_shortener
192 tbox.text(self.pretty_author(), self.author_color, author_font,
193 self.author_lineskip, self.author_shadow, author_shortener)
194 text_img = tbox.image()
195 img.paste(text_img, (self.author_margin_left, top), text_img)
197 top += text_img.size[1] + self.title_top
199 self.width - self.title_margin_left - self.title_margin_right,
202 title_font = self.author_font or ImageFont.truetype(
203 get_resource('fonts/DejaVuSerif.ttf'), 40)
204 title_shortener = None if self.title_wrap else TextBox.title_shortener
205 tbox.text(self.pretty_title(), self.title_color, title_font,
206 self.title_lineskip, self.title_shadow, title_shortener)
207 text_img = tbox.image()
208 img.paste(text_img, (self.title_margin_left, top), text_img)
213 return self.mime_types[self.format]
216 return self.exts[self.format]
218 def save(self, *args, **kwargs):
219 return self.image().save(format=self.format, *args, **kwargs)
222 class WLCover(Cover):
223 """Default Wolne Lektury cover generator."""
225 author_font = ImageFont.truetype(
226 get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
228 title_font = ImageFont.truetype(
229 get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
231 title_box_width = 350
233 background_color = '#444'
234 author_color = '#444'
238 u'Średniowiecze': 30,
245 u'Dwudziestolecie międzywojenne': 240,
246 u'Współczesność': 270,
249 def __init__(self, book_info):
250 super(WLCover, self).__init__(book_info)
251 self.kind = book_info.kind
252 self.epoch = book_info.epoch
253 if book_info.cover_url:
254 from urllib2 import urlopen
255 from StringIO import StringIO
257 bg_src = urlopen(book_info.cover_url)
258 self.background_img = StringIO(bg_src.read())
261 def pretty_author(self):
262 return self.author.upper()
265 from colorsys import hsv_to_rgb
267 img = Image.new('RGB', (self.width, self.height), self.background_color)
268 draw = ImageDraw.Draw(img)
270 if self.epoch in self.epochs:
271 epoch_color = tuple(int(255 * c) for c in hsv_to_rgb(
272 float(self.epochs[self.epoch]) / 360, .7, .7))
275 draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
277 if self.background_img:
278 src = Image.open(self.background_img)
279 trg_size = (self.width - self.bar_width, self.height)
280 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
283 src.size[1] * trg_size[0] / src.size[0]
285 cut = (resized[1] - trg_size[1]) / 2
286 src = src.resize(resized)
287 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
290 src.size[0] * trg_size[1] / src.size[1],
293 cut = (resized[0] - trg_size[0]) / 2
294 src = src.resize(resized)
295 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
297 img.paste(src, (self.bar_width, 0))
300 box = TextBox(self.title_box_width, self.height, padding_y=20)
301 box.text(self.pretty_author(),
302 font=self.author_font,
303 line_height=self.author_lineskip,
304 color=self.author_color,
305 shadow_color=self.author_shadow,
309 box.draw.line((75, box.height, 275, box.height),
310 fill=self.author_color, width=2)
313 box.text(self.pretty_title(),
314 line_height=self.title_lineskip,
315 font=self.title_font,
317 shadow_color=self.title_shadow,
319 box_img = box.image()
321 if self.kind == 'Liryka':
324 elif self.kind == 'Epika':
326 box_top = self.height - 100 - box_img.size[1]
329 box_top = (self.height - box_img.size[1]) / 2
331 box_left = self.bar_width + (self.width - self.bar_width -
333 draw.rectangle((box_left, box_top,
334 box_left + box_img.size[0], box_top + box_img.size[1]),
336 img.paste(box_img, (box_left, box_top), box_img)
342 class VirtualoCover(Cover):
351 class PrestigioCover(Cover):
354 background_img = get_resource('res/cover-prestigio.png')
357 author_margin_left = 118
358 author_margin_right = 62
360 author_color = '#fff'
361 author_shadow = '#000'
362 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
365 title_margin_left = 118
366 title_margin_right = 62
369 title_shadow = '#000'
370 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
372 def pretty_title(self):
373 return u"„%s”" % self.title
376 class BookotekaCover(Cover):
379 background_img = get_resource('res/cover-bookoteka.png')
382 author_margin_left = 307
383 author_margin_right = 233
384 author_lineskip = 156
385 author_color = '#d9d919'
386 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
389 title_margin_left = 307
390 title_margin_right = 233
392 title_color = '#d9d919'
393 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
398 class GandalfCover(Cover):
401 background_img = get_resource('res/cover-gandalf.png')
402 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
403 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
409 class ArtaTechCover(Cover):
412 background_img = get_resource('res/cover-arta-tech.jpg')
414 author_margin_left = 235
415 author_margin_right = 23
417 author_font = ImageFont.truetype(get_resource('fonts/DroidSans.ttf'), 32)
418 author_color = '#555555'
421 title_margin_right = 21
422 title_margin_left = 60
424 title_font = ImageFont.truetype(get_resource('fonts/EBGaramond-Regular.ttf'), 42)
425 title_color = '#222222'
429 def pretty_author(self):
430 return self.author.upper()
434 """ a class factory for simple image covers """
435 img = Image.open(img)
437 class ImgCover(Cover):
443 return self.image().format