be34e26836a40832a3034b41c55e1bc1f2f721b3
[librarian.git] / librarian / 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 import re
7 import Image, ImageFont, ImageDraw, ImageFilter
8 from StringIO import StringIO
9 from librarian import get_resource, OutputFile, URLOpener
10
11
12 class TextBox(object):
13     """Creates an Image with a series of centered strings."""
14
15     SHADOW_X = 3
16     SHADOW_Y = 3
17     SHADOW_BLUR = 3
18
19     def __init__(self, max_width, max_height, padding_x=None, padding_y=None):
20         if padding_x is None:
21             padding_x = self.SHADOW_X + self.SHADOW_BLUR
22         if padding_y is None:
23             padding_y = self.SHADOW_Y + self.SHADOW_BLUR
24
25         self.max_width = max_width
26         self.max_text_width = max_width - 2 * padding_x
27         self.padding_y = padding_y
28         self.height = padding_y
29         self.img = Image.new('RGBA', (max_width, max_height))
30         self.draw = ImageDraw.Draw(self.img)
31         self.shadow_img = None
32         self.shadow_draw = None
33
34     def skip(self, height):
35         """Skips some vertical space."""
36         self.height += height
37
38     def text(self, text, color='#000', font=None, line_height=20,
39              shadow_color=None):
40         """Writes some centered text."""
41         text = re.sub(r'\s+', ' ', text)
42         if shadow_color:
43             if not self.shadow_img:
44                 self.shadow_img = Image.new('RGBA', self.img.size)
45                 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
46         while text:
47             line = text
48             line_width = self.draw.textsize(line, font=font)[0]
49             while line_width > self.max_text_width:
50                 parts = line.rsplit(' ', 1)
51                 if len(parts) == 1:
52                     line_width = self.max_text_width
53                     break
54                 line = parts[0]
55                 line_width = self.draw.textsize(line, font=font)[0]
56             line = line.strip() + ' '
57
58             pos_x = (self.max_width - line_width) / 2
59
60             if shadow_color:
61                 self.shadow_draw.text(
62                         (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
63                         line, font=font, fill=shadow_color
64                 )
65
66             self.draw.text((pos_x, self.height), line, font=font, fill=color)
67             self.height += line_height
68             # go to next line
69             text = text[len(line):]
70
71     def image(self):
72         """Creates the actual Image object."""
73         image = Image.new('RGBA', (self.max_width,
74                                    self.height + self.padding_y))
75         if self.shadow_img:
76             shadow = self.shadow_img.filter(ImageFilter.BLUR)
77             image.paste(shadow, (0, 0), shadow)
78             image.paste(self.img, (0, 0), self.img)
79         else:
80             image.paste(self.img, (0, 0))
81         return image
82
83
84 class Cover(object):
85     """Abstract base class for cover images generator."""
86     width = 600
87     height = 800
88     background_color = '#fff'
89     background_img = None
90
91     author_top = 100
92     author_margin_left = 20
93     author_margin_right = 20
94     author_lineskip = 40
95     author_color = '#000'
96     author_shadow = None
97     author_font = None
98
99     title_top = 100
100     title_margin_left = 20
101     title_margin_right = 20
102     title_lineskip = 54
103     title_color = '#000'
104     title_shadow = None
105     title_font = None
106
107     logo_bottom = None
108     logo_width = None
109     uses_dc_cover = False
110
111     format = 'JPEG'
112
113     exts = {
114         'JPEG': 'jpg',
115         'PNG': 'png',
116         }
117
118     mime_types = {
119         'JPEG': 'image/jpeg',
120         'PNG': 'image/png',
121         }
122
123     def __init__(self, book_info, format=None):
124         self.author = ", ".join(auth.readable() for auth in book_info.authors)
125         self.title = book_info.title
126         if format is not None:
127             self.format = format
128
129     def pretty_author(self):
130         """Allows for decorating author's name."""
131         return self.author
132
133     def pretty_title(self):
134         """Allows for decorating title."""
135         return self.title
136
137     def image(self):
138         img = Image.new('RGB', (self.width, self.height), self.background_color)
139
140         if self.background_img:
141             background = Image.open(self.background_img)
142             img.paste(background, None, background)
143             del background
144
145         # WL logo
146         if self.logo_width:
147             logo = Image.open(get_resource('res/wl-logo.png'))
148             logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
149             img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
150
151         top = self.author_top
152         tbox = TextBox(
153             self.width - self.author_margin_left - self.author_margin_right,
154             self.height - top,
155             )
156         author_font = self.author_font or ImageFont.truetype(
157             get_resource('fonts/DejaVuSerif.ttf'), 30)
158         tbox.text(self.pretty_author(), self.author_color, author_font,
159             self.author_lineskip, self.author_shadow)
160         text_img = tbox.image()
161         img.paste(text_img, (self.author_margin_left, top), text_img)
162
163         top += text_img.size[1] + self.title_top
164         tbox = TextBox(
165             self.width - self.title_margin_left - self.title_margin_right,
166             self.height - top,
167             )
168         title_font = self.author_font or ImageFont.truetype(
169             get_resource('fonts/DejaVuSerif.ttf'), 40)
170         tbox.text(self.pretty_title(), self.title_color, title_font,
171             self.title_lineskip, self.title_shadow)
172         text_img = tbox.image()
173         img.paste(text_img, (self.title_margin_left, top), text_img)
174
175         return img
176
177     def mime_type(self):
178         return self.mime_types[self.format]
179
180     def ext(self):
181         return self.exts[self.format]
182
183     def save(self, *args, **kwargs):
184         return self.image().save(format=self.format, *args, **kwargs)
185
186     def output_file(self, *args, **kwargs):
187         imgstr = StringIO()
188         self.save(imgstr, *args, **kwargs)
189         return OutputFile.from_string(imgstr.getvalue())
190
191
192 class WLCover(Cover):
193     """Default Wolne Lektury cover generator."""
194     width = 600
195     height = 833
196     uses_dc_cover = True
197     author_font = ImageFont.truetype(
198         get_resource('fonts/JunicodeWL-Regular.ttf'), 20)
199     author_lineskip = 30
200     title_font = ImageFont.truetype(
201         get_resource('fonts/DejaVuSerif-Bold.ttf'), 30)
202     title_lineskip = 40
203     title_box_width = 350
204     bar_width = 35
205     background_color = '#444'
206     author_color = '#444'
207     default_background = get_resource('res/cover.png')
208     format = 'JPEG'
209
210     epoch_colors = {
211         u'Starożytność': '#9e3610',
212         u'Średniowiecze': '#564c09',
213         u'Renesans': '#8ca629',
214         u'Barok': '#a6820a',
215         u'Oświecenie': '#f2802e',
216         u'Romantyzm': '#db4b16',
217         u'Pozytywizm': '#961060',
218         u'Modernizm': '#7784e0',
219         u'Dwudziestolecie międzywojenne': '#3044cf',
220         u'Współczesność': '#06393d',
221     }
222
223     def __init__(self, book_info, format=None, image_cache=None):
224         super(WLCover, self).__init__(book_info, format=format)
225         self.kind = book_info.kind
226         self.epoch = book_info.epoch
227         if book_info.cover_url:
228             url = book_info.cover_url
229             bg_src = None
230             if image_cache:
231                 from urllib import quote
232                 try:
233                     bg_src = URLOpener().open(image_cache + quote(url, safe=""))
234                 except:
235                     pass
236             if bg_src is None:
237                 bg_src = URLOpener().open(url)
238             self.background_img = StringIO(bg_src.read())
239             bg_src.close()
240         else:
241             self.background_img = self.default_background
242
243     def pretty_author(self):
244         return self.author.upper()
245
246     def image(self):
247         img = Image.new('RGB', (self.width, self.height), self.background_color)
248         draw = ImageDraw.Draw(img)
249
250         if self.epoch in self.epoch_colors:
251             epoch_color = self.epoch_colors[self.epoch]
252         else:
253             epoch_color = '#000'
254         draw.rectangle((0, 0, self.bar_width, self.height), fill=epoch_color)
255
256         if self.background_img:
257             src = Image.open(self.background_img)
258             trg_size = (self.width - self.bar_width, self.height)
259             if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
260                 resized = (
261                     trg_size[0],
262                     src.size[1] * trg_size[0] / src.size[0]
263                 )
264                 cut = (resized[1] - trg_size[1]) / 2
265                 src = src.resize(resized)
266                 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
267             else:
268                 resized = (
269                     src.size[0] * trg_size[1] / src.size[1],
270                     trg_size[1],
271                 )
272                 cut = (resized[0] - trg_size[0]) / 2
273                 src = src.resize(resized)
274                 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
275
276             img.paste(src, (self.bar_width, 0))
277             del src
278
279         box = TextBox(self.title_box_width, self.height, padding_y=20)
280         box.text(self.pretty_author(),
281                  font=self.author_font,
282                  line_height=self.author_lineskip,
283                  color=self.author_color,
284                  shadow_color=self.author_shadow,
285                 )
286
287         box.skip(10)
288         box.draw.line((75, box.height, 275, box.height),
289                 fill=self.author_color, width=2)
290         box.skip(15)
291
292         box.text(self.pretty_title(),
293                  line_height=self.title_lineskip,
294                  font=self.title_font,
295                  color=epoch_color,
296                  shadow_color=self.title_shadow,
297                 )
298         box_img = box.image()
299
300         if self.kind == 'Liryka':
301             # top
302             box_top = 100
303         elif self.kind == 'Epika':
304             # bottom
305             box_top = self.height - 100 - box_img.size[1]
306         else:
307             # center
308             box_top = (self.height - box_img.size[1]) / 2
309
310         box_left = self.bar_width + (self.width - self.bar_width -
311                         box_img.size[0]) / 2
312         draw.rectangle((box_left, box_top,
313             box_left + box_img.size[0], box_top + box_img.size[1]),
314             fill='#fff')
315         img.paste(box_img, (box_left, box_top), box_img)
316
317         return img
318
319
320
321 class VirtualoCover(Cover):
322     width = 600
323     height = 730
324     author_top = 73
325     title_top = 73
326     logo_bottom = 25
327     logo_width = 250
328
329
330 class PrestigioCover(Cover):
331     width = 580
332     height = 783
333     background_img = get_resource('res/cover-prestigio.png')
334
335     author_top = 446
336     author_margin_left = 118
337     author_margin_right = 62
338     author_lineskip = 60
339     author_color = '#fff'
340     author_shadow = '#000'
341     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
342
343     title_top = 0
344     title_margin_left = 118
345     title_margin_right = 62
346     title_lineskip = 60
347     title_color = '#fff'
348     title_shadow = '#000'
349     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
350
351     def pretty_title(self):
352         return u"„%s”" % self.title
353
354
355 class BookotekaCover(Cover):
356     width = 2140
357     height = 2733
358     background_img = get_resource('res/cover-bookoteka.png')
359
360     author_top = 480
361     author_margin_left = 307
362     author_margin_right = 233
363     author_lineskip = 156
364     author_color = '#d9d919'
365     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
366
367     title_top = 400
368     title_margin_left = 307
369     title_margin_right = 233
370     title_lineskip = 168
371     title_color = '#d9d919'
372     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
373
374     format = 'PNG'
375
376
377 class GandalfCover(Cover):
378     width = 600
379     height = 730
380     background_img = get_resource('res/cover-gandalf.png')
381     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
382     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
383     logo_bottom = 25
384     logo_width = 250
385     format = 'PNG'