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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

97.6
/core/font.lua
1
local icu = require("justenoughicu")
181✔
2

3
local lastshaper
4

5
SILE.registerCommand("font", function (options, content)
362✔
6
   if SU.hasContent(content) then
1,940✔
7
      SILE.settings:pushState()
211✔
8
   end
9
   if options.filename then
970✔
10
      SILE.settings:set("font.filename", options.filename)
4✔
11
   end
12
   if options.family then
970✔
13
      SILE.settings:set("font.family", options.family)
724✔
14
      SILE.settings:set("font.filename", "")
724✔
15
   end
16
   if options.size then
970✔
17
      local size = SU.cast("measurement", options.size)
867✔
18
      if not size then
867✔
NEW
19
         SU.error("Couldn't parse font size " .. options.size)
×
20
      end
21
      SILE.settings:set("font.size", size:absolute())
1,734✔
22
   end
23
   if options.weight then
970✔
24
      SILE.settings:set("font.weight", 0 + options.weight)
688✔
25
   end
26
   if options.style then
970✔
27
      SILE.settings:set("font.style", options.style)
704✔
28
   end
29
   if options.variant then
970✔
30
      SILE.settings:set("font.variant", options.variant)
661✔
31
   end
32
   if options.features then
970✔
33
      SILE.settings:set("font.features", options.features)
668✔
34
   end
35
   if options.variations then
970✔
36
      SILE.settings:set("font.variations", options.variations)
661✔
37
   end
38
   if options.direction then
970✔
39
      SILE.settings:set("font.direction", options.direction)
666✔
40
   end
41
   if options.language then
970✔
42
      if options.language ~= "und" and icu and icu.canonicalize_language then
687✔
43
         local newlang = icu.canonicalize_language(options.language)
687✔
44
         -- if newlang ~= options.language then
45
         -- SU.warn("Language '"..options.language.."' not canonical, '"..newlang.."' will be used instead.")
46
         -- end
47
         options.language = newlang
687✔
48
      end
49
      SILE.settings:set("document.language", options.language)
687✔
50
      fluent:set_locale(options.language)
687✔
51
      SILE.languageSupport.loadLanguage(options.language)
687✔
52
   end
53
   if options.script then
970✔
54
      SILE.settings:set("font.script", options.script)
1,326✔
55
   elseif SILE.settings:get("document.language") then
614✔
56
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
614✔
57
      if lang and lang.defaultScript then
307✔
58
         SILE.settings:set("font.script", lang.defaultScript)
1✔
59
      end
60
   end
61
   if options.hyphenchar then
970✔
62
      -- must be in the form of, for example, "-" or "U+2010" or "0x2010" (Unicode hex codepoint)
63
      SILE.settings:set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
1,324✔
64
   end
65

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

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

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

94
SILE.fontCache = {}
181✔
95

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

110
local font = {
181✔
111

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

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

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

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

191
   _key = _key,
181✔
192
}
193

194
return font
181✔
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