nicer full html
[librarian.git] / librarian / cover.py
index f93e709..c345a50 100644 (file)
@@ -34,22 +34,29 @@ class TextBox(object):
         self.height += height
 
     def text(self, text, color='#000', font=None, line_height=20, 
         self.height += height
 
     def text(self, text, color='#000', font=None, line_height=20, 
-             shadow_color=None):
+             shadow_color=None, shortener=None):
         """Writes some centered text."""
         if shadow_color:
             if not self.shadow_img:
                 self.shadow_img = Image.new('RGBA', self.img.size)
                 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
         while text:
         """Writes some centered text."""
         if shadow_color:
             if not self.shadow_img:
                 self.shadow_img = Image.new('RGBA', self.img.size)
                 self.shadow_draw = ImageDraw.Draw(self.shadow_img)
         while text:
-            line = text
-            line_width = self.draw.textsize(line, font=font)[0]
-            while line_width > self.max_text_width:
-                parts = line.rsplit(' ', 1)
-                if len(parts) == 1:
-                    line_width = self.max_text_width
-                    break
-                line = parts[0]
+            if shortener:
+                for line in shortener(text):
+                    if text_draw.textsize(line, font=font)[0] <= self.max_text_width:
+                        break
+                text = ''
+            else:
+                line = text
                 line_width = self.draw.textsize(line, font=font)[0]
                 line_width = self.draw.textsize(line, font=font)[0]
+                while line_width > self.max_text_width:
+                    parts = line.rsplit(' ', 1)
+                    if len(parts) == 1:
+                        line_width = self.max_text_width
+                        break
+                    line = parts[0]
+                    line_width = self.draw.textsize(line, font=font)[0]
+
             line = line.strip() + ' '
 
             pos_x = (self.max_width - line_width) / 2
             line = line.strip() + ' '
 
             pos_x = (self.max_width - line_width) / 2
@@ -65,6 +72,30 @@ class TextBox(object):
             # go to next line
             text = text[len(line):]
 
             # go to next line
             text = text[len(line):]
 
+    @staticmethod
+    def person_shortener(text):
+        yield text
+        chunks = text.split()
+        n_chunks = len(chunks)
+        # make initials from given names, starting from last
+        for i in range(n_chunks - 2, -1, -1):
+            chunks[i] = chunks[i][0] + '.'
+            yield " ".join(chunks)
+        # remove given names initials, starting from last
+        while len(chunks) > 2:
+            del chunks[1]
+            yield " ".join(chunks)
+
+    @staticmethod
+    def title_shortener(text):
+        yield text
+        chunks = text.split()
+        n_chunks = len(chunks)
+        # remove words, starting from last one
+        while len(chunks) > 1:
+            del chunks[-1]
+            yield " ".join(chunks) + u'…'
+
     def image(self):
         """Creates the actual Image object."""
         image = Image.new('RGBA', (self.max_width,
     def image(self):
         """Creates the actual Image object."""
         image = Image.new('RGBA', (self.max_width,
@@ -85,6 +116,7 @@ class Cover(object):
     background_color = '#fff'
     background_img = None
 
     background_color = '#fff'
     background_img = None
 
+    author_align = 'c'
     author_top = 100
     author_margin_left = 20
     author_margin_right = 20
     author_top = 100
     author_margin_left = 20
     author_margin_right = 20
@@ -92,7 +124,9 @@ class Cover(object):
     author_color = '#000'
     author_shadow = None
     author_font = None
     author_color = '#000'
     author_shadow = None
     author_font = None
+    author_wrap = True
 
 
+    title_align = 'c'
     title_top = 100
     title_margin_left = 20
     title_margin_right = 20
     title_top = 100
     title_margin_left = 20
     title_margin_right = 20
@@ -100,6 +134,7 @@ class Cover(object):
     title_color = '#000'
     title_shadow = None
     title_font = None
     title_color = '#000'
     title_shadow = None
     title_font = None
+    title_wrap = True
 
     logo_bottom = None
     logo_width = None
 
     logo_bottom = None
     logo_width = None
@@ -118,7 +153,7 @@ class Cover(object):
         }
 
     def __init__(self, book_info):
         }
 
     def __init__(self, book_info):
-        self.author = book_info.author.readable()
+        self.author = ", ".join(auth.readable() for auth in book_info.authors)
         self.title = book_info.title
 
     def pretty_author(self):
         self.title = book_info.title
 
     def pretty_author(self):
@@ -134,7 +169,10 @@ class Cover(object):
 
         if self.background_img:
             background = Image.open(self.background_img)
 
         if self.background_img:
             background = Image.open(self.background_img)
-            img.paste(background, None, background)
+            try:
+                img.paste(background, None, background)
+            except ValueError, e:
+                img.paste(background)
             del background
 
         # WL logo
             del background
 
         # WL logo
@@ -150,8 +188,9 @@ class Cover(object):
             )
         author_font = self.author_font or ImageFont.truetype(
             get_resource('fonts/DejaVuSerif.ttf'), 30)
             )
         author_font = self.author_font or ImageFont.truetype(
             get_resource('fonts/DejaVuSerif.ttf'), 30)
