ignore trailing spaces in dc, add curriculum fields
[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 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
107         box.skip(10)
108         box.draw.line((75, box.height, 275, box.height),
109                 fill=self.author_color, width=2)
110         box.skip(15)
111
112         box.text(self.pretty_title(),
113                  line_height=self.title_lineskip,
114                  font=self.title_font,
115                  color=epoch_color,
116                  shadow_color=self.title_shadow,
117                 )
118         box_img = box.image()
119
120         if self.kind == 'Liryka':
121             # top
122             box_top = 100
123         elif self.kind == 'Epika':
124             # bottom
125             box_top = self.height - 100 - box_img.size[1]
126         else:
127             # center
128             box_top = (self.height - box_img.size[1]) / 2
129
130         box_left = self.bar_width + (self.width - self.bar_width -
131                         box_img.size[0]) / 2
132         draw.rectangle((box_left, box_top,
133             box_left + box_img.size[0], box_top + box_img.size[1]),
134             fill='#fff')
135         img.paste(box_img, (box_left, box_top), box_img)
136
137         return img