• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 15507594683

07 Jun 2025 11:54AM UTC coverage: 30.951% (-30.4%) from 61.309%
15507594683

push

github

alerque
chore(tooling): Add post-checkout hook to clear makedeps on branch switch

6363 of 20558 relevant lines covered (30.95%)

3445.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.0
/pagebuilders/base.lua
1
--- SILE pagebuilder class.
2
-- @interfaces pagebuilders
3

4
local module = require("types.module")
11✔
5
local pagebuilder = pl.class(module)
11✔
6
pagebuilder.type = "pagebuilder"
11✔
7

8
function pagebuilder:_init (typesetter)
11✔
9
   self.typesetter = typesetter
27✔
10
   module._init(self)
27✔
11
   self.awful_bad = 1073741823
27✔
12
   self.inf_bad = 10000
27✔
13
   self.eject_penalty = -self.inf_bad
27✔
14
   self.deplorable = 100000
27✔
15
end
16

17
function pagebuilder:collateVboxes (vboxlist)
11✔
18
   local output = SILE.types.node.vbox()
2✔
19
   output:append(vboxlist)
2✔
20
   return output
2✔
21
end
22

23
-- Note: Almost 1/3 of the time in a typical SILE in taken iterating through
24
-- this function. As a result there are some micro-optimizations here that
25
-- make it a-typical of preferred coding styles. In particular note that
26
-- we absolutize heavily iterated lengths as early as possible and make
27
-- make direct calls to their integer amounts, assumed to be in points by
28
-- the point they are called **without actually checking**!
29
function pagebuilder:findBestBreak (options)
11✔
30
   local vboxlist = SU.required(options, "vboxlist", "in findBestBreak")
203✔
31
   local target = SU.required(options, "target", "in findBestBreak", "length")
203✔
32
   local restart = options.restart or false
203✔
33
   local force = options.force or false
203✔
34
   local i = 0
203✔
35
   local totalHeight = SILE.types.length()
203✔
36
   local bestBreak = nil
203✔
37
   local started = false
203✔
38
   if restart and restart.target == target then
203✔
39
      totalHeight = restart.totalHeight
×
40
      i = restart.i
×
41
      started = restart.started
×
42
   end
43
   local leastC = self.inf_bad
203✔
44
   SU.debug("pagebuilder", function ()
406✔
45
      return "Page builder for frame "
×
46
         .. self.typesetter.frame.id
×
47
         .. " called with "
×
48
         .. #vboxlist
×
49
         .. " nodes, "
×
50
         .. tostring(target)
×
51
   end)
52
   if SU.debugging("vboxes") then
406✔
53
      for j, box in ipairs(vboxlist) do
×
54
         SU.debug("vboxes", function ()
×
55
            return (j == i and " >" or "  ") .. j .. ": " .. box
×
56
         end)
57
      end
58
   end
59
   while not started and i < #vboxlist do
522✔
60
      i = i + 1
517✔
61
      if not vboxlist[i].is_vglue then
517✔
62
         started = true
198✔
63
         i = i - 1
198✔
64
         break
198✔
65
      end
66
   end
67
   local pi
68
   while i < #vboxlist do
2,533✔
69
      i = i + 1
2,370✔
70
      local vbox = vboxlist[i]
2,370✔
71
      SU.debug("pagebuilder", "Dealing with VBox", vbox)
2,370✔
72
      if vbox.is_vbox then
2,370✔
73
         totalHeight:___add(vbox.height)
891✔
74
         totalHeight:___add(vbox.depth)
1,782✔
75
      elseif vbox.is_vglue then
1,479✔
76
         totalHeight:___add(vbox.height)
2,678✔
77
      elseif vbox.is_insertion then
140✔
78
         -- TODO: refactor as hook and without side effects!
79
         target = SILE.insertions.processInsertion(vboxlist, i, totalHeight, target)
10✔
80
         vbox = vboxlist[i]
5✔
81
      end
82
      local left = target - totalHeight
2,370✔
83
      SU.debug("pagebuilder", "I have", left, "left")
2,370✔
84
      -- if left < -20 then SU.error("\nCatastrophic page breaking failure!"); end
85
      pi = 0
2,370✔
86
      if vbox.is_penalty then
2,370✔
87
         pi = vbox.penalty
136✔
88
      end
89
      if
90
         vbox.is_penalty and vbox.penalty < self.inf_bad
2,370✔
91
         or (vbox.is_vglue and i > 1 and not vboxlist[i - 1].discardable)
2,283✔
92
      then
93
         local badness
94
         SU.debug("pagebuilder", "totalHeight", totalHeight, "with target", target)
799✔
95
         if totalHeight.length.amount < target.length.amount then -- TeX #1039
799✔
96
            -- Account for infinite stretch?
97
            badness = SU.rateBadness(self.inf_bad, left.length.amount, totalHeight.stretch.amount)
1,566✔
98
         elseif left.length.amount < totalHeight.shrink.amount then
16✔
99
            badness = self.awful_bad
16✔
100
         else
101
            badness = SU.rateBadness(self.inf_bad, -left.length.amount, totalHeight.shrink.amount)
×
102
         end
103

104
         local c
105
         if badness < self.awful_bad then
799✔
106
            if pi <= self.eject_penalty then
783✔
107
               c = pi
24✔
108
            elseif badness < self.inf_bad then
759✔
109
               c = badness + pi -- plus insert
34✔
110
            else
111
               c = self.deplorable
725✔
112
            end
113
         else
114
            c = badness
16✔
115
         end
116
         if c < leastC then
799✔
117
            leastC = c
57✔
118
            bestBreak = i
57✔
119
         else
120
            restart = { totalHeight = totalHeight, i = i, started = started, target = target }
742✔
121
         end
122

123
         SU.debug("pagebuilder", "Badness:", c)
799✔
124
         if c == self.awful_bad or pi <= self.eject_penalty then
799✔
125
            SU.debug("pagebuilder", "outputting")
40✔
126
            local onepage = {}
40✔
127
            if not bestBreak then
40✔
128
               bestBreak = i
15✔
129
            end
130
            for j = 1, bestBreak do
398✔
131
               onepage[j] = table.remove(vboxlist, 1)
716✔
132
            end
133
            while #onepage > 1 and onepage[#onepage].discardable do
81✔
134
               onepage[#onepage] = nil
41✔
135
            end
136
            return onepage, pi
40✔
137
         end
138
      end
139
   end
140
   SU.debug("pagebuilder", "No page break here")
163✔
141
   if force and bestBreak then
163✔
142
      local onepage = {}
×
143
      for j = 1, bestBreak do
×
144
         onepage[j] = table.remove(vboxlist, 1)
×
145
      end
146
      return onepage, pi
×
147
   end
148
   return false, restart
163✔
149
end
150

151
return pagebuilder
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc