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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

92.04
/core/units.lua
1
local units = {
181✔
2
   pt = {
181✔
3
      relative = false,
4
      value = 1,
5
   },
181✔
6
}
7

8
setmetatable(units, {
362✔
9
   __newindex = function (self, unit, spec)
10
      local def = SU.required(spec, "definition", "registering unit " .. unit)
4,356✔
11
      local relative = SU.boolean(spec.relative, false)
4,356✔
12
      if type(def) == "string" then
4,356✔
13
         local parsed = SILE.parserBits.measurement:match(def)
1,629✔
14
         if not parsed then
1,629✔
NEW
15
            SU.error("Could not parse unit definition '" .. def .. "'")
×
16
         end
17
         if not self[parsed.unit] then
1,629✔
NEW
18
            SU.error("Unit " .. unit .. " defined in terms of unknown unit " .. parsed.unit)
×
19
         elseif self[parsed.unit].relative then
1,629✔
20
            rawset(self, unit, {
362✔
21
               relative = true,
22
               converter = function (value)
23
                  return value * self[parsed.unit].converter(parsed.amount)
24✔
24
               end,
25
            })
181✔
26
         else
27
            rawset(self, unit, {
2,896✔
28
               relative = false,
29
               value = parsed.amount * self[parsed.unit].value,
1,448✔
30
            })
31
         end
32
      elseif type(def) == "function" then
2,727✔
33
         rawset(self, unit, {
5,454✔
34
            relative = relative,
2,727✔
35
            converter = def,
2,727✔
36
         })
37
      end
38
   end,
39
})
40

41
units["twip"] = {
181✔
42
   definition = "0.05pt",
43
}
181✔
44

45
units["mm"] = {
181✔
46
   definition = "2.8346457pt",
47
}
181✔
48

49
units["cm"] = {
181✔
50
   definition = "10mm",
51
}
181✔
52

53
units["m"] = {
181✔
54
   definition = "100cm",
55
}
181✔
56

57
units["hm"] = {
181✔
58
   definition = "0.01mm",
59
}
181✔
60

61
units["in"] = {
181✔
62
   definition = "72pt",
63
}
181✔
64

65
units["ft"] = {
181✔
66
   definition = "12in",
67
}
181✔
68

69
-- Picas are 1/6 inch, used in Docbook images
70
units["pc"] = {
181✔
71
   definition = "0.166666667in",
72
}
181✔
73

74
local checkPaperDefined = function ()
75
   if not SILE.documentState or not SILE.documentState.orgPaperSize then
2,856✔
NEW
76
      SU.error("A measurement tried to measure the paper size before the paper was defined", true)
×
77
   end
78
end
79

80
local checkFrameDefined = function ()
81
   if not SILE.typesetter.frame then
13✔
NEW
82
      SU.error("A measurement tried to measure the frame before the frame was defined", true)
×
83
   end
84
end
85

86
units["%pw"] = {
181✔
87
   relative = true,
88
   definition = function (value)
89
      checkPaperDefined()
995✔
90
      return value / 100 * SILE.documentState.orgPaperSize[1]
995✔
91
   end,
92
}
181✔
93

94
units["%ph"] = {
181✔
95
   relative = true,
96
   definition = function (value)
97
      checkPaperDefined()
1,857✔
98
      return value / 100 * SILE.documentState.orgPaperSize[2]
1,857✔
99
   end,
100
}
181✔
101

