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

sile-typesetter / sile / 11826759401

13 Nov 2024 10:16PM UTC coverage: 61.013% (-2.4%) from 63.4%
11826759401

push

github

web-flow
Merge pull request #2165 from Omikhleia/feat-bevelled-fractions

feat(math): Support MathML bevelled fractions

7 of 38 new or added lines in 2 files covered. (18.42%)

581 existing lines in 28 files now uncovered.

11155 of 18283 relevant lines covered (61.01%)

2604.98 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")
338✔
4

5
local lastshaper
6

7
SILE.registerCommand("font", function (options, content)
676✔
8
   if SU.ast.hasContent(content) then
1,972✔
9
      SILE.settings:pushState()
231✔
10
   end
11
   if options.filename then
986✔
UNCOV
12
      SILE.settings:set("font.filename", options.filename)
×
13
   end
14
   if options.family then
986✔
15
      SILE.settings:set("font.family", options.family)
718✔
16
      SILE.settings:set("font.filename", "")
718✔
17
   end
18
   if options.size then
986✔
19
      local size = SU.cast("measurement", options.size)
869✔
20
      if not size then
869✔
21
         SU.error("Couldn't parse font size " .. options.size)
×
22
      end
23
      SILE.settings:set("font.size", size:absolute())
1,738✔
24
   end
25
   if options.weight then
986✔
26
      SILE.settings:set("font.weight", 0 + options.weight)
687✔
27
   end
28
   if options.style then
986✔
29
      SILE.settings:set("font.style", options.style)
732✔
30
   end
31
   if options.variant then
986✔
32
      SILE.settings:set("font.variant", options.variant)
661✔
33
   end
34
   if options.features then
986✔
35
      SILE.settings:set("font.features", options.features)
668✔
36
   end
37
   if options.variations then
986✔
38
      SILE.settings:set("font.variations", options.variations)
661✔
39
   end
40
   if options.direction then
986✔
41
      SILE.settings:set("font.direction", options.direction)
663✔
42
   end
43
   if options.language then
986✔
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.languageSupport.loadLanguage(options.language)
682✔
52
      SILE.settings:set("document.language", options.language)
682✔
53
   end
54
   if options.script then
986✔
55
      SILE.settings:set("font.script", options.script)
1,326✔
56
   elseif SILE.settings:get("document.language") then
646✔
57
      local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
646✔
58
      if lang and lang.defaultScript then
323✔
59
         SILE.settings:set("font.script", lang.defaultScript)
1✔
60
      end
61
   end
62
   if options.hyphenchar then
986✔
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))
1,324✔
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)
1,972✔
71

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

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

95
SILE.fontCache = {}
338✔
96

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

111
local font = {
338✔
112

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

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

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

192
   _key = _key,
338✔
193
}
194

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