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.
This commit is contained in:
Kovid Goyal 2013-11-20 22:20:49 +05:30
parent cccd4770dc
commit 46988f0f9a
2 changed files with 21 additions and 4 deletions

View File

@ -22,3 +22,7 @@ function do_hyphenation(lang) {
Hyphenator.hyphenate(document.body, lang);
}
function hyphenate_text(text, lang) {
return Hyphenator.hyphenate(text, lang);
}

View File

@ -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