102
units["%pmin"] = {
181✔
103
   relative = true,
104
   definition = function (value)
105
      checkPaperDefined()
2✔
106
      return value / 100 * SU.min(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
4✔
107
   end,
108
}
181✔
109

110
units["%pmax"] = {
181✔
111
   relative = true,
112
   definition = function (value)
113
      checkPaperDefined()
2✔
114
      return value / 100 * SU.max(SILE.documentState.orgPaperSize[1], SILE.documentState.orgPaperSize[2])
4✔
115
   end,
116
}
181✔
117

118
units["%fw"] = {
181✔
119
   relative = true,
120
   definition = function (value)
121
      checkFrameDefined()
1✔
122
      return value / 100 * SILE.typesetter.frame:width():tonumber()
3✔
123
   end,
124
}
181✔
125

126
units["%fh"] = {
181✔
127
   relative = true,
128
   definition = function (value)
129
      checkFrameDefined()
3✔
130
      return value / 100 * SILE.typesetter.frame:height():tonumber()
9✔
131
   end,
132
}
181✔
133

134
units["%fmin"] = {
181✔
135
   relative = true,
136
   definition = function (value)
137
      checkFrameDefined()
2✔
138
      return value / 100 * SU.min(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
12✔
139
   end,
140
}
181✔
141

142
units["%fmax"] = {
181✔
143
   relative = true,
144
   definition = function (value)
145
      checkFrameDefined()
2✔
146
      return value / 100 * SU.max(SILE.typesetter.frame:width():tonumber(), SILE.typesetter.frame:height():tonumber())
12✔
147
   end,
148
}
181✔
149

150
units["%lw"] = {
181✔
151
   relative = true,
152
   definition = function (value)
153
      local lskip = SILE.settings:get("document.lskip")
5✔
154
      local rskip = SILE.settings:get("document.rskip")
5✔
155
      local left = lskip and lskip.width:tonumber() or 0
6✔
156
      local right = rskip and rskip.width:tonumber() or 0
6✔
157
      checkFrameDefined()
5✔
158
      return value / 100 * SILE.typesetter.frame:getLineWidth():tonumber() - left - right
15✔
159
   end,
160
}
181✔
161

162
units["ps"] = {
181✔
163
   relative = true,
164
   definition = function (value)
NEW
165
      local ps = SILE.settings:get("document.parskip")
×
NEW
166
      ps = ps.height:tonumber() or 0
×
NEW
167
      return value * ps
×
168
   end,
169
}
181✔
170

171
units["bs"] = {
181✔
172
   relative = true,
173
   definition = function (value)
174
      local bs = SILE.settings:get("document.baselineskip")
14✔
175
      bs = bs.height:tonumber() or 0
28✔
176
      return value * bs
14✔
177
   end,
178
}
181✔
179

180
units["em"] = {
181✔
181
   relative = true,
182
   definition = function (value)
183
      return value * SILE.settings:get("font.size")
4,138✔
184
   end,
185
}
181✔
186

187
units["ex"] = {
181✔
188
   relative = true,
189
   definition = function (value)
190
      return value * SILE.shaper:measureChar("x").height
3,146✔
191
   end,
192
}
181✔
193

194
units["spc"] = {
181✔
195
   relative = true,
196
   definition = function (value)
197
      return value * SILE.shaper:measureChar(" ").width
294✔
198
   end,
199
}
181✔
200

201
units["en"] = {
181✔
202
   relative = true,
203
   definition = "0.5em",
204
}
181✔
205

206
-- jlreq measures distances in units of 1em, but also assumes that an em is the
207
-- width of a full-width character. In SILE terms it isn't: measuring an "m" in
208
-- a 10pt Japanese font gets you 5 points. So we measure a full-width character
209
-- and use that as a unit. We call it zw following ptex (zenkaku width)
210
units["zw"] = {
181✔
211
   relative = true,
212
   definition = function (v)
213
      local zenkakuchar = SILE.settings:get("document.zenkakuchar")
211✔
214
      local measureable, zenkaku = pcall(SILE.shaper.measureChar, SILE.shaper, zenkakuchar)
211✔
215
      if not measureable then
211✔
NEW
216
         SU.warn(string.format(
×
NEW
217
            [[Zenkaku width (全角幅) unit zw is falling back to 1em == 1zw as we
×
218
  cannot measure %s. Either change this char to one suitable for your
219
  language, or load a font that has it.]],
220
            zenkakuchar
221
         ))
222
      end
223
      local width = measureable and zenkaku.width or SILE.settings:get("font.size")
211✔
224
      return v * width
211✔
225
   end,
226
}
181✔
227

228
return units
181✔
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