Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump crengine: better conformance to the HTML Standard rendering #11527

Merged
merged 5 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 78 additions & 17 deletions frontend/apps/reader/modules/readertypeset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local ReaderTypeset = WidgetContainer:extend{
-- @translators This is style in the sense meant by CSS (cascading style sheets), relating to the layout and presentation of the document. See <https://en.wikipedia.org/wiki/CSS> for more information.
css_menu_title = C_("CSS", "Style"),
css = nil,
internal_css = true,
unscaled_margins = nil,
}

Expand All @@ -27,8 +26,16 @@ end

function ReaderTypeset:onReadSettings(config)
self.css = config:readSetting("css")
or G_reader_settings:readSetting("copt_css")
or self.ui.document.default_css
if not self.css then
if self.ui.document.is_fb2 then
self.css = G_reader_settings:readSetting("copt_fb2_css")
else
self.css = G_reader_settings:readSetting("copt_css")
end
end
if not self.css then
self.css = self.ui.document.default_css
end
local tweaks_css = self.ui.styletweak:getCssText()
self.ui.document:setStyleSheet(self.css, tweaks_css)

Expand Down Expand Up @@ -93,7 +100,6 @@ function ReaderTypeset:onToggleEmbeddedStyleSheet(toggle)
else
self.configurable.embedded_css = 0
text = _("Disabled embedded styles.")
self:setStyleSheet(self.ui.document.default_css)
end
self.ui.document:setEmbeddedStyleSheet(self.configurable.embedded_css)
self.ui:handleEvent(Event:new("UpdatePos"))
Expand Down Expand Up @@ -165,32 +171,53 @@ local OBSOLETED_CSS = {
}

function ReaderTypeset:genStyleSheetMenu()
local getStyleMenuItem = function(text, css_file, separator)
local getStyleMenuItem = function(text, css_file, description, fb2_compatible, separator)
return {
text_func = function()
return text .. (css_file == G_reader_settings:readSetting("copt_css") and " ★" or "")
local css_opt = self.ui.document.is_fb2 and "copt_fb2_css" or "copt_css"
return text .. (css_file == G_reader_settings:readSetting(css_opt) and " ★" or "")
end,
callback = function()
self:setStyleSheet(css_file or self.ui.document.default_css)
end,
hold_callback = function(touchmenu_instance)
self:makeDefaultStyleSheet(css_file, text, touchmenu_instance)
self:makeDefaultStyleSheet(css_file, text, description, touchmenu_instance)
end,
checked_func = function()
if not css_file then -- "Auto"
return self.css == self.ui.document.default_css
end
return css_file == self.css
end,
enabled_func = function()
if fb2_compatible == true and not self.ui.document.is_fb2 then
return false
end
if fb2_compatible == false and self.ui.document.is_fb2 then
return false
end
-- if fb2_compatible==nil, we don't know (user css file)
return true
end,
separator = separator,
}
end

local style_table = {}
local obsoleted_table = {}

table.insert(style_table, getStyleMenuItem(_("None"), ""))
table.insert(style_table, getStyleMenuItem(_("Auto"), nil, true))
table.insert(style_table, getStyleMenuItem(
_("None"),
"",
_("This sets an empty User-Agent stylesheet, and expects the document stylesheet to style everything (which publishers probably don't).\nThis is mostly only interesting for testing.")
))
table.insert(style_table, getStyleMenuItem(
_("Auto"),
nil,
_("This selects the default and preferred stylesheet for the document type."),
nil,
true -- separator
))

