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

sile-typesetter / sile / 14908152066

08 May 2025 01:49PM UTC coverage: 61.309% (-5.7%) from 67.057%
14908152066

push

github

alerque
chore(registries): Touchup command registry deprecation shims

1 of 2 new or added lines in 2 files covered. (50.0%)

1379 existing lines in 46 files now uncovered.

13556 of 22111 relevant lines covered (61.31%)

11834.23 hits per line

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

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

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

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

17
function pagebuilder:collateVboxes (vboxlist)
42✔
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)
42✔
30
   local vboxlist = SU.required(options, "vboxlist", "in findBestBreak")
776✔
31
   local target = SU.required(options, "target", "in findBestBreak", "length")
776✔
32
   local restart = options.restart or false
776✔
33
   local force = options.force or false
776✔
34
   local i = 0
776✔
35
   local totalHeight = SILE.types.length()
776✔
36
   local bestBreak = nil
776✔
37
   local started = false
776✔
38
   if restart and restart.target == target then
776✔
39
      totalHeight = restart.totalHeight
×
40
      i = restart.i
×
41
      started = restart.started
×
42
   end
43
   local leastC = self.inf_bad
776✔
44
   SU.debug("pagebuilder", function ()
1,552✔
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
1,552✔
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
1,873✔
60
      i = i + 1
1,845✔
61
      if not vboxlist[i].is_vglue then
1,845✔
62
         started = true
748✔
63
         i = i - 1
748✔
64
         break
748✔
65
      end
66
   end
67
   local pi
68
   while i < #vboxlist do
14,752✔
69
      i = i + 1
14,053✔
70
      local vbox = vboxlist[i]
14,053✔
71
      SU.debug("pagebuilder", "Dealing with VBox", vbox)
14,053✔
72
      if vbox.is_vbox then
14,053✔
73
         totalHeight:___add(vbox.height)
3,733✔
74
         totalHeight:___add(vbox.depth)
7,466✔
75
      elseif vbox.is_vglue then
10,320✔
76
         totalHeight:___add(vbox.height)
13,762✔
77
      elseif vbox.is_insertion then
3,439✔
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
14,053✔
83
      SU.debug("pagebuilder", "I have", left, "left")
14,053✔
84
      -- if left < -20 then SU.error("\nCatastrophic page breaking failure!"); end
85
      pi = 0
14,053✔
86
      if vbox.is_penalty then
14,053✔
87
         pi = vbox.penalty
3,435✔
88
      end
89
      if
90
         vbox.is_penalty and vbox.penalty < self.inf_bad
14,053✔
91
         or (vbox.is_vglue and i > 1 and not vboxlist[i - 1].discardable)
12,237✔
92
      then
93
         local badness
94
         SU.debug("pagebuilder", "totalHeight", totalHeight, "with target", target)
4,232✔
95
         if totalHeight.length.amount < target.length.amount then -- TeX #1039
4,232✔
96
            -- Account for infinite stretch?
97
            badness = SU.rateBadness(self.inf_bad, left.length.amount, totalHeight.stretch.amount)
8,426✔
98
         elseif left.length.amount < totalHeight.shrink.amount then
19✔
99
            badness = self.awful_bad
18✔
100
         else
101
            badness = SU.rateBadness(self.inf_bad, -left.length.amount, totalHeight.shrink.amount)
2✔
102
         end
103

104
         local c
105
         if badness < self.awful_bad then
4,232✔
106
            if pi <= self.eject_penalty then
4,214✔
107
               c = pi
59✔
108
            elseif badness < self.inf_bad then
4,155✔
109
               c = badness + pi -- plus insert
104✔
110
            else
111
               c = self.deplorable
4,051✔
112
            end
113
         else
114
            c = badness
18✔
115
         end
116
         if c < leastC then
4,232✔
117
            leastC = c
162✔
118
            bestBreak = i
162✔
119
         else
120
            restart = { totalHeight = totalHeight, i = i, started = started, target = target }
4,070✔
121
         end
122

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

151
return pagebuilder
42✔
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