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

sile-typesetter / sile / 11827188491

13 Nov 2024 10:24PM UTC coverage: 33.179% (-27.8%) from 61.013%
11827188491

push

github

alerque
chore(deps): Bump Nix flake dependencies

5947 of 17924 relevant lines covered (33.18%)

1920.63 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

74
-- Picas are 1/6 inch, used in Docbook images
75
unittypes["pc"] = {
8✔
76
   definition = "0.166666667in",
77
}
8✔
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"] = {
8✔
83
   definition = "0.75pt",
84
}
8✔
85

86
local checkPaperDefined = function ()
87
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
353✔
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
×
94
      SU.error("A measurement tried to measure the frame before the frame was defined", true)
×
95
   end
96
end
97

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

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

114
unittypes["%pmin"] = {
8✔
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
}
8✔
121

122
unittypes["%pmax"] = {
8✔
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
}
8✔
129

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

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

146
unittypes["%fmin"] = {
8✔
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
}
8✔
153

154
unittypes["%fmax"] = {
8✔
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
}
8✔
161

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

174
unittypes["ps"] = {
8✔
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
}
8✔
182

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

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

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

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

213
unittypes["en"] = {
8✔
214
   relative = true,
215
   definition = "0.5em",
216
}
8✔
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"] = {
8✔
223
   relative = true,
224
   definition = function (v)
225
      local zenkakuchar = SILE.settings:get("document.zenkakuchar")
1✔
226
      local measurable, zenkaku = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar)
1✔
227
      if 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 = measurable and zenkaku.width or SILE.settings:get("font.size")
1✔
236
      return v * width
1✔
237
   end,
238
}
8✔
239

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