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

sile-typesetter / sile / 14859255754

06 May 2025 12:08PM UTC coverage: 56.689% (-3.2%) from 59.865%
14859255754

push

github

alerque
fix(build): Pass --enable-debug through to Cargo to actually get a debug-enabled binary

12191 of 21505 relevant lines covered (56.69%)

3298.74 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")
21✔
5

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

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

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

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

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

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

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

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

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

76
-- Picas are 1/6 inch, used in Docbook images
77
unittypes["pc"] = {
21✔
78
   definition = "0.166666667in",
79
}
21✔
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"] = {
21✔
85
   definition = "0.75pt",
86
}
21✔
87

88
local checkPaperDefined = function ()
89
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
399✔
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"] = {
21✔
101
   relative = true,
102
   definition = function (value)
103
      checkPaperDefined()
135✔
104
      return value / 100 * SILE.documentState.orgPaperSize[1]
135✔
105
   end,
106
}
21✔
107

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

116
unittypes["%pmin"] = {
21✔
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
}
21✔
123

124
unittypes["%pmax"] = {
21✔
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
}
21✔
131

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

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

148
unittypes["%fmin"] = {
21✔
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
}
21✔
155

156
unittypes["%fmax"] = {
21✔
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
}
21✔
163

164
unittypes["%lw"] = {
21✔
165
   relative = true,
166
   definition = function (value)
167
      local lskip = SILE.settings:get("document.lskip")
2✔
168
      local rskip = SILE.settings:get("document.rskip")
2✔
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
}
21✔
175

176
unittypes["ps"] = {
21✔
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
}
21✔
184

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

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

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

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

215
unittypes["en"] = {
21✔
216
   relative = true,
217
   definition = "0.5em",
218
}
21✔
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"] = {
21✔
225
   relative = true,
226
   definition = function (v)
227
      local zenkakuchar = SILE.settings:get("document.zenkakuchar")
2✔
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
}
21✔
241

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