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.
7 from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageEnhance
8 from StringIO import StringIO
9 from librarian import get_resource, OutputFile, URLOpener
13 """Gets metrics from an object, scaling it by a factor."""
14 def __init__(self, obj, scale):
16 self._scale = float(scale)
18 def __getattr__(self, name):
19 src = getattr(self._obj, name)
20 if src and self._scale:
21 src = type(src)(self._scale * src)
25 class TextBox(object):
26 """Creates an Image with a series of centered strings."""
32 def __init__(self, max_width, max_height, padding_x=None, padding_y=None):
34 padding_x = self.SHADOW_X + self.SHADOW_BLUR
36 padding_y = self.SHADOW_Y + self.SHADOW_BLUR
38 self.max_width = max_width
39 self.max_text_width = max_width - 2 * padding_x
40 self.padding_y = padding_y
41 self.height = padding_y
42 self.img = Image.new('RGBA', (max_width, max_height))
43 self.draw = ImageDraw.Draw(self.img)
44 self.shadow_img = None
45 self.shadow_draw = None
47 def skip(self, height):
48 """Skips some vertical space."""
51 def text(self, text, color='#000', font=None, line_height=20,
53 """Writes some centered text."""
54 text = re.sub(r'\s+', ' ', text)
56 if not self.shadow_img:
57 self.shadow_img = Image.new('RGBA', self.img.size)
58 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
61 line_width = self.draw.textsize(line, font=font)[0]
62 while line_width > self.max_text_width:
63 parts = line.rsplit(' ', 1)
65 line_width = self.max_text_width
68 line_width = self.draw.textsize(line, font=font)[0]
69 line = line.strip() + ' '
71 pos_x = (self.max_width - line_width) / 2
74 self.shadow_draw.text(
75 (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
76 line, font=font, fill=shadow_color
79 self.draw.text((pos_x, self.height), line, font=font, fill=color)
80 self.height += line_height
82 text = text[len(line):]
85 """Creates the actual Image object."""
86 image = Image.new('RGBA', (self.max_width,
87 self.height + self.padding_y))
89 shadow = self.shadow_img.filter(ImageFilter.BLUR)
90 image.paste(shadow, (0, 0), shadow)
91 image.paste(self.img, (0, 0), self.img)
93 image.paste(self.img, (0, 0))
98 """Abstract base class for cover images generator."""
101 background_color = '#fff'
102 background_img = None
105 author_margin_left = 20
106 author_margin_right = 20
108 author_color = '#000'
110 author_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
111 author_font_size = 30
114 title_margin_left = 20
115 title_margin_right = 20
119 title_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
124 uses_dc_cover = False
136 'JPEG': 'image/jpeg',
140 def __init__(self, book_info, format=None, width=None, height=None):
141 self.author = ", ".join(auth.readable() for auth in book_info.authors)
142 self.title = book_info.title
143 if format is not None:
146 self.height = height * self.width / width
147 scale = max(float(width or 0) / self.width, float(height or 0) / self.height)
151 self.scale_after = scale
153 def pretty_author(self):
154 """Allows for decorating author's name."""
157 def pretty_title(self):
158 """Allows for decorating title."""
162 metr = Metric(self, self.scale)
163 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
165 if self.background_img:
166 background = Image.open(self.background_img)
167 img.paste(background, None, background)
172 logo = Image.open(get_resource('res/wl-logo.png'))
173 logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]))
174 img.paste(logo, ((metr.width - metr.logo_width) / 2, img.size[1] - logo.size[1] - metr.logo_bottom))
176 top = metr.author_top
178 metr.width - metr.author_margin_left - metr.author_margin_right,
182 author_font = ImageFont.truetype(
183 self.author_font_ttf, metr.author_font_size)
184 tbox.text(self.pretty_author(), self.author_color, author_font,
185 metr.author_lineskip, self.author_shadow)
186 text_img = tbox.image()
187 img.paste(text_img, (metr.author_margin_left, top), text_img)
189 top += text_img.size[1] + metr.title_top
191 metr.width - metr.title_margin_left - metr.title_margin_right,
194 title_font = ImageFont.truetype(
195 self.title_font_ttf, metr.title_font_size)
196 tbox.text(self.pretty_title(), self.title_color, title_font,
197 metr.title_lineskip, self.title_shadow)
198 text_img = tbox.image()
199 img.paste(text_img, (metr.title_margin_left, top), text_img)
203 def final_image(self):
205 if self.scale_after != 1:
207 int(round(img.size[0] * self.scale_after)),
208 int(round(img.size[1] * self.scale_after))),
213 return self.mime_types[self.format]
216 return self.exts[self.format]
218 def save(self, *args, **kwargs):
220 'format': self.format,
223 default_kwargs.update(kwargs)
224 return self.final_image().save(*args, **default_kwargs)
226 def output_file(self, *args, **kwargs):
228 self.save(imgstr, *args, **kwargs)
229 return OutputFile.from_string(imgstr.getvalue())
232 class WLCover(Cover):
233 """Wolne Lektury cover without logos."""
237 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
238 author_font_size = 20
240 title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
243 title_box_width = 350
246 box_bottom_margin = 100
259 box_position = 'middle'
260 background_color = '#444'
261 author_color = '#444'
262 background_img = get_resource('res/cover.png')
266 u'Starożytność': '#9e3610',
267 u'Średniowiecze': '#564c09',
268 u'Renesans': '#8ca629',
270 u'Oświecenie': '#f2802e',
271 u'Romantyzm': '#db4b16',
272 u'Pozytywizm': '#961060',
273 u'Modernizm': '#7784e0',
274 u'Dwudziestolecie międzywojenne': '#3044cf',
275 u'Współczesność': '#06393d',
278 kind_box_position = {
283 def __init__(self, book_info, format=None, width=None, height=None):
284 super(WLCover, self).__init__(book_info, format=format, width=width, height=height)
286 self.box_position = book_info.cover_box_position or \
287 self.kind_box_position.get(book_info.kind, self.box_position)
289 if book_info.cover_bar_color == 'none':
292 self.bar_color = book_info.cover_bar_color or \
293 self.epoch_colors.get(book_info.epoch, self.bar_color)
295 self.title_color = self.epoch_colors.get(book_info.epoch, self.title_color)
297 if book_info.cover_url:
298 url = book_info.cover_url
301 bg_src = URLOpener().open(url)
302 self.background_img = StringIO(bg_src.read())
305 def pretty_author(self):
306 return self.author.upper()
308 def add_box(self, img):
309 if self.box_position == 'none':
312 metr = Metric(self, self.scale)
315 box = TextBox(metr.title_box_width, metr.height, padding_y=metr.box_padding_y)
316 author_font = ImageFont.truetype(
317 self.author_font_ttf, metr.author_font_size)
318 box.text(self.pretty_author(),
320 line_height=metr.author_lineskip,
321 color=self.author_color,
322 shadow_color=self.author_shadow,
325 box.skip(metr.box_above_line)
326 box.draw.line((metr.box_line_left, box.height, metr.box_line_right, box.height),
327 fill=self.author_color, width=metr.box_line_width)
328 box.skip(metr.box_below_line)
331 title_font = ImageFont.truetype(
332 self.title_font_ttf, metr.title_font_size)
333 box.text(self.pretty_title(),
334 line_height=metr.title_lineskip,
336 color=self.title_color,
337 shadow_color=self.title_shadow,
340 box_img = box.image()
343 if self.box_position == 'top':
344 box_top = metr.box_top_margin
345 elif self.box_position == 'bottom':
346 box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
348 box_top = (metr.height - box_img.size[1]) / 2
350 box_left = metr.bar_width + (metr.width - metr.bar_width -
353 # Draw the white box.
354 ImageDraw.Draw(img).rectangle((box_left, box_top,
355 box_left + box_img.size[0], box_top + box_img.size[1]),
357 # Paste the contents into the white box.
358 img.paste(box_img, (box_left, box_top), box_img)
362 metr = Metric(self, self.scale)
363 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
364 draw = ImageDraw.Draw(img)
366 draw.rectangle((0, 0, metr.bar_width, metr.height), fill=self.bar_color)
368 if self.background_img:
369 src = Image.open(self.background_img)
370 trg_size = (metr.width - metr.bar_width, metr.height)
371 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
374 src.size[1] * trg_size[0] / src.size[0]
376 cut = (resized[1] - trg_size[1]) / 2
377 src = src.resize(resized, Image.ANTIALIAS)
378 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
381 src.size[0] * trg_size[1] / src.size[1],
384 cut = (resized[0] - trg_size[0]) / 2
385 src = src.resize(resized, Image.ANTIALIAS)
386 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
388 img.paste(src, (metr.bar_width, 0))
391 img = self.add_box(img)
396 class LogoWLCover(WLCover):
398 gradient_logo_height = 60
399 gradient_logo_margin_right = 30
400 gradient_logo_spacing = 40
401 gradient_color = '#000'
402 gradient_opacity = .6
404 'res/wl-logo-white.png',
405 'res/fnp-logo-white.png',
409 img = super(LogoWLCover, self).image()
410 metr = Metric(self, self.scale)
411 gradient = Image.new('RGBA', (metr.width - metr.bar_width, metr.gradient_height), self.gradient_color)
412 gradient_mask = Image.new('L', (metr.width - metr.bar_width, metr.gradient_height))
413 draw = ImageDraw.Draw(gradient_mask)
414 for line in range(0, metr.gradient_height):
415 draw.line((0, line, metr.width - metr.bar_width, line), fill=int(255 * self.gradient_opacity * line / metr.gradient_height))
417 (metr.bar_width, metr.height - metr.gradient_height), mask=gradient_mask)
419 cursor = metr.width - metr.gradient_logo_margin_right
420 logo_top = metr.height - metr.gradient_height / 2 - metr.gradient_logo_height / 2
421 for logo_path in self.gradient_logos[::-1]:
422 logo = Image.open(get_resource(logo_path))
424 (logo.size[0] * metr.gradient_logo_height / logo.size[1], metr.gradient_logo_height),
426 cursor -= logo.size[0]
427 img.paste(logo, (cursor, logo_top), mask=logo)
428 cursor -= metr.gradient_logo_spacing
433 class EbookpointCover(LogoWLCover):
434 gradient_logo_height = 58
435 gradient_logo_spacing = 25
437 'res/ebookpoint-logo-white.png',
438 'res/wl-logo-white.png',
439 'res/fnp-logo-white.png',
443 class VirtualoCover(Cover):
452 class PrestigioCover(Cover):
455 background_img = get_resource('res/cover-prestigio.png')
458 author_margin_left = 118
459 author_margin_right = 62
461 author_color = '#fff'
462 author_shadow = '#000'
463 author_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
464 author_font_size = 50
467 title_margin_left = 118
468 title_margin_right = 62
471 title_shadow = '#000'
472 title_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
475 def pretty_title(self):
476 return u"„%s”" % self.title
479 class BookotekaCover(Cover):
482 background_img = get_resource('res/cover-bookoteka.png')
485 author_margin_left = 307
486 author_margin_right = 233
487 author_lineskip = 156
488 author_color = '#d9d919'
489 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
490 author_font_size = 130
493 title_margin_left = 307
494 title_margin_right = 233
496 title_color = '#d9d919'
497 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
498 title_font_size = 140
503 class GandalfCover(Cover):
506 background_img = get_resource('res/cover-gandalf.png')
507 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
508 author_font_size = 30
509 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
516 DefaultEbookCover = LogoWLCover