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

sile-typesetter / sile / 9428907827

08 Jun 2024 12:56PM UTC coverage: 57.223% (-11.0%) from 68.206%
9428907827

push

github

alerque
chore(release): 0.15.1

9741 of 17023 relevant lines covered (57.22%)

4415.41 hits per line

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

0.0
/packages/balanced-frames/init.lua
1
local base = require("packages.base")
×
2

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

6
local BALANCE_PENALTY = -17777
×
7

8
local unbalanced_buildPage
9

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

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

28
   -- Really, try and avoid doing anything, where possible.
29
   if colCount == 1 then
×
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
×
36
   local totalHeight = SILE.types.length()
×
37
   local mustBalance = 0
×
38
   for i = 1, #q do
×
39
      totalHeight = totalHeight + q[i].height + q[i].depth
×
40
      if q[i].is_penalty and q[i].penalty <= BALANCE_PENALTY then
×
41
         mustBalance = i
×
42
         break
43
      end
44
   end
45
   if totalHeight.length > target.length and mustBalance == 0 and not independent then
×
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
×
52
      return false
×
53
   end
54
   SU.debug("balancer", "Balancing", totalHeight, "of material over", colCount, "frames (total of", target, ")")
×
55
   SU.debug("balancer", "Must balance because mustBalance =", mustBalance, "and independent =", independent)
×
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
×
60
   SU.debug("balancer", "Each column is now", totalHeight.length / colCount)
×
61
   while frame and frame.balanced == true do
×
62
      frame:relax("bottom")
×
63
      frame:constrain("height", totalHeight.length / colCount)
×
64
      if frame.next then
×
65
         frame = SILE.getFrame(frame.next)
×
66
      else
67
         break
68
      end
69
   end
70
   typesetter.state.lastPenalty = 0
×
71
   local oldPageBuilder = SILE.pagebuilder
×
72
   SILE.pagebuilder = SILE.pagebuilders.base()
×
73
   while typesetter.frame and typesetter.frame.balanced do
×
74
      unbalanced_buildPage(typesetter, true)
×
75
      if typesetter.frame.next and SILE.getFrame(typesetter.frame.next).balanced == true then
×
76
         typesetter:initFrame(SILE.getFrame(typesetter.frame.next))
×
77
         typesetter:runHooks("newframe")
×
78
      else
79
         break -- Break early, because when we return
80
      end
81
   end
82
   SILE.pagebuilder = oldPageBuilder
×
83
   SU.debug("balancer", "Finished this balance, frame id is now", typesetter.frame)
×
84
   -- SILE.typesetter:debugState()
85
   -- We're done.
86
   return true
×
87
end
88

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

100
function package:registerCommands ()
×
101
   self:registerCommand("balancecolumns", function (_, _)
×
102
      SILE.typesetter:leaveHmode()
×
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
]]
×
116

117
return package
×
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