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

sile-typesetter / sile / 11534409649

26 Oct 2024 07:27PM UTC coverage: 33.196% (-28.7%) from 61.897%
11534409649

push

github

alerque
chore(tooling): Update editor-config key for stylua as accepted upstream

Our setting addition is still not in a tagged release, but the PR was
accepted into the default branch of stylua. This means you no longer
need to run my fork of Stylua to get this project's style, you just nead
any build from the main development branch. However the config key was
renamed as part of the acceptance, so this is the relevant adjustment.

5810 of 17502 relevant lines covered (33.2%)

1300.57 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

79
local checkPaperDefined = function ()
80
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
383✔
81
      SU.error("A measurement tried to measure the paper size before the paper was defined", true)
×
82
   end
83
end
84

85
local checkFrameDefined = function ()
86
   if not SILE.typesetter.frame then
×
87
      SU.error("A measurement tried to measure the frame before the frame was defined", true)
×
88
   end
89
end
90

91
unittypes["%pw"] = {
17✔
92
   relative = true,
93
   definition = function (value)
94
      checkPaperDefined()
129✔
95
      return value / 100 * SILE.documentState.orgPaperSize[1]
129✔
96
   end,
97
}
17✔
98

99
unittypes["%ph"] = {
17✔
100
   relative = true,
101
   definition = function (value)
102
      checkPaperDefined()
254✔
103
      return value / 100 * SILE.documentState.orgPaperSize[2]
254✔
104
   end,
105
}
17✔
106

107
unittypes["%pmin"] = {
17✔
108
   relative = true,
109
   definition = function (value)
110
      checkPaperDefined()
×
111
      return value / 100 * SU.min(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
×
112
   end,
113
}
17✔
114

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

123
unittypes["%fw"] = {
17✔
124
   relative = true,
125
   definition = function (value)
126
      checkFrameDefined()
×
127
      return value / 100 * SILE.typesetter.frame:width():tonumber()
×
128
   end,
129
}
17✔
130

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

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

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

155
unittypes["%lw"] = {
17✔
156
   relative = true,
157
   definition = function (value)
158
      local lskip = SILE.settings:get("document.lskip")
×
159
      local rskip = SILE.settings:get("document.rskip")
×
160
      local left = lskip and lskip.width:tonumber() or 0
×
161
      local right = rskip and rskip.width:tonumber() or 0
×
162
      checkFrameDefined()
×
163
      return value / 100 * SILE.typesetter.frame:getLineWidth():tonumber() - left - right
×
164
   end,
165
}
17✔
166

167
unittypes["ps"] = {
17✔
168
   relative = true,
169
   definition = function (value)
170
      local ps = SILE.settings:get("document.parskip")
×
171
      ps = ps.height:tonumber() or 0
×
172
      return value * ps
×
173
   end,
174
}
17✔
175

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

185
unittypes["em"] = {
17✔
186
   relative = true,
187
   definition = function (value)
188
      return value * SILE.settings:get("font.size")
244✔
189
   end,
190
}
17✔
191

192
unittypes["ex"] = {
17✔
193
   relative = true,
194
   definition = function (value)
195
      return value * SILE.shaper:measureChar("x").height
8✔
196
   end,
197
}
17✔
198

199
unittypes["spc"] = {
17✔
200
   relative = true,
201
   definition = function (value)
202
      return value * SILE.shaper:measureChar(" ").width
4✔
203
   end,
204
}
17✔
205

206
unittypes["en"] = {
17✔
207
   relative = true,
208
   definition = "0.5em",
209
}
17✔
210

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

224
            Either change this char to one suitable for your language, or load a font that
225
            has it.
226
         ]]):format(zenkakuchar))
×
227
      end
228
      local width = measurable and zenkaku.width or SILE.settings:get("font.size")
1✔
229
      return v * width
1✔
230
   end,
231
}
17✔
232

233
return unittypes
17✔
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