From 6f09624280c394e189a45fd9f492147e3f5a189b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Oct 2019 12:45:19 +0530 Subject: [PATCH] When calculating CFI in paged mode go column-by-column from left to right and in each column start form midpoint and expand outwards --- src/pyj/read_book/paged_mode.pyj | 46 +++++++++++++++++++------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/pyj/read_book/paged_mode.pyj b/src/pyj/read_book/paged_mode.pyj index 35f76c3197..b8a4584b43 100644 --- a/src/pyj/read_book/paged_mode.pyj +++ b/src/pyj/read_book/paged_mode.pyj @@ -401,25 +401,33 @@ def current_cfi(): # null if it could not be calculated. ans = None if in_paged_mode(): - c = current_column_location() - for x in c, c - col_and_gap, c + col_and_gap: - # Try the current column, the previous column and the next - # column. Each column is tried from top to bottom. - left, right = x, x + col_and_gap - if left < 0 or right > scroll_viewport.paged_content_width(): - continue - deltax = col_and_gap // 25 - deltay = scroll_viewport.height() // 25 - cury = 0 - while cury < scroll_viewport.height(): - curx = left - while curx < right - gap: - cfi = cfi_at_point(curx-Math.ceil(current_scroll_offset()), cury-Math.ceil(window.pageYOffset)) - if cfi: - # print('Viewport cfi:', cfi) - return cfi - curx += deltax - cury += deltay + for cnum in range(cols_per_screen): + left = cnum * (col_and_gap + gap) + right = left + col_width + top, bottom = 0, scroll_viewport.height() + midx = (right - left) // 2 + deltax = (right - left) // 24 + deltay = (bottom - top) // 24 + midy = (bottom - top) // 2 + yidx = 0 + while True: + yb, ya = midy - yidx * deltay, midy + yidx * deltay + 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 else: try: ans = cfi_at_current() or None