style
[librarian.git] / librarian / styles / wolnelektury / cover.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from PIL import Image, ImageFont, ImageDraw
7 from StringIO import StringIO
8 from librarian import get_resource, URLOpener
9 from librarian.cover import Cover, TextBox
10
11
12 class WLCover(Cover):
13     """Default Wolne Lektury cover generator."""
14     width = 600
15     height = 833
16     uses_dc_cover = True
17     author_font = ImageFont.truetype(
18         get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
19     author_lineskip = 30
20     title_font = ImageFont.truetype(
21         get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
22     title_lineskip = 40
23     title_box_width = 350
24     bar_width = 35
25     background_color = '#444'
26     author_color = '#444'
27     default_background = get_resource('res/cover.png')
28     format = 'JPEG'
29
30     epoch_colors = {
31         u'Starożytność': '#9e3610',
32         u'Średniowiecze': '#564c09',
33         u'Renesans': '#8ca629',
34         u'Barok': '#a6820a',
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',
41     }
42
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
49             bg_src = None
50             if image_cache:
51                 from urllib import quote
52                 try:
53                     bg_src = URLOpener().open(image_cache + quote(url, safe=""))
54                 except:
55                     pass
56             if bg_src is None:
57                 bg_src = URLOpener().open(url)
58             self.background_img = StringIO(bg_src.read())
59             bg_src.close()
60         else:
61             self.background_img = self.default_background
62
63     def pretty_author(self):
64         return self.author.upper()
65
66     def image(self):
67         img = Image.new('RGB', (self.width, self.height), self.background_color)
68         draw = ImageDraw.Draw(img)
69
70         if self.epoch in self.epoch_colors:
71             epoch_color = self.epoch_colors[self.epoch]
72         else:
73             epoch_color = '#000'
74         draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
75
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]:
80                 resized = (
81                     trg_size[0],
82                     src.size[1] * trg_size[0] / src.size[0]
83                 )
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))
87             else:
88                 resized = (
89                     src.size[0] * trg_size[1] / src.size[1],
90                     trg_size[1],
91                 )
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]))
95
96             img.paste(src, (self.bar_width, 0))
97             del src
98
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)
105
106         box.skip(10)
107         box.draw.line((75, box.height, 275, box.height), fill=self.author_color, width=2)
108         box.skip(15)
109
110         box.text(self.pretty_title(),
111                  line_height=self.title_lineskip,
112                  font=self.title_font,
113                  color=epoch_color,
114                  shadow_color=self.title_shadow)
115         box_img = box.image()
116
117         if self.kind == 'Liryka':
118             # top
119             box_top = 100
120         elif self.kind == 'Epika':
121             # bottom
122             box_top = self.height - 100 - box_img.size[1]
123         else:
124             # center
125             box_top = (self.height - box_img.size[1]) / 2
126
127         box_left = self.bar_width + (self.width - self.bar_width - box_img.size[0]) / 2
128         draw.rectangle((
129             box_left, box_top,
130             box_left + box_img.size[0], box_top + box_img.size[1]),
131             fill='#fff')
132         img.paste(box_img, (box_left, box_top), box_img)
133
134         return img