Edit Book: Fix non-BMP unicode characters causing matching tag highlighting to be slightly off on linux

This commit is contained in:
Kovid Goyal 2014-06-23 22:19:52 +05:30
parent 34c021a1f0
commit ffde0ca388

View File

@ -15,6 +15,7 @@ from PyQt4.Qt import QTextEdit
from calibre import prepare_string_for_xml from calibre import prepare_string_for_xml
from calibre.gui2 import error_dialog 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.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) get_offset = itemgetter(0)
PARAGRAPH_SEPARATOR = '\u2029' PARAGRAPH_SEPARATOR = '\u2029'
@ -216,8 +217,14 @@ class HTMLSmarts(NullSmarts):
def add_tag(tag): def add_tag(tag):
a = QTextEdit.ExtraSelection() a = QTextEdit.ExtraSelection()
a.cursor, a.format = editor.textCursor(), editor.match_paren_format a.cursor, a.format = editor.textCursor(), editor.match_paren_format
a.cursor.setPosition(tag.start_block.position() + tag.start_offset) a.cursor.setPosition(tag.start_block.position()), a.cursor.movePosition(a.cursor.EndOfBlock, a.cursor.KeepAnchor)
a.cursor.setPosition(tag.end_block.position() + tag.end_offset + 1, 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) ans.append(a)
c = editor.textCursor() c = editor.textCursor()