mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
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.
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"))
|
||||
@@ -165,16 +171,17 @@ 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"
|
||||
@@ -182,6 +189,16 @@ function ReaderTypeset:genStyleSheetMenu()
|
||||
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
|
||||
@@ -189,8 +206,18 @@ function ReaderTypeset:genStyleSheetMenu()
|
||||
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
|
||||
@@ -200,15 +227,39 @@ 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
|
||||
@@ -216,7 +267,7 @@ function ReaderTypeset:genStyleSheetMenu()
|
||||
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
|
||||
@@ -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
|
||||
@@ -266,6 +317,7 @@ function ReaderTypeset:setStyleSheet(new_css)
|
||||
end
|
||||
end
|
||||
|
||||
-- Not used
|
||||
function ReaderTypeset:setEmbededStyleSheetOnly()
|
||||
if self.css ~= nil then
|
||||
-- clear applied css
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user