Merge lp:~user-none/calibre/dev into lp:calibre

Proposed by John Schember
Status: Merged
Merged at revision: 7124
Proposed branch: lp:~user-none/calibre/dev
Merge into: lp:calibre
Diff against target: 116 lines (+34/-13)
2 files modified
src/calibre/ebooks/fb2/fb2ml.py (+26/-13)
src/calibre/ebooks/fb2/output.py (+8/-0)
To merge this branch: bzr merge lp:~user-none/calibre/dev
Reviewer Review Type Date Requested Status
Kovid Goyal Pending
Review via email: mp+42765@code.launchpad.net

Description of the change

FB2 Output: Image changes. Use ImageMagick instead of PIL, support for SVG, various other improvements (mostly image related).

To post a comment you must log in.
lp:~user-none/calibre/dev updated
7124. By Kovid Goyal

FB2 Output: Support SVG images in the input document

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/calibre/ebooks/fb2/fb2ml.py'
2--- src/calibre/ebooks/fb2/fb2ml.py 2010-12-05 01:10:58 +0000
3+++ src/calibre/ebooks/fb2/fb2ml.py 2010-12-05 04:06:45 +0000
4@@ -10,7 +10,9 @@
5
6 from base64 import b64encode
7 from datetime import datetime
8+from mimetypes import types_map
9 import re
10+import uuid
11
12 from lxml import etree
13
14@@ -19,7 +21,7 @@
15 from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace
16 from calibre.ebooks.oeb.stylizer import Stylizer
17 from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
18-from calibre.utils.magick.draw import save_cover_data_to
19+from calibre.utils.magick import Image
20
21 class FB2MLizer(object):
22 '''
23@@ -83,7 +85,8 @@
24 metadata['version'] = __version__
25 metadata['date'] = '%i.%i.%i' % (datetime.now().day, datetime.now().month, datetime.now().year)
26 metadata['lang'] = u''.join(self.oeb_book.metadata.lang) if self.oeb_book.metadata.lang else 'en'
27-
28+ metadata['id'] = '%s' % uuid.uuid4()
29+
30 author_parts = self.oeb_book.metadata.creator[0].value.split(' ')
31 if len(author_parts) == 1:
32 metadata['author_last'] = author_parts[0]
33@@ -118,7 +121,7 @@
34 '</author>' \
35 '<program-used>%(appname)s %(version)s</program-used>' \
36 '<date>%(date)s</date>' \
37- '<id>1</id>' \
38+ '<id>%(id)s</id>' \
39 '<version>1.0</version>' \
40 '</document-info>' \
41 '</description>' % metadata
42@@ -137,12 +140,21 @@
43 return ''.join(text) + '</body>'
44
45 def fb2mlize_images(self):
46+ '''
47+ This function uses the self.image_hrefs dictionary mapping. It is populated by the dump_text function.
48+ '''
49 images = []
50 for item in self.oeb_book.manifest:
51+ # Don't write the image if it's not referenced in the document's text.
52+ if item.href not in self.image_hrefs:
53+ continue
54 if item.media_type in OEB_RASTER_IMAGES:
55 try:
56- data = save_cover_data_to(item.data, None,
57- return_data=True)
58+ if not item.media_type == types_map['.jpeg'] or not item.media_type == types_map['.jpg']:
59+ im = Image()
60+ im.load(item.data)
61+ im.set_compression_quality(70)
62+ data = im.export('jpg')
63 raw_data = b64encode(data)
64 # Don't put the encoded image on a single line.
65 data = ''
66@@ -153,7 +165,7 @@
67 col = 1
68 col += 1
69 data += char
70- images.append('<binary id="%s" content-type="%s">%s\n</binary>' % (self.image_hrefs.get(item.href, '_0000.JPEG'), item.media_type, data))
71+ images.append('<binary id="%s" content-type="image/jpeg">%s\n</binary>' % (self.image_hrefs[item.href], data))
72 except Exception as e:
73 self.log.error('Error: Could not include file %s because ' \
74 '%s.' % (item.href, e))
75@@ -234,14 +246,15 @@
76 fb2_out.append('<title>')
77 tags.append('title')
78 if tag == 'img':
79- # TODO: Check that the image is in the manifest and only write the tag if it is.
80 if elem_tree.attrib.get('src', None):
81- if page.abshref(elem_tree.attrib['src']) not in self.image_hrefs.keys():
82- self.image_hrefs[page.abshref(elem_tree.attrib['src'])] = '_%s.jpg' % len(self.image_hrefs.keys())
83- p_txt, p_tag = self.ensure_p()
84- fb2_out += p_txt
85- tags += p_tag
86- fb2_out.append('<image xlink:href="#%s" />' % self.image_hrefs[page.abshref(elem_tree.attrib['src'])])
87+ # Only write the image tag if it is in the manifest.
88+ if page.abshref(elem_tree.attrib['src']) in self.oeb_book.manifest.hrefs.keys():
89+ if page.abshref(elem_tree.attrib['src']) not in self.image_hrefs.keys():
90+ self.image_hrefs[page.abshref(elem_tree.attrib['src'])] = '_%s.jpg' % len(self.image_hrefs.keys())
91+ p_txt, p_tag = self.ensure_p()
92+ fb2_out += p_txt
93+ tags += p_tag
94+ fb2_out.append('<image xlink:href="#%s" />' % self.image_hrefs[page.abshref(elem_tree.attrib['src'])])
95 elif tag == 'br':
96 if self.in_p:
97 closed_tags = []
98
99=== modified file 'src/calibre/ebooks/fb2/output.py'
100--- src/calibre/ebooks/fb2/output.py 2010-12-04 00:11:33 +0000
101+++ src/calibre/ebooks/fb2/output.py 2010-12-05 04:06:45 +0000
102@@ -29,6 +29,14 @@
103
104 def convert(self, oeb_book, output_path, input_plugin, opts, log):
105 from calibre.ebooks.oeb.transforms.jacket import linearize_jacket
106+ from calibre.ebooks.oeb.transforms.rasterize import SVGRasterizer, Unavailable
107+
108+ try:
109+ rasterizer = SVGRasterizer()
110+ rasterizer(oeb_book, opts)
111+ except Unavailable:
112+ self.log.warn('SVG rasterizer unavailable, SVG will not be converted')
113+
114 linearize_jacket(oeb_book)
115
116 fb2mlizer = FB2MLizer(log)

Subscribers

People subscribed via source and target branches

to status/vote changes: