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 import Image, ImageFont, ImageDraw, ImageFilter
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
135 'JPEG': 'image/jpeg',
139 def __init__(self, book_info, format=None, width=None, height=None):
140 self.author = ", ".join(auth.readable() for auth in book_info.authors)
141 self.title = book_info.title
142 if format is not None:
144 scale = max(float(width or 0) / self.width, float(height or 0) / self.height)
148 def pretty_author(self):
149 """Allows for decorating author's name."""
152 def pretty_title(self):
153 """Allows for decorating title."""
157 metr = Metric(self, self.scale)
158 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
160 if self.background_img:
161 background = Image.open(self.background_img)
162 img.paste(background, None, background)
167 logo = Image.open(get_resource('res/wl-logo.png'))
168 logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]))
169 img.paste(logo, ((metr.width - metr.logo_width) / 2, img.size[1] - logo.size[1] - metr.logo_bottom))
171 top = metr.author_top
173 metr.width - metr.author_margin_left - metr.author_margin_right,
177 author_font = ImageFont.truetype(
178 self.author_font_ttf, metr.author_font_size)
179 tbox.text(self.pretty_author(), self.author_color, author_font,
180 metr.author_lineskip, self.author_shadow)
181 text_img = tbox.image()
182 img.paste(text_img, (metr.author_margin_left, top), text_img)
184 top += text_img.size[1] + metr.title_top
186 metr.width - metr.title_margin_left - metr.title_margin_right,
189 title_font = ImageFont.truetype(
190 self.title_font_ttf, metr.title_font_size)
191 tbox.text(self.pretty_title(), self.title_color, title_font,
192 metr.title_lineskip, self.title_shadow)
193 text_img = tbox.image()
194 img.paste(text_img, (metr.title_margin_left, top), text_img)
199 return self.mime_types[self.format]
202 return self.exts[self.format]
204 def save(self, *args, **kwargs):
205 return self.image().save(format=self.format, quality=95, *args, **kwargs)
207 def output_file(self, *args, **kwargs):
209 self.save(imgstr, *args, **kwargs)
210 return OutputFile.from_string(imgstr.getvalue())
213 class WLCover(Cover):
214 """Default Wolne Lektury cover generator."""
218 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
219 author_font_size = 20
221 title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
224 title_box_width = 350
227 box_bottom_margin = 100
236 background_color = '#444'
237 author_color = '#444'
238 default_background = get_resource('res/cover.png')
242 u'Starożytność': '#9e3610',
243 u'Średniowiecze': '#564c09',
244 u'Renesans': '#8ca629',
246 u'Oświecenie': '#f2802e',
247 u'Romantyzm': '#db4b16',
248 u'Pozytywizm': '#961060',
249 u'Modernizm': '#7784e0',
250 u'Dwudziestolecie międzywojenne': '#3044cf',
251 u'Współczesność': '#06393d',
254 def __init__(self, book_info, format=None, width=None, height=None):
255 super(WLCover, self).__init__(book_info, format=format, width=width, height=height)
256 self.kind = book_info.kind
257 self.epoch = book_info.epoch
258 if book_info.cover_url:
259 url = book_info.cover_url
262 bg_src = URLOpener().open(url)
263 self.background_img = StringIO(bg_src.read())
266 self.background_img = self.default_background
268 def pretty_author(self):
269 return self.author.upper()
272 metr = Metric(self, self.scale)
273 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
274 draw = ImageDraw.Draw(img)
276 if self.epoch in self.epoch_colors:
277 epoch_color = self.epoch_colors[self.epoch]
280 draw.rectangle((0, 0, metr.bar_width, metr.height), fill=epoch_color)
282 if self.background_img:
283 src = Image.open(self.background_img)
284 trg_size = (metr.width - metr.bar_width, metr.height)
285 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
288 src.size[1] * trg_size[0] / src.size[0]
290 cut = (resized[1] - trg_size[1]) / 2
291 src = src.resize(resized)
292 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
295 src.size[0] * trg_size[1] / src.size[1],
298 cut = (resized[0] - trg_size[0]) / 2
299 src = src.resize(resized)
300 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
302 img.paste(src, (metr.bar_width, 0))
305 box = TextBox(metr.title_box_width, metr.height, padding_y=metr.box_padding_y)
306 author_font = ImageFont.truetype(
307 self.author_font_ttf, metr.author_font_size)
308 box.text(self.pretty_author(),
310 line_height=metr.author_lineskip,
311 color=self.author_color,
312 shadow_color=self.author_shadow,
315 box.skip(metr.box_above_line)
316 box.draw.line((metr.box_line_left, box.height, metr.box_line_right, box.height),
317 fill=self.author_color, width=metr.box_line_width)
318 box.skip(metr.box_below_line)
320 title_font = ImageFont.truetype(
321 self.title_font_ttf, metr.title_font_size)
322 box.text(self.pretty_title(),
323 line_height=metr.title_lineskip,
326 shadow_color=self.title_shadow,
328 box_img = box.image()
330 if self.kind == 'Liryka':
332 box_top = metr.box_top_margin
333 elif self.kind == 'Epika':
335 box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
338 box_top = (metr.height - box_img.size[1]) / 2
340 box_left = metr.bar_width + (metr.width - metr.bar_width -
342 draw.rectangle((box_left, box_top,
343 box_left + box_img.size[0], box_top + box_img.size[1]),
345 img.paste(box_img, (box_left, box_top), box_img)
351 class VirtualoCover(Cover):
360 class PrestigioCover(Cover):
363 background_img = get_resource('res/cover-prestigio.png')
366 author_margin_left = 118
367 author_margin_right = 62
369 author_color = '#fff'
370 author_shadow = '#000'
371 author_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
372 author_font_size = 50
375 title_margin_left = 118
376 title_margin_right = 62
379 title_shadow = '#000'
380 title_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
383 def pretty_title(self):
384 return u"„%s”" % self.title
387 class BookotekaCover(Cover):
390 background_img = get_resource('res/cover-bookoteka.png')
393 author_margin_left = 307
394 author_margin_right = 233
395 author_lineskip = 156
396 author_color = '#d9d919'
397 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
398 author_font_size = 130
401 title_margin_left = 307
402 title_margin_right = 233
404 title_color = '#d9d919'
405 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
406 title_font_size = 140
411 class GandalfCover(Cover):
414 background_img = get_resource('res/cover-gandalf.png')
415 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
416 author_font_size = 30
417 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')