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

sile-typesetter / sile / 9450620331

10 Jun 2024 02:49PM UTC coverage: 68.509% (+0.4%) from 68.102%
9450620331

push

github

alerque
chore(tooling): Update Git ignore list with current artifacts

11861 of 17313 relevant lines covered (68.51%)

5099.69 hits per line

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

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

5
local lastshaper
6

7
SILE.registerCommand("font", function (options, content)
596✔
8
   if SU.ast.hasContent(content) then
1,940✔
9
      SILE.settings:pushState()
227✔
10
   end
11
   if options.filename then
970✔
12
      SILE.settings:set("font.filename", options.filename)
×
13
   end
14
   if options.family then
970✔
15
      SILE.settings:set("font.family", options.family)
709✔
16
      SILE.settings:set("font.filename", "")
709✔
17
   end
18
   if options.size then
970✔
19
      local size = SU.cast("measurement", options.size)
858✔
20
      if not size then
858✔
21
         SU.error("Couldn't parse font size " .. options.size)
×
22
      end
23
      SILE.settings:set("font.size", size:absolute())
1,716✔
24
   end
25
   if options.weight then
970✔
26
      SILE.settings:set("font.weight", 0 + options.weight)
687✔
27
   end
28
   if options.style then
970✔
29
      SILE.settings:set("font.style", options.style)
732✔
30
   end
31
   if options.variant then
970✔
32
      SILE.settings:set("font.variant", options.variant)
661✔
33
   end
34
   if options.features then
970✔
35
      SILE.settings:set("font.features", options.features)
667✔
36
   end
37
   if options.variations then
970✔
38
      SILE.settings:set("font.variations", options.variations)
661✔
39
   end
40
   if options.direction then
970✔
41
      SILE.settings:set("font.direction", options.direction)
663✔
42
   end
43
   if options.language then
970✔
44
      if options.language ~= "und" and icu and icu.canonicalize_language then
680✔
45
         local newlang = icu.canonicalize_language(options.language)
680✔
46
         -- if newlang ~= options.language then
47
         -- SU.warn("Language '"..options.language.."' not canonical, '"..newlang.."' will be used instead.")
48
         -- end
49
         options.language = newlang
680✔
50
      end
51
      SILE.settings:set("document.language", options.language)
680✔
52
      fluent:set_locale(options.language)
680✔
53
      SILE.languageSupport.loadLanguage(options.language)
680✔
54
   end
55
   if options.script then
970✔
56
      SILE.settings:set("font.script", options.script)
1,326✔
57
   elseif SILE.settings:get("document.language") then
614✔
58
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
614✔
59
      if lang and lang.defaultScript then
307✔
60
         SILE.settings:set("font.script", lang.defaultScript)
1✔
61
      end
62
   end
63
   if options.hyphenchar then
970✔
64
      -- must be in the form of, for example, "-" or "U+2010" or "0x2010" (Unicode hex codepoint)
65
      SILE.settings:set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
1,324✔
66
   end
67

68
   -- We must *actually* load the font here, because by the time we're inside
69
   -- SILE.shaper.shapeToken, it's too late to respond appropriately to things
70
   -- that the post-load hook might want to do.
71
   SILE.font.cache(SILE.font.loadDefaults({}), SILE.shaper.getFace)
1,940✔
72

73
   if SU.ast.hasContent(content) then
1,940✔
74
      SILE.process(content)
227✔
75
      SILE.settings:popState()
227✔
76
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
227✔
77
         SU.debug("color-fonts", "Switching from color fonts shaper back to previous shaper")
×
78
         SILE.typesetter:leaveHmode(true)
×
79
         lastshaper, SILE.shaper = nil, lastshaper
×
80
      end
81
   end
82
end, "Set current font family, size, weight, style, variant, script, direction and language", nil, true)
1,268✔
83

84
SILE.settings:declare({ parameter = "font.family", type = "string or nil", default = "Gentium Plus" })
298✔
85
SILE.settings:declare({ parameter = "font.size", type = "number or integer", default = 10 })
298✔
86
SILE.settings:declare({ parameter = "font.weight", type = "integer", default = 400 })
298✔
87
SILE.settings:declare({ parameter = "font.variant", type = "string", default = "normal" })
298✔
88
SILE.settings:declare({ parameter = "font.script", type = "string", default = "" })
298✔
89
SILE.settings:declare({ parameter = "font.style", type = "string", default = "" })
298✔
90
SILE.settings:declare({ parameter = "font.direction", type = "string", default = "" })
298✔
91
SILE.settings:declare({ parameter = "font.filename", type = "string or nil", default = "" })
298✔
92
SILE.settings:declare({ parameter = "font.features", type = "string", default = "" })
298✔
93
SILE.settings:declare({ parameter = "font.variations", type = "string", default = "" })
298✔
94
SILE.settings:declare({ parameter = "font.hyphenchar", type = "string", default = "-" })
298✔
95

