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

sile-typesetter / sile / 14684598788

26 Apr 2025 07:55PM UTC coverage: 29.023% (-36.3%) from 65.328%
14684598788

push

github

alerque
test(fonts): Update test to work on new ICU instead of old

5840 of 20122 relevant lines covered (29.02%)

379.34 hits per line

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

80.92
/core/font.lua
1
--- font
2
-- @module SILE.font
3
local icu = require("justenoughicu")
13✔
4

5
local lastshaper
6

7
local bits = require("core.parserbits")
13✔
8
local lpeg = require("lpeg")
13✔
9
local Ct, Cg, P = lpeg.Ct, lpeg.Cg, lpeg.P
13✔
10
local adjust_metric = P("ex-height") + P("cap-height")
13✔
11
-- stylua: ignore start
12
local adjustment = Ct(Cg(bits.number, "amount")^-1 * bits.ws * Cg(adjust_metric, "unit"))
13✔
13
-- stylua: ignore end
14

15
local function measureFontAdjustment (metric)
16
   if metric == "ex-height" then
6✔
17
      -- Uses the height of lowercase letters.
18
      -- This is used to normalize lowercase letters across fonts.
19
      -- The height of the lowercase letter "x" is used as the reference.
20
      -- Another option would be to use the OS/2 font table sxHeight value when available.
21
      return SILE.shaper:measureChar("x").height
12✔
22
   end
23
   if metric == "cap-height" then
×
24
      -- Uses the the height of uppercase letters.
25
      -- This is used to normalize uppercase letters across fonts.
26
      -- The height of the uppercase letter "H" is used as the reference.
27
      -- Another option would be to use the OS/2 font table sCapHeight value when available.
28
      return SILE.shaper:measureChar("H").height
×
29
   end
30
   SU.error("Unknown font adjust metric " .. metric)
×
31
end
32

33
local function adjustedFontSize (options)
34
   local adjust = options.adjust
3✔
35
   local parsed = adjustment:match(adjust)
3✔
36
   if not parsed then
3✔
37
      SU.error("Couldn't parse font adjust value " .. adjust)
×
38
   end
39
   -- Shallow copy: we don't want to modify the original AST as content may be reused
40
   -- in other contexts (e.g. running headers) and may need to adapt to different font sizes.
41
   local baseOpts = pl.tablex.copy(options)
3✔
42
   baseOpts.adjust = nil -- cancel for target font size calculation
3✔
43
   local currentMeasure = measureFontAdjustment(parsed.unit)
3✔
44
   local ratio = parsed.amount or 1
3✔
45
   local newMeasure
46
   -- Apply the target font size to measure the new font
47
   SILE.call("font", baseOpts, function ()
6✔
48
      newMeasure = measureFontAdjustment(parsed.unit)
6✔
49
   end)
50
   return SILE.settings:get("font.size") * ratio * (currentMeasure / newMeasure)
6✔
51
end
52

