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

sile-typesetter / sile / 14859255754

06 May 2025 12:08PM UTC coverage: 56.689% (-3.2%) from 59.865%
14859255754

push

github

alerque
fix(build): Pass --enable-debug through to Cargo to actually get a debug-enabled binary

12191 of 21505 relevant lines covered (56.69%)

3298.74 hits per line

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

86.61
/types/length.lua
1
--- SILE length type.
2
-- Lengths are composed of 3 `measurement`s: a length, a stretch, and a shrink. Each part internally is just
3
-- a measurement, but combined describe a flexible length that is allowed to grow up to the amount defined by stretch or
4
-- compress up to the amount defined by shrink.
5
-- @types length
6

7
local function _error_if_not_number (a)
8
   if type(a) ~= "number" then
8✔
9
      SU.error("We tried to do impossible arithmetic on a " .. SU.type(a) .. ". (That's a bug)", true)
×
10
   end
11
end
12

13
--- @type length
14
local length = pl.class()
21✔
15
length.type = "length"
21✔
16

17
length.length = nil
21✔
18
length.stretch = nil
21✔
19
length.shrink = nil
21✔
20

21
--- Constructor.
22
-- @tparam measurement spec A measurement or value that can be cast to a measurement.
23
-- @tparam[opt=0] measurement stretch A measurement describing how much the length is allowed to grow.
24
-- @tparam[opt=0] measurement shrink A measurement describing how much the length is allowed to grow.
25
-- @treturn length
26
-- @usage
27
-- SILE.types.length("6em", "4pt", "2pt")
28
-- SILE.types.length("6em plus 4pt minus 2pt")
29
-- SILE.types.length(30, 4, 2)
30
function length:_init (spec, stretch, shrink)
21✔
31
   if stretch or shrink then
32,359✔
32
      self.length = SILE.types.measurement(spec or 0)
11,242✔
33
      self.stretch = SILE.types.measurement(stretch or 0)
11,242✔
34
      self.shrink = SILE.types.measurement(shrink or 0)
11,242✔
35
   elseif type(spec) == "number" then
26,738✔
36
      self.length = SILE.types.measurement(spec)
18,912✔
37
   elseif SU.type(spec) == "measurement" then
34,564✔
38
      self.length = spec
382✔
39
   elseif SU.type(spec) == "glue" then
33,800✔
40
      self.length = SILE.types.measurement(spec.width.length or 0)
4✔
41
      self.stretch = SILE.types.measurement(spec.width.stretch or 0)
4✔
42
      self.shrink = SILE.types.measurement(spec.width.shrink or 0)
4✔
43
   elseif type(spec) == "table" then
16,898✔
44
      self.length = SILE.types.measurement(spec.length or 0)
10,044✔
45
      self.stretch = SILE.types.measurement(spec.stretch or 0)
10,044✔
46
      self.shrink = SILE.types.measurement(spec.shrink or 0)
10,044✔
47
   elseif type(spec) == "string" then
11,876✔
48
      local amount = tonumber(spec)
244✔
49
      if type(amount) == "number" then
244✔
50
         self:_init(amount)
4✔
51
      else
52
         local input = pl.stringx.strip(spec)
242✔
53
         local length_only_parser = SILE.parserBits.length * -1
242✔
54
         local parsed = length_only_parser:match(input)
242✔
55
         if not parsed then
242✔
56
            SU.error("Could not parse length '" .. spec .. "'")
×
57
         end
58
         self:_init(parsed)
242✔
59
      end
60
   end
61
   if not self.length then
32,359✔
62
      self.length = SILE.types.measurement()
23,264✔
63
   end
64
   if not self.stretch then
32,359✔
65
      self.stretch = SILE.types.measurement()
42,940✔
66
   end
67
   if not self.shrink then
32,359✔
68
      self.shrink = SILE.types.measurement()
42,940✔
69
   end
70
end
71

