format change
[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 Image, ImageFont, ImageDraw, ImageFilter
7 from librarian import get_resource
8
9
10 class Cover(object):
11     width = 600
12     height = 800
13     background_color = '#fff'
14     background_img = None
15
16     author_align = 'c'
17     author_top = 100
18     author_margin_left = 20
19     author_margin_right = 20
20     author_lineskip = 40
21     author_color = '#000'
22     author_shadow = None
23     author_font = None
24     author_wrap = True
25
26     title_align = 'c'
27     title_top = 100
28     title_margin_left = 20
29     title_margin_right = 20
30     title_lineskip = 54
31     title_color = '#000'
32     title_shadow = None
33     title_font = None
34     title_wrap = True
35
36     logo_bottom = None
37     logo_width = None
38
39     format = 'JPEG'
40
41
42     exts = {
43         'JPEG': 'jpg',
44         'PNG': 'png',
45         }
46
47     mime_types = {
48         'JPEG': 'image/jpeg',
49         'PNG': 'image/png',
50         }
51
52     @staticmethod
53     def person_shortener(text):
54         yield text
55         chunks = text.split()
56         n_chunks = len(chunks)
57         # make initials from given names, starting from last
58         for i in range(n_chunks - 2, -1, -1):
59             chunks[i] = chunks[i][0] + '.'
60             yield " ".join(chunks)
61         # remove given names initials, starting from last
62         while len(chunks) > 2:
63             del chunks[1]
64             yield " ".join(chunks)
65
66     @staticmethod
67     def title_shortener(text):
68         yield text
69         chunks = text.split()
70         n_chunks = len(chunks)
71         # remove words, starting from last one
72         while len(chunks) > 1:
73             del chunks[-1]
74             yield " ".join(chunks) + u'…'
75
76     @staticmethod
77     def draw_text(text, img, font, align, shortener, margin_left, width, pos_y, lineskip, color, shadow_color):
78         if shadow_color:
79             shadow_img = Image.new('RGBA', img.size)
80             shadow_draw = ImageDraw.Draw(shadow_img)
81         text_img = Image.new('RGBA', img.size)
82         text_draw = ImageDraw.Draw(text_img)
83         while text:
84             if shortener:
85                 for line in shortener(text):
86                     if text_draw.textsize(line, font=font)[0] <= width:
87                         break
88                 text = ''
89             else:
90                 line = text
91                 while text_draw.textsize(line, font=font)[0] > width:
92                     try:
93                         line, ext = line.rsplit(' ', 1)
94                     except:
95                         break
96                 text = text[len(line)+1:]
97             pos_x = margin_left
98             if align == 'c':
99                 pos_x += (width - text_draw.textsize(line, font=font)[0]) / 2
100             elif align == 'r':
101                 pos_x += (width - text_draw.textsize(line, font=font)[0])
102             if shadow_color:
103                 shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color)
104             text_draw.text((pos_x, pos_y), line, font=font, fill=color)
105             pos_y += lineskip
106         if shadow_color:
107             shadow_img = shadow_img.filter(ImageFilter.BLUR)
108             img.paste(shadow_img, None, shadow_img)
109         img.paste(text_img, None, text_img)
110         return pos_y
111
112
113     def __init__(self, author='', title=''):
114         self.author = author
115         self.title = title
116
117     def pretty_author(self):
118         return self.author
119
120     def pretty_title(self):
121         return self.title
122
123     def image(self):
124         img = Image.new('RGB', (self.width, self.height), self.background_color)
125
126         if self.background_img:
127             background = Image.open(self.background_img)
128             try:
129                 img.paste(background, None, background)
130             except ValueError, e:
131                 img.paste(background)
132             del background
133
134         # WL logo
135         if self.logo_width:
136             logo = Image.open(get_resource('res/wl-logo.png'))
137             logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
138             img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
139
140         author_font = self.author_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30)
141         author_shortener = None if self.author_wrap else self.person_shortener 
142         title_y = self.draw_text(self.pretty_author(), img, author_font, self.author_align, author_shortener,
143                     self.author_margin_left, self.width - self.author_margin_left - self.author_margin_right, self.author_top,
144                     self.author_lineskip, self.author_color, self.author_shadow) + self.title_top
145         title_shortener = None if self.title_wrap else self.title_shortener 
146         title_font = self.title_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40)
147         self.draw_text(self.pretty_title(), img, title_font, self.title_align, title_shortener,
148                     self.title_margin_left, self.width - self.title_margin_left - self.title_margin_right, title_y,
149                     self.title_lineskip, self.title_color, self.title_shadow)
150
151         return img
152
153     def mime_type(self):
154         return self.mime_types[self.format]
155
156     def ext(self):
157         return self.exts[self.format]
158
159     def save(self, *args, **kwargs):
160         return self.image().save(format=self.format, *args, **kwargs)
161
162
163
164 class VirtualoCover(Cover):
165     width = 600
166     height = 730
167     author_top = 73
168     title_top = 73
169     logo_bottom = 25
170     logo_width = 250
171
172
173 class PrestigioCover(Cover):
174     width = 580
175     height = 783
176     background_img = get_resource('res/cover-prestigio.png')
177
178     author_top = 446
179     author_margin_left = 118
180     author_margin_right = 62
181     author_lineskip = 60
182     author_color = '#fff'
183     author_shadow = '#000'
184     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
185
186     title_top = 0
187     title_margin_left = 118
188     title_margin_right = 62
189     title_lineskip = 60
190     title_color = '#fff'
191     title_shadow = '#000'
192     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
193
194     def pretty_title(self):
195         return u"„%s”" % self.title
196
197
198 class BookotekaCover(Cover):
199     width = 2140
200     height = 2733
201     background_img = get_resource('res/cover-bookoteka.png')
202
203     author_top = 480
204     author_margin_left = 307
205     author_margin_right = 233
206     author_lineskip = 156
207     author_color = '#d9d919'
208     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
209
210     title_top = 400
211     title_margin_left = 307
212     title_margin_right = 233
213     title_lineskip = 168
214     title_color = '#d9d919'
215     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
216
217     format = 'PNG'
218
219
220 class GandalfCover(Cover):
221     width = 600
222     height = 730
223     background_img = get_resource('res/cover-gandalf.png')
224     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
225     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
226     logo_bottom = 25
227     logo_width = 250
228     format = 'PNG'
229
230
231 class ArtaTechCover(Cover):
232     width = 600
233     height = 800
234     background_img = get_resource('res/cover-arta-tech.jpg')
235     author_top = 132
236     author_margin_left = 235
237     author_margin_right = 23
238     author_align = 'r'
239     author_font = ImageFont.truetype(get_resource('fonts/DroidSans.ttf'), 32)
240     author_color = '#555555'
241     author_wrap = False
242     title_top = 17
243     title_margin_right = 21
244     title_margin_left = 60
245     title_align = 'r'
246     title_font = ImageFont.truetype(get_resource('fonts/EBGaramond-Regular.ttf'), 42)
247     title_color = '#222222'
248     title_wrap = False
249     format = 'JPEG'
250
251     def pretty_author(self):
252         return self.author.upper()
253
254
255 def ImageCover(img):
256     """ a class factory for simple image covers """
257     img = Image.open(img)
258
259     class ImgCover(Cover):
260         def image(self):
261             return img
262
263         @property
264         def format(self):
265             return self.image().format
266
267     return ImgCover