dfb0b76bb584ade30880d372dd51c65644b90f21
[librarian.git] / src / 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 from __future__ import unicode_literals
7
8 import re
9 from PIL import Image, ImageFont, ImageDraw, ImageFilter
10 from six import BytesIO
11 from librarian import get_resource, OutputFile, URLOpener
12
13
14 class Metric(object):
15     """Gets metrics from an object, scaling it by a factor."""
16     def __init__(self, obj, scale):
17         self._obj = obj
18         self._scale = float(scale)
19
20     def __getattr__(self, name):
21         src = getattr(self._obj, name)
22         if src and self._scale:
23             return type(src)(self._scale * src)
24         else:
25             return src
26
27
28 class TextBox(object):
29     """Creates an Image with a series of centered strings."""
30
31     SHADOW_X = 3
32     SHADOW_Y = 3
33     SHADOW_BLUR = 3
34
35     def __init__(self, max_width, max_height, padding_x=None, padding_y=None, bar_width=0, bar_color=None):
36         if padding_x is None:
37             padding_x = self.SHADOW_X + self.SHADOW_BLUR
38         if padding_y is None:
39             padding_y = self.SHADOW_Y + self.SHADOW_BLUR
40
41         self.max_width = max_width
42         self.bar_width = bar_width
43         self.bar_color = bar_color
44         self.max_text_width = max_width - 2 * padding_x - bar_width
45         self.padding_x = padding_x
46         self.padding_y = padding_y
47         self.height = padding_y
48         self.img = Image.new('RGBA', (max_width, max_height))
49         self.draw = ImageDraw.Draw(self.img)
50         self.shadow_img = None
51         self.shadow_draw = None
52
53     def skip(self, height):
54         """Skips some vertical space."""
55         self.height += height
56
57     def text(self, text, color='#000', font=None, line_height=20,
58              shadow_color=None, centering=True):
59         """Writes some centered text."""
60         text = re.sub(r'\s+', ' ', text)
61         if shadow_color:
62             if not self.shadow_img:
63                 self.shadow_img = Image.new('RGBA', self.img.size)
64                 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
65         while text:
66             line = text
67             line_width = self.draw.textsize(line, font=font)[0]
68             while line_width > self.max_text_width:
69                 parts = line.rsplit(' ', 1)
70                 if len(parts) == 1:
71                     line_width = self.max_text_width
72                     break
73                 line = parts[0]
74
75                 if line[-2] == ' ':
76                     line = line[:-2]
77
78                 line_width = self.draw.textsize(line, font=font)[0]
79             line = line.strip() + ' '
80
81             if centering:
82                 pos_x = (self.max_width - line_width) // 2
83             else:
84                 pos_x = self.bar_width + self.padding_x
85
86             if shadow_color:
87                 self.shadow_draw.text(
88                         (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
89                         line, font=font, fill=shadow_color
90                 )
91
92             self.draw.text((pos_x, self.height), line, font=font, fill=color)
93             self.height += line_height
94             # go to next line
95             text = text[len(line):]
96
97     def image(self):
98         """Creates the actual Image object."""
99         image = Image.new('RGBA', (self.max_width,
100                                    int(round(self.height + self.padding_y))))
101
102         if self.shadow_img:
103             shadow = self.shadow_img.filter(ImageFilter.BLUR)
104             image.paste(shadow, (0, 0), shadow)
105             image.paste(self.img, (0, 0), self.img)
106         else:
107             image.paste(self.img, (0, 0))
108         return image
109
110
111 class Cover(object):
112     """Abstract base class for cover images generator."""
113     width = 600
114     height = 800
115     background_color = '#fff'
116     background_img = None
117
118     author_top = 100
119     author_margin_left = 20
120     author_margin_right = 20
121     author_lineskip = 40
122     author_color = '#000'
123     author_shadow = None
124     author_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
125     author_font_size = 30
126
127     title_top = 100
128     title_margin_left = 20
129     title_margin_right = 20
130     title_lineskip = 54
131     title_color = '#000'
132     title_shadow = None
133     title_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
134     title_font_size = 40
135
136     logo_bottom = None
137     logo_width = None
138     uses_dc_cover = False
139
140     format = 'JPEG'
141     scale = 1
142     scale_after = 1
143
144     exts = {
145         'JPEG': 'jpg',
146         'PNG': 'png',
147         }
148
149     mime_types = {
150         'JPEG': 'image/jpeg',
151         'PNG': 'image/png',
152         }
153
154     def __init__(self, book_info, format=None, width=None, height=None, cover_logo=None):
155         self.authors = [auth.readable() for auth in book_info.authors]
156         self.title = book_info.title
157         if format is not None:
158             self.format = format
159         if width and height:
160             self.height = int(round(height * self.width / width))
161         scale = max(float(width or 0) / self.width,
162                     float(height or 0) / self.height)
163         if scale >= 1:
164             self.scale = scale
165         elif scale:
166             self.scale_after = scale
167
168     def pretty_authors(self):
169         """Allows for decorating authors' names."""
170         return [self.authors]
171
172     def pretty_title(self):
173         """Allows for decorating title."""
174         return self.title
175
176     def image(self):
177         metr = Metric(self, self.scale)
178         img = Image.new('RGB', (metr.width, metr.height),
179                         self.background_color)
180
181         if self.background_img:
182             background = Image.open(self.background_img)
183             img.paste(background, None, background)
184             del background
185
186         # WL logo
187         if metr.logo_width:
188             logo = Image.open(get_resource('res/wl-logo.png'))
189             logo = logo.resize((
190                 metr.logo_width,
191                 int(round(logo.size[1] * metr.logo_width / logo.size[0]))
192             ))
193             img.paste(logo, (
194                 (metr.width - metr.logo_width) // 2,
195                 img.size[1] - logo.size[1] - metr.logo_bottom
196             ))
197
198         top = metr.author_top
199         tbox = TextBox(
200             metr.width - metr.author_margin_left - metr.author_margin_right,
201             metr.height - top,
202             )
203
204         author_font = ImageFont.truetype(
205             self.author_font_ttf, metr.author_font_size,
206             layout_engine=ImageFont.LAYOUT_BASIC)
207         for pa in self.pretty_authors():
208             tbox.text(pa, self.author_color, author_font, metr.author_lineskip,
209                       self.author_shadow)
210         text_img = tbox.image()
211         img.paste(text_img, (metr.author_margin_left, top), text_img)
212
213         top += text_img.size[1] + metr.title_top
214         tbox = TextBox(
215             metr.width - metr.title_margin_left - metr.title_margin_right,
216             metr.height - top,
217             )
218         title_font = ImageFont.truetype(
219             self.title_font_ttf, metr.title_font_size,
220             layout_engine=ImageFont.LAYOUT_BASIC)
221         tbox.text(self.pretty_title(), self.title_color, title_font,
222                   metr.title_lineskip, self.title_shadow)
223         text_img = tbox.image()
224         img.paste(text_img, (metr.title_margin_left, top), text_img)
225
226         return img
227
228     def final_image(self):
229         img = self.image()
230         if self.scale_after != 1:
231             img = img.resize((
232                     int(round(img.size[0] * self.scale_after)),
233                     int(round(img.size[1] * self.scale_after))),
234                 Image.ANTIALIAS)
235         return img
236
237     def mime_type(self):
238         return self.mime_types[self.format]
239
240     def ext(self):
241         return self.exts[self.format]
242
243     def save(self, *args, **kwargs):
244         default_kwargs = {
245                 'format': self.format,
246                 'quality': 95,
247         }
248         default_kwargs.update(kwargs)
249         return self.final_image().save(*args, **default_kwargs)
250
251     def output_file(self, *args, **kwargs):
252         imgstr = BytesIO()
253         self.save(imgstr, *args, **kwargs)
254         return OutputFile.from_bytes(imgstr.getvalue())
255
256
257 class WLCover(Cover):
258     """Wolne Lektury cover without logos."""
259     width = 600
260     height = 833
261     uses_dc_cover = True
262     author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
263     author_font_size = 20
264     author_lineskip = 30
265     author_centering = True
266     title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
267     title_font_size = 30
268     title_lineskip = 40
269     title_box_width = 350
270     title_centering = True
271
272     box_top_margin = 100
273     box_bottom_margin = 100
274     box_padding_y = 20
275     box_above_line = 10
276     box_below_line = 15
277     box_line_left = 75
278     box_line_right = 275
279     box_line_width = 2
280     box_padding_x = 0
281     box_bar_width = 0
282
283     logo_top = 15
284     logo_width = 140
285
286     bar_width = 35
287     bar_color = '#000'
288     box_position = 'middle'
289     background_color = '#444'
290     author_color = '#444'
291     background_img = get_resource('res/cover.png')
292     background_top = False
293     format = 'JPEG'
294
295     epoch_colors = {
296         u'Starożytność': '#9e3610',
297         u'Średniowiecze': '#564c09',
298         u'Renesans': '#8ca629',
299         u'Barok': '#a6820a',
300         u'Oświecenie': '#f2802e',
301         u'Romantyzm': '#db4b16',
302         u'Pozytywizm': '#961060',
303         u'Modernizm': '#7784e0',
304         u'Dwudziestolecie międzywojenne': '#3044cf',
305         u'Współczesność': '#06393d',
306     }
307     set_title_color = True
308
309     kind_box_position = {
310         u'Liryka': 'top',
311         u'Epika': 'bottom',
312     }
313
314     def __init__(self, book_info, format=None, width=None, height=None,
315                  bleed=0, cover_logo=None):
316         super(WLCover, self).__init__(book_info, format=format, width=width,
317                                       height=height)
318         # Set box position.
319         self.box_position = book_info.cover_box_position or \
320             self.kind_box_position.get(book_info.kind, self.box_position)
321         # Set bar color.
322         if book_info.cover_bar_color == 'none':
323             self.bar_width = 0
324         else:
325             self.bar_color = book_info.cover_bar_color or \
326                 self.get_variable_color(book_info) or self.bar_color
327         # Set title color.
328         if self.set_title_color:
329             self.title_color = self.get_variable_color(book_info) or self.title_color
330
331         self.bleed = bleed
332         self.box_top_margin += bleed
333         self.box_bottom_margin += bleed
334         self.bar_width += bleed
335
336         if book_info.cover_url:
337             url = book_info.cover_url
338             bg_src = None
339             if bg_src is None:
340                 bg_src = URLOpener().open(url)
341             self.background_img = BytesIO(bg_src.read())
342             bg_src.close()
343
344     def get_variable_color(self, book_info):
345         return self.epoch_colors.get(book_info.epoch, None)
346
347     def pretty_authors(self):
348         return [a.upper() for a in self.authors]
349
350     def add_box(self, img):
351         if self.box_position == 'none':
352             return img
353
354         metr = Metric(self, self.scale)
355
356         # Write author name.
357         box = TextBox(metr.title_box_width, metr.height,
358                       padding_y=metr.box_padding_y,
359                       padding_x=metr.box_padding_x,
360                       bar_width=metr.box_bar_width,
361                       bar_color=self.bar_color,
362                       )
363         author_font = ImageFont.truetype(
364             self.author_font_ttf, metr.author_font_size,
365             layout_engine=ImageFont.LAYOUT_BASIC)
366         for pa in self.pretty_authors():
367             box.text(pa, font=author_font, line_height=metr.author_lineskip,
368                      color=self.author_color, shadow_color=self.author_shadow,
369                      centering=self.author_centering
370                      )
371
372         box.skip(metr.box_above_line)
373         box.draw.line(
374             (metr.box_line_left, box.height, metr.box_line_right, box.height),
375             fill=self.author_color, width=metr.box_line_width
376         )
377         box.skip(metr.box_below_line)
378
379         # Write title.
380         title_font = ImageFont.truetype(
381             self.title_font_ttf, metr.title_font_size,
382             layout_engine=ImageFont.LAYOUT_BASIC)
383         box.text(self.pretty_title(),
384                  line_height=metr.title_lineskip,
385                  font=title_font,
386                  color=self.title_color,
387                  shadow_color=self.title_shadow,
388                  centering=self.title_centering
389                  )
390
391         box_img = box.image()
392
393         # Find box position.
394         if self.box_position == 'bottom' or box_img.size[1] + metr.box_top_margin + metr.box_bottom_margin > metr.height:
395             box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
396         elif self.box_position == 'top':
397             box_top = metr.box_top_margin
398         else:   # Middle.
399             box_top = (metr.height - box_img.size[1]) // 2
400
401         box_left = metr.bar_width + (
402             metr.width - metr.bar_width - box_img.size[0]
403         ) // 2
404
405         # Draw the white box.
406         img_draw = ImageDraw.Draw(img)
407         img_draw.rectangle(
408             (
409                 box_left,
410                 box_top,
411                 box_left + box_img.size[0],
412                 box_top + box_img.size[1]
413             ),
414             fill='#fff'
415         )
416         # Paste the contents into the white box.
417         img.paste(box_img, (box_left, box_top), box_img)
418         if self.box_bar_width:
419             img_draw.rectangle(
420                 (
421                     box_left,
422                     box_top,
423                     box_left + metr.box_bar_width,
424                     box_top + box_img.size[1]
425                 ),
426                 fill=self.bar_color
427             )
428         return img
429
430     def add_cut_lines(self, img):
431         line_ratio = 0.5
432         if self.bleed == 0:
433             return img
434         metr = Metric(self, self.scale)
435         draw = ImageDraw.Draw(img)
436         for corner_x, corner_y in (
437                 (0, 0), (metr.width, 0),
438                 (0, metr.height), (metr.width, metr.height)
439                 ):
440             dir_x = 1 if corner_x == 0 else -1
441             dir_y = 1 if corner_y == 0 else -1
442             for offset in (-1, 0, 1):
443                 draw.line(
444                     (
445                         corner_x,
446                         corner_y + dir_y * metr.bleed + offset,
447                         corner_x + dir_x * metr.bleed * line_ratio,
448                         corner_y + dir_y * metr.bleed + offset
449                     ),
450                     fill='black' if offset == 0 else 'white',
451                     width=1
452                 )
453                 draw.line(
454                     (
455                         corner_x + dir_x * metr.bleed + offset,
456                         corner_y,
457                         corner_x + dir_x * metr.bleed + offset,
458                         corner_y + dir_y * metr.bleed * line_ratio
459                     ),
460                     fill='black' if offset == 0 else 'white',
461                     width=1
462                 )
463         return img
464
465     def image(self):
466         metr = Metric(self, self.scale)
467         img = Image.new('RGB', (metr.width, metr.height),
468                         self.background_color)
469         draw = ImageDraw.Draw(img)
470
471         draw.rectangle((0, 0, metr.bar_width, metr.height),
472                        fill=self.bar_color)
473
474         if self.background_img:
475             src = Image.open(self.background_img)
476             trg_size = (metr.width - metr.bar_width, metr.height)
477             if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
478                 resized = (
479                     trg_size[0],
480                     int(round(src.size[1] * trg_size[0] / src.size[0]))
481                 )
482                 if self.background_top:
483                     cut = 0
484                 else:
485                     cut = (resized[1] - trg_size[1]) // 2
486                 src = src.resize(resized, Image.ANTIALIAS)
487                 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
488             else:
489                 resized = (
490                     int(round(src.size[0] * trg_size[1] / src.size[1])),
491                     trg_size[1],
492                 )
493                 cut = (resized[0] - trg_size[0]) // 2
494                 src = src.resize(resized, Image.ANTIALIAS)
495                 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
496
497             img.paste(src, (metr.bar_width, 0))
498             del src
499
500         img = self.add_box(img)
501
502         # img = self.add_cut_lines(img)
503
504         return img
505
506
507 class WLNoBoxCover(WLCover):
508     def add_box(self, img):
509         return img
510
511
512 class LogoWLCover(WLCover):
513     gradient_height = 90
514     gradient_easing = 90
515     gradient_logo_height = 57
516     gradient_logo_max_width = 200
517     gradient_logo_margin_right = 30
518     gradient_logo_spacing = 40
519     gradient_color = '#000'
520     gradient_opacity = .6
521     gradient_logos = [
522         'res/wl-logo-white.png',
523         'res/fnp-logo-white.png',
524     ]
525     annotation = None
526     annotation_height = 10
527
528     logos_right = True
529     gradient_logo_centering = False
530
531
532     def __init__(self, book_info, *args, cover_logo=None, **kwargs):
533         super(LogoWLCover, self).__init__(book_info, *args, **kwargs)
534         self.gradient_height += self.bleed
535         self.gradient_logo_margin_right += self.bleed
536
537         self.additional_cover_logos = [
538             BytesIO(URLOpener().open(cover_logo_url).read())
539             for cover_logo_url in book_info.cover_logo_urls
540         ]
541         self.end_cover_logos = []
542         if cover_logo:
543             self.additional_cover_logos.append(
544                     open(cover_logo, 'rb')
545                     )
546
547     @property
548     def has_gradient_logos(self):
549         return self.gradient_logos or self.additional_cover_logos or self.end_cover_logos or self.annotation
550
551     def add_gradient_logos(self, img, metr):
552         gradient = Image.new(
553             'RGBA',
554             (metr.width - metr.bar_width, metr.gradient_height),
555             self.gradient_color
556         )
557         gradient_mask = Image.new(
558             'L',
559             (metr.width - metr.bar_width, metr.gradient_height)
560         )
561         draw = ImageDraw.Draw(gradient_mask)
562         for line in range(0, metr.gradient_easing):
563             draw.line(
564                 (0, line, metr.width - metr.bar_width, line),
565                 fill=int(
566                     255 * self.gradient_opacity * line / metr.gradient_easing
567                 )
568             )
569         draw.rectangle((
570             0, metr.gradient_easing,
571             metr.width - metr.bar_width, metr.gradient_height
572         ), fill=int(255 * self.gradient_opacity))
573             
574             
575         img.paste(gradient,
576                   (metr.bar_width, metr.height - metr.gradient_height),
577                   mask=gradient_mask)
578
579         if self.logos_right:
580             cursor = metr.width - metr.gradient_logo_margin_right
581         else:
582             cursor = metr.gradient_logo_margin_right
583         logo_top = int(
584             metr.height
585             - metr.gradient_height / 2
586             - metr.gradient_logo_height / 2 - metr.bleed / 2
587         )
588
589         logos = [
590             get_resource(logo_path)
591             for logo_path in self.gradient_logos
592         ]
593
594         logos = self.additional_cover_logos + logos + self.end_cover_logos
595
596         if self.logos_right:
597             logos.reverse()
598
599         logos = [
600             Image.open(logo_bytes).convert('RGBA')
601             for logo_bytes in logos
602         ]
603
604         # See if logos fit into the gradient. If not, scale down accordingly.
605         space_for_logos = (
606             metr.width
607             - metr.bar_width
608             - 2 * metr.gradient_logo_margin_right
609         )
610         widths = [
611             min(
612                 metr.gradient_logo_max_width,
613                 logo.size[0] * metr.gradient_logo_height / logo.size[1]
614             )
615             for logo in logos]
616         taken_space = sum(widths) + (len(logos) - 1) * metr.gradient_logo_spacing
617         if taken_space > space_for_logos:
618             logo_scale = space_for_logos / taken_space
619         else:
620             logo_scale = 1
621             if self.gradient_logo_centering:
622                 cursor += int((space_for_logos - taken_space) / 2)
623         logo_scale = (
624             space_for_logos / taken_space
625             if taken_space > space_for_logos else 1
626         )
627         #logo_top += int(metr.gradient_logo_height * (1 - logo_scale) / 2)
628
629         for i, logo in enumerate(logos):
630             if i == -1:
631                 L_scale = 1
632             else:
633                 L_scale = logo_scale
634             L_top = logo_top + int(metr.gradient_logo_height * (1 - L_scale) / 2)
635
636             logo = logo.resize(
637                 (
638                     int(round(widths[i] * L_scale)),
639                     int(round(
640                         logo.size[1] * widths[i] / logo.size[0] * L_scale
641                     ))
642                 ),
643                 Image.ANTIALIAS)
644             if self.logos_right:
645                 cursor -= logo.size[0]
646
647             img.paste(
648                 logo,
649                 (
650                     cursor,
651                     L_top
652                     #int(round(logo_top + (metr.gradient_logo_height - logo.size[1]) * L_scale / 2))
653                 ),
654                 mask=logo
655             )
656             if self.logos_right:
657                 cursor -= int(round(metr.gradient_logo_spacing * logo_scale))
658             else:
659                 cursor += int(round(metr.gradient_logo_spacing * logo_scale)) + logo.size[0]
660
661         if self.annotation:
662             img2 = Image.new('RGBA', (metr.height, metr.height), color=None)
663             draw = ImageDraw.Draw(img2)
664             author_font = ImageFont.truetype(
665                 self.author_font_ttf, metr.annotation_height,
666                 layout_engine=ImageFont.LAYOUT_BASIC)
667             draw.text((self.annotation_height, self.annotation_height), self.annotation, font=author_font, fill='#FFFFFF')
668             img2.show()
669             img2 = img2.rotate(90)
670             img2.show()
671             img.putalpha(0)
672             img.alpha_composite(img2, (0, 0))
673             img = img.convert('RGB')
674
675         return img
676     
677     def image(self):
678         img = super(LogoWLCover, self).image()
679         metr = Metric(self, self.scale)
680         if self.has_gradient_logos:
681             img = self.add_gradient_logos(img, metr)
682         return img
683
684
685 class LegimiCover(LogoWLCover):
686     width = 210
687     height = 297
688     bar_width = 0
689     # Other bar
690
691     author_font_ttf = get_resource('fonts/Lora-Regular.ttf')
692     author_font_size = 15
693     author_lineskip = 19.5
694     author_centering = False
695     title_font_ttf = get_resource('fonts/Lora-Bold.ttf')
696     title_font_size = 15
697     title_lineskip = 19.5
698     title_centering = False
699     
700     title_box_width = 210
701
702     box_bottom_margin = 20
703     box_padding_x = 20
704     box_padding_y = 10   # do baseline
705     box_above_line = 6
706     box_below_line = 0
707     box_line_left = 0
708     box_line_right = 0
709
710     box_line_width = 0
711
712     box_bar_width = 20
713
714     #logo_top = 15
715     #logo_width = 140
716
717     bar_color = '#000'
718     box_position = 'bottom'
719     background_color = '#444'
720     author_color = '#000'
721     title_color = '#000'
722     set_title_color = False
723
724     kind_box_position = {}
725
726     box_bottom_margin_logos_add = 10
727     gradient_height = 30
728     gradient_easing = 0
729     gradient_logo_height = 20
730     gradient_logo_max_width = 200
731     gradient_logo_margin_right = 10
732     gradient_logo_spacing = 20
733     gradient_color = '#000'
734     gradient_opacity = .5
735     gradient_logos = [
736         'res/wl-logo-white.png',
737     ]
738     logos_right = False
739     gradient_logo_centering = True
740     background_top = True
741
742     genre_colors = {
743         'Artykuł': '#bf001a',
744         'Artykuł naukowy': '#bf001a',
745         'Dziennik': '#bf001a',
746         'Esej': '#bf001a',
747         'Felieton': '#bf001a',
748         'Kronika': '#bf001a',
749         'List': '#bf001a',
750         'Manifest': '#bf001a',
751         'Odczyt': '#bf001a',
752         'Pamiętnik': '#bf001a',
753         'Poradnik': '#bf001a',
754         'Praca naukowa': '#bf001a',
755         'Publicystyka': '#bf001a',
756         'Reportaż': '#bf001a',
757         'Reportaż podróżniczy': '#bf001a',
758         'Rozprawa': '#bf001a',
759         'Rozprawa polityczna': '#bf001a',
760         'Traktat': '#bf001a',
761     }
762     kind_colors = {
763         'Epika': '#9bbb2b',
764         'Liryka': '#3e626f',
765         'Dramat': '#ecbe00',
766     }
767
768     def __init__(self, *args, **kwargs):
769         super().__init__(*args, **kwargs)
770         if self.has_gradient_logos:
771             self.box_bottom_margin += self.box_bottom_margin_logos_add
772
773     def pretty_authors(self):
774         return self.authors
775
776     def get_variable_color(self, book_info):
777         for genre in book_info.genres:
778             if genre in self.genre_colors:
779                 return self.genre_colors[genre]
780         for kind in book_info.kinds:
781             if kind in self.kind_colors:
782                 return self.kind_colors[kind]
783
784 class LegimiCornerCover(LegimiCover):
785     gradient_logos = []
786     corner_width = 120
787     corner_image = get_resource('res/book-band.png')
788     
789     def image(self):
790         image = super().image()
791         metr = Metric(self, self.scale)
792
793         if self.corner_image:
794             corner = Image.open(self.corner_image).convert('RGBA')
795             corner = corner.resize(
796                 (
797                     int(round(metr.corner_width)),
798                     int(round(corner.size[1] / corner.size[0] * metr.corner_width))
799                 )
800             )
801             image.paste(corner, (
802                 metr.width - int(round(metr.corner_width)),
803                 0,
804             ), corner)
805         return image
806
807 class LegimiAudiobookCover(LegimiCornerCover):
808     corner_width = 82.5
809     corner_image = get_resource('res/audiobook-sticker.png')
810     height = 210
811
812
813 class EbookpointCover(LogoWLCover):
814     gradient_logo_height = 58
815     gradient_logo_spacing = 25
816     gradient_logos = [
817         'res/ebookpoint-logo-white.png',
818         'res/wl-logo-white.png',
819         'res/fnp-logo-white.png',
820     ]
821
822
823 class VirtualoCover(Cover):
824     width = 600
825     height = 730
826     author_top = 73
827     title_top = 73
828     logo_bottom = 25
829     logo_width = 250
830
831
832 class PrestigioCover(Cover):
833     width = 580
834     height = 783
835     background_img = get_resource('res/cover-prestigio.png')
836
837     author_top = 446
838     author_margin_left = 118
839     author_margin_right = 62
840     author_lineskip = 60
841     author_color = '#fff'
842     author_shadow = '#000'
843     author_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
844     author_font_size = 50
845
846     title_top = 0
847     title_margin_left = 118
848     title_margin_right = 62
849     title_lineskip = 60
850     title_color = '#fff'
851     title_shadow = '#000'
852     title_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
853     title_font_size = 50
854
855     def pretty_title(self):
856         return u"„%s”" % self.title
857
858
859 class BookotekaCover(Cover):
860     width = 2140
861     height = 2733
862     background_img = get_resource('res/cover-bookoteka.png')
863
864     author_top = 480
865     author_margin_left = 307
866     author_margin_right = 233
867     author_lineskip = 156
868     author_color = '#d9d919'
869     author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
870     author_font_size = 130
871
872     title_top = 400
873     title_margin_left = 307
874     title_margin_right = 233
875     title_lineskip = 168
876     title_color = '#d9d919'
877     title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
878     title_font_size = 140
879
880     format = 'PNG'
881
882
883 class GandalfCover(Cover):
884     width = 600
885     height = 730
886     background_img = get_resource('res/cover-gandalf.png')
887     author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
888     author_font_size = 30
889     title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
890     title_font_size = 40
891     logo_bottom = 25
892     logo_width = 250
893     format = 'PNG'
894
895
896 class KMLUCover(LogoWLCover):
897     gradient_logo_height = 58
898     gradient_logo_spacing = 25
899     gradient_logos = [
900         'res/kmlu-logo-white.png',
901         'res/wl-logo-white.png',
902         'res/fnp-logo-white.png',
903     ]
904
905
906 class MPWCover(LogoWLCover):
907     gradient_logo_height = 57
908     gradient_logo_spacing = 25
909     gradient_logos = [
910         'res/mpw-logo-white.png',
911         'res/wl-logo-white.png',
912         'res/fnp-logo-white.png',
913     ]
914
915
916 class AtriumCover(LogoWLCover):
917     gradient_logo_height = 58
918     gradient_logo_spacing = 25
919     gradient_logos = [
920         'res/atrium-logo.png',
921         'res/wl-logo-white.png',
922         'res/fnp-logo-white.png',
923     ]
924
925
926 class BNCover(LogoWLCover):
927     gradient_logos = [
928         'res/dofinansowano.png',
929         'res/MKIDN.jpg',
930         'res/BN.png',
931         'res/wl-logo-white.png',
932     ]
933 #    annotation = 'Zadanie „Udostępnienie publikacji w formatach cyfrowych” w ramach Narodowego Programu Rozwoju Czytelnictwa. Dofinansowano ze środków Ministra Kultury, Dziedzictwa Narodowego i Sportu.'
934
935
936 COVER_CLASSES = {
937     'default': LogoWLCover,
938     'kmlu': KMLUCover,
939     'mpw': MPWCover,
940     'atrium': AtriumCover,
941     'bn': BNCover,
942     'legimi': LegimiCover,
943     'legimi-corner': LegimiCornerCover,
944     'legimi-audiobook': LegimiAudiobookCover,
945 }
946
947
948 def make_cover(book_info, *args, **kwargs):
949     if 'cover_class' in kwargs:
950         cover_class_name = kwargs.pop('cover_class')
951     else:
952         cover_class_name = book_info.cover_class
953     cover_class = COVER_CLASSES[cover_class_name]
954     return cover_class(book_info, *args, **kwargs)