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 PIL import Image, ImageFont, ImageDraw
7 from PIL import ImageEnhance
9 from librarian.utils import get_resource
10 from .. import Cover, Metric, TextBox
14 """Default Wolne Lektury cover generator."""
15 format_name = u"WL-style cover image"
20 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
23 title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
29 box_bottom_margin = 100
41 background_color = '#444'
43 default_background = get_resource('res/cover.png')
47 u'Starożytność': '#9e3610',
48 u'Średniowiecze': '#564c09',
49 u'Renesans': '#8ca629',
51 u'Oświecenie': '#f2802e',
52 u'Romantyzm': '#db4b16',
53 u'Pozytywizm': '#961060',
54 u'Modernizm': '#7784e0',
55 u'Dwudziestolecie międzywojenne': '#3044cf',
56 u'Współczesność': '#06393d',
59 def __init__(self, doc, format=None, width=None, height=None, with_logo=False):
60 super(WLCover, self).__init__(doc, format=format, width=width, height=height)
61 self.kind = doc.meta.get_one('kind')
62 self.epoch = doc.meta.get_one('epoch')
63 self.with_logo = with_logo
65 # if doc.meta.get('cover_url'):
66 # url = doc.meta.get('cover_url')[0]
69 # bg_src = URLOpener().open(url)
70 # self.background_img = StringIO(bg_src.read())
73 self.background_img = self.default_background
75 def pretty_author(self):
76 return self.author.upper()
79 metr = Metric(self, self.scale)
80 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
81 draw = ImageDraw.Draw(img)
83 if self.epoch in self.epoch_colors:
84 epoch_color = self.epoch_colors[self.epoch]
87 draw.rectangle((0, 0, metr.bar_width, metr.height), fill=epoch_color)
89 if self.background_img:
90 src = Image.open(self.background_img)
91 trg_size = (metr.width - metr.bar_width, metr.height)
92 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
95 src.size[1] * trg_size[0] / src.size[0]
97 cut = (resized[1] - trg_size[1]) / 2
98 src = src.resize(resized, Image.ANTIALIAS)
99 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
102 src.size[0] * trg_size[1] / src.size[1],
105 cut = (resized[0] - trg_size[0]) / 2
106 src = src.resize(resized, Image.ANTIALIAS)
107 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
109 img.paste(src, (metr.bar_width, 0))
112 box = TextBox(metr.title_box_width, metr.height, padding_y=metr.box_padding_y)
113 author_font = ImageFont.truetype(
114 self.author_font_ttf, metr.author_font_size)
116 self.pretty_author(),
118 line_height=metr.author_lineskip,
119 color=self.author_color,
120 shadow_color=self.author_shadow,
123 box.skip(metr.box_above_line)
125 (metr.box_line_left, box.height, metr.box_line_right, box.height),
126 fill=self.author_color, width=metr.box_line_width)
127 box.skip(metr.box_below_line)
129 title_font = ImageFont.truetype(
130 self.title_font_ttf, metr.title_font_size)
133 line_height=metr.title_lineskip,
136 shadow_color=self.title_shadow,
140 logo = Image.open(get_resource('res/wl-logo-mono.png'))
141 logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]), Image.ANTIALIAS)
142 alpha = logo.split()[3]
143 alpha = ImageEnhance.Brightness(alpha).enhance(.75)
145 box.skip(metr.logo_top + logo.size[1])
147 box_img = box.image()
149 if self.kind == 'Liryka':
151 box_top = metr.box_top_margin
152 elif self.kind == 'Epika':
154 box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
157 box_top = (metr.height - box_img.size[1]) / 2
159 box_left = metr.bar_width + (metr.width - metr.bar_width - box_img.size[0]) / 2
160 draw.rectangle((box_left, box_top, box_left + box_img.size[0], box_top + box_img.size[1]), fill='#fff')
161 img.paste(box_img, (box_left, box_top), box_img)
166 (box_left + (box_img.size[0] - logo.size[0]) / 2,
167 box_top + box_img.size[1] - metr.box_padding_y - logo.size[1]), mask=logo)