From ff3af3ac157a10c1b81c82c5c3d799a9f6c520e2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Sep 2023 19:54:56 +0530 Subject: [PATCH] E-book viewer: Make CFI calculation more robust especially on pages with very little content. Fixes #2037454 [E-book viewer: "Back" button doesn't work under some conditions](https://bugs.launchpad.net/calibre/+bug/2037454) We were only testing two x values per y value. Instead test all 48. --- src/pyj/read_book/paged_mode.pyj | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/pyj/read_book/paged_mode.pyj b/src/pyj/read_book/paged_mode.pyj index ce22ac66b7..0d242ed781 100644 --- a/src/pyj/read_book/paged_mode.pyj +++ b/src/pyj/read_book/paged_mode.pyj @@ -618,19 +618,20 @@ def current_cfi(): if yb <= top or ya >= bottom: break yidx += 1 - xidx = 0 ys = v'[ya]' if ya is yb else v'[yb, ya]' for cury in ys: - xb, xa = midx - xidx * deltax, midx + xidx * deltax - if xa <= left or xb >= right: - break - xidx += 1 - xs = v'[xa]' if xa is xb else v'[xb, xa]' - for curx in xs: - cfi = cfi_at_point(curx, cury) - if cfi: - # print('Viewport cfi:', cfi) - return cfi + xidx = 0 + while True: + xb, xa = midx - xidx * deltax, midx + xidx * deltax + if xb <= left or xa >= right: + break + xidx += 1 + xs = v'[xa]' if xa is xb else v'[xb, xa]' + for curx in xs: + cfi = cfi_at_point(curx, cury) + if cfi: + # print('Viewport cfi:', cfi) + return cfi else: try: ans = cfi_at_current() or None