Skip to content

Commit

Permalink
refactor: Compatibility upgrade with SILE 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Aug 1, 2024
1 parent c8e11fa commit 1dbdd7e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/framebox/graphics/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end

--- Builds a PDF graphics color (stroke or fill) from a SILE parsed color.
--
-- @tparam table|nil color SILE color object
-- @tparam table|nil color SILE.types.color object
-- @tparam boolean stroke Stroke or fill
-- @treturn string PDF graphics color
local function makeColorHelper (color, stroke)
Expand Down
36 changes: 19 additions & 17 deletions packages/framebox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
--
-- KNOWN ISSUE: RTL and BTT writing directions are not officialy supported yet (untested)
--
require("silex.types") -- Compatibility shims

local base = require("packages.base")

local package = pl.class(base)
Expand Down Expand Up @@ -85,28 +87,28 @@ function package.declareSettings (_)
SILE.settings:declare({
parameter = "framebox.padding",
type = "measurement",
default = SILE.measurement("2pt"),
default = SILE.types.measurement("2pt"),
help = "Padding applied to a framed box."
})

SILE.settings:declare({
parameter = "framebox.borderwidth",
type = "measurement",
default = SILE.measurement("0.4pt"),
default = SILE.types.measurement("0.4pt"),
help = "Border width applied to a frame box."
})

SILE.settings:declare({
parameter = "framebox.cornersize",
type = "measurement",
default = SILE.measurement("5pt"),
default = SILE.types.measurement("5pt"),
help = "Corner size (arc radius) for rounded boxes."
})

SILE.settings:declare({
parameter = "framebox.shadowsize",
type = "measurement",
default = SILE.measurement("3pt"),
default = SILE.types.measurement("3pt"),
help = "Shadow width applied to a framed box when dropped shadow is enabled."
})
end
Expand All @@ -119,12 +121,12 @@ function package:registerCommands ()
local borderwidth = SU.cast("measurement", options.borderwidth or SILE.settings:get("framebox.borderwidth")):tonumber()
local bordercolor
if borderwidth ~= 0 then
bordercolor = options.bordercolor and SILE.color(options.bordercolor)
bordercolor = options.bordercolor and SILE.types.color(options.bordercolor)
end
local fillcolor = options.fillcolor and SILE.color(options.fillcolor) or 'none'
local fillcolor = options.fillcolor and SILE.types.color(options.fillcolor) or 'none'
local shadow = SU.boolean(options.shadow, false)
local shadowsize = shadow and SU.cast("measurement", options.shadowsize or SILE.settings:get("framebox.shadowsize")):tonumber() or 0
local shadowcolor = shadow and options.shadowcolor and SILE.color(options.shadowcolor)
local shadowcolor = shadow and options.shadowcolor and SILE.types.color(options.shadowcolor)

local hbox, hlist = SILE.typesetter:makeHbox(content)
hbox = adjustPaddingHbox(hbox, padding, padding + shadowsize, padding, padding + shadowsize)
Expand All @@ -150,12 +152,12 @@ function package:registerCommands ()
local borderwidth = SU.cast("measurement", options.borderwidth or SILE.settings:get("framebox.borderwidth")):tonumber()
local bordercolor
if borderwidth ~= 0 then
bordercolor = options.bordercolor and SILE.color(options.bordercolor)
bordercolor = options.bordercolor and SILE.types.color(options.bordercolor)
end
local fillcolor = options.fillcolor and SILE.color(options.fillcolor) or 'none'
local fillcolor = options.fillcolor and SILE.types.color(options.fillcolor) or 'none'
local shadow = SU.boolean(options.shadow, false)
local shadowsize = shadow and SU.cast("measurement", options.shadowsize or SILE.settings:get("framebox.shadowsize")):tonumber() or 0
local shadowcolor = shadow and options.shadowcolor and SILE.color(options.shadowcolor)
local shadowcolor = shadow and options.shadowcolor and SILE.types.color(options.shadowcolor)

local cornersize = SU.cast("measurement", options.cornersize or SILE.settings:get("framebox.cornersize")):tonumber()

Expand Down Expand Up @@ -192,8 +194,8 @@ function package:registerCommands ()
-- (for hachures etc.)
borderwidth = SILE.settings:get("framebox.borderwidth"):tonumber()
end
local bordercolor = options.bordercolor and SILE.color(options.bordercolor)
local fillcolor = options.fillcolor and SILE.color(options.fillcolor)
local bordercolor = options.bordercolor and SILE.types.color(options.bordercolor)
local fillcolor = options.fillcolor and SILE.types.color(options.fillcolor)
local enlarge = SU.boolean(options.enlarge, false)

