From 0250666dd956d52bbc5ce0c0c7b442de7619ac56 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 27 Feb 2017 23:36:17 +0530 Subject: [PATCH] Edit Book: When inserting full screen images, use the actual image dimensions in the generated SVG code, when available --- src/calibre/gui2/tweak_book/boss.py | 4 +++- src/calibre/gui2/tweak_book/editor/text.py | 12 ++++++++---- src/calibre/gui2/tweak_book/editor/widget.py | 5 ++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 3e22e41ba5..677adbae2f 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -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) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index 479bed211a..e76c0b34b0 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -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 = '''\
\ \ -\ -
'''.format('xMidYMid meet' if preserve_aspect_ratio else 'none') +version="1.1" width="100%%" height="100%%" viewBox="0 0 {w} {h}" preserveAspectRatio="{a}">\ +\ +'''.format(w=width, h=height, a='xMidYMid meet' if preserve_aspect_ratio else 'none') else: alt = _('Image') template = '{0}'.format(alt) diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 32a1281316..5122e7c8d9 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -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_() -