53
SILE.registerCommand("font", function (options, content)
26✔
54
   if SU.ast.hasContent(content) then
32✔
55
      SILE.settings:pushState()
10✔
56
   end
57
   if options.adjust then
16✔
58
      if options.size then
3✔
59
         SU.error("Can't specify both 'size' and 'adjust' in a \\font command")
×
60
      end
61
      SILE.settings:set("font.size", adjustedFontSize(options))
6✔
62
   end
63
   if options.filename then
16✔
64
      SILE.settings:set("font.filename", options.filename)
×
65
   end
66
   if options.family then
16✔
67
      SILE.settings:set("font.family", options.family)
11✔
68
      SILE.settings:set("font.filename", "")
11✔
69
   end
70
   if options.size then
16✔
71
      local size = SU.cast("measurement", options.size)
5✔
72
      if not size then
5✔
73
         SU.error("Couldn't parse font size " .. options.size)
×
74
      end
75
      SILE.settings:set("font.size", size:absolute())
10✔
76
   end
77
   if options.weight then
16✔
78
      SILE.settings:set("font.weight", 0 + options.weight)
×
79
   end
80
   if options.style then
16✔
81
      SILE.settings:set("font.style", options.style)
×
82
   end
83
   if options.variant then
16✔
84
      SILE.settings:set("font.variant", options.variant)
×
85
   end
86
   if options.features then
16✔
87
      SILE.settings:set("font.features", options.features)
×
88
   end
89
   if options.variations then
16✔
90
      SILE.settings:set("font.variations", options.variations)
×
91
   end
92
   if options.direction then
16✔
93
      SILE.settings:set("font.direction", options.direction)
×
94
   end
95
   if options.language then
16✔
96
      if options.language ~= "und" and icu and icu.canonicalize_language then
1✔
97
         local newlang = icu.canonicalize_language(options.language)
1✔
98
         -- if newlang ~= options.language then
99
         -- SU.warn("Language '"..options.language.."' not canonical, '"..newlang.."' will be used instead")
100
         -- end
101
         options.language = newlang
1✔
102
      end
103
      SILE.languageSupport.loadLanguage(options.language)
1✔
104
      SILE.settings:set("document.language", options.language)
1✔
105
   end
106
   if options.script then
16✔
107
      SILE.settings:set("font.script", options.script)
×
108
   elseif SILE.settings:get("document.language") then
32✔
109
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
32✔
110
      if lang and lang.defaultScript then
16✔
111
         SILE.settings:set("font.script", lang.defaultScript)
×
112
      end
113
   end
114
   if options.hyphenchar then
16✔
115
      -- must be in the form of, for example, "-" or "U+2010" or "0x2010" (Unicode hex codepoint)
116
      SILE.settings:set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
×
117
   end
118

119
   -- We must *actually* load the font here, because by the time we're inside
120
   -- SILE.shaper.shapeToken, it's too late to respond appropriately to things
121
   -- that the post-load hook might want to do.
122
   SILE.font.cache(SILE.font.loadDefaults(options), SILE.shaper.getFace)
32✔
123

124
   if SU.ast.hasContent(content) then
32✔
125
      SILE.process(content)
10✔
126
      SILE.settings:popState()
10✔
127
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
10✔
128
         SU.debug("color-fonts", "Switching from color fonts shaper back to previous shaper")
×
129
         SILE.typesetter:leaveHmode(true)
×
130
         lastshaper, SILE.shaper = nil, lastshaper
×
131
      end
132
   end
133
end, "Set current font family, size, weight, style, variant, script, direction and language", nil, true)
29✔
134

135
SILE.settings:declare({ parameter = "font.family", type = "string or nil", default = "Gentium Plus" })
13✔
136
SILE.settings:declare({ parameter = "font.size", type = "number or integer", default = 10 })
13✔
137
SILE.settings:declare({ parameter = "font.weight", type = "integer", default = 400 })
13✔
138
SILE.settings:declare({ parameter = "font.variant", type = "string", default = "normal" })
13✔
139
SILE.settings:declare({ parameter = "font.script", type = "string", default = "" })
13✔
140
SILE.settings:declare({ parameter = "font.style", type = "string", default = "" })
13✔
141
SILE.settings:declare({ parameter = "font.direction", type = "string", default = "" })
13✔
142
SILE.settings:declare({ parameter = "font.filename", type = "string or nil", default = "" })
13✔
143
SILE.settings:declare({ parameter = "font.features", type = "string", default = "" })
13✔
144
SILE.settings:declare({ parameter = "font.variations", type = "string", default = "" })
13✔
145
SILE.settings:declare({ parameter = "font.hyphenchar", type = "string", default = "-" })
13✔
146

147
SILE.fontCache = {}
13✔
148

