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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import tempfile
  9. import atexit
  10. import shutil
  11. from PyQt4.Qt import QUrl, QApplication, QSize, QEventLoop, SIGNAL, QPainter, QImage, QObject, Qt
  12. from PyQt4.QtWebKit import QWebPage
  13.  
  14. class HTMLTableRenderer(QObject):
  15.     
  16.     def __init__(self, html, base_dir, width, height, dpi, factor):
  17.         QObject.__init__(self)
  18.         self.app = None
  19.         self.width = width
  20.         self.height = height
  21.         self.dpi = dpi
  22.         self.base_dir = base_dir
  23.         self.images = []
  24.         self.tdir = tempfile.mkdtemp(prefix = 'calibre_render_table')
  25.         self.loop = QEventLoop()
  26.         self.page = QWebPage()
  27.         self.connect(self.page, SIGNAL('loadFinished(bool)'), self.render_html)
  28.         self.page.mainFrame().setTextSizeMultiplier(factor)
  29.         self.page.mainFrame().setHtml(html, QUrl('file:' + os.path.abspath(self.base_dir)))
  30.  
  31.     
  32.     def render_html(self, ok):
  33.         
  34.         try:
  35.             if not ok:
  36.                 return None
  37.             cwidth = self.page.mainFrame().contentsSize().width()
  38.             cheight = self.page.mainFrame().contentsSize().height()
  39.             self.page.setViewportSize(QSize(cwidth, cheight))
  40.             factor = ok if cwidth > self.width else 1
  41.             cutoff_height = int(self.height / factor) - 3
  42.             image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
  43.             image.setDotsPerMeterX(self.dpi * (100 / 2.54))
  44.             image.setDotsPerMeterY(self.dpi * (100 / 2.54))
  45.             painter = QPainter(image)
  46.             self.page.mainFrame().render(painter)
  47.             painter.end()
  48.             cheight = image.height()
  49.             cwidth = image.width()
  50.             pos = 0
  51.             while pos < cheight:
  52.                 img = image.copy(0, pos, cwidth, min(cheight - pos, cutoff_height))
  53.                 pos += cutoff_height - 20
  54.                 if cwidth > self.width:
  55.                     img = img.scaledToWidth(self.width, Qt.SmoothTransform)
  56.                 
  57.                 f = os.path.join(self.tdir, '%d.png' % pos)
  58.                 img.save(f)
  59.                 self.images.append((f, img.width(), img.height()))
  60.         finally:
  61.             QApplication.quit()
  62.  
  63.  
  64.  
  65.  
  66. def render_table(soup, table, css, base_dir, width, height, dpi, factor = 1):
  67.     head = ''
  68.     for e in soup.findAll([
  69.         'link',
  70.         'style']):
  71.         head += unicode(e) + '\n\n'
  72.     
  73.     style = ''
  74.     for key, val in css.items():
  75.         style += key + ':%s;' % val
  76.     
  77.     html = u'<html>\n    <head>\n        %s\n    </head>\n    <body style="width: %dpx; background: white">\n        <style type="text/css">\n            table {%s}\n        </style>\n        %s\n    </body>\n</html>\n    ' % (head, width - 10, style, unicode(table))
  78.     (images, tdir) = do_render(html, base_dir, width, height, dpi, factor)
  79.     atexit.register(shutil.rmtree, tdir)
  80.     return images
  81.  
  82.  
  83. def do_render(html, base_dir, width, height, dpi, factor):
  84.     is_ok_to_use_qt = is_ok_to_use_qt
  85.     import calibre.gui2
  86.     if not is_ok_to_use_qt():
  87.         raise Exception('Not OK to use Qt')
  88.     is_ok_to_use_qt()
  89.     tr = HTMLTableRenderer(html, base_dir, width, height, dpi, factor)
  90.     tr.loop.exec_()
  91.     return (tr.images, tr.tdir)
  92.  
  93.