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

sile-typesetter / sile / 13944301947

19 Mar 2025 10:26AM UTC coverage: 32.822% (-28.9%) from 61.713%
13944301947

push

github

alerque
chore(deps): Bump developer tooling

6590 of 20078 relevant lines covered (32.82%)

1175.23 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

213
unittypes["en"] = {
23✔
214
   relative = true,
215
   definition = "0.5em",
216
}
23✔
217

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

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

240
return unittypes
23✔
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