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 librarian import get_resource
 
  11 class TextBox(object):
 
  12     """Creates an Image with a series of centered strings."""
 
  18     def __init__(self, max_width, max_height, padding_x=None, padding_y=None):
 
  20             padding_x = self.SHADOW_X + self.SHADOW_BLUR
 
  22             padding_y = self.SHADOW_Y + self.SHADOW_BLUR
 
  24         self.max_width = max_width
 
  25         self.max_text_width = max_width - 2 * padding_x
 
  26         self.padding_y = padding_y
 
  27         self.height = padding_y
 
  28         self.img = Image.new('RGBA', (max_width, max_height))
 
  29         self.draw = ImageDraw.Draw(self.img)
 
  30         self.shadow_img = None
 
  31         self.shadow_draw = None
 
  33     def skip(self, height):
 
  34         """Skips some vertical space."""
 
  37     def text(self, text, color='#000', font=None, line_height=20,
 
  39         """Writes some centered text."""
 
  40         text = re.sub(r'\s+', ' ', text)
 
  42             if not self.shadow_img:
 
  43                 self.shadow_img = Image.new('RGBA', self.img.size)
 
  44                 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
 
  47             line_width = self.draw.textsize(line, font=font)[0]
 
  48             while line_width > self.max_text_width:
 
  49                 parts = line.rsplit(' ', 1)
 
  51                     line_width = self.max_text_width
 
  54                 line_width = self.draw.textsize(line, font=font)[0]
 
  55             line = line.strip() + ' '
 
  57             pos_x = (self.max_width - line_width) / 2
 
  60                 self.shadow_draw.text(
 
  61                         (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
 
  62                         line, font=font, fill=shadow_color
 
  65             self.draw.text((pos_x, self.height), line, font=font, fill=color)
 
  66             self.height += line_height
 
  68             text = text[len(line):]
 
  71         """Creates the actual Image object."""
 
  72         image = Image.new('RGBA', (self.max_width,
 
  73                                    self.height + self.padding_y))
 
  75             shadow = self.shadow_img.filter(ImageFilter.BLUR)
 
  76             image.paste(shadow, (0, 0), shadow)
 
  77             image.paste(self.img, (0, 0), self.img)
 
  79             image.paste(self.img, (0, 0))
 
  84     """Abstract base class for cover images generator."""
 
  87     background_color = '#fff'
 
  91     author_margin_left = 20
 
  92     author_margin_right = 20
 
  99     title_margin_left = 20
 
 100     title_margin_right = 20
 
 108     uses_dc_cover = False
 
 118         'JPEG': 'image/jpeg',
 
 122     def __init__(self, book_info):
 
 123         #self.author = ", ".join(auth.readable() for auth in book_info.authors)
 
 124         self.title = book_info.title
 
 126     def pretty_author(self):
 
 127         """Allows for decorating author's name."""
 
 130     def pretty_title(self):
 
 131         """Allows for decorating title."""
 
 135         img = Image.new('RGB', (self.width, self.height), self.background_color)
 
 137         if self.background_img:
 
 138             background = Image.open(self.background_img)
 
 139             img.paste(background, None, background)
 
 144             logo = Image.open(get_resource('res/wl-logo.png'))
 
 145             logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
 
 146             img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
 
 148         top = self.author_top
 
 150             self.width - self.author_margin_left - self.author_margin_right,
 
 153         author_font = self.author_font or ImageFont.truetype(
 
 154             get_resource('fonts/DejaVuSerif.ttf'), 30)
 
 155         tbox.text(self.pretty_author(), self.author_color, author_font,
 
 156             self.author_lineskip, self.author_shadow)
 
 157         text_img = tbox.image()
 
 158         img.paste(text_img, (self.author_margin_left, top), text_img)
 
 160         top += text_img.size[1] + self.title_top
 
 162             self.width - self.title_margin_left - self.title_margin_right,
 
 165         title_font = self.author_font or ImageFont.truetype(
 
 166             get_resource('fonts/DejaVuSerif.ttf'), 40)
 
 167         tbox.text(self.pretty_title(), self.title_color, title_font,
 
 168             self.title_lineskip, self.title_shadow)
 
 169         text_img = tbox.image()
 
 170         img.paste(text_img, (self.title_margin_left, top), text_img)
 
 175         return self.mime_types[self.format]
 
 178         return self.exts[self.format]
 
 180     def save(self, *args, **kwargs):
 
 181         return self.image().save(format=self.format, *args, **kwargs)
 
 184 class WLCover(Cover):
 
 185     """Default Wolne Lektury cover generator."""
 
 189     author_font = ImageFont.truetype(
 
 190         get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
 
 192     title_font = ImageFont.truetype(
 
 193         get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
 
 195     title_box_width = 350
 
 197     background_color = '#444'
 
 198     author_color = '#444'
 
 199     default_background = get_resource('res/cover.png')
 
 203         u'Starożytność': '#9e3610',
 
 204         u'Średniowiecze': '#564c09',
 
 205         u'Renesans': '#8ca629',
 
 207         u'Oświecenie': '#f2802e',
 
 208         u'Romantyzm': '#db4b16',
 
 209         u'Pozytywizm': '#961060',
 
 210         u'Modernizm': '#7784e0',
 
 211         u'Dwudziestolecie międzywojenne': '#3044cf',
 
 212         u'Współczesność': '#06393d',
 
 215     def __init__(self, book_info):
 
 216         super(WLCover, self).__init__(book_info)
 
 217         self.kind = book_info.kind
 
 218         self.epoch = book_info.epoch
 
 219         print book_info.cover_url
 
 220         if book_info.cover_url:
 
 221             from urllib2 import urlopen
 
 222             from StringIO import StringIO
 
 224             bg_src = urlopen(book_info.cover_url)
 
 225             self.background_img = StringIO(bg_src.read())
 
 228             self.background_img = self.default_background
 
 230     def pretty_author(self):
 
 231         return self.author.upper()
 
 234         img = Image.new('RGB', (self.width, self.height), self.background_color)
 
 235         draw = ImageDraw.Draw(img)
 
 237         if self.epoch in self.epoch_colors:
 
 238             epoch_color = self.epoch_colors[self.epoch]
 
 241         draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
 
 243         if self.background_img:
 
 244             src = Image.open(self.background_img)
 
 245             trg_size = (self.width - self.bar_width, self.height)
 
 246             if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
 
 249                     src.size[1] * trg_size[0] / src.size[0]
 
 251                 cut = (resized[1] - trg_size[1]) / 2
 
 252                 src = src.resize(resized)
 
 253                 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
 
 256                     src.size[0] * trg_size[1] / src.size[1],
 
 259                 cut = (resized[0] - trg_size[0]) / 2
 
 260                 src = src.resize(resized)
 
 261                 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
 
 263             img.paste(src, (self.bar_width, 0))
 
 266         box = TextBox(self.title_box_width, self.height, padding_y=20)
 
 267         box.text(self.pretty_author(),
 
 268                  font=self.author_font,
 
 269                  line_height=self.author_lineskip,
 
 270                  color=self.author_color,
 
 271                  shadow_color=self.author_shadow,
 
 275         box.draw.line((75, box.height, 275, box.height),
 
 276                 fill=self.author_color, width=2)
 
 279         box.text(self.pretty_title(),
 
 280                  line_height=self.title_lineskip,
 
 281                  font=self.title_font,
 
 283                  shadow_color=self.title_shadow,
 
 285         box_img = box.image()
 
 287         if self.kind == 'Liryka':
 
 290         elif self.kind == 'Epika':
 
 292             box_top = self.height - 100 - box_img.size[1]
 
 295             box_top = (self.height - box_img.size[1]) / 2
 
 297         box_left = self.bar_width + (self.width - self.bar_width -
 
 299         draw.rectangle((box_left, box_top,
 
 300             box_left + box_img.size[0], box_top + box_img.size[1]),
 
 302         img.paste(box_img, (box_left, box_top), box_img)
 
 308 class VirtualoCover(Cover):
 
 317 class PrestigioCover(Cover):
 
 320     background_img = get_resource('res/cover-prestigio.png')
 
 323     author_margin_left = 118
 
 324     author_margin_right = 62
 
 326     author_color = '#fff'
 
 327     author_shadow = '#000'
 
 328     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
 
 331     title_margin_left = 118
 
 332     title_margin_right = 62
 
 335     title_shadow = '#000'
 
 336     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
 
 338     def pretty_title(self):
 
 339         return u"„%s”" % self.title
 
 342 class BookotekaCover(Cover):
 
 345     background_img = get_resource('res/cover-bookoteka.png')
 
 348     author_margin_left = 307
 
 349     author_margin_right = 233
 
 350     author_lineskip = 156
 
 351     author_color = '#d9d919'
 
 352     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
 
 355     title_margin_left = 307
 
 356     title_margin_right = 233
 
 358     title_color = '#d9d919'
 
 359     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
 
 364 class GandalfCover(Cover):
 
 367     background_img = get_resource('res/cover-gandalf.png')
 
 368     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
 
 369     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
 
 374 class ImageCover(WLCover):
 
 376     def __init__(self, *args, **kwargs):
 
 377         super(ImageCover, self).__init__(*args, **kwargs)
 
 378         self.im = Image.open(self.background_img)
 
 379         self.width, self.height = self.im.size