MOBI Output: Fix rasterizing of svg images

MOBI Output: Fix rendering of SVG images that embed large raster images
in 64bit calibre installs.
Fixes #1191020 [Conversion to mobi (from ePub) removes inserted images](https://bugs.launchpad.net/calibre/+bug/1191020)
This commit is contained in:
Kovid Goyal 2013-06-14 21:40:17 +05:30
parent 74cd76d02c
commit 4637e39350

View File

@ -8,9 +8,8 @@ __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
import os
from urlparse import urldefrag
import base64
from lxml import etree
from PyQt4.QtCore import Qt
from PyQt4.QtCore import Qt, QUrl
from PyQt4.QtCore import QByteArray
from PyQt4.QtCore import QBuffer
from PyQt4.QtCore import QIODevice
@ -18,11 +17,14 @@ from PyQt4.QtGui import QColor
from PyQt4.QtGui import QImage
from PyQt4.QtGui import QPainter
from PyQt4.QtSvg import QSvgRenderer
from calibre.constants import iswindows
from calibre.ebooks.oeb.base import XHTML, XLINK
from calibre.ebooks.oeb.base import SVG_MIME, PNG_MIME
from calibre.ebooks.oeb.base import xml2str, xpath
from calibre.ebooks.oeb.base import urlnormalize
from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.imghdr import what
IMAGE_TAGS = set([XHTML('img'), XHTML('object')])
KEEP_ATTRS = set(['class', 'style', 'width', 'height', 'align'])
@ -46,6 +48,7 @@ class SVGRasterizer(object):
def __call__(self, oeb, context):
oeb.logger.info('Rasterizing SVG images...')
self.temp_files = []
self.stylizer_cache = {}
self.oeb = oeb
self.opts = context
@ -54,6 +57,11 @@ class SVGRasterizer(object):
self.dataize_manifest()
self.rasterize_spine()
self.rasterize_cover()
for pt in self.temp_files:
try:
os.remove(pt)
except:
pass
def rasterize_svg(self, elem, width=0, height=0, format='PNG'):
view_box = elem.get('viewBox', elem.get('viewbox', None))
@ -112,9 +120,15 @@ class SVGRasterizer(object):
if abshref not in hrefs:
continue
linkee = hrefs[abshref]
data = base64.encodestring(str(linkee))
data = "data:%s;base64,%s" % (linkee.media_type, data)
elem.attrib[XLINK('href')] = data
data = str(linkee)
ext = what(None, data) or 'jpg'
with PersistentTemporaryFile(suffix='.'+ext) as pt:
pt.write(data)
self.temp_files.append(pt.name)
href = unicode(QUrl.fromLocalFile(pt.name).toString())[len('file://'):]
if iswindows:
href = href[1:]
elem.attrib[XLINK('href')] = href
return svg
def stylizer(self, item):