Skip to content

Commit

Permalink
refactor: Improve liner API hiding internal implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Jan 30, 2024
1 parent 90254f4 commit 539e5b5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
4 changes: 1 addition & 3 deletions silex/packages/color/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ function package:registerCommands ()
-- (folio, footnotes, etc.)
SILE.typesetter:liner("color", content, function (box, typesetter, line)
SILE.outputter:pushColor(color)
for _, node in ipairs(box.inner) do
node:outputYourself(typesetter, line)
end
box:outputContent(typesetter, line)
SILE.outputter:popColor()
end)
end, "Changes the active ink color to the color <color>.")
Expand Down
6 changes: 2 additions & 4 deletions silex/packages/pdf/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ function package:registerCommands ()

-- Build the content.
-- Cursor will be moved by the actual definitive size.
for _, node in ipairs(box.inner) do
node:outputYourself(typesetter, line)
end

box:outputContent(typesetter, line)
local x1 = typesetter.frame.state.cursorX:tonumber()
local y1 = (SILE.documentState.paperSize[2] - typesetter.frame.state.cursorY + box.height):tonumber()

SILE.outputter:endLink(dest, opts, x0, y0, x1, y1) -- Unstable API
end
)
Expand Down
9 changes: 3 additions & 6 deletions silex/packages/rules/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ function package:registerCommands ()

-- Build the content.
-- Cursor will be moved by the actual definitive size.
for _, node in ipairs(box.inner) do
node:outputYourself(typesetter, line)
end
box:outputContent(typesetter, line)
local newX = typesetter.frame.state.cursorX

-- Output a line.
Expand All @@ -180,10 +178,9 @@ function package:registerCommands ()

-- Build the content.
-- Cursor will be moved by the actual definitive size.
for _, node in ipairs(box.inner) do
node:outputYourself(typesetter, line)
end
box:outputContent(typesetter, line)
local newX = typesetter.frame.state.cursorX

-- Output a line.
-- NOTE: The OpenType spec is not explicit regarding how the size
-- (thickness) affects the position. We opt to distribute evenly
Expand Down
74 changes: 42 additions & 32 deletions silex/typesetters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,30 @@ end
function linerLeaveNode:__tostring ()
return "-L[" .. self.name .. "]"
end
local linerBox = pl.class(SILE.nodefactory.hbox)
function linerBox:_init(name, outputMethod)
SILE.nodefactory.hbox._init(self)
self.width = SILE.length()
self.height = SILE.length()
self.depth = SILE.length()
self.name = name
self.inner = {}
self.outputYourself = outputMethod
end
function linerBox:add(node)
self.inner[#self.inner+1] = node
self.width = self.width + node.width
self.height = SU.max(self.height, node.height)
self.depth = SU.max(self.depth, node.depth)
end
function linerBox:outputContent(typesetter, line)
for _, node in ipairs(self.inner) do
node.outputYourself(node, typesetter, line)
end
end
function linerBox:__tostring ()
return "*L[" .. self.name .. "]"
end

--- Any unclosed liner is reopened on the current line, so we clone and repeat
-- it.
Expand Down Expand Up @@ -1040,43 +1064,32 @@ end
function typesetter:reboxLiners (slice)
local outSlice = {}
local migratingList = {}
local hboxStack = {}
local lboxStack = {}
for i = 1, #slice do
local node = slice[i]
if node.is_enter then
SU.debug("typesetter.liner", "Start reboxing", node)
local n = SILE.nodefactory.hbox({
width = SILE.length(),
height = SILE.length(),
depth = SILE.length(),
name = node.name, -- For mere debug
inner = {},
outputYourself = node.outputMethod
})
hboxStack[#hboxStack+1] = n
local n = linerBox(node.name, node.outputMethod)
lboxStack[#lboxStack+1] = n
elseif node.is_leave then
if #hboxStack == 0 then
if #lboxStack == 0 then
SU.error("Multiliner box stacking mismatch" .. node)
elseif #hboxStack == 1 then
elseif #lboxStack == 1 then
SU.debug("typesetter.liner", "End reboxing", node, "(toplevel)")
outSlice[#outSlice+1] = hboxStack[1]
outSlice[#outSlice+1] = lboxStack[1]
else
SU.debug("typesetter.liner", "End reboxing", node, "(nested)")
local hbox = hboxStack[#hboxStack - 1]
hbox.inner[#hbox.inner+1] = hboxStack[#hboxStack]
local hbox = lboxStack[#lboxStack - 1]
hbox.inner[#hbox.inner+1] = lboxStack[#lboxStack]
end
hboxStack[#hboxStack] = nil
lboxStack[#lboxStack] = nil
pl.tablex.insertvalues(outSlice, migratingList)
migratingList = {}
else
if #hboxStack > 0 then
if #lboxStack > 0 then
if not node.is_migrating then
local hbox = hboxStack[#hboxStack]
-- Add node and recomputes dimensions
hbox.inner[#hbox.inner+1] = node
hbox.width = hbox.width + node.width
hbox.height = SU.max(hbox.height, node.height)
hbox.depth = SU.max(hbox.depth, node.depth)
local lbox = lboxStack[#lboxStack]
lbox:add(node)
else
migratingList[#migratingList+1] = node
end
Expand Down Expand Up @@ -1454,8 +1467,9 @@ end
-- into a box.
-- These boxes will be formatted according to some output logic.
-- The output method has the same signature as the outputYourself method
-- of a box, and is responsible for outputting the liner content, which
-- is in the "inner" field of this wrapping box.
-- of a box, and is responsible for outputting the liner inner content with the
-- outputContent(typesetter, line) method, possibly surrounded by some additional
-- effects.
-- If we are already in horizontal-restricted mode, the liner is processed
-- immediately, since line breaking won't occur then.
---@param name string Name of the liner (usefull for debugging)
Expand All @@ -1465,13 +1479,9 @@ function typesetter:liner (name, content, outputYourself)
if self.state.hmodeOnly then
SU.debug("typesetter.liner", "Applying liner in horizontal-restricted mode")
local hbox, hlist = self:makeHbox(content)
self:pushHbox({
width = hbox.width,
height = hbox.height,
depth = hbox.depth,
inner = { hbox },
outputYourself = outputYourself,
})
local lbox = linerBox(name, outputYourself)
lbox:add(hbox)
self:pushHorizontal(lbox)
self:pushHlist(hlist)
else
self.state.linerCount = (self.state.linerCount or 0) + 1
Expand Down

0 comments on commit 539e5b5

Please sign in to comment.