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

sile-typesetter / sile / 10616097075

29 Aug 2024 01:29PM UTC coverage: 59.298% (-5.5%) from 64.818%
10616097075

push

github

alerque
ci(actions): Switch to Lua actions forks that fix current LuaJIT issues

10338 of 17434 relevant lines covered (59.3%)

3374.78 hits per line

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

91.23
/types/unit.lua
1
--- SILE unit type.
2
-- @types unit
3

4
local bits = require("core.parserbits")
34✔
5

6
local unittypes = {
34✔
7
   pt = {
34✔
8
      relative = false,
9
      value = 1,
10
   },
34✔
11
}
12

13
setmetatable(unittypes, {
68✔
14
   __newindex = function (self, unit, spec)
15
      local def = SU.required(spec, "definition", "registering unit " .. unit)
817✔
16
      local relative = SU.boolean(spec.relative, false)
817✔
17
      if type(def) == "string" then
817✔
18
         local parsed = bits.measurement:match(def)
306✔
19
         if not parsed then
306✔
20
            SU.error("Could not parse unit definition '" .. def .. "'")
×
21
         end
22
         if not self[parsed.unit] then
306✔
23
            SU.error("Unit " .. unit .. " defined in terms of unknown unit " .. parsed.unit)
×
24
         elseif self[parsed.unit].relative then
306✔
25
            rawset(self, unit, {
68✔
26
               relative = true,
27
               converter = function (value)
28
                  return value * self[parsed.unit].converter(parsed.amount)
×
29
               end,
30
            })
34✔
31
         else
32
            rawset(self, unit, {
544✔
33
               relative = false,
34
               value = parsed.amount * self[parsed.unit].value,
272✔
35
            })
36
         end
37
      elseif type(def) == "function" then
511✔
38
         rawset(self, unit, {
1,022✔
39
            relative = relative,
511✔
40
            converter = def,
511✔
41
         })
42
      end
43
   end,
44
})
45

46
unittypes["twip"] = {
34✔
47
   definition = "0.05pt",
48
}
34✔
49

50
unittypes["mm"] = {
34✔
51
   definition = "2.8346457pt",
52
}
34✔
53

54
unittypes["cm"] = {
34✔
55
   definition = "10mm",
56
}
34✔
57

58
unittypes["m"] = {
34✔
59
   definition = "100cm",
60
}
34✔
61

62
unittypes["hm"] = {
34✔
63
   definition = "0.01mm",
64
}
34✔
65

66
unittypes["in"] = {
34✔
67
   definition = "72pt",
68
}
34✔
69

70
unittypes["ft"] = {
34✔
71
   definition = "12in",
72
}
34✔
73

74
-- Picas are 1/6 inch, used in Docbook images
75
unittypes["pc"] = {
34✔
76
   definition = "0.166666667in",
77
}
34✔
78

79
local checkPaperDefined = function ()
80
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
471✔
81
      SU.error("A measurement tried to measure the paper size before the paper was defined", true)
×
82
   end
83
end
84

85
local checkFrameDefined = function ()
86
   if not SILE.typesetter.frame then
8✔
87
      SU.error("A measurement tried to measure the frame before the frame was defined", true)
×
88
   end
89
end
90

91
unittypes["%pw"] = {
34✔
92
   relative = true,
93
   definition = function (value)
94
      checkPaperDefined()
162✔
95
      return value / 100 * SILE.documentState.orgPaperSize[1]
162✔
96
   end,
97
}
34✔
98

99
unittypes["%ph"] = {
34✔
100
   relative = true,
101
   definition = function (value)
102
      checkPaperDefined()
305✔
103
      return value / 100 * SILE.documentState.orgPaperSize[2]
305✔
104
   end,
105
}
34✔
106

