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
 
   7 from StringIO import StringIO
 
   8 from librarian import get_resource, URLOpener
 
   9 from librarian.cover import Cover, TextBox
 
  13     """Default Wolne Lektury cover generator."""
 
  17     author_font = ImageFont.truetype(
 
  18         get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
 
  20     title_font = ImageFont.truetype(
 
  21         get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
 
  25     background_color = '#444'
 
  27     default_background = get_resource('res/cover.png')
 
  31         u'Starożytność': '#9e3610',
 
  32         u'Średniowiecze': '#564c09',
 
  33         u'Renesans': '#8ca629',
 
  35         u'Oświecenie': '#f2802e',
 
  36         u'Romantyzm': '#db4b16',
 
  37         u'Pozytywizm': '#961060',
 
  38         u'Modernizm': '#7784e0',
 
  39         u'Dwudziestolecie międzywojenne': '#3044cf',
 
  40         u'Współczesność': '#06393d',
 
  43     def __init__(self, book_info, format=None, image_cache=None):
 
  44         super(WLCover, self).__init__(book_info, format=format)
 
  45         self.kind = book_info.kind
 
  46         self.epoch = book_info.epoch
 
  47         if book_info.cover_url:
 
  48             url = book_info.cover_url
 
  51                 from urllib import quote
 
  53                     bg_src = URLOpener().open(image_cache + quote(url, safe=""))
 
  57                 bg_src = URLOpener().open(url)
 
  58             self.background_img = StringIO(bg_src.read())
 
  61             self.background_img = self.default_background
 
  63     def pretty_author(self):
 
  64         return self.author.upper()
 
  67         img = Image.new('RGB', (self.width, self.height), self.background_color)
 
  68         draw = ImageDraw.Draw(img)
 
  70         if self.epoch in self.epoch_colors:
 
  71             epoch_color = self.epoch_colors[self.epoch]
 
  74         draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
 
  76         if self.background_img:
 
  77             src = Image.open(self.background_img)
 
  78             trg_size = (self.width - self.bar_width, self.height)
 
  79             if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
 
  82                     src.size[1] * trg_size[0] / src.size[0]
 
  84                 cut = (resized[1] - trg_size[1]) / 2
 
  85                 src = src.resize(resized)
 
  86                 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
 
  89                     src.size[0] * trg_size[1] / src.size[1],
 
  92                 cut = (resized[0] - trg_size[0]) / 2
 
  93                 src = src.resize(resized)
 
  94                 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
 
  96             img.paste(src, (self.bar_width, 0))
 
  99         box = TextBox(self.title_box_width, self.height, padding_y=20)
 
 100         box.text(self.pretty_author(),
 
 101                  font=self.author_font,
 
 102                  line_height=self.author_lineskip,
 
 103                  color=self.author_color,
 
 104                  shadow_color=self.author_shadow,
 
 108         box.draw.line((75, box.height, 275, box.height),
 
 109                 fill=self.author_color, width=2)
 
 112         box.text(self.pretty_title(),
 
 113                  line_height=self.title_lineskip,
 
 114                  font=self.title_font,
 
 116                  shadow_color=self.title_shadow,
 
 118         box_img = box.image()
 
 120         if self.kind == 'Liryka':
 
 123         elif self.kind == 'Epika':
 
 125             box_top = self.height - 100 - box_img.size[1]
 
 128             box_top = (self.height - box_img.size[1]) / 2
 
 130         box_left = self.bar_width + (self.width - self.bar_width -
 
 132         draw.rectangle((box_left, box_top,
 
 133             box_left + box_img.size[0], box_top + box_img.size[1]),
 
 135         img.paste(box_img, (box_left, box_top), box_img)