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

sile-typesetter / sile / 9428435077

08 Jun 2024 11:35AM UTC coverage: 64.56% (-9.9%) from 74.46%
9428435077

push

github

web-flow
Merge pull request #2047 from alerque/end-pars

23 of 46 new or added lines in 5 files covered. (50.0%)

1684 existing lines in 60 files now uncovered.

11145 of 17263 relevant lines covered (64.56%)

4562.45 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")
254✔
4

5
local lastshaper
6

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

73
   if SU.ast.hasContent(content) then
1,410✔
74
      SILE.process(content)
185✔
75
      SILE.settings:popState()
185✔
76
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
185✔
UNCOV
77
         SU.debug("color-fonts", "Switching from color fonts shaper back to previous shaper")
×
UNCOV
78
         SILE.typesetter:leaveHmode(true)
×
UNCOV
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)
959✔
83

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

96
SILE.fontCache = {}
254✔
97

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

112
local font = {
254✔
113

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

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

171
   finish = function ()
172
      for key, font in pairs(SILE.fontCache) do
379✔
173
         if font.tempfilename ~= font.filename then
251✔
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")
5,506✔
182
      local font = ot.parseFont(face)
5,506✔
183
      if font.cpal then
5,506✔
UNCOV
184
         SILE.require("packages.color-fonts")
×
UNCOV
185
         if SILE.shaper._name ~= "harfbuzzWithColor" then
×
UNCOV
186
            SU.debug("color-fonts", "Switching to color font Shaper")
×
UNCOV
187
            SILE.typesetter:leaveHmode(true)
×
UNCOV
188
            lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
189
         end
190
      end
191
   end,
192

193
   _key = _key,
254✔
194
}
195

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