+        author_shortener = None if self.author_wrap else TextBox.person_shortener 
         tbox.text(self.pretty_author(), self.author_color, author_font,
         tbox.text(self.pretty_author(), self.author_color, author_font,
-            self.author_lineskip, self.author_shadow)
+            self.author_lineskip, self.author_shadow, author_shortener)
         text_img = tbox.image()
         img.paste(text_img, (self.author_margin_left, top), text_img)
         
         text_img = tbox.image()
         img.paste(text_img, (self.author_margin_left, top), text_img)
         
@@ -162,8 +201,9 @@ class Cover(object):
             )
         title_font = self.author_font or ImageFont.truetype(
             get_resource('fonts/DejaVuSerif.ttf'), 40)
             )
         title_font = self.author_font or ImageFont.truetype(
             get_resource('fonts/DejaVuSerif.ttf'), 40)
+        title_shortener = None if self.title_wrap else TextBox.title_shortener 
         tbox.text(self.pretty_title(), self.title_color, title_font,
         tbox.text(self.pretty_title(), self.title_color, title_font,
-            self.title_lineskip, self.title_shadow)
+            self.title_lineskip, self.title_shadow, title_shortener)
         text_img = tbox.image()
         img.paste(text_img, (self.title_margin_left, top), text_img)
 
         text_img = tbox.image()
         img.paste(text_img, (self.title_margin_left, top), text_img)
 
@@ -192,6 +232,8 @@ class WLCover(Cover):
     bar_width = 35
     background_color = '#444'
     author_color = '#444'
     bar_width = 35
     background_color = '#444'
     author_color = '#444'
+    default_background = get_resource('res/cover.png')
+    format = 'JPEG'
 
     epochs = {
         u'Starożytność': 0,
 
     epochs = {
         u'Starożytność': 0,
@@ -217,6 +259,8 @@ class WLCover(Cover):
             bg_src = urlopen(book_info.cover_url)
             self.background_img = StringIO(bg_src.read())
             bg_src.close()
             bg_src = urlopen(book_info.cover_url)
             self.background_img = StringIO(bg_src.read())
             bg_src.close()
+        else:
+            self.background_img = self.default_background
 
     def pretty_author(self):
         return self.author.upper()
 
     def pretty_author(self):
         return self.author.upper()
@@ -364,3 +408,42 @@ class GandalfCover(Cover):
     logo_bottom = 25
     logo_width = 250
     format = 'PNG'
     logo_bottom = 25
     logo_width = 250
     format = 'PNG'
+
+
+class ArtaTechCover(Cover):
+    width = 600
+    height = 800
+    background_img = get_resource('res/cover-arta-tech.jpg')
+    author_top = 132
+    author_margin_left = 235
+    author_margin_right = 23
+    author_align = 'r'
+    author_font = ImageFont.truetype(get_resource('fonts/DroidSans.ttf'), 32)
+    author_color = '#555555'
+    author_wrap = False
+    title_top = 17
+    title_margin_right = 21
+    title_margin_left = 60
+    title_align = 'r'
+    title_font = ImageFont.truetype(get_resource('fonts/EBGaramond-Regular.ttf'), 42)
+    title_color = '#222222'
+    title_wrap = False
+    format = 'JPEG'
+
+    def pretty_author(self):
+        return self.author.upper()
+
+
+def ImageCover(img):
+    """ a class factory for simple image covers """
+    img = Image.open(img)
+
+    class ImgCover(Cover):
+        def image(self):
+            return img
+
+        @property
+        def format(self):
+            return self.image().format
+
+    return ImgCover