From 8c897a086f88fa5e7d00ef51d5f65cc942479202 Mon Sep 17 00:00:00 2001 From: poire-z Date: Tue, 9 Jan 2018 21:38:49 +0100 Subject: [PATCH] HTML dict: enable color rendering and per-dict user fix html func (#3585) Also fix possibility of crash in HtmlBoxWidget:free() --- .../apps/reader/modules/readerdictionary.lua | 33 ++++++++++++++++++- frontend/ui/widget/htmlboxwidget.lua | 18 +++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/frontend/apps/reader/modules/readerdictionary.lua b/frontend/apps/reader/modules/readerdictionary.lua index e4c6e07b3..243e97163 100644 --- a/frontend/apps/reader/modules/readerdictionary.lua +++ b/frontend/apps/reader/modules/readerdictionary.lua @@ -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("
", "
") +-- 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
tag for line break diff --git a/frontend/ui/widget/htmlboxwidget.lua b/frontend/ui/widget/htmlboxwidget.lua index 8cb29ca52..62e5f56b4 100644 --- a/frontend/ui/widget/htmlboxwidget.lua +++ b/frontend/ui/widget/htmlboxwidget.lua @@ -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()