some WLURI related fixes
[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_top = 100
17     author_margin_left = 20
18     author_margin_right = 20
19     author_lineskip = 40
20     author_color = '#000'
21     author_shadow = None
22     author_font = None
23
24     title_top = 100
25     title_margin_left = 20
26     title_margin_right = 20
27     title_lineskip = 54
28     title_color = '#000'
29     title_shadow = None
30     title_font = None
31
32     logo_bottom = None
33     logo_width = None
34
35     format = 'JPEG'
36
37
38     exts = {
39         'JPEG': 'jpg',
40         'PNG': 'png',
41         }
42
43     mime_types = {
44         'JPEG': 'image/jpeg',
45         'PNG': 'image/png',
46         }
47
48
49     @staticmethod
50     def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color):
51         if shadow_color:
52             shadow_img = Image.new('RGBA', img.size)
53             shadow_draw = ImageDraw.Draw(shadow_img)
54         text_img = Image.new('RGBA', img.size)
55         text_draw = ImageDraw.Draw(text_img)
56         while text:
57             line = text
58             while text_draw.textsize(line, font=font)[0] > width:
59                 try:
60                     line, ext = line.rsplit(' ', 1)
61                 except:
62                     break
63             pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2
64             if shadow_color:
65                 shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color)
66             text_draw.text((pos_x, pos_y), line, font=font, fill=color)
67             pos_y += lineskip
68             text = text[len(line)+1:]
69         if shadow_color:
70             shadow_img = shadow_img.filter(ImageFilter.BLUR)
71             img.paste(shadow_img, None, shadow_img)
72         img.paste(text_img, None, text_img)
73         return pos_y
74
75
76     def __init__(self, author='', title=''):
77         self.author = author
78         self.title = title
79
80     def pretty_author(self):
81         return self.author
82
83     def pretty_title(self):
84         return self.title
85
86     def image(self):
87         img = Image.new('RGB', (self.width, self.height), self.background_color)
88
89         if self.background_img:
90             background = Image.open(self.background_img)
91             img.paste(background, None, background)
92             del background
93
94         # WL logo
95         if self.logo_width:
96             logo = Image.open(get_resource('res/wl-logo.png'))
97             logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
98             img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
99
100         author_font = self.author_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30)
101         title_y = self.draw_centered_text(self.pretty_author(), img, author_font,
102                     self.author_margin_left, self.width - self.author_margin_left - self.author_margin_right, self.author_top,
103                     self.author_lineskip, self.author_color, self.author_shadow) + self.title_top
104         title_font = self.title_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40)
105         self.draw_centered_text(self.pretty_title(), img, title_font,
106                     self.title_margin_left, self.width - self.title_margin_left - self.title_margin_right, title_y,
107                     self.title_lineskip, self.title_color, self.title_shadow)
108
109         return img
110
111     def mime_type(self):
112         return self.mime_types[self.format]
113
114     def ext(self):
115         return self.exts[self.format]
116
117     def save(self, *args, **kwargs):
118         return self.image().save(format=self.format, *args, **kwargs)
119
120
121 class WLCover(Cover):
122     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 40)
123     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
124     logo_width = 250
125     logo_bottom = 20
126
127
128
129 class VirtualoCover(Cover):
130     width = 600
131     height = 730
132     author_top = 73
133     title_top = 73
134     logo_bottom = 25
135     logo_width = 250
136
137
138 class PrestigioCover(Cover):
139     width = 580
140     height = 783
141     background_img = get_resource('res/cover-prestigio.png')
142
143     author_top = 446
144     author_margin_left = 118
145     author_margin_right = 62
146     author_lineskip = 60
147     author_color = '#fff'
148     author_shadow = '#000'
149     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
150
151     title_top = 0
152     title_margin_left = 118
153     title_margin_right = 62
154     title_lineskip = 60
155     title_color = '#fff'
156     title_shadow = '#000'
157     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
158
159     def pretty_title(self):
160         return u"„%s”" % self.title
161
162
163 class BookotekaCover(Cover):
164     width = 2140
165     height = 2733
166     background_img = get_resource('res/cover-bookoteka.png')
167
168     author_top = 480
169     author_margin_left = 307
170     author_margin_right = 233
171     author_lineskip = 156
172     author_color = '#d9d919'
173     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
174
175     title_top = 400
176     title_margin_left = 307
177     title_margin_right = 233
178     title_lineskip = 168
179     title_color = '#d9d919'
180     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
181
182     format = 'PNG'
183
184
185 class GandalfCover(Cover):
186     width = 600
187     height = 730
188     background_img = get_resource('res/cover-gandalf.png')
189     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
190     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
191     logo_bottom = 25
192     logo_width = 250
193     format = 'PNG'