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

sile-typesetter / sile / 14361761590

09 Apr 2025 03:53PM UTC coverage: 29.317% (-31.1%) from 60.462%
14361761590

push

github

alerque
chore(deps): Bump LuaRocks dependencies to latest versions

5876 of 20043 relevant lines covered (29.32%)

4237.53 hits per line

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

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

4
local pagebuilder = pl.class()
3✔
5
pagebuilder.type = "pagebuilder"
3✔
6
pagebuilder._name = "base"
3✔
7

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

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

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

102
         local c
103
         if badness < self.awful_bad then
193✔
104
            if pi <= self.eject_penalty then
178✔
105
               c = pi
16✔
106
            elseif badness < self.inf_bad then
162✔
107
               c = badness + pi -- plus insert
21✔
108
            else
109
               c = self.deplorable
141✔
110
            end
111
         else
112
            c = badness
15✔
113
         end
114
         if c < leastC then
193✔
115
            leastC = c
36✔
116
            bestBreak = i
36✔
117
         else
118
            restart = { totalHeight = totalHeight, i = i, started = started, target = target }
157✔
119
         end
120

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

149
return pagebuilder
3✔
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