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

sile-typesetter / sile / 10881107806

16 Sep 2024 09:29AM UTC coverage: 61.663% (-7.2%) from 68.912%
10881107806

push

github

web-flow
chore(deps): Bump DeterminateSystems/nix-installer-action from 13 to 14 (#2110)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

10751 of 17435 relevant lines covered (61.66%)

2338.25 hits per line

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

90.0
/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
1,431✔
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()
82✔
15
length.type = "length"
82✔
16

17
length.length = nil
82✔
18
length.stretch = nil
82✔
19
length.shrink = nil
82✔
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)
82✔
31
   if stretch or shrink then
121,358✔
32
      self.length = SILE.types.measurement(spec or 0)
50,168✔
33
      self.stretch = SILE.types.measurement(stretch or 0)
50,168✔
34
      self.shrink = SILE.types.measurement(shrink or 0)
50,168✔
35
   elseif type(spec) == "number" then
96,274✔
36
      self.length = SILE.types.measurement(spec)
73,760✔
37
   elseif SU.type(spec) == "measurement" then
118,788✔
38
      self.length = spec
1,723✔
39
   elseif SU.type(spec) == "glue" then
115,342✔
40
      self.length = SILE.types.measurement(spec.width.length or 0)
×
41
      self.stretch = SILE.types.measurement(spec.width.stretch or 0)
×
42
      self.shrink = SILE.types.measurement(spec.width.shrink or 0)
×
43
   elseif type(spec) == "table" then
57,671✔
44
      self.length = SILE.types.measurement(spec.length or 0)
40,736✔
45
      self.stretch = SILE.types.measurement(spec.stretch or 0)
40,736✔
46
      self.shrink = SILE.types.measurement(spec.shrink or 0)
40,736✔
47
   elseif type(spec) == "string" then
37,303✔
48
      local amount = tonumber(spec)
1,138✔
49
      if type(amount) == "number" then
1,138✔
50
         self:_init(amount)
6✔
51
      else
52
         local parsed = SILE.parserBits.length:match(spec)
1,135✔
53
         if not parsed then
1,135✔
54
            SU.error("Could not parse length '" .. spec .. "'")
×
55
         end
56
         self:_init(parsed)
1,135✔
57
      end
58
   end
59
   if not self.length then
121,358✔
60
      self.length = SILE.types.measurement()
72,330✔
61
   end
62
   if not self.stretch then
121,358✔
63
      self.stretch = SILE.types.measurement()
149,536✔
64
   end
65
   if not self.shrink then
121,358✔
66
      self.shrink = SILE.types.measurement()
149,536✔
67
   end
68
end
69

70
function length:absolute ()
82✔
71
   return SILE.types.length(self.length:tonumber(), self.stretch:tonumber(), self.shrink:tonumber())
42,704✔
72
end
73

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

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

82
function length:tonumber ()
82✔
83
   return self.length:tonumber()
71,547✔
84
end
85

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

97
function length:__add (other)
82✔
98
   if type(self) == "number" then
12,454✔
99
      self, other = other, self
3,621✔
100
   end
101
   other = SU.cast("length", other)
24,908✔
102
   return SILE.types.length(self.length + other.length, self.stretch + other.stretch, self.shrink + other.shrink)
49,816✔
103
end
104

105
-- See usage comments on SILE.types.measurement:___add()
106
function length:___add (other)
82✔
107
   if SU.type(other) ~= "length" then
68,150✔
108
      self.length:___add(other)
3,434✔
109
   else
110
      self.length:___add(other.length)
32,358✔
111
      self.stretch:___add(other.stretch)
32,358✔
112
      self.shrink:___add(other.shrink)
32,358✔
113
   end
114
   return nil
34,075✔
115
end
116

117
function length:__sub (other)
82✔
118
   local result = SILE.types.length(self)
12,771✔
119
   other = SU.cast("length", other)
25,542✔
120
   result.length = result.length - other.length
25,542✔
121
   result.stretch = result.stretch - other.stretch
25,542✔
122
   result.shrink = result.shrink - other.shrink
25,542✔
123
   return result
12,771✔
124
end
125

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

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

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

155
function length:__unm ()
82✔
156
   local result = SILE.types.length(self)
26✔
157
   result.length = result.length:__unm()
52✔
158
   return result
26✔
159
end
160

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

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

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

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