From 31b0c321fc256fc293adef2e6d2abfd014acab15 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 31 Jan 2023 19:26:42 +0530 Subject: [PATCH] DRYer --- src/pyj/read_book/paged_mode.pyj | 15 ++------------- src/pyj/read_book/viewport.pyj | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/pyj/read_book/paged_mode.pyj b/src/pyj/read_book/paged_mode.pyj index 3b417a6b2e..707355d273 100644 --- a/src/pyj/read_book/paged_mode.pyj +++ b/src/pyj/read_book/paged_mode.pyj @@ -33,7 +33,7 @@ from read_book.cfi import ( ) from read_book.globals import current_spine_item, get_boss, rtl_page_progression from read_book.settings import opts -from read_book.viewport import scroll_viewport, line_height, rem_size +from read_book.viewport import scroll_viewport, line_height, rem_size, get_unit_size_in_pixels from utils import ( get_elem_data, set_elem_data ) @@ -155,18 +155,7 @@ def cps_by_em_size(): if fs is 0: ans = cps_by_em_size.ans = 16 else: - d = document.createElement('span') - d.style.position = 'absolute' - d.style.visibility = 'hidden' - d.style.width = '1rem' - d.style.fontSize = '1rem' - d.style.paddingTop = d.style.paddingBottom = d.style.paddingLeft = d.style.paddingRight = '0' - d.style.marginTop = d.style.marginBottom = d.style.marginLeft = d.style.marginRight = '0' - d.style.borderStyle = 'none' - document.body.appendChild(d) - w = d.clientWidth - document.body.removeChild(d) - ans = cps_by_em_size.ans = max(2, w) + ans = cps_by_em_size.ans = max(2, get_unit_size_in_pixels('rem')) cps_by_em_size.at_font_size = fs return ans diff --git a/src/pyj/read_book/viewport.pyj b/src/pyj/read_book/viewport.pyj index d4d73d4452..ecc7dfbfd9 100644 --- a/src/pyj/read_book/viewport.pyj +++ b/src/pyj/read_book/viewport.pyj @@ -330,6 +330,22 @@ for attr in FUNCTIONS: scroll_viewport['paged_' + attr] = scroll_viewport[attr] viewport_mode_changer(scroll_viewport.set_mode) + +def get_unit_size_in_pixels(unit): + d = document.createElement('span') + d.style.position = 'absolute' + d.style.visibility = 'hidden' + d.style.width = f'1{unit}' + d.style.fontSize = f'1{unit}' + d.style.paddingTop = d.style.paddingBottom = d.style.paddingLeft = d.style.paddingRight = '0' + d.style.marginTop = d.style.marginBottom = d.style.marginLeft = d.style.marginRight = '0' + d.style.borderStyle = 'none' + document.body.appendChild(d) + ans = d.clientWidth + document.body.removeChild(d) + return ans + + def rem_size(reset): if reset: rem_size.ans = None @@ -344,7 +360,7 @@ def rem_size(reset): d.style.marginTop = d.style.marginBottom = d.style.marginLeft = d.style.marginRight = '0' d.style.borderStyle = 'none' document.body.appendChild(d) - rem_size.ans = d.clientWidth + rem_size.ans = max(2, d.clientWidth) document.body.removeChild(d) return rem_size.ans