home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1409 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.9 KB  |  158 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from calibre.utils.magick import Image, DrawingWand, create_canvas
  8. from calibre.constants import __appname__, __version__
  9.  
  10. def save_cover_data_to(data, path, bgcolor = 'white', resize_to = None):
  11.     img = Image()
  12.     img.load(data)
  13.     if resize_to is not None:
  14.         img.size = (resize_to[0], resize_to[1])
  15.     
  16.     canvas = create_canvas(img.size[0], img.size[1], bgcolor)
  17.     canvas.compose(img)
  18.     canvas.save(path)
  19.  
  20.  
  21. def identify_data(data):
  22.     img = Image()
  23.     img.load(data)
  24.     (width, height) = img.size
  25.     fmt = img.format
  26.     return (width, height, fmt)
  27.  
  28.  
  29. def identify(path):
  30.     data = open(path, 'rb').read()
  31.     return identify_data(data)
  32.  
  33.  
  34. def add_borders_to_image(path_to_image, left = 0, top = 0, right = 0, bottom = 0, border_color = 'white'):
  35.     img = Image()
  36.     img.open(path_to_image)
  37.     (lwidth, lheight) = img.size
  38.     canvas = create_canvas(lwidth + left + right, lheight + top + bottom, border_color)
  39.     canvas.compose(img, left, top)
  40.     canvas.save(path_to_image)
  41.  
  42.  
  43. def create_text_wand(font_size, font_path = None):
  44.     if font_path is None:
  45.         font_path = P('fonts/liberation/LiberationSerif-Bold.ttf')
  46.     
  47.     ans = DrawingWand()
  48.     ans.font = font_path
  49.     ans.font_size = font_size
  50.     ans.gravity = 'CenterGravity'
  51.     ans.text_alias = True
  52.     return ans
  53.  
  54.  
  55. def create_text_arc(text, font_size, font = None, bgcolor = 'white'):
  56.     if isinstance(text, unicode):
  57.         text = text.encode('utf-8')
  58.     
  59.     canvas = create_canvas(300, 300, bgcolor)
  60.     tw = create_text_wand(font_size, font_path = font)
  61.     m = canvas.font_metrics(tw, text)
  62.     canvas = create_canvas(int(m.text_width) + 20, int(m.text_height * 3.5), bgcolor)
  63.     canvas.annotate(tw, 0, 0, 0, text)
  64.     canvas.distort('ArcDistortion', [
  65.         120], True)
  66.     canvas.trim(0)
  67.     return canvas
  68.  
  69.  
  70. def _get_line(img, dw, tokens, line_width):
  71.     line = tokens
  72.     rest = []
  73.     while True:
  74.         m = img.font_metrics(dw, ' '.join(line))
  75.         width = m.text_width
  76.         height = m.text_height
  77.         if width < line_width:
  78.             return (line, rest)
  79.         rest = line[-1:] + rest
  80.         line = line[:-1]
  81.         continue
  82.         width < line_width
  83.  
  84.  
  85. def annotate_img(img, dw, left, top, rotate, text, translate_from_top_left = True):
  86.     if isinstance(text, unicode):
  87.         text = text.encode('utf-8')
  88.     
  89.     if translate_from_top_left:
  90.         m = img.font_metrics(dw, text)
  91.         (img_width, img_height) = img.size
  92.         left = (left - img_width / 2) + m.text_width / 2
  93.         top = (top - img_height / 2) + m.text_height / 2
  94.     
  95.     img.annotate(dw, left, top, rotate, text)
  96.  
  97.  
  98. def draw_centered_line(img, dw, line, top):
  99.     m = img.font_metrics(dw, line)
  100.     width = m.text_width
  101.     height = m.text_height
  102.     img_width = img.size[0]
  103.     left = max(int((img_width - width) / 2), 0)
  104.     annotate_img(img, dw, left, top, 0, line)
  105.     return top + height
  106.  
  107.  
  108. def draw_centered_text(img, dw, text, top, margin = 10):
  109.     img_width = img.size[0]
  110.     tokens = text.split(' ')
  111.     while tokens:
  112.         (line, tokens) = _get_line(img, dw, tokens, img_width - 2 * margin)
  113.         if not line:
  114.             line = tokens[:1]
  115.             tokens = tokens[1:]
  116.         
  117.         bottom = draw_centered_line(img, dw, ' '.join(line), top)
  118.         top = bottom
  119.     return top
  120.  
  121.  
  122. class TextLine(object):
  123.     
  124.     def __init__(self, text, font_size, bottom_margin = 30, font_path = None):
  125.         self.text = text
  126.         self.font_size = font_size
  127.         self.bottom_margin = bottom_margin
  128.         self.font_path = font_path
  129.  
  130.     
  131.     def __repr__(self):
  132.         return u'TextLine:%r:%f' % (self.text, self.font_size)
  133.  
  134.  
  135.  
  136. def create_cover_page(top_lines, logo_path, width = 590, height = 750, bgcolor = 'white', output_format = 'jpg'):
  137.     canvas = create_canvas(width, height, bgcolor)
  138.     bottom = 10
  139.     for line in top_lines:
  140.         twand = create_text_wand(line.font_size, font_path = line.font_path)
  141.         bottom = draw_centered_text(canvas, twand, line.text, bottom)
  142.         bottom += line.bottom_margin
  143.     
  144.     bottom -= top_lines[-1].bottom_margin
  145.     vanity = create_text_arc(__appname__ + ' ' + __version__, 24, font = P('fonts/liberation/LiberationMono-Regular.ttf'))
  146.     (lwidth, lheight) = vanity.size
  147.     left = int(max(0, (width - lwidth) / 2))
  148.     top = height - lheight - 10
  149.     canvas.compose(vanity, left, top)
  150.     logo = Image()
  151.     logo.open(logo_path)
  152.     (lwidth, lheight) = logo.size
  153.     left = int(max(0, (width - lwidth) / 2))
  154.     top = max(int((height - lheight) / 2), bottom + 20)
  155.     canvas.compose(logo, left, top)
  156.     return canvas.export(output_format)
  157.  
  158.