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

sile-typesetter / sile / 9307175333

30 May 2024 06:08PM UTC coverage: 70.644% (-3.5%) from 74.124%
9307175333

push

github

web-flow
Merge b18390e74 into 70ff5c335

1910 of 2592 new or added lines in 108 files covered. (73.69%)

901 existing lines in 52 files now uncovered.

12203 of 17274 relevant lines covered (70.64%)

6112.16 hits per line

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

95.45
/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 amout 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
1,596✔
NEW
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()
209✔
15
length.type = "length"
209✔
16

17
length.length = nil
209✔
18
length.stretch = nil
209✔
19
length.shrink = nil
209✔
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)
209✔
31
   if stretch or shrink then
283,507✔
32
      self.length = SILE.types.measurement(spec or 0)
113,510✔
33
      self.stretch = SILE.types.measurement(stretch or 0)
113,510✔
34
      self.shrink = SILE.types.measurement(shrink or 0)
113,510✔
35
   elseif type(spec) == "number" then
226,752✔
36
      self.length = SILE.types.measurement(spec)
174,432✔
37
   elseif SU.type(spec) == "measurement" then
279,072✔
38
      self.length = spec
3,257✔
39
   elseif SU.type(spec) == "glue" then
272,558✔
40
      self.length = SILE.types.measurement(spec.width.length or 0)
58✔
41
      self.stretch = SILE.types.measurement(spec.width.stretch or 0)
58✔
42
      self.shrink = SILE.types.measurement(spec.width.shrink or 0)
58✔
43
   elseif type(spec) == "table" then
136,250✔
44
      self.length = SILE.types.measurement(spec.length or 0)
79,958✔
45
      self.stretch = SILE.types.measurement(spec.stretch or 0)
79,958✔
46
      self.shrink = SILE.types.measurement(spec.shrink or 0)
79,958✔
47
   elseif type(spec) == "string" then
96,271✔
48
      local amount = tonumber(spec)
2,343✔
49
      if type(amount) == "number" then
2,343✔
50
         self:_init(amount)
6✔
51
      else
52
         local parsed = SILE.parserBits.length:match(spec)
2,340✔
53
         if not parsed then
2,340✔
NEW
54
            SU.error("Could not parse length '" .. spec .. "'")
×
55
         end
56
         self:_init(parsed)
2,340✔
57
      end
58
   end
59
   if not self.length then
283,507✔
60
      self.length = SILE.types.measurement()
187,856✔
61
   end
62
   if not self.stretch then
283,507✔
63
      self.stretch = SILE.types.measurement()
368,802✔
64
   end
65
   if not self.shrink then
283,507✔
66
      self.shrink = SILE.types.measurement()
368,802✔
67
   end
68
end
69

70
function length:absolute ()
209✔
71
   return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
110,620✔
72
end
73

74
function length:negate ()
209✔
NEW
75
   return self:__unm()
×
76
end
77

78
function length:tostring ()
209✔
NEW
79
   return self:__tostring()
×
80
end
81

82
function length:tonumber ()
209✔
83
   return self.length:tonumber()
210,866✔
84
end
85

86
function length:__tostring ()
209✔
87
   local str = tostring(self.length)
32✔
88
   if self.stretch.amount ~= 0 then
32✔
89
      str = str .. " plus " .. tostring(self.stretch)
44✔
90
   end
91
   if self.shrink.amount ~= 0 then
32✔
NEW
92
      str = str .. " minus " .. tostring(self.shrink)
×
93
   end
94
   return str
32✔
95
end
96

97
function length:__add (other)
209✔
98
   if type(self) == "number" then
23,681✔
99
      self, other = other, self
11,197✔
100
   end
101
   other = SU.cast("length", other)
47,362✔
102
   return SILE.types.length(self.length + other.length, self.stretch + other.stretch, self.shrink + other.shrink)
94,724✔
103
end
104

105
-- See usage comments on SILE.types.measurement:___add()
106
function length:___add (other)
209✔
107
   if SU.type(other) ~= "length" then
164,636✔
108
      self.length:___add(other)
9,738✔
109
   else
110
      self.length:___add(other.length)
77,449✔
111
      self.stretch:___add(other.stretch)
77,449✔
112
      self.shrink:___add(other.shrink)
77,449✔
113
   end
114
   return nil
82,318✔
115
end
116

117
function length:__sub (other)
209✔
118
   local result = SILE.types.length(self)
22,013✔
119
   other = SU.cast("length", other)
44,026✔
120
   result.length = result.length - other.length
44,026✔
121
   result.stretch = result.stretch - other.stretch
44,026✔
122
   result.shrink = result.shrink - other.shrink
44,026✔
123
   return result
22,013✔
124
end
125

126
-- See usage comments on SILE.types.measurement:___add()
127
function length:___sub (other)
209✔
128
   self.length:___sub(other.length)
5,128✔
129
   self.stretch:___sub(other.stretch)
5,128✔
130
   self.shrink:___sub(other.shrink)
5,128✔
131
   return nil
5,128✔
132
end
133

134
function length:__mul (other)
209✔
135
   if type(self) == "number" then
1,397✔
136
      self, other = other, self
6✔
137
   end
138
   _error_if_not_number(other)
1,397✔
139
   local result = SILE.types.length(self)
1,397✔
140
   result.length = result.length * other
2,794✔
141
   result.stretch = result.stretch * other
2,794✔
142
   result.shrink = result.shrink * other
2,794✔
143
   return result
1,397✔
144
end
145

146
function length:__div (other)
209✔
147
   local result = SILE.types.length(self)
199✔
148
   _error_if_not_number(other)
199✔
149
   result.length = result.length / other
398✔
150
   result.stretch = result.stretch / other
398✔
151
   result.shrink = result.shrink / other
398✔
152
   return result
199✔
153
end
154

155
function length:__unm ()
209✔
156
   local result = SILE.types.length(self)
64✔
157
   result.length = result.length:__unm()
128✔
158
   return result
64✔
159
end
160

161
function length:__lt (other)
209✔
162
   local a = SU.cast("number", self)
63,804✔
163
   local b = SU.cast("number", other)
63,804✔
164
   return a - b < 0
63,804✔
165
end
166

167
function length:__le (other)
209✔
168
   local a = SU.cast("number", self)
26✔
169
   local b = SU.cast("number", other)
26✔
170
   return a - b <= 0
26✔
171
end
172

173
function length:__eq (other)
209✔
174
   local a = SU.cast("length", self)
6✔
175
   local b = SU.cast("length", other)
6✔
176
   return a.length == b.length and a.stretch == b.stretch and a.shrink == b.shrink
24✔
177
end
178

179
return length
209✔
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