9070344d103b7b55aa22954fdf479b72eef0474b
[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
122 class VirtualoCover(Cover):
123     width = 600
124     height = 730
125     author_top = 73
126     title_top = 73
127     logo_bottom = 25
128     logo_width = 250
129
130
131 class PrestigioCover(Cover):
132     width = 580
133     height = 783
134     background_img = get_resource('res/cover-prestigio.png')
135
136     author_top = 446
137     author_margin_left = 118
138     author_margin_right = 62
139     author_lineskip = 60
140     author_color = '#fff'
141     author_shadow = '#000'
142     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
143
144     title_top = 0
145     title_margin_left = 118
146     title_margin_right = 62
147     title_lineskip = 60
148     title_color = '#fff'
149     title_shadow = '#000'
150     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50)
151
152     def pretty_title(self):
153         return u"„%s”" % self.title
154
155
156 class BookotekaCover(Cover):
157     width = 2140
158     height = 2733
159     background_img = get_resource('res/cover-bookoteka.png')
160
161     author_top = 480
162     author_margin_left = 307
163     author_margin_right = 233
164     author_lineskip = 156
165     author_color = '#d9d919'
166     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 130)
167
168     title_top = 400
169     title_margin_left = 307
170     title_margin_right = 233
171     title_lineskip = 168
172     title_color = '#d9d919'
173     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 140)
174
175     format = 'PNG'
176
177
178 class GandalfCover(Cover):
179     width = 600
180     height = 730
181     background_img = get_resource('res/cover-gandalf.png')
182     author_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 30)
183     title_font = ImageFont.truetype(get_resource('fonts/JunicodeWL-Regular.ttf'), 40)
184     logo_bottom = 25
185     logo_width = 250
186     format = 'PNG'