local hbox, hlist = SILE.typesetter:makeHbox(content)
Expand Down Expand Up @@ -227,11 +229,11 @@ function package:registerCommands ()
end, "Frames content in a rough (sketchy) box.")

self:registerCommand("bracebox", function(options, content)
local padding = SU.cast("measurement", options.padding or SILE.measurement("0.25em")):tonumber()
local strokewidth = SU.cast("measurement", options.strokewidth or SILE.measurement("0.033em")):tonumber()
local bracecolor = options.bracecolor and SILE.color(options.bracecolor)
local bracewidth = SU.cast("measurement", options.bracewidth or SILE.measurement("0.25em")):tonumber()
local bracethickness = SU.cast("measurement", options.bracethickness or SILE.measurement("0.05em")):tonumber()
local padding = SU.cast("measurement", options.padding or SILE.types.measurement("0.25em")):tonumber()
local strokewidth = SU.cast("measurement", options.strokewidth or SILE.types.measurement("0.033em")):tonumber()
local bracecolor = options.bracecolor and SILE.types.color(options.bracecolor)
local bracewidth = SU.cast("measurement", options.bracewidth or SILE.types.measurement("0.25em")):tonumber()
local bracethickness = SU.cast("measurement", options.bracethickness or SILE.types.measurement("0.05em")):tonumber()
local curvyness = SU.cast("number", options.curvyness or 0.6)
local left, right
if options.side == "left" or not options.side then left = true
Expand Down
44 changes: 23 additions & 21 deletions packages/parbox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
--
-- Known limitations: LTR-TTB writing direction is assumed.
--
require("silex.types") -- Compatibility shims

local base = require("packages.base")

local package = pl.class(base)
Expand All @@ -25,9 +27,9 @@ local function parboxTempFrame (options)
id = id
})
nb_ = nb_+1
newFrame:constrain("top", SILE.length())
newFrame:constrain("bottom", SILE.length())
newFrame:constrain("left", SILE.length())
newFrame:constrain("top", SILE.types.length())
newFrame:constrain("bottom", SILE.types.length())
newFrame:constrain("left", SILE.types.length())
newFrame:constrain("right", options.width)
return newFrame
end
Expand Down Expand Up @@ -105,7 +107,7 @@ local function getBaselineExtents (vboxlist)
-- depth of the last line, so we can use these to vertically align the parbox
-- with the surrounding content.
-- The way we do it likely assumes top-to-bottom writing...
local baseHeight, baseDepth = SILE.length(), SILE.length()
local baseHeight, baseDepth = SILE.types.length(), SILE.types.length()
for i = 1, #vboxlist do
if vboxlist[i].is_vbox and vboxlist[i].height then
baseHeight = vboxlist[i].height
Expand Down Expand Up @@ -174,10 +176,10 @@ function package.makeParbox(_, options, content)
local border = options.border and parseBorderOrPadding(options.border, "border") or { 0, 0, 0, 0 }
local valign = options.valign or "top"
local padding = options.padding and parseBorderOrPadding(options.padding, "padding") or { 0, 0, 0, 0 }
local bordercolor = options.bordercolor and SILE.color(options.bordercolor)
local bordercolor = options.bordercolor and SILE.types.color(options.bordercolor)
local minimize = SU.boolean(options.minimize, false)

width = SILE.length(SU.cast("measurement", width)):absolute()
width = SILE.types.length(SU.cast("measurement", width)):absolute()

local vboxes, hlist = parboxFraming({ width = width }, content)

Expand All @@ -187,13 +189,13 @@ function package.makeParbox(_, options, content)
elseif strut == "character" then
strutDimen = SILE.call("strut", { method = "character" })
else
strutDimen = { height = SILE.length(0), depth = SILE.length(0) }
strutDimen = { height = SILE.types.length(0), depth = SILE.types.length(0) }
end

local baseHeight, baseDepth = getBaselineExtents(vboxes)

local wmax = SILE.length()
local totalHeight = SILE.length()
local wmax = SILE.types.length()
local totalHeight = SILE.types.length()
local vboxWidths = {}
for i = 1, #vboxes do
-- Try to cancel vertical stretching/shrinking
Expand All @@ -202,13 +204,13 @@ function package.makeParbox(_, options, content)
-- by the page builder. We cannot tweak directly their height or depth as we
-- sometimes do with other boxes, as it would have a side effect. So we have
-- to re-create a new vglue with the appropriate fixed dimension.
vboxes[i] = SILE.nodefactory.vglue(SILE.length(vboxes[i].height.length))
vboxes[i] = SILE.types.node.vglue(SILE.types.length(vboxes[i].height.length))
end
totalHeight = totalHeight + vboxes[i].height:absolute() + vboxes[i].depth:absolute()

