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'
235 default_background = get_resource('res/cover.png')
240 u'Średniowiecze': 30,
247 u'Dwudziestolecie międzywojenne': 240,
248 u'Współczesność': 270,
251 def __init__(self, book_info):
252 super(WLCover, self).__init__(book_info)
253 self.kind = book_info.kind
254 self.epoch = book_info.epoch
255 if book_info.cover_url:
256 from urllib2 import urlopen
257 from StringIO import StringIO
259 bg_src = urlopen(book_info.cover_url)
260 self.background_img = StringIO(bg_src.read())
263 self.background_img = self.default_background
265 def pretty_author(self):
266 return self.author.upper()
269 from colorsys import hsv_to_rgb
271 img = Image.new('RGB', (self.width, self.height), self.background_color)
272 draw = ImageDraw.Draw(img)
274 if self.epoch in self.epochs:
275 epoch_color = tuple(int(255 * c) for c in hsv_to_rgb(
276 float(self.epochs[self.epoch]) / 360, .7, .7))
279 draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
281 if self.background_img:
282 src = Image.open(self.background_img)
283 trg_size = (self.width - self.bar_width, self.height)
284 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
287 src.size[1] * trg_size[0] / src.size[0]
289 cut = (resized[1] - trg_size[1]) / 2
290 src = src.resize(resized)
291 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
294 src.size[0] * trg_size[1] / src.size[1],
297 cut = (resized[0] - trg_size[0]) / 2
298 src = src.resize(resized)
299 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
301 img.paste(src, (self.bar_width, 0))
304 box = TextBox(self.title_box_width, self.height, padding_y=20)
305 box.text(self.pretty_author(),
306 font=self.author_font,
307 line_height=self.author_lineskip,
308 color=self.author_color,
309 shadow_color=self.author_shadow,
313 box.draw.line((75, box.height, 275, box.height),
314 fill=self.author_color, width=2)
317 box.text(self.pretty_title(),
318 line_height=self.title_lineskip,
319 font=self.title_font,
321 shadow_color=self.title_shadow,
323 box_img = box.image()
325 if self.kind == 'Liryka':
328 elif self.kind == 'Epika':
330 box_top = self.height - 100 - box_img.size[1]
333 box_top = (self.height - box_img.size[1]) / 2
335 box_left = self.bar_width + (self.width - self.bar_width -
337 draw.rectangle((box_left, box_top,
338 box_left + box_img.size[0], box_top + box_img.size[1]),
340 img.paste(box_img, (box_left, box_top), box_img)
346 class VirtualoCover(Cover):
355 class PrestigioCover(Cover):
358 background_img = get_resource('res/cover-prestigio.png')
361 author_margin_left = 118
362 author_margin_right = 62
364 author_color = '#fff'
365 author_shadow = '#000'
366 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
369 title_margin_left = 118
370 title_margin_right = 62
373 title_shadow = '#000'
374 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
376 def pretty_title(self):
377 return u"„%s”" % self.title
380 class BookotekaCover(Cover):
383 background_img = get_resource('res/cover-bookoteka.png')
386 author_margin_left = 307
387 author_margin_right = 233
388 author_lineskip = 156
389 author_color = '#d9d919'
390 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
393 title_margin_left = 307
394 title_margin_right = 233
396 title_color = '#d9d919'
397 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
402 class GandalfCover(Cover):
405 background_img = get_resource('res/cover-gandalf.png')
406 author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
407 title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
413 class ArtaTechCover(Cover):
416 background_img = get_resource('res/cover-arta-tech.jpg')
418 author_margin_left = 235
419 author_margin_right = 23
421 author_font = ImageFont.truetype(get_resource('fonts/DroidSans.ttf'), 32)
422 author_color = '#555555'
425 title_margin_right = 21
426 title_margin_left = 60
428 title_font = ImageFont.truetype(get_resource('fonts/EBGaramond-Regular.ttf'), 42)
429 title_color = '#222222'
433 def pretty_author(self):
434 return self.author.upper()
438 """ a class factory for simple image covers """
439 img = Image.open(img)
441 class ImgCover(Cover):
447 return self.image().format