Skip to content

Commit

Permalink
Merge 55f0c9c into 648bb5d
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Dec 14, 2022
2 parents 648bb5d + 55f0c9c commit a35317a
Show file tree
Hide file tree
Showing 28 changed files with 801 additions and 640 deletions.
3 changes: 2 additions & 1 deletion .commitlintrc.yml
Expand Up @@ -46,13 +46,14 @@ rules:
- nodes
- outputters
- packages
- pagebuilders
- pdf
- readme
- release
- settings
- shapers
- tooling
- typesetter
- typesetters
- utilities
help: |
**Possible types**:
Expand Down
2 changes: 1 addition & 1 deletion build-aux/list-dist-files.sh.in
Expand Up @@ -7,7 +7,7 @@ finder () {
}

printf '%s' "SILEDATA ="
finder core classes inputters languages outputters packages shapers typesetters -name '*.lua' -not -name '*_spec.lua' -not -name 'version.lua'
finder core classes inputters languages outputters packages shapers typesetters pagebuilders -name '*.lua' -not -name '*_spec.lua' -not -name 'version.lua'
finder i18n -name '*.ftl'

printf '\n%s' "LUALIBRARIES ="
Expand Down
2 changes: 1 addition & 1 deletion classes/base.lua
Expand Up @@ -58,7 +58,7 @@ function class:_init (options)
self_.pageTemplate.firstContentFrame = self_.pageTemplate.frames[self_.firstContentFrame]
end
local frame = self_:initialFrame()
SILE.typesetter = SILE.defaultTypesetter(frame)
SILE.typesetter = SILE.typesetters.base(frame)
SILE.typesetter:registerPageEndHook(function ()
SU.debug("frames", function ()
for _, v in pairs(SILE.frames) do SILE.outputter:debugFrame(v) end
Expand Down
2 changes: 1 addition & 1 deletion classes/tplain.lua
Expand Up @@ -20,7 +20,7 @@ function class:_t_common ()
self:loadPackage("hanmenkyoshi")
self:registerPostinit(function (class_)
class_:bidiDisableTypesetter(SILE.typesetter)
class_:bidiDisableTypesetter(SILE.defaultTypesetter)
class_:bidiDisableTypesetter(SILE.typesetters.base)
end)
self.defaultFrameset.content.tate = self.options.layout == "tate"
self:declareHanmenFrame("content", self.defaultFrameset.content)
Expand Down
129 changes: 2 additions & 127 deletions core/pagebuilder.lua
@@ -1,128 +1,3 @@
return pl.class({
SU.deprecated("core.pagebuilder", "pagebuilder.base", "0.14.6", "0.15.0")

_init = function (self)
self.awful_bad = 1073741823
self.inf_bad = 10000
self.eject_penalty = -self.inf_bad
self.deplorable = 100000
end,

collateVboxes = function(_, vboxlist)
local output = SILE.nodefactory.vbox()
output:append(vboxlist)
return output
end,

-- Note: Almost 1/3 of the time in a typical SILE in taken iterating through
-- this function. As a result there are some micro-optimizations here that
-- make it a-typical of preferred coding styles. In particular note that
-- we absolutize heavily iterated lengths as early as possible and make
-- make direct calls to their integer amounts, assumed to be in points by
-- the point they are called **without actually checking**!
findBestBreak = function(self, options)
local vboxlist = SU.required(options, "vboxlist", "in findBestBreak")
local target = SU.required(options, "target", "in findBestBreak", "length")
local restart = options.restart or false
local force = options.force or false
local i = 0
local totalHeight = SILE.length()
local bestBreak = nil
local started = false
if restart and restart.target == target then
totalHeight = restart.totalHeight
i = restart.i
started = restart.started
end
local leastC = self.inf_bad
SU.debug("pagebuilder", function ()
return "Page builder for frame " .. SILE.typesetter.frame.id .. " called with " .. #vboxlist .. " nodes, " .. tostring(target)
end)
if SU.debugging("vboxes") then
for j, box in ipairs(vboxlist) do
SU.debug("vboxes", function ()
return (j == i and " >" or " ") .. j .. ": " .. box
end)
end
end
while not started and i < #vboxlist do
i = i + 1
if not vboxlist[i].is_vglue then
started = true
i = i - 1
break
end
end
local pi
while i < #vboxlist do
i = i + 1
local vbox = vboxlist[i]
SU.debug("pagebuilder", "Dealing with VBox", vbox)
if vbox.is_vbox then
totalHeight:___add(vbox.height)
totalHeight:___add(vbox.depth)
elseif vbox.is_vglue then
totalHeight:___add(vbox.height)
elseif vbox.is_insertion then
-- TODO: refactor as hook and without side effects!
target = SILE.insertions.processInsertion(vboxlist, i, totalHeight, target)
vbox = vboxlist[i]
end
local left = target - totalHeight
SU.debug("pagebuilder", "I have", left, "left")
-- if left < -20 then SU.error("\nCatastrophic page breaking failure!"); end
pi = 0
if vbox.is_penalty then
pi = vbox.penalty
-- print("PI "..pi)
end
if vbox.is_penalty and vbox.penalty < self.inf_bad
or (vbox.is_vglue and i > 1 and not vboxlist[i-1].discardable) then
local badness
SU.debug("pagebuilder", "totalHeight", totalHeight, "with target", target)
if totalHeight.length.amount < target.length.amount then -- TeX #1039
-- Account for infinite stretch?
badness = SU.rateBadness(self.inf_bad, left.length.amount, totalHeight.stretch.amount)
-- print("Height == "..totalHeight.length, "target=="..target, "stretch=="..totalHeight.stretch)
elseif left.length.amount < totalHeight.shrink.amount then badness = self.awful_bad
else badness = SU.rateBadness(self.inf_bad, -left.length.amount, totalHeight.shrink.amount)
end

local c
if badness < self.awful_bad then
if pi <= self.eject_penalty then c = pi
elseif badness < self.inf_bad then c = badness + pi -- plus insert
else c = self.deplorable
end
else c = badness end
if c < leastC then
leastC = c
bestBreak = i
else
restart = { totalHeight = totalHeight, i = i, started = started, target = target}
end
-- print("Badness "..badness .." c = "..c)
SU.debug("pagebuilder", "Badness:", c)
if c == self.awful_bad or pi <= self.eject_penalty then
SU.debug("pagebuilder", "outputting")
local onepage = {}
if not bestBreak then bestBreak = i end
for j=1,bestBreak do
onepage[j] = table.remove(vboxlist,1)
end
while(#onepage > 1 and onepage[#onepage].discardable) do onepage[#onepage] = nil end
return onepage, pi
end
end
end
SU.debug("pagebuilder", "No page break here")
if force and bestBreak then
local onepage = {}
for j=1,bestBreak do
onepage[j] = table.remove(vboxlist,1)
end
return onepage, pi
end
return false, restart
end

})
return require("pagebuilders.base")
23 changes: 20 additions & 3 deletions core/sile.lua
Expand Up @@ -74,7 +74,8 @@ SILE.shapers = core_loader("shapers")
SILE.outputters = core_loader("outputters")
SILE.classes = core_loader("classes")
SILE.packages = core_loader("packages")
-- SILE.typesetters = core_loader("typesetters")
SILE.typesetters = core_loader("typesetters")
SILE.pagebuilders = core_loader("pagebuilders")

-- Internal libraries that don't make assumptions on load, only use
SILE.traceStack = require("core.tracestack")()
Expand Down Expand Up @@ -133,6 +134,7 @@ SILE.init = function ()
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.dummy()
end
SILE.pagebuilder = SILE.pagebuilders.base()
runEvals(SILE.input.evaluates, "evaluate")
end

Expand Down Expand Up @@ -165,6 +167,9 @@ SILE.use = function (module, options)
elseif pack.type == "typesetter" then
SILE.typesetters[name] = pack
SILE.typesetter = pack(options)
elseif pack.type == "pagebuilder" then
SILE.pagebuilders[name] = pack
SILE.pagebuilder = pack(options)
elseif pack.type == "package" then
SILE.packages[name] = pack
if class then
Expand Down Expand Up @@ -330,6 +335,20 @@ function SILE.processFile (filename, format, options)
return ret
end

-- TODO: this probably needs deprecating, moved here just to get out of the way so
-- typesetters classing works as expected
SILE.typesetNaturally = function (frame, func)
local saveTypesetter = SILE.typesetter
if SILE.typesetter.frame then SILE.typesetter.frame:leave(SILE.typesetter) end
SILE.typesetter = SILE.typesetters.base(frame)
SILE.settings:temporarily(func)
SILE.typesetter:leaveHmode()
SILE.typesetter:chuck()
SILE.typesetter.frame:leave(SILE.typesetter)
SILE.typesetter = saveTypesetter
if SILE.typesetter.frame then SILE.typesetter.frame:enter(SILE.typesetter) end
end

-- Sort through possible places files could be
function SILE.resolveFile (filename, pathprefix)
local candidates = {}
Expand Down Expand Up @@ -421,8 +440,6 @@ end

-- Internal libraries that run core SILE functions on load
SILE.settings = require("core.settings")()
SILE.pagebuilder = require("core.pagebuilder")()
require("typesetters.base")
require("core.hyphenator-liang")
require("core.languages")
require("core.packagemanager")
Expand Down
2 changes: 1 addition & 1 deletion packages/balanced-frames/init.lua
Expand Up @@ -78,7 +78,7 @@ function package:_init (class)
if not unbalanced_buildPage then
unbalanced_buildPage = SILE.typesetter.buildPage
SILE.typesetter.buildPage = buildPage
SILE.defaultTypesetter.buildPage = buildPage
SILE.typesetters.base.buildPage = buildPage
end
end)
end
Expand Down
2 changes: 1 addition & 1 deletion packages/bidi/init.lua
Expand Up @@ -256,7 +256,7 @@ function package:_init ()
if SILE.typesetter then
self:bidiEnableTypesetter(SILE.typesetter)
end
self:bidiEnableTypesetter(SILE.defaultTypesetter)
self:bidiEnableTypesetter(SILE.typesetters.base)
end

function package:registerCommands ()
Expand Down
29 changes: 1 addition & 28 deletions packages/break-firstfit/init.lua
Expand Up @@ -7,36 +7,9 @@ package._name = "break-firstfit"
-- algorithm, especially when you're dealing with vertical
-- typesetting. Oh, and it's really fast too.

local firstfit = function (typesetter, nl, breakWidth)
local breaks = {}
local length = SILE.length()
for i = 1,#nl do local n = nl[i]
if n.is_box then
SU.debug("break", n, n:lineContribution())
length = length + n:lineContribution()
SU.debug("break", " Length now", length, "breakwidth", breakWidth)
end
if not n.is_box or n.isHangable then
SU.debug("break", n)
if n.is_glue then
length = length + n.width:absolute()
end
SU.debug("break", " Length now", length, "breakwidth", breakWidth)
-- Can we break?
if length:tonumber() >= breakWidth:tonumber() then
SU.debug("break", "Breaking!")
breaks[#breaks+1] = { position = i, width = breakWidth}
length = SILE.length()
end
end
end
breaks[#breaks+1] = { position = #nl, width = breakWidth}
return typesetter:breakpointsToLines(breaks)
end

function package:_init ()
base._init(self)
SILE.typesetter._breakIntoLines_firstfit = firstfit
SILE.typesetters.firstfit:cast(SILE.typesetter)
end

package.documentation = [[
Expand Down

0 comments on commit a35317a

Please sign in to comment.