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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

93.44
/packages/balanced-frames/init.lua
1
local base = require("packages.base")
15✔
2

3
local package = pl.class(base)
15✔
4
package._name = "balanced-frames"
15✔
5

6
local BALANCE_PENALTY = -17777
15✔
7

8
local unbalanced_buildPage
9

10
local function buildPage (typesetter, independent)
11
   local frame = typesetter.frame
188✔
12
   if not (frame.balanced == true) then
188✔
13
      return unbalanced_buildPage(typesetter, independent)
185✔
14
   end
15

16
   local colCount = 0
3✔
17
   local target = SILE.length()
3✔
18
   while frame and frame.balanced == true do
12✔
19
      target = target + frame:height()
24✔
20
      colCount = colCount + 1
12✔
21
      if frame.next then
12✔
22
         frame = SILE.getFrame(frame.next)
18✔
23
      else
24
         break
25
      end
26
   end
27

28
   -- Really, try and avoid doing anything, where possible.
29
   if colCount == 1 then
3✔
NEW
30
      return unbalanced_buildPage(typesetter, independent)
×
31
   end
32
   -- If the total amount of stuff on the output list is greater then the total
33
   -- of frame space on the page, and there are no magic requests to balance the
34
   -- columns, then we have a full page. Just send it out normally.
35
   local q = typesetter.state.outputQueue
3✔
36
   local totalHeight = SILE.length()
3✔
37
   local mustBalance = 0
3✔
38
   for i = 1, #q do
6✔
39
      totalHeight = totalHeight + q[i].height + q[i].depth
8✔
40
      if q[i].is_penalty and q[i].penalty <= BALANCE_PENALTY then
4✔
41
         mustBalance = i
1✔
42
         break
1✔
43
      end
44
   end
45
   if totalHeight.length > target.length and mustBalance == 0 and not independent then
3✔
NEW
46
      return unbalanced_buildPage(typesetter, independent)
×
47
   end
48

49
   -- Have we been explicitly asked to find a pagebreak at this point?
50
   -- If not, don't bother
51
   if mustBalance == 0 and not independent then
3✔
52
      return false
2✔
53
   end
54
   SU.debug("balancer", "Balancing", totalHeight, "of material over", colCount, "frames (total of", target, ")")
1✔
55
   SU.debug("balancer", "Must balance because mustBalance =", mustBalance, "and independent =", independent)
1✔
56
   -- OK. Now we have to balance the frames. We are going to cheat and
57
   -- adjust the height of each frame to be an appropriate fraction of
58
   -- the content height
59
   frame = typesetter.frame
1✔
60
   SU.debug("balancer", "Each column is now", totalHeight.length / colCount)
2✔
61
   while frame and frame.balanced == true do
4✔
62
      frame:relax("bottom")
4✔
63
      frame:constrain("height", totalHeight.length / colCount)
8✔
64
      if frame.next then
4✔
65
         frame = SILE.getFrame(frame.next)
6✔
66
      else
67
         break
68
      end
69
   end
70
   typesetter.state.lastPenalty = 0
1✔
71
   local oldPageBuilder = SILE.pagebuilder
1✔
72
   SILE.pagebuilder = require("core.pagebuilder")()
2✔
73
   while typesetter.frame and typesetter.frame.balanced do
4✔
74
      unbalanced_buildPage(typesetter, true)
4✔
75
      if typesetter.frame.next and SILE.getFrame(typesetter.frame.next).balanced == true then
7✔
76
         typesetter:initFrame(SILE.getFrame(typesetter.frame.next))
6✔
77
         typesetter:runHooks("newframe")
6✔
78
      else
79
         break -- Break early, because when we return
80
      end
81
   end
82
   SILE.pagebuilder = oldPageBuilder
1✔
83
   SU.debug("balancer", "Finished this balance, frame id is now", typesetter.frame)
1✔
84
   -- SILE.typesetter:debugState()
85
   -- We're done.
86
   return true
1✔
87
end
88

89
function package:_init (class)
15✔
90
   base._init(self, class)
15✔
91
   self.class:registerPostinit(function (_)
30✔
92
      if not unbalanced_buildPage then
15✔
93
         unbalanced_buildPage = SILE.typesetter.buildPage
15✔
94
         SILE.typesetter.buildPage = buildPage
15✔
95
         SILE.typesetters.base.buildPage = buildPage
15✔
96
      end
97
   end)
98
end
99

100
function package:registerCommands ()
15✔
101
   self:registerCommand("balancecolumns", function (_, _)
30✔
NEW
102
      SILE.typesetter:leaveHmode()
×
NEW
103
      SILE.call("penalty", { penalty = BALANCE_PENALTY })
×
104
   end)
105
end
106

107
package.documentation = [[
108
\begin{document}
109
This package attempts to ensure that the main content frames on a page are balanced; that is, that they have the same height.
110
In your frame definitions for the columns, you will need to ensure that they have the parameter \autodoc:parameter{balanced} set to \code{true}.
111
See the example in \code{tests/balanced.sil}.
112

113
The current algorithm does not work particularly well, and a better solution to the column problem is being developed.
114
\end{document}
115
]]
15✔
116

117
return package
15✔
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

© 2025 Coveralls, Inc