local css_files = {}
for f in lfs.dir("./data") do
Expand All @@ -200,23 +227,47 @@ function ReaderTypeset:genStyleSheetMenu()
end
-- Add the 3 main styles
if css_files["epub.css"] then
table.insert(style_table, getStyleMenuItem(_("HTML / EPUB (epub.css)"), css_files["epub.css"]))
table.insert(style_table, getStyleMenuItem(
_("Traditional book look (epub.css)"),
css_files["epub.css"],
_([[
This is our book look-alike stylesheet: it extends the HTML standard stylesheet with styles aimed at making HTML content look more like a paper book (with justified text and indentation on paragraphs) than like a web page.
It is perfect for unstyled books, and might make styled books more readable.
It may cause some small issues on some books (miscentered titles, headings or separators, or unexpected text indentation), as publishers don't expect to have our added styles at play and need to reset them; try switching to html5.css when you notice such issues.]]),
false -- not fb2_compatible
))
css_files["epub.css"] = nil
end
if css_files["html5.css"] then
table.insert(style_table, getStyleMenuItem(_("HTML5 (html5.css)"), css_files["html5.css"]))
table.insert(style_table, getStyleMenuItem(
_("HTML Standard rendering (html5.css)"),
css_files["html5.css"],
_([[
This stylesheet conforms to the HTML Standard rendering suggestions (with a few limitations), similar to what most web browsers use.
As most publishers nowadays make and test their book with tools based on web browser engines, it is the stylesheet to use to see a book as these publishers intended.
On unstyled books though, it may give them the look of a web page (left aligned paragraphs without indentation and with spacing between them); try switching to epub.css when that happens.]]),
false -- not fb2_compatible
))
css_files["html5.css"] = nil
end
if css_files["fb2.css"] then
table.insert(style_table, getStyleMenuItem(_("FictionBook (fb2.css)"), css_files["fb2.css"], true))
table.insert(style_table, getStyleMenuItem(
_("FictionBook (fb2.css)"),
css_files["fb2.css"],
_([[
This stylesheet is to be used only with FB2 and FB3 documents, which are not classic HTML, and need some specific styling.
(FictionBook 2 & 3 are open XML-based e-book formats which originated and gained popularity in Russia.)]]),
true, -- fb2_compatible
true -- separator
))
css_files["fb2.css"] = nil
end
-- Add the obsoleted ones to the Obsolete sub menu
local obsoleted_css = {} -- for check_func of the Obsolete sub menu itself
for __, css in ipairs(OBSOLETED_CSS) do
obsoleted_css[css_files[css]] = css
if css_files[css] then
table.insert(obsoleted_table, getStyleMenuItem(css, css_files[css]))
table.insert(obsoleted_table, getStyleMenuItem(css, css_files[css], _("This stylesheet is obsolete: don't use it. It is kept solely to be able to open documents last read years ago and to migrate their highlights.")))
css_files[css] = nil
end
end
Expand All @@ -227,7 +278,7 @@ function ReaderTypeset:genStyleSheetMenu()
end
table.sort(user_files)
for __, css in ipairs(user_files) do
table.insert(style_table, getStyleMenuItem(css, css_files[css]))
table.insert(style_table, getStyleMenuItem(css, css_files[css], _("This is a user added stylesheet.")))
end

style_table[#style_table].separator = true
Expand Down Expand Up @@ -266,6 +317,7 @@ function ReaderTypeset:setStyleSheet(new_css)
end
end

-- Not used
function ReaderTypeset:setEmbededStyleSheetOnly()
if self.css ~= nil then
-- clear applied css
Expand Down Expand Up @@ -355,11 +407,20 @@ function ReaderTypeset:addToMainMenu(menu_items)
}
end

function ReaderTypeset:makeDefaultStyleSheet(css, text, touchmenu_instance)
function ReaderTypeset:makeDefaultStyleSheet(css, name, description, touchmenu_instance)
local text = self.ui.document.is_fb2 and T(_("Set default style for FB2 documents to %1?"), BD.filename(name))
or T(_("Set default style to %1?"), BD.filename(name))
if description then
text = text .. "\n\n" .. description
end
UIManager:show(ConfirmBox:new{
text = T(_("Set default style to %1?"), BD.filename(text)),
text = text,
ok_callback = function()
G_reader_settings:saveSetting("copt_css", css)
if self.ui.document.is_fb2 then
G_reader_settings:saveSetting("copt_fb2_css", css)
else
G_reader_settings:saveSetting("copt_css", css)
end
if touchmenu_instance then touchmenu_instance:updateItems() end
end,
})
Expand Down
1 change: 1 addition & 0 deletions frontend/document/credocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function CreDocument:init()
self.default_css = "./data/epub.css"
if file_type == "fb2" or file_type == "fb3" then
self.default_css = "./data/fb2.css"
self.is_fb2 = true -- FB2 won't look good with any html-oriented stylesheet
end

-- This mode must be the same as the default one set as ReaderView.view_mode
Expand Down
14 changes: 14 additions & 0 deletions frontend/ui/data/css_tweaks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ Further small adjustments can be done with 'Line Spacing' in the bottom menu.]])
},
{
title = _("Font size and families"),
{
id = "font_no_presentational_hints",
title = _("Ignore font related HTML presentational hints"),
description = _("Ignore HTML attributes that contribute to styles on the elements <body> (bgcolor, text…) and <font> (face, size, color…)."),
css = [[body, font { -cr-hint: no-presentational; }]],
separator = true,
},
{
id = "font_family_all_inherit",
title = _("Ignore publisher font families"),
Expand Down Expand Up @@ -564,6 +571,13 @@ body, h1, h2, h3, h4, h5, h6, div, li, td, th { text-indent: 0 !important; }
title = _("Tables, links, images"),
{
title = _("Tables"),
{
id = "table_no_presentational_hints",
title = _("Ignore tables related HTML presentational hints"),
description = _("Ignore HTML attributes that contribute to styles on the <table> element and its sub-elements (ie. align, valign, frame, rules, border, cellpading, cellspacing…)."),
css = [[table, caption, colgroup, col, thead, tbody, tfoot, tr, td, th { -cr-hint: no-presentational; }]],
separator = true,
},
{
id = "table_full_width",
title = _("Full-width tables"),
Expand Down
88 changes: 84 additions & 4 deletions frontend/ui/wikipedia.lua
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,84 @@ function Wikipedia:createEpub(epub_path, page, lang, with_images)
-- to look more alike wikipedia web pages (that the user can ignore
-- with "Embedded Style" off)
epub:add("OEBPS/stylesheet.css", [[
/* Generic styling picked from our epub.css (see it for comments),
to give this epub a book look even if used with html5.css */
body {
text-align: justify;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 0.7em;
margin-bottom: 0.5em;
hyphens: none;
}
h1 { font-size: 150%; }
h2 { font-size: 140%; }
h3 { font-size: 130%; }
h4 { font-size: 120%; }
h5 { font-size: 110%; }
h6 { font-size: 100%; }
p {
text-indent: 1.2em;
margin-top: 0;
margin-bottom: 0;
}
blockquote {
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: 2em;
margin-right: 1em;
}
blockquote:dir(rtl) {
margin-left: 1em;
margin-right: 2em;
}
dl {
margin-left: 0;
}
dt {
margin-left: 0;
margin-top: 0.3em;
font-weight: bold;
}
dd {
margin-left: 1.3em;
}
dd:dir(rtl) {
margin-left: unset;
margin-right: 1.3em;
}
pre {
text-align: left;
margin-top: 0.5em;
margin-bottom: 0.5em;
}
hr {
border-style: solid;
}
table {
font-size: 80%;
margin: 3px 0;
border-spacing: 1px;
}
table table { /* stop imbricated tables from getting smaller */
font-size: 100%;
}
th, td {
padding: 3px;
}
th {
background-color: #DDD;
text-align: center;
}
table caption {
padding: 4px;
background-color: #EEE;
}
sup { font-size: 70%; }
sub { font-size: 70%; }

/* Specific for our Wikipedia EPUBs */

/* Make section headers looks left aligned and avoid some page breaks */
h1, h2 {
page-break-before: always;
Expand Down Expand Up @@ -978,10 +1056,7 @@ a.newwikinonexistent {

/* Don't waste left margin for TOC, notes and other lists */
ul, ol {
margin-left: 0;
}
ul:dir(rtl), ol:dir(rtl) {
margin-right: 0;
margin: 0;
}
/* OL in Wikipedia pages may inherit their style-type from a wrapping div,
* ensure they fallback to decimal with inheritance */
Expand Down Expand Up @@ -1176,6 +1251,11 @@ table {
.citation {
font-style: italic;
}
abbr.abbr {
/* Prevent these from looking like a link */
text-decoration: inherit;
}

/* hide some view/edit/discuss short links displayed as "v m d" */
.nv-view, .nv-edit, .nv-talk {
display: none;
Expand Down
5 changes: 5 additions & 0 deletions frontend/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,11 @@ function util.prettifyCSS(css_text, condensed)
s = s:gsub(";", "\b")
return s
end)
-- Protect ',' inside () (ie. ":is(td, th)") by replacing them with rare control chars
css_text = css_text:gsub("%b()/", function(s)
s = s:gsub(",", "\v")
return s
end)
-- Cleanup declarations (the most nested ones only, which may be
-- contained in "@supports (...) {...}" or "@media (...) {...}")
css_text = css_text:gsub(" *{([^{}]*)} *", function(s)
Expand Down