style
[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 from PIL import Image, ImageFont, ImageDraw, ImageFilter
8 from StringIO import StringIO
9 from librarian import get_resource, IOFile
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         try:
125             self.author = ", ".join(auth.readable() for auth in book_info.authors)
126         except AttributeError:
127             self.author = ""
128         self.title = book_info.title
129         if format is not None:
130             self.format = format
131
132     def pretty_author(self):
133         """Allows for decorating author's name."""
134         return self.author
135
136     def pretty_title(self):
137         """Allows for decorating title."""
138         return self.title
139
140     def image(self):
141         img = Image.new('RGB', (self.width, self.height), self.background_color)
142
143         if self.background_img:
144             background = Image.open(self.background_img)
145             img.paste(background, None, background)
146             del background
147
148         # WL logo
149         if self.logo_width:
150             logo = Image.open(get_resource('res/wl-logo.png'))
151             logo = logo.resize((self.logo_width, logo.size[1] * self.logo_width / logo.size[0]))
152             img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom))
153
154         top = self.author_top
155         tbox = TextBox(
156             self.width - self.author_margin_left - self.author_margin_right,
157             self.height - top)
158         author_font = self.author_font or ImageFont.truetype(
159             get_resource('fonts/DejaVuSerif.ttf'), 30)
160         tbox.text(self.pretty_author(), self.author_color, author_font,
161                   self.author_lineskip, self.author_shadow)
162         text_img = tbox.image()
163         img.paste(text_img, (self.author_margin_left, top), text_img)
164
165         top += text_img.size[1] + self.title_top
166         tbox = TextBox(
167             self.width - self.title_margin_left - self.title_margin_right,
168             self.height - top)
169         title_font = self.author_font or ImageFont.truetype(
170             get_resource('fonts/DejaVuSerif.ttf'), 40)
171         tbox.text(self.pretty_title(), self.title_color, title_font,
172                   self.title_lineskip, self.title_shadow)
173         text_img = tbox.image()
174         img.paste(text_img, (self.title_margin_left, top), text_img)
175
176         return img
177
178     def mime_type(self):
179         return self.mime_types[self.format]
180
181     def ext(self):
182         return self.exts[self.format]
183
184     def save(self, *args, **kwargs):
185         return self.image().save(format=self.format, *args, **kwargs)
186
187     def output_file(self, *args, **kwargs):
188         imgstr = StringIO()
189         self.save(imgstr, *args, **kwargs)
190         return IOFile.from_string(imgstr.getvalue())
191
192     def for_pdf(self):
193         return IOFile.from_filename(get_resource('pdf/cover_image.sty'), {
194             'cover.png': self.output_file(),
195         })