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

sile-typesetter / sile / 11105020997

30 Sep 2024 11:17AM UTC coverage: 32.811% (-31.0%) from 63.766%
11105020997

push

github

alerque
docs(cli): Copy-edit main description to appease linter *and* read better

5611 of 17101 relevant lines covered (32.81%)

478.6 hits per line

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

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

5
local lastshaper
6

7
SILE.registerCommand("font", function (options, content)
40✔
8
   if SU.ast.hasContent(content) then
22✔
9
      SILE.settings:pushState()
7✔
10
   end
11
   if options.filename then
11✔
12
      SILE.settings:set("font.filename", options.filename)
×
13
   end
14
   if options.family then
11✔
15
      SILE.settings:set("font.family", options.family)
6✔
16
      SILE.settings:set("font.filename", "")
6✔
17
   end
18
   if options.size then
11✔
19
      local size = SU.cast("measurement", options.size)
6✔
20
      if not size then
6✔
21
         SU.error("Couldn't parse font size " .. options.size)
×
22
      end
23
      SILE.settings:set("font.size", size:absolute())
12✔
24
   end
25
   if options.weight then
11✔
26
      SILE.settings:set("font.weight", 0 + options.weight)
×
27
   end
28
   if options.style then
11✔
29
      SILE.settings:set("font.style", options.style)
×
30
   end
31
   if options.variant then
11✔
32
      SILE.settings:set("font.variant", options.variant)
×
33
   end
34
   if options.features then
11✔
35
      SILE.settings:set("font.features", options.features)
×
36
   end
37
   if options.variations then
11✔
38
      SILE.settings:set("font.variations", options.variations)
×
39
   end
40
   if options.direction then
11✔
41
      SILE.settings:set("font.direction", options.direction)
×
42
   end
43
   if options.language then
11✔
44
      if options.language ~= "und" and icu and icu.canonicalize_language then
1✔
45
         local newlang = icu.canonicalize_language(options.language)
1✔
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
1✔
50
      end
51
      SILE.languageSupport.loadLanguage(options.language)
1✔
52
      SILE.settings:set("document.language", options.language)
1✔
53
   end
54
   if options.script then
11✔
55
      SILE.settings:set("font.script", options.script)
×
56
   elseif SILE.settings:get("document.language") then
22✔
57
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
22✔
58
      if lang and lang.defaultScript then
11✔
59
         SILE.settings:set("font.script", lang.defaultScript)
×
60
      end
61
   end
62
   if options.hyphenchar then
11✔
63
      -- must be in the form of, for example, "-" or "U+2010" or "0x2010" (Unicode hex codepoint)
64
      SILE.settings:set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
×
65
   end
66

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

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

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

95
SILE.fontCache = {}
20✔
96

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

111
local font = {
20✔
112

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

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

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

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

192
   _key = _key,
20✔
193
}
194

195
return font
20✔
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