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

sile-typesetter / sile / 11645827362

02 Nov 2024 09:08PM UTC coverage: 54.19% (-15.2%) from 69.34%
11645827362

push

github

web-flow
Merge pull request #2151 from Omikhleia/math-more-fixes-and-features

Math more fixes and features

103 of 208 new or added lines in 4 files covered. (49.52%)

2652 existing lines in 91 files now uncovered.

9816 of 18114 relevant lines covered (54.19%)

671.83 hits per line

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

91.38
/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)
851✔
16
      local relative = SU.boolean(spec.relative, false)
851✔
17
      if type(def) == "string" then
851✔
18
         local parsed = bits.measurement:match(def)
340✔
19
         if not parsed then
340✔
20
            SU.error("Could not parse unit definition '" .. def .. "'")
×
21
         end
22
         if not self[parsed.unit] then
340✔
23
            SU.error("Unit " .. unit .. " defined in terms of unknown unit " .. parsed.unit)
×
24
         elseif self[parsed.unit].relative then
340✔
25
            rawset(self, unit, {
68✔
26
               relative = true,
27
               converter = function (value)
UNCOV
28
                  return value * self[parsed.unit].converter(parsed.amount)
×
29
               end,
30
            })
34✔
31
         else
32
            rawset(self, unit, {
612✔
33
               relative = false,
34
               value = parsed.amount * self[parsed.unit].value,
306✔
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
-- 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"] = {
34✔
83
   definition = "0.75pt",
84
}
34✔
85

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

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

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

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

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

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

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

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

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

162
unittypes["%lw"] = {
34✔
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
3✔
168
      local right = rskip and rskip.width:tonumber() or 0
3✔
169
      checkFrameDefined()
2✔
170
      return value / 100 * SILE.typesetter.frame:getLineWidth():tonumber() - left - right
6✔
171
   end,
172
}
34✔
173

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

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

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

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

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

213
unittypes["en"] = {
34✔
214
   relative = true,
215
   definition = "0.5em",
216
}
34✔
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"] = {
34✔
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
}
34✔
239

240
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

© 2026 Coveralls, Inc