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

sile-typesetter / sile / 13989845427

21 Mar 2025 10:15AM UTC coverage: 32.874% (+2.1%) from 30.742%
13989845427

push

github

alerque
chore(tooling): Hard code spelling fix to keep changelog chugging

6628 of 20162 relevant lines covered (32.87%)

1147.94 hits per line

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

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

5
local lastshaper
6

7
local bits = require("core.parserbits")
83✔
8
local lpeg = require("lpeg")
83✔
9
local Ct, Cg, P = lpeg.Ct, lpeg.Cg, lpeg.P
83✔
10
local adjust_metric = P("ex-height") + P("cap-height")
83✔
11
-- stylua: ignore start
12
local adjustment = Ct(Cg(bits.number, "amount")^-1 * bits.ws * Cg(adjust_metric, "unit"))
83✔
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)
166✔
54
   if SU.ast.hasContent(content) then
802✔
55
      SILE.settings:pushState()
25✔
56
   end
57
   if options.adjust then
401✔
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
401✔
64
      SILE.settings:set("font.filename", options.filename)
×
65
   end
66
   if options.family then
401✔
67
      SILE.settings:set("font.family", options.family)
374✔
68
      SILE.settings:set("font.filename", "")
374✔
69
   end
70
   if options.size then
401✔
71
      local size = SU.cast("measurement", options.size)
389✔
72
      if not size then
389✔
73
         SU.error("Couldn't parse font size " .. options.size)
×
74
      end
75
      SILE.settings:set("font.size", size:absolute())
778✔
76
   end
77
   if options.weight then
401✔
78
      SILE.settings:set("font.weight", 0 + options.weight)
357✔
79
   end
80
   if options.style then
401✔
81
      SILE.settings:set("font.style", options.style)
355✔
82
   end
83
   if options.variant then
401✔
84
      SILE.settings:set("font.variant", options.variant)
354✔
85
   end
86
   if options.features then
401✔
87
      SILE.settings:set("font.features", options.features)
354✔
88
   end
89
   if options.variations then
401✔
90
      SILE.settings:set("font.variations", options.variations)
354✔
91
   end
92
   if options.direction then
401✔
93
      SILE.settings:set("font.direction", options.direction)
354✔
94
   end
95
   if options.language then
401✔
96
      if options.language ~= "und" and icu and icu.canonicalize_language then
357✔
97
         local newlang = icu.canonicalize_language(options.language)
357✔
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
357✔
102
      end
103
      SILE.languageSupport.loadLanguage(options.language)
357✔
104
      SILE.settings:set("document.language", options.language)
357✔
105
   end
106
   if options.script then
401✔
107
      SILE.settings:set("font.script", options.script)
710✔
108
   elseif SILE.settings:get("document.language") then
92✔
109
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
92✔
110
      if lang and lang.defaultScript then
46✔
111
         SILE.settings:set("font.script", lang.defaultScript)
×
112
      end
113
   end
114
   if options.hyphenchar then
401✔
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))
710✔
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)
802✔
123

124
   if SU.ast.hasContent(content) then
802✔
125
      SILE.process(content)
25✔
126
      SILE.settings:popState()
25✔
127
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
25✔
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)
484✔
134

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

147
SILE.fontCache = {}
83✔
148

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

163
local font = {
83✔
164

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

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

222
   finish = function ()
223
      for key, font in pairs(SILE.fontCache) do
149✔
224
         if font.tempfilename ~= font.filename then
107✔
225
            SU.debug("fonts", "Removing temporary file of", key, ":", font.tempfilename)
×
226
            os.remove(font.tempfilename)
×
227
         end
228
      end
229
   end,
230

231
   postLoadHook = function (face)
232
      local ot = require("core.opentype-parser")
3,378✔
233
      local font = ot.parseFont(face)
3,378✔
234
      if font.cpal then
3,378✔
235
         SILE.require("packages.color-fonts")
×
236
         if SILE.shaper._name ~= "harfbuzzWithColor" then
×
237
            SU.debug("color-fonts", "Switching to color font Shaper")
×
238
            SILE.typesetter:leaveHmode(true)
×
239
            lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
240
         end
241
      end
242
   end,
243

244
   _key = _key,
83✔
245
}
246

247
return font
83✔
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