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

sile-typesetter / sile / 12272864087

11 Dec 2024 08:55AM UTC coverage: 29.607% (-41.0%) from 70.614%
12272864087

push

github

web-flow
Merge 95cccf286 into f394f608c

5834 of 19705 relevant lines covered (29.61%)

429.05 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
28✔
9
      SILE.settings:pushState()
7✔
10
   end
11
   if options.filename then
14✔
12
      SILE.settings:set("font.filename", options.filename)
×
13
   end
14
   if options.family then
14✔
15
      SILE.settings:set("font.family", options.family)
9✔
16
      SILE.settings:set("font.filename", "")
9✔
17
   end
18
   if options.size then
14✔
19
      local size = SU.cast("measurement", options.size)
9✔
20
      if not size then
9✔
21
         SU.error("Couldn't parse font size " .. options.size)
×
22
      end
23
      SILE.settings:set("font.size", size:absolute())
18✔
24
   end
25
   if options.weight then
14✔
26
      SILE.settings:set("font.weight", 0 + options.weight)
×
27
   end
28
   if options.style then
14✔
29
      SILE.settings:set("font.style", options.style)
×
30
   end
31
   if options.variant then
14✔
32
      SILE.settings:set("font.variant", options.variant)
×
33
   end
34
   if options.features then
14✔
35
      SILE.settings:set("font.features", options.features)
×
36
   end
37
   if options.variations then
14✔
38
      SILE.settings:set("font.variations", options.variations)
×
39
   end
40
   if options.direction then
14✔
41
      SILE.settings:set("font.direction", options.direction)
×
42
   end
43
   if options.language then
14✔
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
14✔
55
      SILE.settings:set("font.script", options.script)
×
56
   elseif SILE.settings:get("document.language") then
28✔
57
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
28✔
58
      if lang and lang.defaultScript then
14✔
59
         SILE.settings:set("font.script", lang.defaultScript)
×
60
      end
61
   end
62
   if options.hyphenchar then
14✔
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)
28✔
71

72
   if SU.ast.hasContent(content) then
28✔
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)
34✔
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,036✔
99
      options.family,
2,036✔
100
      ("%g"):format(SILE.types.measurement(options.size):tonumber()),
6,108✔
101
      ("%d"):format(options.weight or 0),
2,036✔
102
      options.style,
2,036✔
103
      options.variant,
2,036✔
104
      options.features,
2,036✔
105
      options.variations,
2,036✔
106
      options.direction,
2,036✔
107
      options.filename,
2,036✔
108
   }, ";")
2,036✔
109
end
110

111
local font = {
20✔
112

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

158
   cache = function (options, callback)
159
      local key = _key(options)
323✔
160
      if not SILE.fontCache[key] then
323✔
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]
323✔
166
      SILE.font.postLoadHook(cached)
323✔
167
      return cached
323✔
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")
323✔
181
      local font = ot.parseFont(face)
323✔
182
      if font.cpal then
323✔
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