home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1473 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  7.7 KB  |  220 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. import os
  8. from calibre.utils.magick import Image, DrawingWand, create_canvas
  9. from calibre.constants import __appname__, __version__
  10. from calibre.utils.config import tweaks
  11. from calibre import fit_image
  12.  
  13. def normalize_format_name(fmt):
  14.     fmt = fmt.lower()
  15.     if fmt == 'jpeg':
  16.         fmt = 'jpg'
  17.     
  18.     return fmt
  19.  
  20.  
  21. def save_cover_data_to(data, path, bgcolor = '#ffffff', resize_to = None, return_data = False, compression_quality = 90):
  22.     changed = False
  23.     if isinstance(data, Image):
  24.         img = data
  25.     else:
  26.         img = Image()
  27.         img.load(data)
  28.     orig_fmt = normalize_format_name(img.format)
  29.     fmt = os.path.splitext(path)[1]
  30.     fmt = normalize_format_name(fmt[1:])
  31.     if resize_to is not None:
  32.         img.size = (resize_to[0], resize_to[1])
  33.         changed = True
  34.     
  35.     if img.has_transparent_pixels():
  36.         canvas = create_canvas(img.size[0], img.size[1], bgcolor)
  37.         canvas.compose(img)
  38.         img = canvas
  39.         changed = True
  40.     
  41.     if not changed:
  42.         changed = fmt != orig_fmt
  43.     
  44.     if return_data:
  45.         if changed:
  46.             if hasattr(img, 'set_compression_quality') and fmt == 'jpg':
  47.                 img.set_compression_quality(compression_quality)
  48.             
  49.             return img.export(fmt)
  50.         return data
  51.  
  52.  
  53. def thumbnail(data, width = 120, height = 120, bgcolor = '#ffffff', fmt = 'jpg'):
  54.     img = Image()
  55.     img.load(data)
  56.     (owidth, oheight) = img.size
  57.     (scaled, nwidth, nheight) = fit_image(owidth, oheight, width, height)
  58.     if scaled:
  59.         img.size = (nwidth, nheight)
  60.     
  61.     canvas = create_canvas(img.size[0], img.size[1], bgcolor)
  62.     canvas.compose(img)
  63.     if fmt == 'jpg':
  64.         canvas.set_compression_quality(70)
  65.     
  66.     return (canvas.size[0], canvas.size[1], canvas.export(fmt))
  67.  
  68.  
  69. def identify_data(data):
  70.     img = Image()
  71.     img.load(data)
  72.     (width, height) = img.size
  73.     fmt = img.format
  74.     return (width, height, fmt)
  75.  
  76.  
  77. def identify(path):
  78.     data = open(path, 'rb').read()
  79.     return identify_data(data)
  80.  
  81.  
  82. def add_borders_to_image(img_data, left = 0, top = 0, right = 0, bottom = 0, border_color = '#ffffff', fmt = 'jpg'):
  83.     img = Image()
  84.     img.load(img_data)
  85.     (lwidth, lheight) = img.size
  86.     canvas = create_canvas(lwidth + left + right, lheight + top + bottom, border_color)
  87.     canvas.compose(img, left, top)
  88.     return canvas.export(fmt)
  89.  
  90.  
  91. def create_text_wand(font_size, font_path = None):
  92.     if font_path is None:
  93.         font_path = tweaks['generate_cover_title_font']
  94.         if font_path is None:
  95.             font_path = P('fonts/liberation/LiberationSerif-Bold.ttf')
  96.         
  97.     
  98.     ans = DrawingWand()
  99.     ans.font = font_path
  100.     ans.font_size = font_size
  101.     ans.gravity = 'CenterGravity'
  102.     ans.text_alias = True
  103.     return ans
  104.  
  105.  
  106. def create_text_arc(text, font_size, font = None, bgcolor = '#ffffff'):
  107.     if isinstance(text, unicode):
  108.         text = text.encode('utf-8')
  109.     
  110.     canvas = create_canvas(300, 300, bgcolor)
  111.     tw = create_text_wand(font_size, font_path = font)
  112.     m = canvas.font_metrics(tw, text)
  113.     canvas = create_canvas(int(m.text_width) + 20, int(m.text_height * 3.5), bgcolor)
  114.     canvas.annotate(tw, 0, 0, 0, text)
  115.     canvas.distort('ArcDistortion', [
  116.         120], True)
  117.     canvas.trim(0)
  118.     return canvas
  119.  
  120.  
  121. def _get_line(img, dw, tokens, line_width):
  122.     line = tokens
  123.     rest = []
  124.     while True:
  125.         m = img.font_metrics(dw, ' '.join(line))
  126.         width = m.text_width
  127.         height = m.text_height
  128.         if width < line_width:
  129.             return (line, rest)
  130.         rest = line[-1:] + rest
  131.         line = line[:-1]
  132.         continue
  133.         width < line_width
  134.  
  135.  
  136. def annotate_img(img, dw, left, top, rotate, text, translate_from_top_left = True):
  137.     if isinstance(text, unicode):
  138.         text = text.encode('utf-8')
  139.     
  140.     if translate_from_top_left:
  141.         m = img.font_metrics(dw, text)
  142.         (img_width, img_height) = img.size
  143.         left = (left - img_width / 2) + m.text_width / 2
  144.         top = (top - img_height / 2) + m.text_height / 2
  145.     
  146.     img.annotate(dw, left, top, rotate, text)
  147.  
  148.  
  149. def draw_centered_line(img, dw, line, top):
  150.     m = img.font_metrics(dw, line)
  151.     width = m.text_width
  152.     height = m.text_height
  153.     img_width = img.size[0]
  154.     left = max(int((img_width - width) / 2), 0)
  155.     annotate_img(img, dw, left, top, 0, line)
  156.     return top + height
  157.  
  158.  
  159. def draw_centered_text(img, dw, text, top, margin = 10):
  160.     img_width = img.size[0]
  161.     tokens = text.split(' ')
  162.     while tokens:
  163.         (line, tokens) = _get_line(img, dw, tokens, img_width - 2 * margin)
  164.         if not line:
  165.             line = tokens[:1]
  166.             tokens = tokens[1:]
  167.         
  168.         bottom = draw_centered_line(img, dw, ' '.join(line), top)
  169.         top = bottom
  170.     return top
  171.  
  172.  
  173. class TextLine(object):
  174.     
  175.     def __init__(self, text, font_size, bottom_margin = 30, font_path = None):
  176.         self.text = text
  177.         self.font_size = font_size
  178.         self.bottom_margin = bottom_margin
  179.         self.font_path = font_path
  180.  
  181.     
  182.     def __repr__(self):
  183.         return u'TextLine:%r:%f' % (self.text, self.font_size)
  184.  
  185.  
  186.  
  187. def create_cover_page(top_lines, logo_path, width = 590, height = 750, bgcolor = '#ffffff', output_format = 'jpg'):
  188.     canvas = create_canvas(width, height, bgcolor)
  189.     bottom = 10
  190.     for line in top_lines:
  191.         twand = create_text_wand(line.font_size, font_path = line.font_path)
  192.         bottom = draw_centered_text(canvas, twand, line.text, bottom)
  193.         bottom += line.bottom_margin
  194.     
  195.     bottom -= top_lines[-1].bottom_margin
  196.     foot_font = tweaks['generate_cover_foot_font']
  197.     if not foot_font:
  198.         foot_font = P('fonts/liberation/LiberationMono-Regular.ttf')
  199.     
  200.     vanity = create_text_arc(__appname__ + ' ' + __version__, 24, font = foot_font)
  201.     (lwidth, lheight) = vanity.size
  202.     left = int(max(0, (width - lwidth) / 2))
  203.     top = height - lheight - 10
  204.     canvas.compose(vanity, left, top)
  205.     available = (width, int(top - bottom) - 20)
  206.     if available[1] > 40:
  207.         logo = Image()
  208.         logo.open(logo_path)
  209.         (lwidth, lheight) = logo.size
  210.         (scaled, lwidth, lheight) = fit_image(lwidth, lheight, *available)
  211.         if scaled:
  212.             logo.size = (lwidth, lheight)
  213.         
  214.         left = int(max(0, (width - lwidth) / 2))
  215.         top = bottom + 10
  216.         canvas.compose(logo, left, top)
  217.     
  218.     return canvas.export(output_format)
  219.  
  220.