From e4eb1c61d147f60156b048e3e163dced726f4db3 Mon Sep 17 00:00:00 2001 From: poire-z Date: Thu, 23 Jun 2022 13:54:08 +0200 Subject: [PATCH] Footnote popup: allow using book font as popup font Also move footnote popup settings into a submenu. --- frontend/apps/reader/modules/readerlink.lua | 25 ++++++++- frontend/ui/widget/footnotewidget.lua | 59 +++++++++++++-------- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/frontend/apps/reader/modules/readerlink.lua b/frontend/apps/reader/modules/readerlink.lua index 87625cba4..157c095c7 100644 --- a/frontend/apps/reader/modules/readerlink.lua +++ b/frontend/apps/reader/modules/readerlink.lua @@ -275,7 +275,13 @@ The footnote content may be empty, truncated, or include other footnotes. From the footnote popup, you can jump to the footnote location in the book by swiping to the left.]]), }) + local footnote_popup_settings_items = {} table.insert(menu_items.follow_links.sub_item_table, 5, { + text = _("Footnote popup settings"), + sub_item_table = footnote_popup_settings_items, + separator = true, + }) + table.insert(footnote_popup_settings_items, { text = _("Show more links as footnotes"), enabled_func = function() return isFootnoteLinkInPopupEnabled() and @@ -287,8 +293,23 @@ From the footnote popup, you can jump to the footnote location in the book by sw not isPreferFootnoteEnabled()) end, help_text = _([[Loosen footnote detection rules to show more links as footnotes.]]), + separator = true, }) - table.insert(menu_items.follow_links.sub_item_table, 6, { + table.insert(footnote_popup_settings_items, { + text = _("Use book font as popup font"), + enabled_func = function() + return isFootnoteLinkInPopupEnabled() and + (isTapToFollowLinksOn() or isSwipeToFollowNearestLinkEnabled()) + end, + checked_func = function() + return G_reader_settings:isTrue("footnote_popup_use_book_font") + end, + callback = function() + G_reader_settings:flipNilOrFalse("footnote_popup_use_book_font") + end, + help_text = _([[Display the footnote popup text with the font set as the document font (the book text may still render with a different font if the book uses embedded fonts).]]), + }) + table.insert(footnote_popup_settings_items, { text = _("Set footnote popup font size"), enabled_func = function() return isFootnoteLinkInPopupEnabled() and @@ -358,7 +379,6 @@ The recommended value is -2.]]), help_text = _([[ The footnote popup font adjusts to the font size you've set for the document. This allows you to specify how much smaller or larger it should be relative to the document font size.]]), - separator = true, }) end end @@ -1345,6 +1365,7 @@ function ReaderLink:showAsFootnotePopup(link, neglect_current_location) local popup popup = FootnoteWidget:new{ html = html, + doc_font_name = self.ui.font.font_face, doc_font_size = Screen:scaleBySize(self.ui.font.font_size), doc_margins = self.ui.document:getPageMargins(), close_callback = close_callback, diff --git a/frontend/ui/widget/footnotewidget.lua b/frontend/ui/widget/footnotewidget.lua index 84633aa52..8ae1d45a4 100644 --- a/frontend/ui/widget/footnotewidget.lua +++ b/frontend/ui/widget/footnotewidget.lua @@ -21,26 +21,6 @@ local Screen = Device.screen local T = require("ffi/util").template local time = require("ui/time") --- If we wanted to use the default font set for the book, --- we'd need to add a few functions to crengine and cre.cpp --- to get the font files paths (for each font, regular, italic, --- bold...) so we can pass them to MuPDF with: --- @font-face { --- font-family: 'KOReader Footnote Font'; --- src: url("%1"); --- } --- @font-face { --- font-family: 'KOReader Footnote Font'; --- src: url("%2"); --- font-style: italic; --- } --- body { --- font-family: 'KOReader Footnote Font'; --- } --- But it looks quite fine if we use "Noto Sans": the difference in font look --- (Sans, vs probably Serif in the book) will help noticing this is a KOReader --- UI element, and somehow justify the difference in looks. - -- Note: we can't use < or > in comments in the CSS, or MuPDF complains with: -- error: css syntax error: unterminated comment. -- So, HTML tags in comments are written upppercase (eg:
  • => LI) @@ -53,6 +33,7 @@ local PAGE_CSS = [[ font-family: '%5'; } %6 +%7 ]] -- Make default MuPDF styles (source/html/html-layout.c) a bit @@ -62,7 +43,7 @@ local DEFAULT_CSS = [[ body { margin: 0; /* MuPDF: margin: 1em */ padding: 0; - line-height: 1.3; + line-height: 1.3; /* MuPDF defaults to 1.2 */ text-align: justify; } /* We keep left and right margin the same so it also displays as expected in RTL */ @@ -139,6 +120,7 @@ local FootnoteWidget = InputContainer:new{ -- For the doc_* values, we expect to be provided with the real -- (already scaled) sizes in screen pixels doc_font_size = Screen:scaleBySize(18), + doc_font_name = nil, doc_margins = { left = Screen:scaleBySize(20), right = Screen:scaleBySize(20), @@ -233,6 +215,38 @@ function FootnoteWidget:init() font_size = self.doc_font_size + (G_reader_settings:readSetting("footnote_popup_relative_font_size") or -2) end + local font_css = "" + if G_reader_settings:isTrue("footnote_popup_use_book_font") then + -- Note: we can't provide any base weight (as supported by crengine), as MuPDF + -- will use the bold font for anything with a weight > 400. We can only use the + -- font as-is, without its natural weight tweaked. + local seen_font_path = {} + for i=1, 4 do + local bold = i >= 3 + local italic = i == 2 or i ==4 + -- We assume the font is not from a collection, and ignore the index. + local font_path = cre.getFontFaceFilenameAndFaceIndex(self.doc_font_name, bold, italic) + -- crengine returns the regular filename when requesting a bold that + -- it has synthesized; but MuPDF would consider it as real bold and + -- would use it as-is: by not providing the fake bold font file path, + -- we let MuPDF itself synthesize the bold (and also italic if none + -- provided). So, keep track of what's been seen and used. + if font_path and not seen_font_path[font_path] then + seen_font_path[font_path] = true + font_css = font_css .. T("\n@font-face { font-family: 'KOReaderFootnoteFont'; src: url('%1')%2%3}", + font_path, + bold and "; font-weight: bold" or "", + italic and "; font-style: italic" or "") + end + end + if font_css ~= "" then + -- If not using our standard font, override "line-height:1.3" (which is fine + -- with Noto Sans) to use something smaller (looks like MuPDF's default is 1.2 + -- and we can't make it use the font natural line height...) + font_css = font_css .. "\nbody { font-family: 'KOReaderFootnoteFont'; line-height: 1.2 !important; }\n" + end + end + -- We want to display the footnote text with the same margins as -- the document, but keep the scrollbar in the right margin, so -- both small footnotes (without scrollbar) and longer ones (with @@ -245,8 +259,9 @@ function FootnoteWidget:init() if BD.mirroredUILayout() then html_left_margin, html_right_margin = html_right_margin, html_left_margin end + local css = T(PAGE_CSS, "0", html_right_margin, "0", html_left_margin, -- top right bottom left - self.font_face, DEFAULT_CSS) + self.font_face, font_css, DEFAULT_CSS) if self.css then -- add any provided css css = css .. "\n" .. self.css end