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

sile-typesetter / sile / 9400954821

06 Jun 2024 11:12AM UTC coverage: 61.625% (-11.6%) from 73.209%
9400954821

push

github

web-flow
Merge pull request #2041 from alerque/keep-space-after-envs

2 of 30 new or added lines in 2 files covered. (6.67%)

1933 existing lines in 67 files now uncovered.

10613 of 17222 relevant lines covered (61.62%)

2870.98 hits per line

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

76.4
/core/frame.lua
1
SILE.frames = {}
145✔
2

3
-- Buggy, relies on side effects
4
-- luacheck: ignore solver frame
5
-- See https://github.com/sile-typesetter/sile/issues/694
6

7
local cassowary = require("cassowary")
145✔
8
local solver = cassowary.SimplexSolver()
145✔
9
local solverNeedsReloading = true
145✔
10

11
local widthdims = pl.Set({ "left", "right", "width" })
145✔
12
local heightdims = pl.Set({ "top", "bottom", "height" })
145✔
13
local alldims = widthdims + heightdims
145✔
14

15
SILE.framePrototype = pl.class({
290✔
16
   direction = "LTR-TTB",
17
   enterHooks = {},
145✔
18
   leaveHooks = {},
145✔
19

20
   -- This gets called by Penlght when creating the frame instance
21
   _init = function (self, spec, dummy)
22
      local direction = SILE.documentState.direction
376✔
23
      if direction then
376✔
24
         self.direction = direction
6✔
25
      end
26
      self.constraints = {}
376✔
27
      self.variables = {}
376✔
28
      self.id = spec.id
376✔
29
      for k, v in pairs(spec) do
2,265✔
30
         if not alldims[k] then
1,889✔
31
            self[k] = v
414✔
32
         end
33
      end
34
      self.balanced = SU.boolean(self.balanced, false)
752✔
35
      if not dummy then
376✔
36
         for method in pairs(alldims) do
2,632✔
37
            self.variables[method] = cassowary.Variable({ name = spec.id .. "_" .. method })
4,512✔
38
            self[method] = function (instance_self)
39
               instance_self:solve()
3,332✔
40
               return SILE.types.measurement(instance_self.variables[method].value)
3,332✔
41
            end
42
         end
43
         -- Add definitions of width and height
44
         for method in pairs(alldims) do
2,632✔
45
            if spec[method] then
2,256✔
46
               self:constrain(method, spec[method])
1,475✔
47
            end
48
         end
49
      end
50
   end,
51

52
   -- This gets called by us in typesetter before we start to use the frame
53
   init = function (self, typesetter)
54
      self.state = { totals = { height = SILE.types.measurement(0) } }
402✔
55
      self:enter(typesetter)
201✔
56
      self:newLine(typesetter)
201✔
57
      if self:pageAdvanceDirection() == "TTB" then
402✔
58
         self.state.cursorY = self:top()
400✔
59
      elseif self:pageAdvanceDirection() == "LTR" then
2✔
UNCOV
60
         self.state.cursorX = self:left()
×
61
      elseif self:pageAdvanceDirection() == "RTL" then
2✔
62
         self.state.cursorX = self:right()
2✔
UNCOV
63
      elseif self:pageAdvanceDirection() == "BTT" then
×
UNCOV
64
         self.state.cursorY = self:bottom()
×
65
      end
66
   end,
67

68
   constrain = function (self, method, dimension)
69
      self.constraints[method] = tostring(dimension)
1,533✔
70
      self:invalidate()
1,505✔
71
   end,
72

73
   invalidate = function ()
74
      solverNeedsReloading = true
1,684✔
75
   end,
76

77
   relax = function (self, method)
78
      self.constraints[method] = nil
18✔
79
   end,
80

81
   reifyConstraint = function (self, solver, method, stay)
82
      local constraint = self.constraints[method]
2,681✔
83
      if not constraint then
2,681✔
84
         return
×
85
      end
86
      constraint = SU.type(constraint) == "measurement" and constraint:tonumber() or SILE.frameParser:match(constraint)
5,362✔
87
      SU.debug("frames", "Adding constraint", self.id, function ()
5,362✔
88
         return "(" .. method .. ") = " .. tostring(constraint)
×
89
      end)
90
      local eq = cassowary.Equation(self.variables[method], constraint)
2,681✔
91
      solver:addConstraint(eq)
2,681✔
92
      if stay then
2,681✔
93
         solver:addStay(eq)
568✔
94
      end
95
   end,
96

97
   addWidthHeightDefinitions = function (self, solver)
98
      local vars = self.variables
676✔
99
      solver:addConstraint(cassowary.Equation(vars.width, cassowary.minus(vars.right, vars.left)))
2,028✔
100
      solver:addConstraint(cassowary.Equation(vars.height, cassowary.minus(vars.bottom, vars.top)))
2,028✔
101
   end,
102

103
   -- This is hideously inefficient,
104
   -- but it's the easiest way to allow users to reconfigure frames at runtime.
105
   solve = function (_)
106
      if not solverNeedsReloading then
3,332✔
107
         return
3,190✔
108
      end
109
      SU.debug("frames", "Solving...")
142✔
110
      solver = cassowary.SimplexSolver()
284✔
111
      if SILE.frames.page then
142✔
112
         for method, _ in pairs(SILE.frames.page.constraints) do
710✔
113
            SILE.frames.page:reifyConstraint(solver, method, true)
568✔
114
         end
115
         SILE.frames.page:addWidthHeightDefinitions(solver)
142✔
116
      end
117
      for id, frame in pairs(SILE.frames) do
818✔
118
         if not (id == "page") then
676✔
119
            for method, _ in pairs(frame.constraints) do
2,647✔
120
               frame:reifyConstraint(solver, method)
2,113✔
121
            end
122
            frame:addWidthHeightDefinitions(solver)
534✔
123
         end
124
      end
125
      solver:solve()
142✔
126
      solverNeedsReloading = false
142✔
127
   end,
128

129
   writingDirection = function (self)
130
      return self.direction:match("^(%a+)") or "LTR"
45,810✔
131
   end,
132

133
   pageAdvanceDirection = function (self)
134
      return self.direction:match("-(%a+)$") or "TTB"
3,827✔
135
   end,
136

137
   advanceWritingDirection = function (self, length)
138
      local amount = SU.cast("number", length)
10,795✔
139
      if amount == 0 then
10,795✔
140
         return
1,641✔
141
      end
142
      if self:writingDirection() == "RTL" then
18,308✔
143
         self.state.cursorX = self.state.cursorX - amount
434✔
144
      elseif self:writingDirection() == "LTR" then
17,874✔
145
         self.state.cursorX = self.state.cursorX + amount
17,862✔
146
      elseif self:writingDirection() == "TTB" then
12✔
147
         self.state.cursorY = self.state.cursorY + amount
12✔
UNCOV
148
      elseif self:writingDirection() == "BTT" then
×
UNCOV
149
         self.state.cursorY = self.state.cursorY - amount
×
150
      end
151
   end,
152

153
   advancePageDirection = function (self, length)
154
      local amount = SU.cast("number", length)
2,867✔
155
      if amount == 0 then
2,867✔
156
         return
208✔
157
      end
158
      if self:pageAdvanceDirection() == "TTB" then
5,318✔
159
         self.state.cursorY = self.state.cursorY + amount
5,312✔
160
      elseif self:pageAdvanceDirection() == "RTL" then
6✔
161
         self.state.cursorX = self.state.cursorX - amount
6✔
UNCOV
162
      elseif self:pageAdvanceDirection() == "LTR" then
×
UNCOV
163
         self.state.cursorX = self.state.cursorX + amount
×
UNCOV
164
      elseif self:pageAdvanceDirection() == "BTT" then
×
UNCOV
165
         self.state.cursorY = self.state.cursorY - amount
×
166
      end
167
   end,
168

169
   newLine = function (self, _)
170
      if self:writingDirection() == "LTR" then
1,866✔
171
         self.state.cursorX = self:left()
1,822✔
172
      elseif self:writingDirection() == "RTL" then
44✔
173
         self.state.cursorX = self:right()
40✔
174
      elseif self:writingDirection() == "TTB" then
4✔
175
         self.state.cursorY = self:top()
4✔
UNCOV
176
      elseif self:writingDirection() == "BTT" then
×
UNCOV
177
         self.state.cursorY = self:bottom()
×
178
      end
179
   end,
180

181
   lineWidth = function (self)
182
      SU.warn("Method :lineWidth() is deprecated, please use :getLineWidth()")
×
183
      return self:getLineWidth()
×
184
   end,
185

186
   getLineWidth = function (self)
187
      if self:writingDirection() == "LTR" or self:writingDirection() == "RTL" then
1,066✔
188
         return self:width()
527✔
189
      else
190
         return self:height()
1✔
191
      end
192
   end,
193

194
   pageTarget = function (self)
195
      SU.warn("Method :pageTarget() is deprecated, please use :getTargetLength()")
×
196
      return self:getTargetLength()
×
197
   end,
198

199
   getTargetLength = function (self)
200
      local direction = self:pageAdvanceDirection()
962✔
201
      if direction == "TTB" or direction == "BTT" then
962✔
202
         return self:height()
958✔
203
      else
204
         return self:width()
4✔
205
      end
206
   end,
207

208
   enter = function (self, typesetter)
209
      for i = 1, #self.enterHooks do
254✔
210
         self.enterHooks[i](self, typesetter)
3✔
211
      end
212
   end,
213

214
   leave = function (self, typesetter)
215
      for i = 1, #self.leaveHooks do
252✔
216
         self.leaveHooks[i](self, typesetter)
9✔
217
      end
218
   end,
219

220
   isAbsoluteConstraint = function (self, method)
221
      if not self.constraints[method] then
88✔
222
         return false
1✔
223
      end
224
      local constraint = SILE.frameParser:match(self.constraints[method])
87✔
225
      if type(constraint) ~= "table" then
87✔
226
         return true
21✔
227
      end
228
      if not constraint.terms then
66✔
229
         return false
64✔
230
      end
231
      for clv, _ in pairs(constraint.terms) do
2✔
232
         if clv.name and not clv.name:match("^page_") then
2✔
233
            return false
2✔
234
         end
235
      end
236
      return true
×
237
   end,
238

239
   isMainContentFrame = function (self)
240
      local tpt = SILE.documentState.thisPageTemplate
39✔
241
      local frame = tpt.firstContentFrame
39✔
242
      while frame do
42✔
243
         if frame == self then
42✔
244
            return true
25✔
245
         end
246
         if frame.next then
17✔
247
            frame = SILE.getFrame(frame.next)
6✔
248
         else
249
            return false
14✔
250
         end
251
      end
252
      return false
×
253
   end,
254

255
   __tostring = function (self)
256
      local str = "<Frame: " .. self.id .. ": "
×
257
      str = str .. " next=" .. (self.next or "nil") .. " "
×
258
      for method, dimension in pairs(self.constraints) do
×
259
         str = str .. method .. "=" .. dimension .. "; "
×
260
      end
261
      if self.hanmen then
×
262
         str = str .. "tate=" .. (self.tate and "true" or "false") .. "; "
×
263
         str = str .. "gridsize=" .. self.gridsize .. "; "
×
264
         str = str .. "linegap=" .. self.linegap .. "; "
×
265
         str = str .. "linelength=" .. self.linelength .. "; "
×
266
         str = str .. "linecount=" .. self.linecount .. "; "
×
267
      end
268
      str = str .. ">"
×
269
      return str
×
270
   end,
271
})
145✔
272