72
function length:absolute ()
21✔
73
   return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
12,424✔
74
end
75

76
function length:negate ()
21✔
77
   return self:__unm()
×
78
end
79

80
function length:tostring ()
21✔
81
   return self:__tostring()
×
82
end
83

84
function length:tonumber ()
21✔
85
   return self.length:tonumber()
27,055✔
86
end
87

88
function length:__tostring ()
21✔
89
   local str = tostring(self.length)
2✔
90
   if self.stretch.amount ~= 0 then
2✔
91
      str = str .. " plus " .. tostring(self.stretch)
2✔
92
   end
93
   if self.shrink.amount ~= 0 then
2✔
94
      str = str .. " minus " .. tostring(self.shrink)
×
95
   end
96
   return str
2✔
97
end
98

99
function length:__add (other)
21✔
100
   if type(self) == "number" then
1,895✔
101
      self, other = other, self
1,458✔
102
   end
103
   other = SU.cast("length", other)
3,790✔
104
   return SILE.types.length(self.length + other.length, self.stretch + other.stretch, self.shrink + other.shrink)
7,580✔
105
end
106

107
-- See usage comments on SILE.types.measurement:___add()
108
function length:___add (other)
21✔
109
   if SU.type(other) ~= "length" then
23,252✔
110
      self.length:___add(other)
1,044✔
111
   else
112
      self.length:___add(other.length)
11,104✔
113
      self.stretch:___add(other.stretch)
11,104✔
114
      self.shrink:___add(other.shrink)
11,104✔
115
   end
116
   return nil
11,626✔
117
end
118

119
function length:__sub (other)
21✔
120
   local result = SILE.types.length(self)
2,819✔
121
   other = SU.cast("length", other)
5,638✔
122
   result.length = result.length - other.length
5,638✔
123
   result.stretch = result.stretch - other.stretch
5,638✔
124
   result.shrink = result.shrink - other.shrink
5,638✔
125
   return result
2,819✔
126
end
127

128
-- See usage comments on SILE.types.measurement:___add()
129
function length:___sub (other)
21✔
130
   self.length:___sub(other.length)
780✔
131
   self.stretch:___sub(other.stretch)
780✔
132
   self.shrink:___sub(other.shrink)
780✔
133
   return nil
780✔
134
end
135

136
function length:__mul (other)
21✔
137
   if type(self) == "number" then
8✔
138
      self, other = other, self
×
139
   end
140
   _error_if_not_number(other)
8✔
141
   local result = SILE.types.length(self)
8✔
142
   result.length = result.length * other
16✔
143
   result.stretch = result.stretch * other
16✔
144
   result.shrink = result.shrink * other
16✔
145
   return result
8✔
146
end
147

148
function length:__div (other)
21✔
149
   local result = SILE.types.length(self)
×
150
   _error_if_not_number(other)
×
151
   result.length = result.length / other
×
152
   result.stretch = result.stretch / other
×
153
   result.shrink = result.shrink / other
×
154
   return result
×
155
end
156

157
function length:__unm ()
21✔
158
   local result = SILE.types.length(self)
4✔
159
   result.length = result.length:__unm()
8✔
160
   return result
4✔
161
end
162

163
function length:__lt (other)
21✔
164
   local a = SU.cast("number", self)
8,032✔
165
   local b = SU.cast("number", other)
8,032✔
166
   return a - b < 0
8,032✔
167
end
168

169
function length:__le (other)
21✔
170
   local a = SU.cast("number", self)
3✔
171
   local b = SU.cast("number", other)
3✔
172
   return a - b <= 0
3✔
173
end
174

175
function length:__eq (other)
21✔
176
   local a = SU.cast("length", self)
×
177
   local b = SU.cast("length", other)
×
178
   return a.length == b.length and a.stretch == b.stretch and a.shrink == b.shrink
×
179
end
180

181
return length
21✔
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