107
unittypes["%pmin"] = {
34✔
108
   relative = true,
109
   definition = function (value)
110
      checkPaperDefined()
2✔
111
      return value / 100 * SU.min(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
4✔
112
   end,
113
}
34✔
114

115
unittypes["%pmax"] = {
34✔
116
   relative = true,
117
   definition = function (value)
118
      checkPaperDefined()
2✔
119
      return value / 100 * SU.max(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
4✔
120
   end,
121
}
34✔
122

123
unittypes["%fw"] = {
34✔
124
   relative = true,
125
   definition = function (value)
126
      checkFrameDefined()
1✔
127
      return value / 100 * SILE.typesetter.frame:width():tonumber()
3✔
128
   end,
129
}
34✔
130

131
unittypes["%fh"] = {
34✔
132
   relative = true,
133
   definition = function (value)
134
      checkFrameDefined()
1✔
135
      return value / 100 * SILE.typesetter.frame:height():tonumber()
3✔
136
   end,
137
}
34✔
138

139
unittypes["%fmin"] = {
34✔
140
   relative = true,
141
   definition = function (value)
142
      checkFrameDefined()
2✔
143
      return value / 100 * SU.min(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
12✔
144
   end,
145
}
34✔
146

147
unittypes["%fmax"] = {
34✔
148
   relative = true,
149
   definition = function (value)
150
      checkFrameDefined()
2✔
151
      return value / 100 * SU.max(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
12✔
152
   end,
153
}
34✔
154

155
unittypes["%lw"] = {
34✔
156
   relative = true,
157
   definition = function (value)
158
      local lskip = SILE.settings:get("document.lskip")
2✔
159
      local rskip = SILE.settings:get("document.rskip")
2✔
160
      local left = lskip and lskip.width:tonumber() or 0
3✔
161
      local right = rskip and rskip.width:tonumber() or 0
3✔
162
      checkFrameDefined()
2✔
163
      return value / 100 * SILE.typesetter.frame:getLineWidth():tonumber() - left - right
6✔
164
   end,
165
}
34✔
166

167
unittypes["ps"] = {
34✔
168
   relative = true,
169
   definition = function (value)
170
      local ps = SILE.settings:get("document.parskip")
×
171
      ps = ps.height:tonumber() or 0
×
172
      return value * ps
×
173
   end,
174
}
34✔
175

176
unittypes["bs"] = {
34✔
177
   relative = true,
178
   definition = function (value)
179
      local bs = SILE.settings:get("document.baselineskip")
13✔
180
      bs = bs.height:tonumber() or 0
26✔
181
      return value * bs
13✔
182
   end,
183
}
34✔
184

185
unittypes["em"] = {
34✔
186
   relative = true,
187
   definition = function (value)
188
      return value * SILE.settings:get("font.size")
382✔
189
   end,
190
}
34✔
191

192
unittypes["ex"] = {
34✔
193
   relative = true,
194
   definition = function (value)
195
      return value * SILE.shaper:measureChar("x").height
192✔
196
   end,
197
}
34✔
198

199
unittypes["spc"] = {
34✔
200
   relative = true,
201
   definition = function (value)
202
      return value * SILE.shaper:measureChar(" ").width
4✔
203
   end,
204
}
34✔
205

206
unittypes["en"] = {
34✔
207
   relative = true,
208
   definition = "0.5em",
209
}
34✔
210

211
-- jlreq measures distances in units of 1em, but also assumes that an em is the
212
-- width of a full-width character. In SILE terms it isn't: measuring an "m" in
213
-- a 10pt Japanese font gets you 5 points. So we measure a full-width character
214
-- and use that as a unit. We call it zw following ptex (zenkaku width)
215
unittypes["zw"] = {
34✔
216
   relative = true,
217
   definition = function (v)
218
      local zenkakuchar = SILE.settings:get("document.zenkakuchar")
1✔
219
      local measureable, zenkaku = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar)
1✔
220
      if not measureable then
1✔
221
         SU.warn(string.format(
×
222
            [[Zenkaku width (全角幅) unit zw is falling back to 1em == 1zw as we
×
223
  cannot measure %s. Either change this char to one suitable for your
224
  language, or load a font that has it.]],
225
            zenkakuchar
226
         ))
227
      end
228
      local width = measureable and zenkaku.width or SILE.settings:get("font.size")
1✔
229
      return v * width
1✔
230
   end,
231
}
34✔
232

233
return unittypes
34✔
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