HTML dict: enable color rendering and per-dict user fix html func (#3585)

Also fix possibility of crash in HtmlBoxWidget:free()
pull/3587/head
poire-z 6 years ago committed by Frans de Jonge
parent 30200b516f
commit 8c897a086f

@ -67,6 +67,8 @@ local ReaderDictionary = InputContainer:new{
lookup_msg = _("Searching dictionary for:\n%1"),
}
-- For a HTML dict, one can specify a specific stylesheet
-- in a file named as the .ifo with a .css extension
local function readDictionaryCss(path)
local f = io.open(path, "r")
if not f then
@ -78,6 +80,26 @@ local function readDictionaryCss(path)
return content
end
-- For a HTML dict, one can specify a function called on
-- the raw returned definition to "fix" the HTML if needed
-- (as MuPDF, used for rendering, is quite sensitive to the
-- HTML quality) in a file named as the .ifo with a .lua
-- extension, containing for example:
-- return function(html)
-- html = html:gsub("<hr>", "<hr/>")
-- return html
-- end
local function getDictionaryFixHtmlFunc(path)
if lfs.attributes(path, "mode") == "file" then
local ok, func = pcall(dofile, path)
if ok and func then
return func
else
logger.warn("Dict's user provided file failed:", func)
end
end
end
function ReaderDictionary:init()
self.ui.menu:registerToMainMenu(self)
self.data_dir = os.getenv("STARDICT_DATA_DIR") or
@ -108,7 +130,8 @@ function ReaderDictionary:init()
file = ifo_file,
name = dictname,
is_html = is_html,
css = readDictionaryCss(ifo_file:gsub("%.ifo$", ".css"))
css = readDictionaryCss(ifo_file:gsub("%.ifo$", ".css")),
fix_html_func = getDictionaryFixHtmlFunc(ifo_file:gsub("%.ifo$", ".lua")),
})
end
end
@ -368,6 +391,14 @@ local function tidyMarkup(results)
if ifo and ifo.is_html then
result.is_html = ifo.is_html
result.css = ifo.css
if ifo.fix_html_func then
local ok, fixed_definition = pcall(ifo.fix_html_func, result.definition)
if ok then
result.definition = fixed_definition
else
logger.warn("Dict's user provided funcion failed:", fixed_definition)
end
end
else
local def = result.definition
-- preserve the <br> tag for line break

@ -5,10 +5,11 @@ HTML widget (without scroll bars).
local DrawContext = require("ffi/drawcontext")
local Geom = require("ui/geometry")
local InputContainer = require("ui/widget/container/inputcontainer")
local logger = require("logger")
local Mupdf = require("ffi/mupdf")
local util = require("util")
local Screen = require("device").screen
local TimeVal = require("ui/timeval")
local logger = require("logger")
local util = require("util")
local HtmlBoxWidget = InputContainer:new{
bb = nil,
@ -63,10 +64,17 @@ function HtmlBoxWidget:_render()
return
end
-- In pdfdocument.lua, color is activated only at the moment of
-- rendering and then immediately disabled, for safety with kopt.
-- We do the same here.
Mupdf.color = Screen:isColorEnabled()
local page = self.document:openPage(self.page_number)
local dc = DrawContext.new()
self.bb = page:draw_new(dc, self.dimen.w, self.dimen.h, 0, 0)
page:close()
Mupdf.color = false
end
function HtmlBoxWidget:getSize()
@ -99,8 +107,10 @@ end
function HtmlBoxWidget:free()
self:freeBb()
self.document:close()
self.document = nil
if self.document then
self.document:close()
self.document = nil
end
end
function HtmlBoxWidget:onCloseWidget()

Loading…
Cancel
Save