if minimize then
-- We go through all lines to retrieve their natural line.
local w = SILE.length()
local w = SILE.types.length()
if vboxes[i].nodes then
w = naturalWidth(width, vboxes[i].nodes)
if w > wmax then wmax = w end
Expand All @@ -220,7 +222,7 @@ function package.makeParbox(_, options, content)
if minimize then
-- The max line width can actually be bigger than our target width,
-- (i.e. notwithstanding its shrinkeability).
local wmaxlen = SILE.length(wmax.length) -- Lua5.1 doesn't like comparing appels and oranges
local wmaxlen = SILE.types.length(wmax.length) -- Lua5.1 doesn't like comparing appels and oranges
width = SU.min(wmaxlen, width)
-- We recompute all line ratios based on the new target width.
for i = 1, #vboxes do
Expand All @@ -233,30 +235,30 @@ function package.makeParbox(_, options, content)

local adjustDepth = SU.max(baseDepth, strutDimen.depth) - baseDepth
local adjustHeight = SU.max(baseHeight, strutDimen.height) - baseHeight
local z0 = SILE.length(0)
local z0 = SILE.types.length(0)
local depth, height
if valign == "bottom" then
depth = z0 + SILE.length(padding[2]) + SU.max(baseDepth, strutDimen.depth)
height = totalHeight + SILE.length(padding[1]) - baseDepth + adjustHeight
depth = z0 + SILE.types.length(padding[2]) + SU.max(baseDepth, strutDimen.depth)
height = totalHeight + SILE.types.length(padding[1]) - baseDepth + adjustHeight
elseif valign == "middle" then
local padwidth = SILE.length(padding[2] + padding[1])
local padwidth = SILE.types.length(padding[2] + padding[1])
local half = (totalHeight + adjustHeight + adjustDepth + padwidth) / 2
depth = half
height = half
else -- valign == top
depth = totalHeight + SILE.length(padding[2]) - baseHeight + adjustDepth
height = z0 + SILE.length(padding[1]) + SU.max(baseHeight, strutDimen.height)
depth = totalHeight + SILE.types.length(padding[2]) - baseHeight + adjustDepth
height = z0 + SILE.types.length(padding[1]) + SU.max(baseHeight, strutDimen.height)
end

