2 from librarian import get_resource
 
   3 from librarian.cover import Metric
 
   4 from ..utils.textbox import TextBox, DoesNotFit, split_words
 
   5 from .base import Widget
 
   8 class AuthorBox(Widget):
 
  12     def __init__(self, cover, width):
 
  14         self.m = Metric(self, cover.m._scale)
 
  15         super().__init__(cover)
 
  18         author_font = PIL.ImageFont.truetype(
 
  19             get_resource('fonts/SourceSans3VF-Roman.ttf'),
 
  21             layout_engine=PIL.ImageFont.LAYOUT_BASIC
 
  23         author_font.set_variation_by_axes([600])
 
  25         translator_font = PIL.ImageFont.truetype(
 
  26             get_resource('fonts/SourceSans3VF-Roman.ttf'),
 
  28             layout_engine=PIL.ImageFont.LAYOUT_BASIC
 
  30         translator_font.set_variation_by_axes([400])
 
  32         authors = [a.readable() for a in self.cover.book_info.authors]
 
  33         translators = [a.readable() for a in self.cover.book_info.translators]
 
  36         if authors and translators:
 
  39             authors_shortened = False
 
  41             while author_box is None:
 
  42                 author_str = ', '.join(authors)
 
  45                     author_str += ' i in.'
 
  59                     authors_shortened = True
 
  61             translators_shortened = False
 
  63             while translator_box is None:
 
  64                 translator_str = '(tłum. ' + ', '.join(translators)
 
  65                 if translators_shortened:
 
  66                     translator_str += ' i in.'
 
  69                     translator_box = TextBox(
 
  81                     translators_shortened = True
 
  91             while author_box is None:
 
  92                 if not shortened and len(authors) == 2:
 
  94                 elif len(authors) > 2:
 
  95                     parts = [author + ',' for author in authors[:-1]] + [authors[-1]]
 
  99                     parts = split_words(authors[0])
 
 101                         parts.append('i in.')
 
 105                         # Author in two lines.
 
 106                         author_box = TextBox(
 
 117                         # author in one line.
 
 118                         author_box = TextBox(
 
 132             self.textboxes = [author_box]
 
 135             self.margin_top = self.textboxes[0].margin_top
 
 137     def build(self, w, h):
 
 138         img = PIL.Image.new('RGBA', (self.width, self.m.leading * 2))
 
 140         for i, tb in enumerate(self.textboxes):
 
 141             sub_img = tb.as_pil_image(self.cover.color_scheme['text'])
 
 142             img.paste(sub_img, (0, self.m.leading * i), sub_img)