py3: port oeb rasterize string types

The problem is I'm not sure what manifest's item.data is supposed to be.
The docs for calibre.ebooks.oeb.base.Manifest.Item.data say "it should
be a str type", but that doesn't seem to make sense for a binary file
(at least for python3). More likely it should be a string_or_bytes, and
consumers should check whether the item data is an instance of bytes
(which it should be for image files that aren't representable as decoded
bytes).
This commit is contained in:
Eli Schwartz 2019-07-05 16:31:59 -04:00
parent 73ea548159
commit cc30b3f408
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
2 changed files with 9 additions and 9 deletions

View File

@ -1024,8 +1024,8 @@ class Manifest(object):
- XML content is parsed and returned as an lxml.etree element. - XML content is parsed and returned as an lxml.etree element.
- CSS and CSS-variant content is parsed and returned as a css_parser - CSS and CSS-variant content is parsed and returned as a css_parser
CSS DOM stylesheet. CSS DOM stylesheet.
- All other content is returned as a :class:`str` object with no - All other content is returned as a :class:`str` or :class:`bytes`
special parsing. object with no special parsing.
""" """
data = self._data data = self._data
if data is None: if data is None:

View File

@ -1,7 +1,7 @@
''' '''
SVG rasterization transform. SVG rasterization transform.
''' '''
from __future__ import with_statement from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>' __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
@ -77,7 +77,7 @@ class SVGRasterizer(object):
logger.info('Found SVG image height in %, trying to convert...') logger.info('Found SVG image height in %, trying to convert...')
try: try:
h = float(image.get('height').replace('%', ''))/100. h = float(image.get('height').replace('%', ''))/100.
image.set('height', str(h*sizes[1])) image.set('height', unicode_type(h*sizes[1]))
except: except:
logger.exception('Failed to convert percentage height:', logger.exception('Failed to convert percentage height:',
image.get('height')) image.get('height'))
@ -101,7 +101,7 @@ class SVGRasterizer(object):
buffer = QBuffer(array) buffer = QBuffer(array)
buffer.open(QIODevice.WriteOnly) buffer.open(QIODevice.WriteOnly)
image.save(buffer, format) image.save(buffer, format)
return str(array) return array.data()
def dataize_manifest(self): def dataize_manifest(self):
for item in self.oeb.manifest.values(): for item in self.oeb.manifest.values():
@ -121,10 +121,10 @@ class SVGRasterizer(object):
if abshref not in hrefs: if abshref not in hrefs:
continue continue
linkee = hrefs[abshref] linkee = hrefs[abshref]
data = str(linkee) data = unicode_type(linkee)
ext = what(None, data) or 'jpg' ext = what(None, data) or 'jpg'
with PersistentTemporaryFile(suffix='.'+ext) as pt: with PersistentTemporaryFile(suffix='.'+ext) as pt:
pt.write(data) pt.write(data.encode('utf-8'))
self.temp_files.append(pt.name) self.temp_files.append(pt.name)
elem.attrib[XLINK('href')] = pt.name elem.attrib[XLINK('href')] = pt.name
return svg return svg
@ -182,7 +182,7 @@ class SVGRasterizer(object):
height = style['height'] height = style['height']
width = (width / 72) * self.profile.dpi width = (width / 72) * self.profile.dpi
height = (height / 72) * self.profile.dpi height = (height / 72) * self.profile.dpi
data = QByteArray(str(svgitem)) data = QByteArray(unicode_type(svgitem).encode('utf-8'))
svg = QSvgRenderer(data) svg = QSvgRenderer(data)
size = svg.defaultSize() size = svg.defaultSize()
size.scale(width, height, Qt.KeepAspectRatio) size.scale(width, height, Qt.KeepAspectRatio)
@ -202,7 +202,7 @@ class SVGRasterizer(object):
buffer = QBuffer(array) buffer = QBuffer(array)
buffer.open(QIODevice.WriteOnly) buffer.open(QIODevice.WriteOnly)
image.save(buffer, 'PNG') image.save(buffer, 'PNG')
data = str(array) data = array.data()
manifest = self.oeb.manifest manifest = self.oeb.manifest
href = os.path.splitext(svgitem.href)[0] + '.png' href = os.path.splitext(svgitem.href)[0] + '.png'
id, href = manifest.generate(svgitem.id, href) id, href = manifest.generate(svgitem.id, href)