From 99ce8f9876827a1aa9c0ee7af35a93340bd03c60 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 25 Oct 2019 20:37:01 +0530 Subject: [PATCH] Viewer: Fix rare exception when navigating to a CFI. Fixes #1849775 [Exception in ebook viewer](https://bugs.launchpad.net/calibre/+bug/1849775) --- src/pyj/read_book/cfi.pyj | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index 513ca6ad37..d922f5f9dd 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -540,12 +540,22 @@ def scroll_to(cfi, callback, doc): # {{{ p.normalize() # Reset the range to what it was before the span was added - r.setStart(sc, so) - try: - r.setEnd(ec, eo) - except: - # this happens for some japanese books - r.setEnd(ec, eo-1) + offset = so + while offset > -1: + try: + r.setStart(sc, offset) + break + except: + offset -= 1 + + offset = eo + while offset > -1: + try: + r.setEnd(ec, offset) + break + except: + offset -= 1 + rects = r.getClientRects() if rects.length > 0: rect = rects[0]