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

sile-typesetter / sile / 9304049654

30 May 2024 02:12PM UTC coverage: 60.021% (-14.7%) from 74.707%
9304049654

push

github

web-flow
Merge 1a26b4f22 into a1fd105f8

6743 of 12900 new or added lines in 186 files covered. (52.27%)

347 existing lines in 49 files now uncovered.

10311 of 17179 relevant lines covered (60.02%)

3307.34 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()
19✔
5
pagebuilder.type = "pagebuilder"
19✔
6
pagebuilder._name = "base"
19✔
7

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

15
function pagebuilder.collateVboxes (_, vboxlist)
19✔
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)
19✔
28
   local vboxlist = SU.required(options, "vboxlist", "in findBestBreak")
255✔
29
   local target = SU.required(options, "target", "in findBestBreak", "length")
255✔
30
   local restart = options.restart or false
255✔
31
   local force = options.force or false
255✔
32
   local i = 0
255✔
33
   local totalHeight = SILE.types.length()
255✔
34
   local bestBreak = nil
255✔
35
   local started = false
255✔
36
   if restart and restart.target == target then
255✔
NEW
37
      totalHeight = restart.totalHeight
×
NEW
38
      i = restart.i
×
NEW
39
      started = restart.started
×
40
   end
41
   local leastC = self.inf_bad
255✔
42
   SU.debug("pagebuilder", function ()
510✔
NEW
43
      return "Page builder for frame "
×
NEW
44
         .. SILE.typesetter.frame.id
×
NEW
45
         .. " called with "
×
NEW
46
         .. #vboxlist
×
NEW
47
         .. " nodes, "
×
NEW
48
         .. tostring(target)
×
49
   end)
50
   if SU.debugging("vboxes") then
510✔
NEW
51
      for j, box in ipairs(vboxlist) do
×
NEW
52
         SU.debug("vboxes", function ()
×
NEW
53
            return (j == i and " >" or "  ") .. j .. ": " .. box
×
54
         end)
55
      end
56
   end
57
   while not started and i < #vboxlist do
613✔
58
      i = i + 1
613✔
59
      if not vboxlist[i].is_vglue then
613✔
60
         started = true
255✔
61
         i = i - 1
255✔
62
         break
255✔
63
      end
64
   end
65
   local pi
66
   while i < #vboxlist do
3,249✔
67
      i = i + 1
3,042✔
68
      local vbox = vboxlist[i]
3,042✔
69
      SU.debug("pagebuilder", "Dealing with VBox", vbox)
3,042✔
70
      if vbox.is_vbox then
3,042✔
71
         totalHeight:___add(vbox.height)
973✔
72
         totalHeight:___add(vbox.depth)
1,946✔
73
      elseif vbox.is_vglue then
2,069✔
74
         totalHeight:___add(vbox.height)
3,864✔
75
      elseif vbox.is_insertion then
137✔
76
         -- TODO: refactor as hook and without side effects!
77
         target = SILE.insertions.processInsertion(vboxlist, i, totalHeight, target)
6✔
78
         vbox = vboxlist[i]
3✔
79
      end
80
      local left = target - totalHeight
3,042✔
81
      SU.debug("pagebuilder", "I have", left, "left")
3,042✔
82
      -- if left < -20 then SU.error("\nCatastrophic page breaking failure!"); end
83
      pi = 0
3,042✔
84
      if vbox.is_penalty then
3,042✔
85
         pi = vbox.penalty
135✔
86
      end
87
      if
88
         vbox.is_penalty and vbox.penalty < self.inf_bad
3,042✔
89
         or (vbox.is_vglue and i > 1 and not vboxlist[i - 1].discardable)
2,941✔
90
      then
91
         local badness
92
         SU.debug("pagebuilder", "totalHeight", totalHeight, "with target", target)
1,207✔
93
         if totalHeight.length.amount < target.length.amount then -- TeX #1039
1,207✔
94
            -- Account for infinite stretch?
95
            badness = SU.rateBadness(self.inf_bad, left.length.amount, totalHeight.stretch.amount)
2,382✔
96
         elseif left.length.amount < totalHeight.shrink.amount then
16✔
97
            badness = self.awful_bad
16✔
98
         else
NEW
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
1,207✔
104
            if pi <= self.eject_penalty then
1,191✔
105
               c = pi
32✔
106
            elseif badness < self.inf_bad then
1,159✔
107
               c = badness + pi -- plus insert
62✔
108
            else
109
               c = self.deplorable
1,097✔
110
            end
111
         else
112
            c = badness
16✔
113
         end
114
         if c < leastC then
1,207✔
115
            leastC = c
82✔
116
            bestBreak = i
82✔
117
         else
118
            restart = { totalHeight = totalHeight, i = i, started = started, target = target }
1,125✔
119
         end
120

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

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