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

sile-typesetter / sile / 9432254829

08 Jun 2024 11:20PM UTC coverage: 60.675% (+3.5%) from 57.223%
9432254829

push

github

alerque
fix(build): Bundle all assets in source distribution

...even when configured for doing a static binary build with embedded
assets. They don't get installed, but they should be in the dist file in
case the good folks building want to configure it a different way.

10441 of 17208 relevant lines covered (60.68%)

1913.98 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")
90✔
4

5
local lastshaper
6

7
SILE.registerCommand("font", function (options, content)
180✔
8
   if SU.ast.hasContent(content) then
816✔
9
      SILE.settings:pushState()
33✔
10
   end
11
   if options.filename then
408✔
12
      SILE.settings:set("font.filename", options.filename)
×
13
   end
14
   if options.family then
408✔
15
      SILE.settings:set("font.family", options.family)
371✔
16
      SILE.settings:set("font.filename", "")
371✔
17
   end
18
   if options.size then
408✔
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
408✔
26
      SILE.settings:set("font.weight", 0 + options.weight)
357✔
27
   end
28
   if options.style then
408✔
29
      SILE.settings:set("font.style", options.style)
361✔
30
   end
31
   if options.variant then
408✔
32
      SILE.settings:set("font.variant", options.variant)
354✔
33
   end
34
   if options.features then
408✔
35
      SILE.settings:set("font.features", options.features)
354✔
36
   end
37
   if options.variations then
408✔
38
      SILE.settings:set("font.variations", options.variations)
354✔
39
   end
40
   if options.direction then
408✔
41
      SILE.settings:set("font.direction", options.direction)
354✔
42
   end
43
   if options.language then
408✔
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.settings:set("document.language", options.language)
358✔
52
      fluent:set_locale(options.language)
358✔
53
      SILE.languageSupport.loadLanguage(options.language)
358✔
54
   end
55
   if options.script then
408✔
56
      SILE.settings:set("font.script", options.script)
710✔
57
   elseif SILE.settings:get("document.language") then
106✔
58
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
106✔
59
      if lang and lang.defaultScript then
53✔
60
         SILE.settings:set("font.script", lang.defaultScript)
1✔
61
      end
62
   end
63
   if options.hyphenchar then
408✔
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))
710✔
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)
816✔
72

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

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

96
SILE.fontCache = {}
90✔
97

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

112
local font = {
90✔
113

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

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

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

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

© 2025 Coveralls, Inc