From 46383bf264ccf38fe1582d83d34c434d77e29002 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Feb 2014 09:09:18 +0530 Subject: [PATCH] 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 --- .../ebooks/oeb/transforms/rasterize.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/oeb/transforms/rasterize.py b/src/calibre/ebooks/oeb/transforms/rasterize.py index acd815524e..48788279db 100644 --- a/src/calibre/ebooks/oeb/transforms/rasterize.py +++ b/src/calibre/ebooks/oeb/transforms/rasterize.py @@ -6,7 +6,7 @@ from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' -import os +import os, re from urlparse import urldefrag from lxml import etree from PyQt4.QtCore import Qt @@ -68,17 +68,21 @@ class SVGRasterizer(object): logger = self.oeb.logger if view_box is not None: - box = [float(x) for x in view_box.split()] - sizes = [box[2]-box[0], box[3] - box[1]] - for image in elem.xpath('descendant::*[local-name()="image" and ' - '@height and contains(@height, "%")]'): - logger.info('Found SVG image height in %, trying to convert...') - try: - 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')) + try: + box = [float(x) for x in filter(None, re.split('[, ]', view_box))] + sizes = [box[2]-box[0], box[3] - box[1]] + except (TypeError, ValueError, IndexError): + logger.warn('SVG image has invalid viewBox="%s", ignoring the viewBox' % view_box) + else: + for image in elem.xpath('descendant::*[local-name()="image" and ' + '@height and contains(@height, "%")]'): + logger.info('Found SVG image height in %, trying to convert...') + try: + 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)) svg = QSvgRenderer(data)