Edit Book: When inserting full screen images, use the actual image dimensions in the generated SVG code, when available

This commit is contained in:
Kovid Goyal 2017-02-27 23:36:17 +05:30
parent 34f28d22dd
commit 0250666dd9
3 changed files with 13 additions and 8 deletions

View File

@ -48,6 +48,7 @@ from calibre.gui2.tweak_book.widgets import (
InsertSemantics, BusyCursor, InsertTag, FilterCSS, AddCover)
from calibre.utils.config import JSONConfig
from calibre.utils.icu import numeric_sort_key
from calibre.utils.imghdr import identify
_diff_dialogs = []
last_used_transform_rules = []
@ -861,7 +862,8 @@ class Boss(QObject):
self.refresh_file_list()
chosen_name = chosen_image_is_external[0]
href = current_container().name_to_href(chosen_name, edname)
ed.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_ar)
fmt, width, height = identify(current_container().raw_data(chosen_name, decode=False))
ed.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_ar, width=width, height=height)
elif action[0] == 'insert_hyperlink':
self.commit_all_editors_to_container()
d = InsertLink(current_container(), edname, initial_text=ed.get_smart_selection(), parent=self.gui)

View File

@ -836,7 +836,11 @@ class TextEdit(PlainTextEdit):
c.setPosition(c.position() - len(suffix))
self.setTextCursor(c)
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False):
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1):
if width <= 0:
width = 1200
if height <= 0:
height = 1600
c = self.textCursor()
template, alt = 'url(%s)', ''
left = min(c.position(), c.anchor)
@ -849,9 +853,9 @@ class TextEdit(PlainTextEdit):
template = '''\
<div style="page-break-before:always; page-break-after:always; page-break-inside:avoid">\
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" \
version="1.1" width="100%%" height="100%%" viewBox="0 0 1200 1600" preserveAspectRatio="{}">\
<image width="1200" height="1600" xlink:href="%s"/>\
</svg></div>'''.format('xMidYMid meet' if preserve_aspect_ratio else 'none')
version="1.1" width="100%%" height="100%%" viewBox="0 0 {w} {h}" preserveAspectRatio="{a}">\
<image width="{w}" height="{h}" xlink:href="%s"/>\
</svg></div>'''.format(w=width, h=height, a='xMidYMid meet' if preserve_aspect_ratio else 'none')
else:
alt = _('Image')
template = '<img alt="{0}" src="%s" />'.format(alt)

View File

@ -228,8 +228,8 @@ class Editor(QMainWindow):
func = getattr(self.editor, action)
func(*args)
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False):
self.editor.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_aspect_ratio)
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1):
self.editor.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_aspect_ratio, width=width, height=height)
def insert_hyperlink(self, href, text):
self.editor.insert_hyperlink(href, text)
@ -619,4 +619,3 @@ def launch_editor(path_to_edit, path_is_raw=False, syntax='html', callback=None)
callback(t)
t.show()
app.exec_()