Skip to content

Commit

Permalink
feat: Add a better fallback support for highlighted content
Browse files Browse the repository at this point in the history
If we have liners (SILE 0.15 or resilient with sile-x compatibility
layer) use them. Otherwise, fallback to an even simpler hbox, though
this is not fully satisfying (as it cannot be broken across lines).
  • Loading branch information
Omikhleia authored and Didier Willis committed Feb 13, 2024
1 parent 6bd05c2 commit f4d91d3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
44 changes: 43 additions & 1 deletion packages/markdown/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Please consider using a resilient-compatible class!]])
cascade:call("font", { features = "+smcp" })
end
if hasClass(options, "mark") then
cascade:call("color", { color = "red" }) -- FIXME TODO We'd need real support
cascade:call("markdown:fallback:mark") -- FIXME Better that before... but be more generic and style-aware
end
if hasClass(options, "strike") then
cascade:call("strikethrough")
Expand Down Expand Up @@ -832,6 +832,48 @@ Please consider using a resilient-compatible class!]])
SILE.call("smallskip")
end, "A fallback command for Markdown to insert a captioned figure")

self:registerCommand("markdown:fallback:mark", function (_, content)
local leading = SILE.measurement("1bs"):tonumber()
local bsratio = utils.computeBaselineRatio()
if SILE.typesetter.liner then
SILE.typesetter:liner("markdown:fallback:mark", content,
function (box, typesetter, line)
local outputWidth = SU.rationWidth(box.width, box.width, line.ratio)
local H = SU.max(box.height:tonumber(), (1 - bsratio) * leading)
local D = SU.max(box.depth:tonumber(), bsratio * leading)
local X = typesetter.frame.state.cursorX
SILE.outputter:pushColor(SILE.color("yellow"))
SILE.outputter:drawRule(X, typesetter.frame.state.cursorY - H, outputWidth, H + D)
SILE.outputter:popColor()
box:outputContent(typesetter, line)
end
)
else
SU.debug("markdown", "Feature detection: No liner, using a simpler fallback for mark")
-- Liners are introduced in SILE 0.15.
-- Resilient (with the silex compatibility layer) has them too for SILE 0.14.
-- For now, also support older versions of SILE when used in a non-resilient context.
-- This is not as good, since an hbox can't be broken across lines.
local hbox, hlist = SILE.typesetter:makeHbox(content)
SILE.typesetter:pushHbox({
width = hbox.width,
height = hbox.height,
depth = hbox.depth,
outputYourself = function (box, typesetter, line)
local outputWidth = SU.rationWidth(box.width, box.width, line.ratio)
local H = SU.max(box.height:tonumber(), (1 - bsratio) * leading)
local D = SU.max(box.depth:tonumber(), bsratio * leading)
local X = typesetter.frame.state.cursorX
SILE.outputter:pushColor(SILE.color("yellow"))
SILE.outputter:drawRule(X, typesetter.frame.state.cursorY - H, outputWidth, H + D)
SILE.outputter:popColor()
hbox:outputYourself(typesetter, line)
end
})
SILE.typesetter:pushHlist(hlist)
end
end)

-- C. Customizable hooks

self:registerCommand("markdown:custom-style:hook", function (options, content)
Expand Down
19 changes: 19 additions & 0 deletions packages/markdown/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,30 @@ local function hasEmbedHandler (options)
return nil
end

local metrics = require("fontmetrics")
local bsratiocache = {}

--- Compute the baseline ratio for the current font.
--- This is a ratio of the descender to the theoretical height of the font.
--- @return number Descender ratio
local computeBaselineRatio = function ()
local fontoptions = SILE.font.loadDefaults({})
local bsratio = bsratiocache[SILE.font._key(fontoptions)]
if not bsratio then
local face = SILE.font.cache(fontoptions, SILE.shaper.getFace)
local m = metrics.get_typographic_extents(face)
bsratio = m.descender / (m.ascender + m.descender)
bsratiocache[SILE.font._key(fontoptions)] = bsratio
end
return bsratio
end

--- @export
return {
getFileExtension = getFileExtension,
nbspFilter = nbspFilter,
hasClass = hasClass,
hasRawHandler = hasRawHandler,
hasEmbedHandler = hasEmbedHandler,
computeBaselineRatio = computeBaselineRatio,
}

0 comments on commit f4d91d3

Please sign in to comment.