• 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

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

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

UNCOV
6
local BALANCE_PENALTY = -17777
×
7

8
local unbalanced_buildPage
9

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

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

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

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

UNCOV
100
function package:registerCommands ()
×
NEW
101
   self:registerCommand("balancecolumns", function (_, _)
×
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}
UNCOV
115
]]
×
116

UNCOV
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

© 2026 Coveralls, Inc