local parbox = SILE.nodefactory.hbox({
width = width + SILE.length(padding[3] + padding[4]),
local parbox = SILE.types.node.hbox({
width = width + SILE.types.length(padding[3] + padding[4]),
depth = depth,
height = height,
inner = vboxes,
valign = valign,
padding = padding,
yAdjust = adjustHeight, -- TTB is assumed
offset = SILE.measurement(), -- INTERNAL: See comment below.
offset = SILE.types.measurement(), -- INTERNAL: See comment below.
border = border,
bordercolor = bordercolor,
outputYourself= function (node, typesetter, _)
Expand Down
22 changes: 12 additions & 10 deletions packages/ptable/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
-- 2021-2023 Didier Willis
-- License: MIT
--
require("silex.types") -- Compatibility shims

local base = require("packages.base")

local makeParbox -- assigned at package initialization
Expand Down Expand Up @@ -75,12 +77,12 @@ end
-- on top of that... In a heavy-handed way (this function
-- might even be called where uneeded, strictly speaking).
local function vglueNoStretch (vg)
return SILE.nodefactory.vglue(SILE.length(vg.height.length))
return SILE.types.node.vglue(SILE.types.length(vg.height.length))
end
local function temporarilyClearFragileSettings (callback)
SILE.settings:pushState()
-- Kill that small lineskip thing that may move rows a bit.
SILE.settings:set("document.lineskip", SILE.length())
SILE.settings:set("document.lineskip", SILE.types.length())
-- Kill stretchability at baseline and paragraph level.
SILE.settings:set("document.baselineskip", vglueNoStretch(SILE.settings:get("document.baselineskip")))
SILE.settings:set("document.parskip", vglueNoStretch(SILE.settings:get("document.parskip")))
Expand Down Expand Up @@ -195,7 +197,7 @@ local cellTableNode = pl.class({
temporarilyClearFragileSettings(function()
for i = 1, #self.rows do
-- Set up queue but avoid a newPar? Apparently not needed here.
-- SILE.typesetter.state.nodes[#SILE.typesetter.state.nodes+1] = SILE.nodefactory.zerohbox()
-- SILE.typesetter.state.nodes[#SILE.typesetter.state.nodes+1] = SILE.types.node.zerohbox()
self.rows[i]:shipout()
end
end)
Expand All @@ -212,9 +214,9 @@ local rowNode = pl.class({
self.color = color
end,
height = function (self)
local h = SILE.length()
local h = SILE.types.length()
for i = 1, #self.cells do
h = SU.max(self.cells[i]:height(), SILE.length(h))
h = SU.max(self.cells[i]:height(), SILE.types.length(h))
end
return h
end,
Expand All @@ -229,7 +231,7 @@ local rowNode = pl.class({
-- Important hack or a parindent occurs sometimes: Set up queue but avoid a newPar.
-- We had do to the same weird magic in the parbox package too at one step, see the
-- comment there.
SILE.typesetter.state.nodes[#SILE.typesetter.state.nodes+1] = SILE.nodefactory.zerohbox()
SILE.typesetter.state.nodes[#SILE.typesetter.state.nodes+1] = SILE.types.node.zerohbox()
local hbox, hlist = SILE.typesetter:makeHbox(function ()
for i = 1, #self.cells do
self.cells[i]:shipout()
Expand All @@ -247,7 +249,7 @@ local processTable = {}

processTable["cell"] = function (content, args, tablespecs)
local span = SU.cast("integer", content.options.span or 1)
local color = content.options.background and SILE.color(content.options.background)
local color = content.options.background and SILE.types.color(content.options.background)
local pad = parsePadding(content.options.padding or tablespecs.cellpadding)
local width = computeCellWidth(args.col, span, tablespecs.cols)

Expand Down Expand Up @@ -284,9 +286,9 @@ processTable["celltable"] = function (content, args, tablespecs)
end

processTable["row"] = function (content, args, tablespecs)
local color = content.options.background and SILE.color(content.options.background)
local color = content.options.background and SILE.types.color(content.options.background)

SILE.settings:set("document.lineskip", SILE.length())
SILE.settings:set("document.lineskip", SILE.types.length())
local iCell = args.col and args.col or 1
local cells = {}
for i = 1, #content do
Expand Down Expand Up @@ -380,7 +382,7 @@ function package:registerCommands ()

local headerVbox
temporarilyClearFragileSettings(function()
SILE.settings:set("document.parindent", SILE.length())
SILE.settings:set("document.parindent", SILE.types.length())
local iRow = 1
for i = 1, #content do
if type(content[i]) == "table" then
Expand Down
14 changes: 8 additions & 6 deletions packages/struts/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
-- 2021-2023 Didier Willis
-- License: MIT
--
require("silex.types") -- Compatibility shims

local base = require("packages.base")

local package = pl.class(base)
Expand All @@ -24,14 +26,14 @@ function package.declareSettings (_)
SILE.settings:declare({
parameter = "strut.ruledepth",
type = "measurement",
default = SILE.measurement("0.3bs"),
default = SILE.types.measurement("0.3bs"),
help = "Strut rule depth"
})

SILE.settings:declare({
parameter = "strut.ruleheight",
type = "measurement",
default = SILE.measurement("1bs"),
default = SILE.types.measurement("1bs"),
help = "Strut rule height"
})
end
Expand Down Expand Up @@ -68,17 +70,17 @@ function package:registerCommands ()
local strut
if method == "rule" then
strut = {
height = SILE.length(SILE.settings:get("strut.ruleheight")):absolute(),
depth = SILE.length(SILE.settings:get("strut.ruledepth")):absolute(),
height = SILE.types.length(SILE.settings:get("strut.ruleheight")):absolute(),
depth = SILE.types.length(SILE.settings:get("strut.ruledepth")):absolute(),
}
if show then
-- The content there could be anything, we just want to be sure we get a box
SILE.call("rebox", { phantom = true, height = strut.height, depth = strut.depth, width = SILE.length() }, {})
SILE.call("rebox", { phantom = true, height = strut.height, depth = strut.depth, width = SILE.types.length() }, {})
end
elseif method == "character" then
strut = characterStrut()
if show then
SILE.call("rebox", { phantom = true, width = SILE.length() }, { SILE.settings:get("strut.character") })
SILE.call("rebox", { phantom = true, width = SILE.types.length() }, { SILE.settings:get("strut.character") })
end
else
SU.error("Unknown strut method '" .. method .. "'")
Expand Down
Loading

0 comments on commit 1dbdd7e

Please sign in to comment.