1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   5 from librarian import get_resource
 
   6 from librarian.cover import Metric
 
   7 from ..utils.textbox import TextBox, DoesNotFit, split_words
 
   8 from .base import Widget
 
  11 class AuthorBox(Widget):
 
  15     def __init__(self, cover, width):
 
  17         self.m = Metric(self, cover.m._scale)
 
  18         super().__init__(cover)
 
  21         author_font = PIL.ImageFont.truetype(
 
  22             get_resource('fonts/SourceSans3VF-Roman.ttf'),
 
  24             layout_engine=PIL.ImageFont.Layout.BASIC
 
  26         author_font.set_variation_by_axes([600])
 
  28         translator_font = PIL.ImageFont.truetype(
 
  29             get_resource('fonts/SourceSans3VF-Roman.ttf'),
 
  31             layout_engine=PIL.ImageFont.Layout.BASIC
 
  33         translator_font.set_variation_by_axes([400])
 
  35         authors = [a.readable() for a in self.cover.book_info.authors]
 
  36         translators = [a.readable() for a in self.cover.book_info.translators]
 
  39         if authors and translators:
 
  42             authors_shortened = False
 
  44             while author_box is None:
 
  45                 author_str = ', '.join(authors)
 
  48                     author_str += ' i in.'
 
  62                     authors_shortened = True
 
  64             translators_shortened = False
 
  66             while translator_box is None:
 
  67                 translator_str = '(tłum. ' + ', '.join(translators)
 
  68                 if translators_shortened:
 
  69                     translator_str += ' i in.'
 
  72                     translator_box = TextBox(
 
  84                     translators_shortened = True
 
  94             while author_box is None:
 
  95                 if not shortened and len(authors) == 2:
 
  97                 elif len(authors) > 2:
 
  98                     parts = [author + ',' for author in authors[:-1]] + [authors[-1]]
 
 100                         parts.append('i in.')
 
 102                     parts = split_words(authors[0])
 
 104                         parts.append('i in.')
 
 108                         # Author in two lines.
 
 109                         author_box = TextBox(
 
 120                         # author in one line.
 
 121                         author_box = TextBox(
 
 135             self.textboxes = [author_box]
 
 138             translators_shortened = False
 
 139             translator_box = None
 
 140             while translator_box is None:
 
 141                 translator_str = 'tłum. ' + ', '.join(translators)
 
 142                 if translators_shortened:
 
 143                     translator_str += ' i in.'
 
 145                     translator_box = TextBox(
 
 157                     translators_shortened = True
 
 159             self.textboxes = [translator_box]
 
 162             self.margin_top = self.textboxes[0].margin_top
 
 164     def build(self, w, h):
 
 165         img = PIL.Image.new('RGBA', (self.width, self.m.leading * 2))
 
 167         for i, tb in enumerate(self.textboxes):
 
 168             sub_img = tb.as_pil_image(self.cover.color_scheme['text'])
 
 169             img.paste(sub_img, (0, self.m.leading * i), sub_img)