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

sile-typesetter / sile / 9414689691

07 Jun 2024 09:15AM UTC coverage: 71.828% (+2.4%) from 69.448%
9414689691

push

github

alerque
chore(core): Update deprecated usage of \rightalign with \raggedleft

1 of 1 new or added line in 1 file covered. (100.0%)

137 existing lines in 12 files now uncovered.

12445 of 17326 relevant lines covered (71.83%)

6664.76 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")
327✔
4

5
local lastshaper
6

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

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

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

96
SILE.fontCache = {}
327✔
97

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

112
local font = {
327✔
113

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

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

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

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