mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
74cd76d02c
commit
4637e39350
@ -8,9 +8,8 @@ __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from urlparse import urldefrag
|
from urlparse import urldefrag
|
||||||
import base64
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from PyQt4.QtCore import Qt
|
from PyQt4.QtCore import Qt, QUrl
|
||||||
from PyQt4.QtCore import QByteArray
|
from PyQt4.QtCore import QByteArray
|
||||||
from PyQt4.QtCore import QBuffer
|
from PyQt4.QtCore import QBuffer
|
||||||
from PyQt4.QtCore import QIODevice
|
from PyQt4.QtCore import QIODevice
|
||||||
@ -18,11 +17,14 @@ from PyQt4.QtGui import QColor
|
|||||||
from PyQt4.QtGui import QImage
|
from PyQt4.QtGui import QImage
|
||||||
from PyQt4.QtGui import QPainter
|
from PyQt4.QtGui import QPainter
|
||||||
from PyQt4.QtSvg import QSvgRenderer
|
from PyQt4.QtSvg import QSvgRenderer
|
||||||
|
from calibre.constants import iswindows
|
||||||
from calibre.ebooks.oeb.base import XHTML, XLINK
|
from calibre.ebooks.oeb.base import XHTML, XLINK
|
||||||
from calibre.ebooks.oeb.base import SVG_MIME, PNG_MIME
|
from calibre.ebooks.oeb.base import SVG_MIME, PNG_MIME
|
||||||
from calibre.ebooks.oeb.base import xml2str, xpath
|
from calibre.ebooks.oeb.base import xml2str, xpath
|
||||||
from calibre.ebooks.oeb.base import urlnormalize
|
from calibre.ebooks.oeb.base import urlnormalize
|
||||||
from calibre.ebooks.oeb.stylizer import Stylizer
|
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')])
|
IMAGE_TAGS = set([XHTML('img'), XHTML('object')])
|
||||||
KEEP_ATTRS = set(['class', 'style', 'width', 'height', 'align'])
|
KEEP_ATTRS = set(['class', 'style', 'width', 'height', 'align'])
|
||||||
@ -46,6 +48,7 @@ class SVGRasterizer(object):
|
|||||||
|
|
||||||
def __call__(self, oeb, context):
|
def __call__(self, oeb, context):
|
||||||
oeb.logger.info('Rasterizing SVG images...')
|
oeb.logger.info('Rasterizing SVG images...')
|
||||||
|
self.temp_files = []
|
||||||
self.stylizer_cache = {}
|
self.stylizer_cache = {}
|
||||||
self.oeb = oeb
|
self.oeb = oeb
|
||||||
self.opts = context
|
self.opts = context
|
||||||
@ -54,6 +57,11 @@ class SVGRasterizer(object):
|
|||||||
self.dataize_manifest()
|
self.dataize_manifest()
|
||||||
self.rasterize_spine()
|
self.rasterize_spine()
|
||||||
self.rasterize_cover()
|
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'):
|
def rasterize_svg(self, elem, width=0, height=0, format='PNG'):
|
||||||
view_box = elem.get('viewBox', elem.get('viewbox', None))
|
view_box = elem.get('viewBox', elem.get('viewbox', None))
|
||||||
@ -112,9 +120,15 @@ class SVGRasterizer(object):
|
|||||||
if abshref not in hrefs:
|
if abshref not in hrefs:
|
||||||
continue
|
continue
|
||||||
linkee = hrefs[abshref]
|
linkee = hrefs[abshref]
|
||||||
data = base64.encodestring(str(linkee))
|
data = str(linkee)
|
||||||
data = "data:%s;base64,%s" % (linkee.media_type, data)
|
ext = what(None, data) or 'jpg'
|
||||||
elem.attrib[XLINK('href')] = data
|
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
|
return svg
|
||||||
|
|
||||||
def stylizer(self, item):
|
def stylizer(self, item):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user