273
SILE.newFrame = function (spec, prototype)
145✔
274
   SU.required(spec, "id", "frame declaration")
376✔
275
   prototype = prototype or SILE.framePrototype
376✔
276
   local frame = prototype(spec)
376✔
277
   SILE.frames[spec.id] = frame
376✔
278
   return frame
376✔
279
end
280

281
SILE.getFrame = function (id)
145✔
282
   if type(id) == "table" then
140✔
283
      SU.error("Passed a table, expected a string", true)
×
284
   end
285
   local frame, last_attempt
286
   while not frame do
280✔
287
      frame = SILE.frames[id]
141✔
288
      id = id:gsub("_$", "")
141✔
289
      if id == last_attempt then
141✔
290
         break
1✔
291
      end
292
      last_attempt = id
140✔
293
   end
294
   return frame or SU.warn("Couldn't find frame ID " .. id, true)
141✔
295
end
296

297
SILE.parseComplexFrameDimension = function (dimension)
145✔
298
   local length = SILE.frameParser:match(SU.cast("string", dimension))
×
299
   if type(length) == "table" then
×
300
      local g = cassowary.Variable({ name = "t" })
×
301
      local eq = cassowary.Equation(g, length)
×
302
      solverNeedsReloading = true
×
303
      solver:addConstraint(eq)
×
304
      SILE.frames.page:solve()
×
305
      solverNeedsReloading = true
×
306
      return g.value
×
307
   end
308
   return length
×
309
end
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