From 46988f0f9ace9162e0f967b3cf5504a65745621c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 20 Nov 2013 22:20:49 +0530 Subject: [PATCH] E-book viewer: Allow searching for whole words to work with hyphenation E-book viewer: Allow searching to work with hyphenation enabled, provided you are searching for whole words. Searching for partial words may still not work. For robust searching, turn off hyphenation in the viewer preferences. --- resources/viewer/hyphenation.js | 6 +++++- src/calibre/gui2/viewer/documentview.py | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/resources/viewer/hyphenation.js b/resources/viewer/hyphenation.js index 858d1757f8..d5c6fd5fac 100644 --- a/resources/viewer/hyphenation.js +++ b/resources/viewer/hyphenation.js @@ -8,7 +8,7 @@ function do_hyphenation(lang) { Hyphenator.config( { 'minwordlength' : 6, - //'hyphenchar' : '|', + // 'hyphenchar' : '|', 'displaytogglebox' : false, 'remoteloading' : false, 'doframes' : true, @@ -22,3 +22,7 @@ function do_hyphenation(lang) { Hyphenator.hyphenate(document.body, lang); } +function hyphenate_text(text, lang) { + return Hyphenator.hyphenate(text, lang); +} + diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 6424ae741b..a54b84d82e 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -139,6 +139,15 @@ class Document(QWebPage): # {{{ data += b64encode(raw.encode('utf-8')) self.settings().setUserStyleSheetUrl(QUrl(data)) + def findText(self, q, flags): + if self.hyphenatable: + q = unicode(q) + hyphenated_q = self.javascript( + 'hyphenate_text(%s, "%s")' % (json.dumps(q, ensure_ascii=False), self.loaded_lang), typ='string') + if QWebPage.findText(self, hyphenated_q, flags): + return True + return QWebPage.findText(self, q, flags) + def misc_config(self, opts): self.hyphenate = opts.hyphenate self.hyphenate_default_lang = opts.hyphenate_default_lang @@ -198,10 +207,14 @@ class Document(QWebPage): # {{{ def animated_scroll_done(self): self.emit(SIGNAL('animated_scroll_done()')) + @property + def hyphenatable(self): + # Qt fails to render soft hyphens correctly on windows xp + return not isxp and self.hyphenate and getattr(self, 'loaded_lang', '') + @pyqtSignature("") def init_hyphenate(self): - # Qt fails to render soft hyphens correctly on windows xp - if not isxp and self.hyphenate and getattr(self, 'loaded_lang', ''): + if self.hyphenatable: self.javascript('do_hyphenation("%s")'%self.loaded_lang) @pyqtSlot(int) @@ -782,7 +795,7 @@ class DocumentView(QWebView): # {{{ def search(self, text, backwards=False): flags = self.document.FindBackward if backwards else self.document.FindFlags(0) - found = self.findText(text, flags) + found = self.document.findText(text, flags) if found and self.document.in_paged_mode: self.document.javascript('paged_display.snap_to_selection()') return found