Conversion: When converting SVG images for formats that do not support SVG and the SVG has an invalid viewBox, ignore the viewBox instead of aborting conversion

This commit is contained in:
Kovid Goyal 2014-02-20 09:09:18 +05:30
parent dd54ccc0d0
commit 46383bf264

View File

@ -6,7 +6,7 @@ from __future__ import with_statement
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>' __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
import os import os, re
from urlparse import urldefrag from urlparse import urldefrag
from lxml import etree from lxml import etree
from PyQt4.QtCore import Qt from PyQt4.QtCore import Qt
@ -68,17 +68,21 @@ class SVGRasterizer(object):
logger = self.oeb.logger logger = self.oeb.logger
if view_box is not None: if view_box is not None:
box = [float(x) for x in view_box.split()] try:
sizes = [box[2]-box[0], box[3] - box[1]] box = [float(x) for x in filter(None, re.split('[, ]', view_box))]
for image in elem.xpath('descendant::*[local-name()="image" and ' sizes = [box[2]-box[0], box[3] - box[1]]
'@height and contains(@height, "%")]'): except (TypeError, ValueError, IndexError):
logger.info('Found SVG image height in %, trying to convert...') logger.warn('SVG image has invalid viewBox="%s", ignoring the viewBox' % view_box)
try: else:
h = float(image.get('height').replace('%', ''))/100. for image in elem.xpath('descendant::*[local-name()="image" and '
image.set('height', str(h*sizes[1])) '@height and contains(@height, "%")]'):
except: logger.info('Found SVG image height in %, trying to convert...')
logger.exception('Failed to convert percentage height:', try:
image.get('height')) h = float(image.get('height').replace('%', ''))/100.
image.set('height', str(h*sizes[1]))
except:
logger.exception('Failed to convert percentage height:',
image.get('height'))
data = QByteArray(xml2str(elem, with_tail=False)) data = QByteArray(xml2str(elem, with_tail=False))
svg = QSvgRenderer(data) svg = QSvgRenderer(data)