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

sile-typesetter / sile / 9322841962

31 May 2024 06:41PM UTC coverage: 47.763% (-8.9%) from 56.674%
9322841962

push

github

web-flow
Merge 71aa6f2a1 into bb89a7e79

8081 of 16919 relevant lines covered (47.76%)

246.83 hits per line

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

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

5
local lastshaper
6

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

73
   if SU.ast.hasContent(content) then
12✔
74
      SILE.process(content)
5✔
75
      SILE.settings:popState()
5✔
76
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
5✔
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)
13✔
83

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

96
SILE.fontCache = {}
7✔
97

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

112
local font = {
7✔
113

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

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

171
   finish = function ()
172
      for key, font in pairs(SILE.fontCache) do
17✔
173
         if font.tempfilename ~= font.filename then
12✔
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")
440✔
182
      local font = ot.parseFont(face)
440✔
183
      if font.cpal then
440✔
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,
7✔
194
}
195

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