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

sile-typesetter / sile / 14684598788

26 Apr 2025 07:55PM UTC coverage: 29.023% (-36.3%) from 65.328%
14684598788

push

github

alerque
test(fonts): Update test to work on new ICU instead of old

5840 of 20122 relevant lines covered (29.02%)

379.34 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()
13✔
15
length.type = "length"
13✔
16

17
length.length = nil
13✔
18
length.stretch = nil
13✔
19
length.shrink = nil
13✔
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)
13✔
31
   if stretch or shrink then
28,554✔
32
      self.length = SILE.types.measurement(spec or 0)
9,574✔
33
      self.stretch = SILE.types.measurement(stretch or 0)
9,574✔
34
      self.shrink = SILE.types.measurement(shrink or 0)
9,574✔
35
   elseif type(spec) == "number" then
23,767✔
36
      self.length = SILE.types.measurement(spec)
17,118✔
37
   elseif SU.type(spec) == "measurement" then
30,416✔
38
      self.length = spec
319✔
39
   elseif SU.type(spec) == "glue" then
29,778✔
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
14,887✔
44
      self.length = SILE.types.measurement(spec.length or 0)
8,850✔
45
      self.stretch = SILE.types.measurement(spec.stretch or 0)
8,850✔
46
      self.shrink = SILE.types.measurement(spec.shrink or 0)
8,850✔
47
   elseif type(spec) == "string" then
10,462✔
48
      local amount = tonumber(spec)
175✔
49
      if type(amount) == "number" then
175✔
50
         self:_init(amount)
4✔
51
      else
52
         local input = pl.stringx.strip(spec)
173✔
53
         local length_only_parser = SILE.parserBits.length * -1
173✔
54
         local parsed = length_only_parser:match(input)
173✔
55
         if not parsed then
173✔
56
            SU.error("Could not parse length '" .. spec .. "'")
×
57
         end
58
         self:_init(parsed)
173✔
59
      end
60
   end
61
   if not self.length then
28,554✔
62
      self.length = SILE.types.measurement()
20,574✔
63
   end
64
   if not self.stretch then
28,554✔
65
      self.stretch = SILE.types.measurement()
38,330✔
66
   end
67
   if not self.shrink then
28,554✔
68
      self.shrink = SILE.types.measurement()
38,330✔
69
   end
70
end
71

72
function length:absolute ()
13✔
73
   return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
10,460✔
74
end
75

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

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

84
function length:tonumber ()
13✔
85
   return self.length:tonumber()
23,890✔
86
end
87

88
function length:__tostring ()
13✔
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)
13✔
100
   if type(self) == "number" then
1,692✔
101
      self, other = other, self
1,333✔
102
   end
103
   other = SU.cast("length", other)
3,384✔
104
   return SILE.types.length(self.length + other.length, self.stretch + other.stretch, self.shrink + other.shrink)
6,768✔
105
end
106

107
-- See usage comments on SILE.types.measurement:___add()
108
function length:___add (other)
13✔
109
   if SU.type(other) ~= "length" then
20,682✔
110
      self.length:___add(other)
804✔
111
   else
112
      self.length:___add(other.length)
9,939✔
113
      self.stretch:___add(other.stretch)
9,939✔
114
      self.shrink:___add(other.shrink)
9,939✔
115
   end
116
   return nil
10,341✔
117
end
118

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

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

136
function length:__mul (other)
13✔
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)
13✔
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 ()
13✔
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)
13✔
164
   local a = SU.cast("number", self)
7,072✔
165
   local b = SU.cast("number", other)
7,072✔
166
   return a - b < 0
7,072✔
167
end
168

169
function length:__le (other)
13✔
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)
13✔
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
13✔
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