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

sile-typesetter / sile / 15507594683

07 Jun 2025 11:54AM UTC coverage: 30.951% (-30.4%) from 61.309%
15507594683

push

github

alerque
chore(tooling): Add post-checkout hook to clear makedeps on branch switch

6363 of 20558 relevant lines covered (30.95%)

3445.44 hits per line

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

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

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

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

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

48
unittypes["twip"] = {
11✔
49
   definition = "0.05pt",
50
}
11✔
51

52
unittypes["mm"] = {
11✔
53
   definition = "2.8346457pt",
54
}
11✔
55

56
unittypes["cm"] = {
11✔
57
   definition = "10mm",
58
}
11✔
59

60
unittypes["m"] = {
11✔
61
   definition = "100cm",
62
}
11✔
63

64
unittypes["hm"] = {
11✔
65
   definition = "0.01mm",
66
}
11✔
67

68
unittypes["in"] = {
11✔
69
   definition = "72pt",
70
}
11✔
71

72
unittypes["ft"] = {
11✔
73
   definition = "12in",
74
}
11✔
75

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

81
-- Pixel, by convention 1px = 1/96in = 0.75pt
82
-- (CSS Values and Units Module Level 3, §5.2)
83
-- Used in MathML, etc.
84
unittypes["px"] = {
11✔
85
   definition = "0.75pt",
86
}
11✔
87

88
local checkPaperDefined = function ()
89
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
389✔
90
      SU.error("A measurement tried to measure the paper size before the paper was defined", true)
×
91
   end
92
end
93

94
local checkFrameDefined = function ()
95
   if not SILE.typesetter.frame then
2✔
96
      SU.error("A measurement tried to measure the frame before the frame was defined", true)
×
97
   end
98
end
99

100
unittypes["%pw"] = {
11✔
101
   relative = true,
102
   definition = function (value)
103
      checkPaperDefined()
131✔
104
      return value / 100 * SILE.documentState.orgPaperSize[1]
131✔
105
   end,
106
}
11✔
107

108
unittypes["%ph"] = {
11✔
109
   relative = true,
110
   definition = function (value)
111
      checkPaperDefined()
258✔
112
      return value / 100 * SILE.documentState.orgPaperSize[2]
258✔
113
   end,
114
}
11✔
115

116
unittypes["%pmin"] = {
11✔
117
   relative = true,
118
   definition = function (value)
119
      checkPaperDefined()
×
120
      return value / 100 * SU.min(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
×
121
   end,
122
}
11✔
123

124
unittypes["%pmax"] = {
11✔
125
   relative = true,
126
   definition = function (value)
127
      checkPaperDefined()
×
128
      return value / 100 * SU.max(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
×
129
   end,
130
}
11✔
131

132
unittypes["%fw"] = {
11✔
133
   relative = true,
134
   definition = function (value)
135
      checkFrameDefined()
×
136
      return value / 100 * SILE.typesetter.frame:width():tonumber()
×
137
   end,
138
}
11✔
139

140
unittypes["%fh"] = {
11✔
141
   relative = true,
142
   definition = function (value)
143
      checkFrameDefined()
×
144
      return value / 100 * SILE.typesetter.frame:height():tonumber()
×
145
   end,
146
}
11✔
147

148
unittypes["%fmin"] = {
11✔
149
   relative = true,
150
   definition = function (value)
151
      checkFrameDefined()
×
152
      return value / 100 * SU.min(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
×
153
   end,
154
}
11✔
155

156
unittypes["%fmax"] = {
11✔
157
   relative = true,
158
   definition = function (value)
159
      checkFrameDefined()
×
160
      return value / 100 * SU.max(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
×
161
   end,
162
}
11✔
163

164
unittypes["%lw"] = {
11✔
165
   relative = true,
166
   definition = function (value)
167
      local lskip = SILE.settings:get("document.lskip")
4✔
168
      local rskip = SILE.settings:get("document.rskip")
4✔
169
      local left = lskip and lskip.width:tonumber() or 0
4✔
170
      local right = rskip and rskip.width:tonumber() or 0
4✔
171
      checkFrameDefined()
2✔
172
      return value / 100 * SILE.typesetter.frame:getLineWidth():tonumber() - left - right
6✔
173
   end,
174
}
11✔
175

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

185
unittypes["bs"] = {
11✔
186
   relative = true,
187
   definition = function (value)
188
      local bs = SILE.settings:get("document.baselineskip")
10✔
189
      bs = bs.height:tonumber() or 0
10✔
190
      return value * bs
5✔
191
   end,
192
}
11✔
193

194
unittypes["em"] = {
11✔
195
   relative = true,
196
   definition = function (value)
197
      return value * SILE.settings:get("font.size")
417✔
198
   end,
199
}
11✔
200

201
unittypes["ex"] = {
11✔
202
   relative = true,
203
   definition = function (value)
204
      return value * SILE.shaper:measureChar("x").height
16✔
205
   end,
206
}
11✔
207

208
unittypes["spc"] = {
11✔
209
   relative = true,
210
   definition = function (value)
211
      return value * SILE.shaper:measureChar(" ").width
20✔
212
   end,
213
}
11✔
214

215
unittypes["en"] = {
11✔
216
   relative = true,
217
   definition = "0.5em",
218
}
11✔
219

220
-- jlreq measures distances in units of 1em, but also assumes that an em is the
221
-- width of a full-width character. In SILE terms it isn't: measuring an "m" in
222
-- a 10pt Japanese font gets you 5 points. So we measure a full-width character
223
-- and use that as a unit. We call it zw following ptex (zenkaku width)
224
unittypes["zw"] = {
11✔
225
   relative = true,
226
   definition = function (v)
227
      local zenkakuchar = SILE.settings:get("document.zenkakuchar")
4✔
228
      local measurable, zenkaku, found = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar)
2✔
229
      if not found or not measurable then
2✔
230
         SU.warn(([[
×
231
            Zenkaku width (全角幅) unit zw is falling back to 1em == 1zw as we cannot measure %s
232

233
            Either change this char to one suitable for your language, or load a font that
234
            has it.
235
         ]]):format(zenkakuchar))
×
236
      end
237
      local width = found and measurable and zenkaku.width or SILE.settings:get("font.size")
2✔
238
      return v * width
2✔
239
   end,
240
}
11✔
241

242
return unittypes
11✔
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