149
local _key = function (options)
150
   return table.concat({
1,988✔
151
      options.family or "",
1,988✔
152
      ("%g"):format(SILE.types.measurement(options.size):tonumber()),
5,964✔
153
      ("%d"):format(options.weight or 0),
1,988✔
154
      options.style,
1,988✔
155
      options.variant,
1,988✔
156
      options.features,
1,988✔
157
      options.variations,
1,988✔
158
      options.direction,
1,988✔
159
      options.filename or "",
1,988✔
160
   }, ";")
1,988✔
161
end
162

163
local font = {
13✔
164

165
   loadDefaults = function (options)
166
      if not options.family then
115✔
167
         options.family = SILE.settings:get("font.family")
192✔
168
      end
169
      if not options.size then
115✔
170
         options.size = SILE.settings:get("font.size")
204✔
171
      end
172
      if not options.weight then
115✔
173
         options.weight = SILE.settings:get("font.weight")
230✔
174
      end
175
      if not options.style then
115✔
176
         options.style = SILE.settings:get("font.style")
230✔
177
      end
178
      if not options.variant then
115✔
179
         options.variant = SILE.settings:get("font.variant")
230✔
180
      end
181
      if SILE.settings:get("font.filename") ~= "" then
230✔
182
         options.filename = SILE.settings:get("font.filename")
×
183
         options.family = ""
×
184
      end
185
      if not options.language then
115✔
186
         options.language = SILE.settings:get("document.language")
212✔
187
      end
188
      if not options.script then
115✔
189
         options.script = SILE.settings:get("font.script")
230✔
190
      end
191
      if not options.direction then
115✔
192
         options.direction = SILE.settings:get("font.direction")
230✔
193
         if not options.direction or options.direction == "" then
115✔
194
            options.direction = SILE.typesetter and SILE.typesetter.frame and SILE.typesetter.frame:writingDirection()
115✔
195
               or "LTR"
115✔
196
         end
197
      end
198
      if not options.features then
115✔
199
         options.features = SILE.settings:get("font.features")
230✔
200
      end
201
      if not options.variations then
115✔
202
         options.variations = SILE.settings:get("font.variations")
230✔
203
      end
204
      if not options.hyphenchar then
115✔
205
         options.hyphenchar = SILE.settings:get("font.hyphenchar")
230✔
206
      end
207
      return options
115✔
208
   end,
209

210
   cache = function (options, callback)
211
      local key = _key(options)
287✔
212
      if not SILE.fontCache[key] then
287✔
213
         SU.debug("fonts", "Looking for", key)
14✔
214
         local face = callback(options)
14✔
215
         SILE.fontCache[key] = face
14✔
216
      end
217
      local cached = SILE.fontCache[key]
287✔
218
      SILE.font.postLoadHook(cached)
287✔
219
      return cached
287✔
220
   end,
221

222
   finish = function ()
223
      for key, font in pairs(SILE.fontCache) do
22✔
224
         -- Don't do anything for Pango fonts
225
         if type(font) ~= "userdata" and type(font.insert) ~= "function" then
14✔
226
            if font.tempfilename ~= font.filename then
14✔
227
               SU.debug("fonts", "Removing temporary file of", key, ":", font.tempfilename)
×
228
               os.remove(font.tempfilename)
×
229
            end
230
         end
231
      end
232
   end,
233

234
   postLoadHook = function (face)
235
      -- Don't do anything for Pango fonts (here face could be a Pango Attribute Lists)
236
      if type(face) == "userdata" and type(face.insert) == "function" then
287✔
237
         return
×
238
      end
239
      local ot = require("core.opentype-parser")
287✔
240
      local font = ot.parseFont(face)
287✔
241
      if font.cpal then
287✔
242
         SILE.require("packages.color-fonts")
×
243
         if SILE.shaper._name ~= "harfbuzzWithColor" then
×
244
            SU.debug("color-fonts", "Switching to color font Shaper")
×
245
            SILE.typesetter:leaveHmode(true)
×
246
            lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
247
         end
248
      end
249
   end,
250

251
   _key = _key,
13✔
252
}
253

254
return font
13✔
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