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

sile-typesetter / sile / 10618011310

29 Aug 2024 03:10PM UTC coverage: 61.03% (-0.7%) from 61.736%
10618011310

push

github

alerque
chore(release): 0.15.5

10640 of 17434 relevant lines covered (61.03%)

1775.46 hits per line

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

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

5
local lastshaper
6

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

72
   if SU.ast.hasContent(content) then
804✔
73
      SILE.process(content)
28✔
74
      SILE.settings:popState()
28✔
75
      if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
28✔
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)
487✔
82

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

95
SILE.fontCache = {}
85✔
96

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

111
local font = {
85✔
112

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

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

170
   finish = function ()
171
      for key, font in pairs(SILE.fontCache) do
144✔
172
         if font.tempfilename ~= font.filename then
100✔
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")
2,809✔
181
      local font = ot.parseFont(face)
2,809✔
182
      if font.cpal then
2,809✔
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,
85✔
193
}
194

195
return font
85✔
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