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

sile-typesetter / sile / 6915746301

18 Nov 2023 07:02PM UTC coverage: 56.433% (-12.3%) from 68.751%
6915746301

push

github

web-flow
Merge 8b3fdc301 into f64e235fa

8729 of 15468 relevant lines covered (56.43%)

932.75 hits per line

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

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

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

39
units["twip"] = {
34✔
40
  definition = "0.05pt"
×
41
}
34✔
42

43
units["mm"] = {
34✔
44
  definition = "2.8346457pt"
×
45
}
34✔
46

47
units["cm"] = {
34✔
48
  definition = "10mm"
×
49
}
34✔
50

51
units["m"] = {
34✔
52
  definition = "100cm"
×
53
}
34✔
54

55
units["hm"] = {
34✔
56
  definition = "0.01mm"
×
57
}
34✔
58

59
units["in"] = {
34✔
60
  definition = "72pt"
×
61
}
34✔
62

63
units["ft"] = {
34✔
64
  definition = "12in"
×
65
}
34✔
66

67
-- Picas are 1/6 inch, used in Docbook images
68
units["pc"] = {
34✔
69
  definition = "0.166666667in"
×
70
}
34✔
71

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

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

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

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

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

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

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

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

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

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

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

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

169
units["bs"] = {
34✔
170
  relative = true,
171
  definition = function (value)
172
    local bs = SILE.settings:get("document.baselineskip")
2✔
173
    bs = bs.height:tonumber() or 0
4✔
174
    return value * bs
2✔
175
  end
176
}
34✔
177

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

185
units["ex"] = {
34✔
186
  relative = true,
187
  definition = function (value)
188
    return value * SILE.shaper:measureChar("x").height
988✔
189
  end
190
}
34✔
191

192
units["spc"] = {
34✔
193
  relative = true,
194
  definition = function (value)
195
    return value * SILE.shaper:measureChar(" ").width
4✔
196
  end
197
}
34✔
198

199
units["en"] = {
34✔
200
  relative = true,
201
  definition = "0.5em"
×
202
}
34✔
203

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

223
return units
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

© 2025 Coveralls, Inc