home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1001 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.4 KB  |  137 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 textwrap
  8. import cStringIO
  9. from urllib import unquote
  10. from lxml import etree
  11.  
  12. try:
  13.     from PIL import Image as PILImage
  14.     PILImage
  15. except ImportError:
  16.     import Image as PILImage
  17.  
  18. from calibre import guess_type
  19.  
  20. class CoverManager(object):
  21.     SVG_TEMPLATE = textwrap.dedent('        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n            <head>\n                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n                <meta name="calibre:cover" content="true" />\n                <title>Cover</title>\n                <style type="text/css" title="override_css">\n                    @page {padding: 0pt; margin:0pt}\n                    body { text-align: center; padding:0pt; margin: 0pt; }\n                </style>\n            </head>\n            <body>\n                <svg version="1.1" xmlns="http://www.w3.org/2000/svg"\n                    xmlns:xlink="http://www.w3.org/1999/xlink"\n                    width="100%%" height="100%%" viewBox="__viewbox__"\n                    preserveAspectRatio="__ar__">\n                    <image width="__width__" height="__height__" xlink:href="%s"/>\n                </svg>\n            </body>\n        </html>\n        ')
  22.     NONSVG_TEMPLATE = textwrap.dedent('        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n            <head>\n                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n                <meta name="calibre:cover" content="true" />\n                <title>Cover</title>\n                <style type="text/css" title="override_css">\n                    @page {padding: 0pt; margin:0pt}\n                    body { text-align: center; padding:0pt; margin: 0pt }\n                    div { padding:0pt; margin: 0pt }\n                    img { padding:0pt; margin: 0pt }\n                </style>\n            </head>\n            <body>\n                <div>\n                    <img src="%s" alt="cover" __style__ />\n                </div>\n            </body>\n        </html>\n    ')
  23.     
  24.     def __init__(self, no_default_cover = False, no_svg_cover = False, preserve_aspect_ratio = False, fixed_size = None):
  25.         self.no_default_cover = no_default_cover
  26.         self.no_svg_cover = no_svg_cover
  27.         self.preserve_aspect_ratio = preserve_aspect_ratio
  28.         ar = None if preserve_aspect_ratio else 'none'
  29.         self.svg_template = self.SVG_TEMPLATE.replace('__ar__', ar)
  30.         if fixed_size is None:
  31.             style = 'style="height: 100%%"'
  32.         else:
  33.             (width, height) = fixed_size
  34.             style = 'style="height: %s; width: %s"' % (width, height)
  35.         self.non_svg_template = self.NONSVG_TEMPLATE.replace('__style__', style)
  36.  
  37.     
  38.     def __call__(self, oeb, opts, log):
  39.         self.oeb = oeb
  40.         self.log = log
  41.         self.insert_cover()
  42.  
  43.     
  44.     def default_cover(self):
  45.         authors_to_string = authors_to_string
  46.         import calibre.ebooks.metadata
  47.         if self.no_default_cover:
  48.             return None
  49.         self.log('Generating default cover')
  50.         m = self.oeb.metadata
  51.         title = unicode(m.title[0])
  52.         authors = _[1]
  53.         
  54.         try:
  55.             create_cover_page = create_cover_page
  56.             TextLine = TextLine
  57.             import calibre.utils.magick.draw
  58.             lines = [
  59.                 TextLine(title, 44),
  60.                 TextLine(authors_to_string(authors), 32)]
  61.             img_data = create_cover_page(lines, I('library.png'))
  62.             (id, href) = self.oeb.manifest.generate('cover_image', 'cover_image.jpg')
  63.             item = self.oeb.manifest.add(id, href, guess_type('t.jpg')[0], data = img_data)
  64.             m.clear('cover')
  65.             m.add('cover', item.id)
  66.             return item.href
  67.         except:
  68.             []
  69.             []
  70.             self.no_default_cover
  71.             self.log.exception('Failed to generate default cover')
  72.  
  73.  
  74.     
  75.     def inspect_cover(self, href):
  76.         urlnormalize = urlnormalize
  77.         import calibre.ebooks.oeb.base
  78.         for x in self.oeb.manifest:
  79.             if x.href == urlnormalize(href):
  80.                 
  81.                 try:
  82.                     raw = x.data
  83.                     f = cStringIO.StringIO(raw)
  84.                     im = PILImage.open(f)
  85.                     return im.size
  86.                 self.log.exception('Failed to read image dimensions')
  87.  
  88.                 continue
  89.         
  90.         return (None, None)
  91.  
  92.     
  93.     def insert_cover(self):
  94.         urldefrag = urldefrag
  95.         import calibre.ebooks.oeb.base
  96.         g = self.oeb.guide
  97.         m = self.oeb.manifest
  98.         item = None
  99.         if 'titlepage' not in g:
  100.             if 'cover' in g:
  101.                 href = g['cover'].href
  102.             else:
  103.                 href = self.default_cover()
  104.             if href is None:
  105.                 return None
  106.             (width, height) = self.inspect_cover(href)
  107.             if width is None or height is None:
  108.                 self.log.warning('Failed to read cover dimensions')
  109.                 (width, height) = (600, 800)
  110.             
  111.             if self.preserve_aspect_ratio:
  112.                 (width, height) = (600, 800)
  113.             
  114.             self.svg_template = self.svg_template.replace('__viewbox__', '0 0 %d %d' % (width, height))
  115.             self.svg_template = self.svg_template.replace('__width__', str(width))
  116.             self.svg_template = self.svg_template.replace('__height__', str(height))
  117.             if href is not None:
  118.                 templ = None if self.no_svg_cover else self.svg_template
  119.                 tp = templ % unquote(href)
  120.                 (id, href) = m.generate('titlepage', 'titlepage.xhtml')
  121.                 item = m.add(id, href, guess_type('t.xhtml')[0], data = etree.fromstring(tp))
  122.             
  123.         else:
  124.             item = self.oeb.manifest.hrefs[urldefrag(self.oeb.guide['titlepage'].href)[0]]
  125.         if item is not None:
  126.             self.oeb.spine.insert(0, item, True)
  127.             if 'cover' not in self.oeb.guide.refs:
  128.                 self.oeb.guide.add('cover', 'Title Page', 'a')
  129.             
  130.             self.oeb.guide.refs['cover'].href = item.href
  131.             if 'titlepage' in self.oeb.guide.refs:
  132.                 self.oeb.guide.refs['titlepage'].href = item.href
  133.             
  134.         
  135.  
  136.  
  137.