LRF Output: Disable the table as image option

Implementing it using WebEngine is too painful and LRF is a useless
format anyway.
This commit is contained in:
Kovid Goyal 2019-06-26 15:15:09 +05:30
parent 6f86896da8
commit a6576f0eff
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 13 additions and 142 deletions

View File

@ -117,8 +117,7 @@ class LRFOutput(OutputFormatPlugin):
), ),
OptionRecommendation(name='render_tables_as_images', OptionRecommendation(name='render_tables_as_images',
recommended_value=False, recommended_value=False,
help=_('Render tables in the HTML as images (useful if the ' help=_('This option has no effect')
'document has large or complex tables)')
), ),
OptionRecommendation(name='text_size_multiplier_for_rendered_tables', OptionRecommendation(name='text_size_multiplier_for_rendered_tables',
recommended_value=1.0, recommended_value=1.0,

View File

@ -1727,44 +1727,18 @@ class HTMLConverter(object):
self.previous_text = ' ' self.previous_text = ' '
self.process_children(tag, tag_css, tag_pseudo_css) self.process_children(tag, tag_css, tag_pseudo_css)
elif tagname == 'table' and not self.ignore_tables and not self.in_table: elif tagname == 'table' and not self.ignore_tables and not self.in_table:
if self.render_tables_as_images: tag_css = self.tag_css(tag)[0] # Table should not inherit CSS
print('Rendering table...') try:
from calibre.ebooks.lrf.html.table_as_image import render_table self.process_table(tag, tag_css)
pheight = int(self.current_page.pageStyle.attrs['textheight']) except Exception as err:
pwidth = int(self.current_page.pageStyle.attrs['textwidth']) self.log.warning(_('An error occurred while processing a table: %s. Ignoring table markup.')%repr(err))
images = render_table(self.soup, tag, tag_css, self.log.exception('')
os.path.dirname(self.target_prefix), self.log.debug(_('Bad table:\n%s')%unicode_type(tag)[:300])
pwidth, pheight, self.profile.dpi, self.in_table = False
self.text_size_multiplier_for_rendered_tables) self.process_children(tag, tag_css, tag_pseudo_css)
for path, width, height in images: finally:
stream = ImageStream(path, encoding='PNG') if self.minimize_memory_usage:
im = Image(stream, x0=0, y0=0, x1=width, y1=height, tag.extract()
xsize=width, ysize=height)
pb = self.current_block
self.end_current_para()
self.process_alignment(tag_css)
self.current_para.append(Plot(im, xsize=width*720./self.profile.dpi,
ysize=height*720./self.profile.dpi))
self.current_block.append(self.current_para)
self.current_page.append(self.current_block)
self.current_block = self.book.create_text_block(
textStyle=pb.textStyle,
blockStyle=pb.blockStyle)
self.current_para = Paragraph()
else:
tag_css = self.tag_css(tag)[0] # Table should not inherit CSS
try:
self.process_table(tag, tag_css)
except Exception as err:
self.log.warning(_('An error occurred while processing a table: %s. Ignoring table markup.')%repr(err))
self.log.exception('')
self.log.debug(_('Bad table:\n%s')%unicode_type(tag)[:300])
self.in_table = False
self.process_children(tag, tag_css, tag_pseudo_css)
finally:
if self.minimize_memory_usage:
tag.extract()
else: else:
self.process_children(tag, tag_css, tag_pseudo_css) self.process_children(tag, tag_css, tag_pseudo_css)
finally: finally:

View File

@ -1,102 +0,0 @@
#!/usr/bin/env python2
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
'''
Render HTML tables as images.
'''
import os, tempfile, atexit, shutil
from PyQt5.Qt import QUrl, QApplication, QSize, QEventLoop, \
QPainter, QImage, QObject, Qt
from PyQt5.QtWebKitWidgets import QWebPage
from polyglot.builtins import unicode_type
class HTMLTableRenderer(QObject):
def __init__(self, html, base_dir, width, height, dpi, factor):
'''
`width, height`: page width and height in pixels
`base_dir`: The directory in which the HTML file that contains the table resides
'''
from calibre.gui2 import secure_web_page
QObject.__init__(self)
self.app = None
self.width, self.height, self.dpi = width, height, dpi
self.base_dir = base_dir
self.images = []
self.tdir = tempfile.mkdtemp(prefix='calibre_render_table')
self.loop = QEventLoop()
self.page = QWebPage()
secure_web_page(self.page.settings())
self.page.loadFinished.connect(self.render_html)
self.page.mainFrame().setTextSizeMultiplier(factor)
self.page.mainFrame().setHtml(html,
QUrl('file:'+os.path.abspath(self.base_dir)))
def render_html(self, ok):
try:
if not ok:
return
cwidth, cheight = self.page.mainFrame().contentsSize().width(), self.page.mainFrame().contentsSize().height()
self.page.setViewportSize(QSize(cwidth, cheight))
factor = float(self.width)/cwidth if cwidth > self.width else 1
cutoff_height = int(self.height/factor)-3
image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
image.setDotsPerMeterX(self.dpi*(100/2.54))
image.setDotsPerMeterY(self.dpi*(100/2.54))
painter = QPainter(image)
self.page.mainFrame().render(painter)
painter.end()
cheight = image.height()
cwidth = image.width()
pos = 0
while pos < cheight:
img = image.copy(0, pos, cwidth, min(cheight-pos, cutoff_height))
pos += cutoff_height-20
if cwidth > self.width:
img = img.scaledToWidth(self.width, Qt.SmoothTransform)
f = os.path.join(self.tdir, '%d.png'%pos)
img.save(f)
self.images.append((f, img.width(), img.height()))
finally:
QApplication.quit()
def render_table(soup, table, css, base_dir, width, height, dpi, factor=1.0):
head = ''
for e in soup.findAll(['link', 'style']):
head += unicode_type(e)+'\n\n'
style = ''
for key, val in css.items():
style += key + ':%s;'%val
html = '''\
<html>
<head>
%s
</head>
<body style="width: %dpx; background: white">
<style type="text/css">
table {%s}
</style>
%s
</body>
</html>
'''%(head, width-10, style, unicode_type(table))
images, tdir = do_render(html, base_dir, width, height, dpi, factor)
atexit.register(shutil.rmtree, tdir)
return images
def do_render(html, base_dir, width, height, dpi, factor):
from calibre.gui2 import is_ok_to_use_qt
if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
tr = HTMLTableRenderer(html, base_dir, width, height, dpi, factor)
tr.loop.exec_()
return tr.images, tr.tdir