home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_999 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.6 KB  |  133 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                <div>\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                </div>\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.         fmt_sidx = fmt_sidx
  47.         import calibre.ebooks.metadata
  48.         if self.no_default_cover:
  49.             return None
  50.         self.log('Generating default cover')
  51.         m = self.oeb.metadata
  52.         title = unicode(m.title[0])
  53.         authors = _[1]
  54.         series_string = None
  55.         
  56.         try:
  57.             calibre_cover = calibre_cover
  58.             import calibre.ebooks
  59.             img_data = calibre_cover(title, authors_to_string(authors), series_string = series_string)
  60.             (id, href) = self.oeb.manifest.generate('cover_image', 'cover_image.jpg')
  61.             item = self.oeb.manifest.add(id, href, guess_type('t.jpg')[0], data = img_data)
  62.             m.clear('cover')
  63.             m.add('cover', item.id)
  64.             return item.href
  65.         except:
  66.             self.no_default_cover if m.series and m.series_index else []
  67.             self.log.exception('Failed to generate default cover')
  68.  
  69.  
  70.     
  71.     def inspect_cover(self, href):
  72.         urlnormalize = urlnormalize
  73.         import calibre.ebooks.oeb.base
  74.         for x in self.oeb.manifest:
  75.             if x.href == urlnormalize(href):
  76.                 
  77.                 try:
  78.                     raw = x.data
  79.                     f = cStringIO.StringIO(raw)
  80.                     im = PILImage.open(f)
  81.                     return im.size
  82.                 self.log.exception('Failed to read image dimensions')
  83.  
  84.                 continue
  85.         
  86.         return (None, None)
  87.  
  88.     
  89.     def insert_cover(self):
  90.         urldefrag = urldefrag
  91.         import calibre.ebooks.oeb.base
  92.         g = self.oeb.guide
  93.         m = self.oeb.manifest
  94.         item = None
  95.         if 'titlepage' not in g:
  96.             if 'cover' in g:
  97.                 href = g['cover'].href
  98.             else:
  99.                 href = self.default_cover()
  100.             if href is None:
  101.                 return None
  102.             (width, height) = self.inspect_cover(href)
  103.             if width is None or height is None:
  104.                 self.log.warning('Failed to read cover dimensions')
  105.                 (width, height) = (600, 800)
  106.             
  107.             if self.preserve_aspect_ratio:
  108.                 (width, height) = (600, 800)
  109.             
  110.             self.svg_template = self.svg_template.replace('__viewbox__', '0 0 %d %d' % (width, height))
  111.             self.svg_template = self.svg_template.replace('__width__', str(width))
  112.             self.svg_template = self.svg_template.replace('__height__', str(height))
  113.             if href is not None:
  114.                 templ = None if self.no_svg_cover else self.svg_template
  115.                 tp = templ % unquote(href)
  116.                 (id, href) = m.generate('titlepage', 'titlepage.xhtml')
  117.                 item = m.add(id, href, guess_type('t.xhtml')[0], data = etree.fromstring(tp))
  118.             
  119.         else:
  120.             item = self.oeb.manifest.hrefs[urldefrag(self.oeb.guide['titlepage'].href)[0]]
  121.         if item is not None:
  122.             self.oeb.spine.insert(0, item, True)
  123.             if 'cover' not in self.oeb.guide.refs:
  124.                 self.oeb.guide.add('cover', 'Title Page', 'a')
  125.             
  126.             self.oeb.guide.refs['cover'].href = item.href
  127.             if 'titlepage' in self.oeb.guide.refs:
  128.                 self.oeb.guide.refs['titlepage'].href = item.href
  129.             
  130.         
  131.  
  132.  
  133.