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 librarian.utils import get_resource
8 from .. import Cover, Metric, TextBox
12 """Default Wolne Lektury cover generator."""
13 format_name = u"WL-style cover image"
18 author_font_ttf = get_resource('fonts/JunicodeWL-Regular.ttf')
21 title_font_ttf = get_resource('fonts/DejaVuSerif-Bold.ttf')
27 box_bottom_margin = 100
39 background_color = '#444'
41 default_background = get_resource('res/cover.png')
45 u'Starożytność': '#9e3610',
46 u'Średniowiecze': '#564c09',
47 u'Renesans': '#8ca629',
49 u'Oświecenie': '#f2802e',
50 u'Romantyzm': '#db4b16',
51 u'Pozytywizm': '#961060',
52 u'Modernizm': '#7784e0',
53 u'Dwudziestolecie międzywojenne': '#3044cf',
54 u'Współczesność': '#06393d',
57 def __init__(self, doc, format=None, width=None, height=None, with_logo=False):
58 super(WLCover, self).__init__(doc, format=format, width=width, height=height)
59 self.kind = doc.meta.get_one('kind')
60 self.epoch = doc.meta.get_one('epoch')
61 self.with_logo = with_logo
63 if doc.meta.get('cover_url'):
64 url = doc.meta.get('cover_url')[0]
67 bg_src = URLOpener().open(url)
68 self.background_img = StringIO(bg_src.read())
71 self.background_img = self.default_background
73 def pretty_author(self):
74 return self.author.upper()
77 metr = Metric(self, self.scale)
78 img = Image.new('RGB', (metr.width, metr.height), self.background_color)
79 draw = ImageDraw.Draw(img)
81 if self.epoch in self.epoch_colors:
82 epoch_color = self.epoch_colors[self.epoch]
85 draw.rectangle((0, 0, metr.bar_width, metr.height), fill=epoch_color)
87 if self.background_img:
88 src = Image.open(self.background_img)
89 trg_size = (metr.width - metr.bar_width, metr.height)
90 if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
93 src.size[1] * trg_size[0] / src.size[0]
95 cut = (resized[1] - trg_size[1]) / 2
96 src = src.resize(resized, Image.ANTIALIAS)
97 src = src.crop((0, cut, src.size[0], src.size[1] - cut))
100 src.size[0] * trg_size[1] / src.size[1],
103 cut = (resized[0] - trg_size[0]) / 2
104 src = src.resize(resized, Image.ANTIALIAS)
105 src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))
107 img.paste(src, (metr.bar_width, 0))
110 box = TextBox(metr.title_box_width, metr.height, padding_y=metr.box_padding_y)
111 author_font = ImageFont.truetype(
112 self.author_font_ttf, metr.author_font_size)
113 box.text(self.pretty_author(),
115 line_height=metr.author_lineskip,
116 color=self.author_color,
117 shadow_color=self.author_shadow,
120 box.skip(metr.box_above_line)
121 box.draw.line((metr.box_line_left, box.height, metr.box_line_right, box.height),
122 fill=self.author_color, width=metr.box_line_width)
123 box.skip(metr.box_below_line)
125 title_font = ImageFont.truetype(
126 self.title_font_ttf, metr.title_font_size)
127 box.text(self.pretty_title(),
128 line_height=metr.title_lineskip,
131 shadow_color=self.title_shadow,
135 logo = Image.open(get_resource('res/wl-logo-mono.png'))
136 logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]), Image.ANTIALIAS)
137 alpha = logo.split()[3]
138 alpha = ImageEnhance.Brightness(alpha).enhance(.75)
140 box.skip(metr.logo_top + logo.size[1])
142 box_img = box.image()
144 if self.kind == 'Liryka':
146 box_top = metr.box_top_margin
147 elif self.kind == 'Epika':
149 box_top = metr.height - metr.box_bottom_margin - box_img.size[1]
152 box_top = (metr.height - box_img.size[1]) / 2
154 box_left = metr.bar_width + (metr.width - metr.bar_width -
156 draw.rectangle((box_left, box_top,
157 box_left + box_img.size[0], box_top + box_img.size[1]),
159 img.paste(box_img, (box_left, box_top), box_img)
163 (box_left + (box_img.size[0] - logo.size[0]) / 2,
164 box_top + box_img.size[1] - metr.box_padding_y - logo.size[1]), mask=logo)