ReaderTypeset: tweak Style> menu logic and defaults

- Reword and document most menu items.
- Handle internally two default styles, one applying only
  to FB2/FB2 books, and the other to all other formats.
- Also don't reset the stylesheet to epub.css when
  toggling Embedded Styles to off.
reviewable/pr11533/r1
poire-z 3 months ago
parent 93b3e3504d
commit 9467034f3f

@ -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. -- @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_menu_title = C_("CSS", "Style"),
css = nil, css = nil,
internal_css = true,
unscaled_margins = nil, unscaled_margins = nil,
} }
@ -27,8 +26,16 @@ end
function ReaderTypeset:onReadSettings(config) function ReaderTypeset:onReadSettings(config)
self.css = config:readSetting("css") self.css = config:readSetting("css")
or G_reader_settings:readSetting("copt_css") if not self.css then
or self.ui.document.default_css 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() local tweaks_css = self.ui.styletweak:getCssText()
self.ui.document:setStyleSheet(self.css, tweaks_css) self.ui.document:setStyleSheet(self.css, tweaks_css)
@ -93,7 +100,6 @@ function ReaderTypeset:onToggleEmbeddedStyleSheet(toggle)
else else
self.configurable.embedded_css = 0 self.configurable.embedded_css = 0
text = _("Disabled embedded styles.") text = _("Disabled embedded styles.")
self:setStyleSheet(self.ui.document.default_css)
end end
self.ui.document:setEmbeddedStyleSheet(self.configurable.embedded_css) self.ui.document:setEmbeddedStyleSheet(self.configurable.embedded_css)
self.ui:handleEvent(Event:new("UpdatePos")) self.ui:handleEvent(Event:new("UpdatePos"))
@ -165,16 +171,17 @@ local OBSOLETED_CSS = {
} }
function ReaderTypeset:genStyleSheetMenu() function ReaderTypeset:genStyleSheetMenu()
local getStyleMenuItem = function(text, css_file, separator) local getStyleMenuItem = function(text, css_file, description, fb2_compatible, separator)
return { return {
text_func = function() 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, end,
callback = function() callback = function()
self:setStyleSheet(css_file or self.ui.document.default_css) self:setStyleSheet(css_file or self.ui.document.default_css)
end, end,
hold_callback = function(touchmenu_instance) hold_callback = function(touchmenu_instance)
self:makeDefaultStyleSheet(css_file, text, touchmenu_instance) self:makeDefaultStyleSheet(css_file, text, description, touchmenu_instance)
end, end,
checked_func = function() checked_func = function()
if not css_file then -- "Auto" if not css_file then -- "Auto"
@ -182,6 +189,16 @@ function ReaderTypeset:genStyleSheetMenu()
end end
return css_file == self.css return css_file == self.css
end, 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, separator = separator,
} }
end end
@ -189,8 +206,18 @@ function ReaderTypeset:genStyleSheetMenu()
local style_table = {} local style_table = {}
local obsoleted_table = {} local obsoleted_table = {}
table.insert(style_table, getStyleMenuItem(_("None"), "")) table.insert(style_table, getStyleMenuItem(
table.insert(style_table, getStyleMenuItem(_("Auto"), nil, true)) _("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 = {} local css_files = {}
for f in lfs.dir("./data") do for f in lfs.dir("./data") do
@ -200,15 +227,39 @@ function ReaderTypeset:genStyleSheetMenu()
end end
-- Add the 3 main styles -- Add the 3 main styles
if css_files["epub.css"] then 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 css_files["epub.css"] = nil
end end
if css_files["html5.css"] then 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 css_files["html5.css"] = nil
end end
if css_files["fb2.css"] then 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 css_files["fb2.css"] = nil
end end
-- Add the obsoleted ones to the Obsolete sub menu -- Add the obsoleted ones to the Obsolete sub menu
@ -216,7 +267,7 @@ function ReaderTypeset:genStyleSheetMenu()
for __, css in ipairs(OBSOLETED_CSS) do for __, css in ipairs(OBSOLETED_CSS) do
obsoleted_css[css_files[css]] = css obsoleted_css[css_files[css]] = css
if css_files[css] then 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 css_files[css] = nil
end end
end end
@ -227,7 +278,7 @@ function ReaderTypeset:genStyleSheetMenu()
end end
table.sort(user_files) table.sort(user_files)
for __, css in ipairs(user_files) do 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 end
style_table[#style_table].separator = true style_table[#style_table].separator = true
@ -266,6 +317,7 @@ function ReaderTypeset:setStyleSheet(new_css)
end end
end end
-- Not used
function ReaderTypeset:setEmbededStyleSheetOnly() function ReaderTypeset:setEmbededStyleSheetOnly()
if self.css ~= nil then if self.css ~= nil then
-- clear applied css -- clear applied css
@ -355,11 +407,20 @@ function ReaderTypeset:addToMainMenu(menu_items)
} }
end 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{ UIManager:show(ConfirmBox:new{
text = T(_("Set default style to %1?"), BD.filename(text)), text = text,
ok_callback = function() 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 if touchmenu_instance then touchmenu_instance:updateItems() end
end, end,
}) })

@ -173,6 +173,7 @@ function CreDocument:init()
self.default_css = "./data/epub.css" self.default_css = "./data/epub.css"
if file_type == "fb2" or file_type == "fb3" then if file_type == "fb2" or file_type == "fb3" then
self.default_css = "./data/fb2.css" self.default_css = "./data/fb2.css"
self.is_fb2 = true -- FB2 won't look good with any html-oriented stylesheet
end end
-- This mode must be the same as the default one set as ReaderView.view_mode -- This mode must be the same as the default one set as ReaderView.view_mode

Loading…
Cancel
Save