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

sile-typesetter / sile / 5410635959

pending completion
5410635959

push

github

web-flow
ci(actions): Use new Nix cache action for faster builds (#1823)

11722 of 15764 relevant lines covered (74.36%)

6982.0 hits per line

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

90.91
/pagebuilders/base.lua
1
local pagebuilder = pl.class()
172✔
2
pagebuilder.type = "pagebuilder"
172✔
3
pagebuilder._name = "base"
172✔
4

5
function pagebuilder:_init ()
172✔
6
  self.awful_bad = 1073741823
172✔
7
  self.inf_bad = 10000
172✔
8
  self.eject_penalty = -self.inf_bad
172✔
9
  self.deplorable = 100000
172✔
10
end
11

12
function pagebuilder.collateVboxes (_, vboxlist)
172✔
13
  local output = SILE.nodefactory.vbox()
48✔
14
  output:append(vboxlist)
48✔
15
  return output
48✔
16
end
17

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

90
      local c
91
      if badness < self.awful_bad then
8,912✔
92
        if pi <= self.eject_penalty then c = pi
8,869✔
93
        elseif badness < self.inf_bad then c = badness + pi -- plus insert
8,661✔
94
        else c = self.deplorable
7,861✔
95
        end
96
      else c = badness end
43✔
97
      if c < leastC then
8,912✔
98
        leastC = c
858✔
99
        bestBreak = i
858✔
100
      else
101
        restart = { totalHeight = totalHeight, i = i, started = started, target = target}
8,054✔
102
      end
103

104
      SU.debug("pagebuilder", "Badness:", c)
8,912✔
105
      if c == self.awful_bad or pi <= self.eject_penalty then
8,912✔
106
        SU.debug("pagebuilder", "outputting")
251✔
107
        local onepage = {}
251✔
108
        if not bestBreak then bestBreak = i end
251✔
109
        for j=1,bestBreak do
4,544✔
110
          onepage[j] = table.remove(vboxlist,1)
8,586✔
111
        end
112
        while(#onepage > 1 and onepage[#onepage].discardable) do onepage[#onepage] = nil end
541✔
113
        return onepage, pi
251✔
114
      end
115
    end
116
  end
117
  SU.debug("pagebuilder", "No page break here")
1,377✔
118
  if force and bestBreak then
1,377✔
119
    local onepage = {}
1✔
120
    for j=1,bestBreak do
9✔
121
      onepage[j] = table.remove(vboxlist,1)
16✔
122
    end
123
    return onepage, pi
1✔
124
  end
125
  return false, restart
1,376✔
126
end
127

128
return pagebuilder
172✔
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