From 96312bfa105f923e7561f0aedfb61d655f825fd1 Mon Sep 17 00:00:00 2001 From: poire-z Date: Sun, 25 Aug 2024 20:40:12 +0200 Subject: [PATCH] HtmlBoxWidget: help MuPDF with block elements it doesn't know MuPDF's own stylesheet is incomplete, and doesn't specify that a few common (and uncommon) elements are display:block, so it would consider them inline, and render them badly (MuPDF also doesn't handle well
inside inline elements, so this fixes
ones in these elements). Fix this in HtmlBoxWidget so it solves any issue in HTML dict and popup footnote. For popup footnotes, also include crengine internal block elements, as we may indeed get them in the HTML content. --- frontend/ui/widget/footnotewidget.lua | 4 ++++ frontend/ui/widget/htmlboxwidget.lua | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/frontend/ui/widget/footnotewidget.lua b/frontend/ui/widget/footnotewidget.lua index 58ba99d60..47b11f719 100644 --- a/frontend/ui/widget/footnotewidget.lua +++ b/frontend/ui/widget/footnotewidget.lua @@ -72,6 +72,10 @@ body > li { list-style-type: none; } /* Remove any (possibly multiple) backlinks in Wikipedia EPUBs footnotes */ .noprint { display: none; } +/* Let MuPDF know about crengine internal block elements, + * so it doesn't render them inline */ +autoBoxing, floatBox, tabularBox { display: block; } + /* Style some FB2 tags not known to MuPDF */ strike, strikethrough { text-decoration: line-through; } underline { text-decoration: underline; } diff --git a/frontend/ui/widget/htmlboxwidget.lua b/frontend/ui/widget/htmlboxwidget.lua index 3b4b36291..598a1bcdd 100644 --- a/frontend/ui/widget/htmlboxwidget.lua +++ b/frontend/ui/widget/htmlboxwidget.lua @@ -36,13 +36,27 @@ function HtmlBoxWidget:init() end end -function HtmlBoxWidget:setContent(body, css, default_font_size, is_xhtml) +-- These are generic "fixes" to MuPDF HTML stylesheet: +-- - MuPDF doesn't set some elements as being display:block, and would +-- consider them inline, and would badly handle
inside them. +-- Note: this is a generic issue with
inside inline elements, see: +-- https://github.com/koreader/koreader/issues/12258#issuecomment-2267629234 +local mupdf_css_fixes = [[ +article, aside, button, canvas, datalist, details, dialog, dir, fieldset, figcaption, +figure, footer, form, frame, frameset, header, hgroup, iframe, legend, listing, +main, map, marquee, multicol, nav, noembed, noframes, noscript, optgroup, output, +plaintext, search, select, summary, template, textarea, video, xmp { + display: block; +} +]] + +function HtmlBoxWidget:setContent(body, css, default_font_size, is_xhtml, no_css_fixes) -- fz_set_user_css is tied to the context instead of the document so to easily support multiple -- HTML dictionaries with different CSS, we embed the stylesheet into the HTML instead of using -- that function. local head = "" - if css then - head = string.format("", css) + if css or not no_css_fixes then + head = string.format("", mupdf_css_fixes, css or "") end local html = string.format("%s%s", head, body)