96
SILE.fontCache = {}
298✔
97

98
local _key = function (options)
99
   return table.concat({
36,846✔
100
      options.family,
36,846✔
101
      ("%g"):format(SILE.types.measurement(options.size):tonumber()),
110,538✔
102
      ("%d"):format(options.weight or 0),
36,846✔
103
      options.style,
36,846✔
104
      options.variant,
36,846✔
105
      options.features,
36,846✔
106
      options.variations,
36,846✔
107
      options.direction,
36,846✔
108
      options.filename,
36,846✔
109
   }, ";")
36,846✔
110
end
111

112
local font = {
298✔
113

114
   loadDefaults = function (options)
115
      if not options.family then
6,805✔
116
         options.family = SILE.settings:get("font.family")
9,518✔
117
      end
118
      if not options.size then
6,805✔
119
         options.size = SILE.settings:get("font.size")
9,518✔
120
      end
121
      if not options.weight then
6,805✔
122
         options.weight = SILE.settings:get("font.weight")
9,818✔
123
      end
124
      if not options.style then
6,805✔
125
         options.style = SILE.settings:get("font.style")
9,818✔
126
      end
127
      if not options.variant then
6,805✔
128
         options.variant = SILE.settings:get("font.variant")
13,610✔
129
      end
130
      if SILE.settings:get("font.filename") ~= "" then
13,610✔
131
         options.filename = SILE.settings:get("font.filename")
×
132
         options.family = ""
×
133
      end
134
      if not options.language then
6,805✔
135
         options.language = SILE.settings:get("document.language")
13,310✔
136
      end
137
      if not options.script then
6,805✔
138
         options.script = SILE.settings:get("font.script")
13,610✔
139
      end
140
      if not options.direction then
6,805✔
141
         options.direction = SILE.settings:get("font.direction")
13,610✔
142
         if not options.direction or options.direction == "" then
6,805✔
143
            options.direction = SILE.typesetter and SILE.typesetter.frame and SILE.typesetter.frame:writingDirection()
6,122✔
144
               or "LTR"
6,122✔
145
         end
146
      end
147
      if not options.features then
6,805✔
148
         options.features = SILE.settings:get("font.features")
13,610✔
149
      end
150
      if not options.variations then
6,805✔
151
         options.variations = SILE.settings:get("font.variations")
13,610✔
152
      end
153
      if not options.hyphenchar then
6,805✔
154
         options.hyphenchar = SILE.settings:get("font.hyphenchar")
13,610✔
155
      end
156
      return options
6,805✔
157
   end,
158

159
   cache = function (options, callback)
160
      local key = _key(options)
6,198✔
161
      if not SILE.fontCache[key] then
6,198✔
162
         SU.debug("fonts", "Looking for", key)
286✔
163
         local face = callback(options)
286✔
164
         SILE.fontCache[key] = face
286✔
165
      end
166
      local cached = SILE.fontCache[key]
6,198✔
167
      SILE.font.postLoadHook(cached)
6,198✔
168
      return cached
6,198✔
169
   end,
170

171
   finish = function ()
172
      for key, font in pairs(SILE.fontCache) do
436✔
173
         if font.tempfilename ~= font.filename then
286✔
174
            SU.debug("fonts", "Removing temporary file of", key, ":", font.tempfilename)
×
175
            os.remove(font.tempfilename)
×
176
         end
177
      end
178
   end,
179

180
   postLoadHook = function (face)
181
      local ot = require("core.opentype-parser")
6,198✔
182
      local font = ot.parseFont(face)
6,198✔
183
      if font.cpal then
6,198✔
184
         SILE.require("packages.color-fonts")
×
185
         if SILE.shaper._name ~= "harfbuzzWithColor" then
×
186
            SU.debug("color-fonts", "Switching to color font Shaper")
×
187
            SILE.typesetter:leaveHmode(true)
×
188
            lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
189
         end
190
      end
191
   end,
192

193
   _key = _key,
298✔
194
}
195

196
return font
298✔
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