1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from __future__ import unicode_literals
9 from PIL import Image, ImageFont, ImageDraw, ImageFilter
10 from six import BytesIO
11 from librarian import get_resource, OutputFile, URLOpener
15 """Gets metrics from an object, scaling it by a factor."""
16 def __init__(self, obj, scale):
18 self._scale = float(scale)
20 def __getattr__(self, name):
21 src = getattr(self._obj, name)
22 if src and self._scale:
23 return type(src)(self._scale * src)
28 class TextBox(object):
29 """Creates an Image with a series of centered strings."""
35 def __init__(self, max_width, max_height, padding_x=None, padding_y=None, bar_width=0, bar_color=None):
37 padding_x = self.SHADOW_X + self.SHADOW_BLUR
39 padding_y = self.SHADOW_Y + self.SHADOW_BLUR
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
53 def skip(self, height):
54 """Skips some vertical space."""
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)
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)
67 line_width = self.draw.textsize(line, font=font)[0]
68 while line_width > self.max_text_width:
69 parts = line.rsplit(' ', 1)
71 line_width = self.max_text_width
75 if len(line) > 2 and line[-2] == ' ':
78 line_width = self.draw.textsize(line, font=font)[0]
79 line = line.strip() + ' '
82 pos_x = (self.max_width - line_width) // 2
84 pos_x = self.bar_width + self.padding_x
87 self.shadow_draw.text(
88 (pos_x + self.SHADOW_X, self.height + self.SHADOW_Y),
89 line, font=font, fill=shadow_color
92 self.draw.text((pos_x, self.height), line, font=font, fill=color)
93 self.height += line_height
95 text = text[len(line):]
98 """Creates the actual Image object."""
99 image = Image.new('RGBA', (self.max_width,
100 int(round(self.height + self.padding_y))))
103 shadow = self.shadow_img.filter(ImageFilter.BLUR)
104 image.paste(shadow, (0, 0), shadow)
105 image.paste(self.img, (0, 0), self.img)
107 image.paste(self.img, (0, 0))
112 """Abstract base class for cover images generator."""
115 background_color = '#fff'
116 background_img = None
119 author_margin_left = 20
120 author_margin_right = 20
122 author_color = '#000'
124 author_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
125 author_font_size = 30
128 title_margin_left = 20
129 title_margin_right = 20
133 title_font_ttf = get_resource('fonts/DejaVuSerif.ttf')
138 uses_dc_cover = False
150 'JPEG': 'image/jpeg',
154 def __init__(self, book_info, format=None, width=None, height=None, cover_logo=None):
155 self.book_info = book_info
156 self.authors = [auth.readable() for auth in book_info.authors]
157 self.title = book_info.title
158 if format is not None:
160 self.set_size(width, height)
162 def set_size(self, width, height):
164 self.height = int(round(height * self.width / width))
165 scale = max(float(width or 0) / self.width,
166 float(height or 0) / self.height)
170 self.scale_after = scale
172 def pretty_authors(self):
173 """Allows for decorating authors' names."""
176 def pretty_title(self):
177 """Allows for decorating title."""
181 metr = Metric(self, self.scale)
182 img = Image.new('RGB', (metr.width, metr.height),
183 self.background_color)
185 if self.background_img:
186 background = Image.open(self.background_img)
187 img.paste(background, None, background)
192 logo = Image.open(get_resource('res/wl-logo.png'))
195 int(round(logo.size[1] * metr.logo_width / logo.size[0]))
198 (metr.width - metr.logo_width) // 2,
199 img.size[1] - logo.size[1] - metr.logo_bottom
202 top = metr.author_top
204 metr.width - metr.author_margin_left - metr.author_margin_right,
208 author_font = ImageFont.truetype(
209 self.author_font_ttf, metr.author_font_size,
210 layout_engine=ImageFont.LAYOUT_BASIC)
211 for pa in self.pretty_authors():
212 tbox.text(pa, self.author_color, author_font, metr.author_lineskip,
214 text_img = tbox.image()
215 img.paste(text_img, (metr.author_margin_left, top), text_img)
217 top += text_img.size[1] + metr.title_top
219 metr.width - metr.title_margin_left - metr.title_margin_right,
222 title_font = ImageFont.truetype(
223 self.title_font_ttf, metr.title_font_size,
224 layout_engine=ImageFont.LAYOUT_BASIC)
225 tbox.text(self.pretty_title(), self.title_color, title_font,
226 metr.title_lineskip, self.title_shadow)
227 text_img = tbox.image()
228 img.paste(text_img, (metr.title_margin_left, top), text_img)
232 def final_image(self):
234 if self.scale_after != 1:
236 int(round(img.size[0] * self.scale_after)),
237 int(round(img.size[1] * self.scale_after))),
242 return self.mime_types[self.format]
245 return self.exts[self.format]
247 def save(self, *args, **kwargs):
249 'format': self.format,
252 default_kwargs.update(kwargs)
253 return self.final_image().save(*args, **default_kwargs)
255 def output_file(self, *args, **kwargs):
257 self.save(imgstr, *args, **kwargs)
258 return OutputFile.from_bytes(imgstr.getvalue())
261 class WLCover(Cover):
262 """Wolne Lektury cover without logos."""
266 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
267 author_font_size = 20
269 author_centering = True
270 title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
273 title_box_width = 350
274 title_centering = True
277 box_bottom_margin = 100
292 box_position = 'middle'
293 background_color = '#444'
294 author_color = '#444'
295 background_img = get_resource('res/cover.png')
296 background_top = False
300 u'Starożytność': '#9e3610',
301 u'Średniowiecze': '#564c09',
302 u'Renesans': '#8ca629',
304 u'Oświecenie': '#f2802e',
305 u'Romantyzm': '#db4b16',
306 u'Pozytywizm': '#961060',
307 u'Modernizm': '#7784e0',
308 u'Dwudziestolecie międzywojenne': '#3044cf',
309 u'Współczesność': '#06393d',
311 set_title_color = True
313 kind_box_position = {
318 def __init__(self, book_info, format=None, width=None, height=None,
319 bleed=0, cover_logo=None):
320 super(WLCover, self).__init__(book_info, format=format, width=width,
323 self.slug = book_info.url.slug
325 self.box_position = book_info.cover_box_position or \
326 self.kind_box_position.get(book_info.kind, self.box_position)
328 if book_info.cover_bar_color == 'none':
331 self.bar_color = book_info.cover_bar_color or \
332 self.get_variable_color(book_info) or self.bar_color
334 if self.set_title_color:
335 self.title_color = self.get_variable_color(book_info) or self.title_color
338 self.box_top_margin += bleed
339 self.box_bottom_margin += bleed
340 self.bar_width += bleed
342 if book_info.cover_url:
343 url = book_info.cover_url
349 bg_src = requests.get(url, timeout=5)
350 self.background_img = BytesIO(bg_src.content)
352 except Exception as e:
360 def get_variable_color(self, book_info):
361 return self.epoch_colors.get(book_info.epoch, None)
363 def pretty_authors(self):
364 return [a.upper() for a in self.authors]
366 def add_box(self, img):
367 if self.box_position == 'none':
370 metr = Metric(self, self.scale)
373 box = TextBox(metr.title_box_width - 2 * self.bleed, metr.height,
374 padding_y=metr.box_padding_y,
375 padding_x=metr.box_padding_x,
376 bar_width=metr.box_bar_width,
377 bar_color=self.bar_color,
379 author_font = ImageFont.truetype(
380 self.author_font_ttf, metr.author_font_size,
381 layout_engine=ImageFont.LAYOUT_BASIC)
382 for pa in self.pretty_authors():
383 box.text(pa, font=author_font, line_height=metr.author_lineskip,
384 color=self.author_color, shadow_color=self.author_shadow,
385 centering=self.author_centering
388 box.skip(metr.box_above_line)
390 (metr.box_line_left, box.height, metr.box_line_right, box.height),
391 fill=self.author_color, width=metr.box_line_width
393 box.skip(metr.box_below_line)
396 title_font = ImageFont.truetype(
397 self.title_font_ttf, metr.title_font_size,
398 layout_engine=ImageFont.LAYOUT_BASIC)
399 box.text(self.pretty_title(),
400 line_height=metr.title_lineskip,
402 color=self.title_color,
403 shadow_color=self.title_shadow,
404 centering=self.title_centering
407 box_img = box.image()
410 if self.box_position == 'bottom' or box_img.size[1] + metr.box_top_margin + metr.box_bottom_margin > metr.height:
411 box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
412 elif self.box_position == 'top':
413 box_top = metr.box_top_margin
415 box_top = (metr.height - box_img.size[1]) // 2
417 box_left = metr.bar_width + (
418 metr.width - metr.bar_width - box_img.size[0] - self.bleed
421 # Draw the white box.
422 img_draw = ImageDraw.Draw(img)
427 box_left + box_img.size[0],
428 box_top + box_img.size[1]
432 # Paste the contents into the white box.
433 img.paste(box_img, (box_left, box_top), box_img)
434 if self.box_bar_width:
439 box_left + metr.box_bar_width,
440 box_top + box_img.size[1]
446 def add_cut_lines(self, img):
450 metr = Metric(self, self.scale)
451 draw = ImageDraw.Draw(img)
452 for corner_x, corner_y in (
453 (0, 0), (metr.width, 0),
454 (0, metr.height), (metr.width, metr.height)
456 dir_x = 1 if corner_x == 0 else -1
457 dir_y = 1 if corner_y == 0 else -1
458 for offset in (-1, 0, 1):
462 corner_y + dir_y * metr.bleed + offset,
463 corner_x + dir_x * metr.bleed * line_ratio,
464 corner_y + dir_y * metr.bleed + offset
466 fill='black' if offset == 0 else 'white',
471 corner_x + dir_x * metr.bleed + offset,
473 corner_x + dir_x * metr.bleed + offset,
474 corner_y + dir_y * metr.bleed * line_ratio
476 fill='black' if offset == 0 else 'white',
482 metr = Metric(self, self.scale)
483 img = Image.new('RGB', (metr.width, metr.height),
484 self.background_color)
485 draw = ImageDraw.Draw(img)
487 draw.rectangle((0, 0, metr.bar_width, metr.height),
490 if self.background_img:
491 src = Image.open(self.background_img)
492 trg_size = (metr.width - metr.bar_width, metr.height)
493 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
496 int(round(src.size[1] * trg_size[0] / src.size[0]))
498 if self.background_top:
501 cut = (resized[1] - trg_size[1]) // 2
502 src = src.resize(resized, Image.ANTIALIAS)
503 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
506 int(round(src.size[0] * trg_size[1] / src.size[1])),
509 cut = (resized[0] - trg_size[0]) // 2
510 src = src.resize(resized, Image.ANTIALIAS)
511 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
513 img.paste(src, (metr.bar_width, 0))
516 img = self.add_box(img)
518 # img = self.add_cut_lines(img)
523 class WLNoBoxCover(WLCover):
524 def add_box(self, img):
528 class LogoWLCover(WLCover):
531 gradient_logo_height = 57
532 gradient_logo_max_width = 200
533 gradient_logo_margin_right = 30
534 gradient_logo_spacing = 40
535 gradient_color = '#000'
536 gradient_opacity = .6
538 'res/wl-logo-white.png',
539 'res/fnp-logo-white.png',
542 annotation_height = 10
545 gradient_logo_centering = False
550 def __init__(self, book_info, *args, cover_logo=None, **kwargs):
551 super(LogoWLCover, self).__init__(book_info, *args, **kwargs)
552 self.gradient_height += self.bleed
553 self.gradient_logo_margin_right += self.bleed
555 self.additional_cover_logos = [
556 BytesIO(URLOpener().open(cover_logo_url).read())
557 for cover_logo_url in book_info.cover_logo_urls
559 self.end_cover_logos = []
561 self.additional_cover_logos.append(
562 open(cover_logo, 'rb')
566 def has_gradient_logos(self):
567 return self.gradient_logos or self.additional_cover_logos or self.end_cover_logos or self.annotation or self.qrcode is not None
569 def add_gradient_logos(self, img, metr):
570 gradient = Image.new(
572 (metr.width - metr.bar_width, metr.gradient_height),
575 gradient_mask = Image.new(
577 (metr.width - metr.bar_width, metr.gradient_height)
579 draw = ImageDraw.Draw(gradient_mask)
580 for line in range(0, metr.gradient_easing):
582 (0, line, metr.width - metr.bar_width, line),
584 255 * self.gradient_opacity * line / metr.gradient_easing
588 0, metr.gradient_easing,
589 metr.width - metr.bar_width, metr.gradient_height
590 ), fill=int(255 * self.gradient_opacity))
594 (metr.bar_width, metr.height - metr.gradient_height),
598 cursor = metr.width - metr.gradient_logo_margin_right
600 cursor = metr.gradient_logo_margin_right
603 - metr.gradient_height / 2
604 - metr.gradient_logo_height / 2 - metr.bleed
608 get_resource(logo_path)
609 for logo_path in self.gradient_logos
612 logos = self.additional_cover_logos + logos + self.end_cover_logos
615 Image.open(logo_bytes).convert('RGBA')
616 for logo_bytes in logos
619 if self.qrcode is not None:
621 logos.append(qrcode.make(f'https://wolnelektury.pl/katalog/lektura/{self.slug}/?{self.qrcode}'))
626 # See if logos fit into the gradient. If not, scale down accordingly.
630 - 2 * metr.gradient_logo_margin_right + self.bleed
634 metr.gradient_logo_max_width,
635 logo.size[0] * metr.gradient_logo_height / logo.size[1]
638 taken_space = sum(widths) + (len(logos) - 1) * metr.gradient_logo_spacing
639 if taken_space > space_for_logos:
640 logo_scale = space_for_logos / taken_space
643 if self.gradient_logo_centering:
644 cursor += int((space_for_logos - taken_space) / 2)
646 space_for_logos / taken_space
647 if taken_space > space_for_logos else 1
649 #logo_top += int(metr.gradient_logo_height * (1 - logo_scale) / 2)
651 for i, logo in enumerate(logos):
656 L_top = logo_top + int(metr.gradient_logo_height * (1 - L_scale) / 2)
660 int(round(widths[i] * L_scale)),
662 logo.size[1] * widths[i] / logo.size[0] * L_scale
667 cursor -= logo.size[0]
674 #int(round(logo_top + (metr.gradient_logo_height - logo.size[1]) * L_scale / 2))
679 cursor -= int(round(metr.gradient_logo_spacing * logo_scale))
681 cursor += int(round(metr.gradient_logo_spacing * logo_scale)) + logo.size[0]
684 img2 = Image.new('RGBA', (metr.height, metr.height), color=None)
685 draw = ImageDraw.Draw(img2)
686 author_font = ImageFont.truetype(
687 self.author_font_ttf, metr.annotation_height,
688 layout_engine=ImageFont.LAYOUT_BASIC)
689 draw.text((self.annotation_height, self.annotation_height), self.annotation, font=author_font, fill='#FFFFFF')
691 img2 = img2.rotate(90)
694 img.alpha_composite(img2, (0, 0))
695 img = img.convert('RGB')
700 img = super(LogoWLCover, self).image()
701 metr = Metric(self, self.scale)
702 if self.has_gradient_logos:
703 img = self.add_gradient_logos(img, metr)
707 class LegimiCover(LogoWLCover):
713 author_font_ttf = get_resource('fonts/Lora-Regular.ttf')
714 author_font_size = 15
715 author_lineskip = 19.5
716 author_centering = False
717 title_font_ttf = get_resource('fonts/Lora-Bold.ttf')
719 title_lineskip = 19.5
720 title_centering = False
722 title_box_width = 210
724 box_bottom_margin = 20
726 box_padding_y = 10 # do baseline
740 box_position = 'bottom'
741 background_color = '#444'
742 author_color = '#000'
744 set_title_color = False
746 kind_box_position = {}
748 box_bottom_margin_logos_add = 10
751 gradient_logo_height = 20
752 gradient_logo_max_width = 200
753 gradient_logo_margin_right = 10
754 gradient_logo_spacing = 20
755 gradient_color = '#000'
756 gradient_opacity = .5
758 'res/wl-logo-white.png',
761 gradient_logo_centering = True
762 background_top = True
765 'Artykuł': '#bf001a',
766 'Artykuł naukowy': '#bf001a',
767 'Dziennik': '#bf001a',
769 'Felieton': '#bf001a',
770 'Kronika': '#bf001a',
772 'Manifest': '#bf001a',
774 'Pamiętnik': '#bf001a',
775 'Poradnik': '#bf001a',
776 'Praca naukowa': '#bf001a',
777 'Publicystyka': '#bf001a',
778 'Reportaż': '#bf001a',
779 'Reportaż podróżniczy': '#bf001a',
780 'Rozprawa': '#bf001a',
781 'Rozprawa polityczna': '#bf001a',
782 'Traktat': '#bf001a',
790 def __init__(self, *args, **kwargs):
791 super().__init__(*args, **kwargs)
792 if self.has_gradient_logos:
793 self.box_bottom_margin += self.box_bottom_margin_logos_add
795 def pretty_authors(self):
798 def get_variable_color(self, book_info):
799 for genre in book_info.genres:
800 if genre in self.genre_colors:
801 return self.genre_colors[genre]
802 for kind in book_info.kinds:
803 if kind in self.kind_colors:
804 return self.kind_colors[kind]
806 class LegimiCornerCover(LegimiCover):
809 corner_image = get_resource('res/book-band.png')
812 image = super().image()
813 metr = Metric(self, self.scale)
815 if self.corner_image:
816 corner = Image.open(self.corner_image).convert('RGBA')
817 corner = corner.resize(
819 int(round(metr.corner_width)),
820 int(round(corner.size[1] / corner.size[0] * metr.corner_width))
823 image.paste(corner, (
824 metr.width - int(round(metr.corner_width)),
829 class LegimiAudiobookCover(LegimiCornerCover):
831 corner_image = get_resource('res/audiobook-sticker.png')
835 class EbookpointCover(LogoWLCover):
836 gradient_logo_height = 58
837 gradient_logo_spacing = 25
839 'res/ebookpoint-logo-white.png',
840 'res/wl-logo-white.png',
841 'res/fnp-logo-white.png',
845 class VirtualoCover(Cover):
854 class PrestigioCover(Cover):
857 background_img = get_resource('res/cover-prestigio.png')
860 author_margin_left = 118
861 author_margin_right = 62
863 author_color = '#fff'
864 author_shadow = '#000'
865 author_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
866 author_font_size = 50
869 title_margin_left = 118
870 title_margin_right = 62
873 title_shadow = '#000'
874 title_font_ttf = get_resource('fonts/JunicodeWL-Italic.ttf')
877 def pretty_title(self):
878 return u"„%s”" % self.title
881 class BookotekaCover(Cover):
884 background_img = get_resource('res/cover-bookoteka.png')
887 author_margin_left = 307
888 author_margin_right = 233
889 author_lineskip = 156
890 author_color = '#d9d919'
891 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
892 author_font_size = 130
895 title_margin_left = 307
896 title_margin_right = 233
898 title_color = '#d9d919'
899 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
900 title_font_size = 140
905 class GandalfCover(Cover):
908 background_img = get_resource('res/cover-gandalf.png')
909 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
910 author_font_size = 30
911 title_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
918 class KMLUCover(LogoWLCover):
919 gradient_logo_height = 58
920 gradient_logo_spacing = 25
922 'res/kmlu-logo-white.png',
923 'res/wl-logo-white.png',
924 'res/fnp-logo-white.png',
928 class MPWCover(LogoWLCover):
929 gradient_logo_height = 57
930 gradient_logo_spacing = 25
932 'res/mpw-logo-white.png',
933 'res/wl-logo-white.png',
934 'res/fnp-logo-white.png',
938 class AtriumCover(LogoWLCover):
939 gradient_logo_height = 58
940 gradient_logo_spacing = 25
942 'res/atrium-logo.png',
943 'res/wl-logo-white.png',
944 'res/fnp-logo-white.png',
948 class BNCover(LogoWLCover):
950 'res/dofinansowano.png',
953 'res/wl-logo-white.png',
955 # annotation = 'Zadanie „Udostępnienie publikacji w formatach cyfrowych” w ramach Narodowego Programu Rozwoju Czytelnictwa. Dofinansowano ze środków Ministra Kultury, Dziedzictwa Narodowego i Sportu.'
958 class FactoryCover(LogoWLCover):
961 'res/wl-logo-white.png',
963 qrcode = 'pk_campaign=factory22'
966 def __init__(self, *args, **kwargs):
967 kwargs.setdefault('bleed', 10)
968 return super().__init__(*args, **kwargs)
973 def output_file(self, *args, **kwargs):
974 imgfile = super().output_file(*args, **kwargs)
977 with tempfile.TemporaryDirectory(prefix='factory-', dir='.') as d:
980 with open(d + '/cover.png', 'wb') as f:
981 f.write(imgfile.get_bytes())
982 shutil.copy(get_resource('res/factory-cover.svg'), d)
983 subprocess.call(['inkscape', f'--export-filename={d}/cover.pdf', d + '/factory-cover.svg'])
984 with open(d + '/cover.pdf', 'rb') as f:
985 return OutputFile.from_bytes(f.read())
989 from librarian.covers.marquise import MarquiseCover, LabelMarquiseCover
992 'default': LogoWLCover,
995 'atrium': AtriumCover,
997 'legimi': LegimiCover,
998 'legimi-corner': LegimiCornerCover,
999 'legimi-audiobook': LegimiAudiobookCover,
1000 'factory': FactoryCover,
1002 'm-label': LabelMarquiseCover,
1006 def make_cover(book_info, *args, **kwargs):
1007 if 'cover_class' in kwargs:
1008 cover_class_name = kwargs.pop('cover_class')
1010 cover_class_name = book_info.cover_class
1011 cover_class = COVER_CLASSES[cover_class_name]
1012 return cover_class(book_info, *args, **kwargs)