diff --git a/src/calibre/gui2/tweak_book/editor/smart/html.py b/src/calibre/gui2/tweak_book/editor/smart/html.py index 43319521de..973d513441 100644 --- a/src/calibre/gui2/tweak_book/editor/smart/html.py +++ b/src/calibre/gui2/tweak_book/editor/smart/html.py @@ -15,6 +15,7 @@ from PyQt4.Qt import QTextEdit from calibre import prepare_string_for_xml from calibre.gui2 import error_dialog from calibre.gui2.tweak_book.editor.syntax.html import ATTR_NAME, ATTR_END, ATTR_START, ATTR_VALUE +from calibre.utils.icu import utf16_length get_offset = itemgetter(0) PARAGRAPH_SEPARATOR = '\u2029' @@ -216,8 +217,14 @@ class HTMLSmarts(NullSmarts): def add_tag(tag): a = QTextEdit.ExtraSelection() a.cursor, a.format = editor.textCursor(), editor.match_paren_format - a.cursor.setPosition(tag.start_block.position() + tag.start_offset) - a.cursor.setPosition(tag.end_block.position() + tag.end_offset + 1, a.cursor.KeepAnchor) + a.cursor.setPosition(tag.start_block.position()), a.cursor.movePosition(a.cursor.EndOfBlock, a.cursor.KeepAnchor) + text = unicode(a.cursor.selectedText()) + start_pos = utf16_length(text[:tag.start_offset]) + a.cursor.setPosition(tag.end_block.position()), a.cursor.movePosition(a.cursor.EndOfBlock, a.cursor.KeepAnchor) + text = unicode(a.cursor.selectedText()) + end_pos = utf16_length(text[:tag.end_offset + 1]) + a.cursor.setPosition(tag.start_block.position() + start_pos) + a.cursor.setPosition(tag.end_block.position() + end_pos, a.cursor.KeepAnchor) ans.append(a) c = editor.textCursor()