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

sile-typesetter / sile / 8921272340

02 May 2024 09:00AM UTC coverage: 66.229% (+2.5%) from 63.747%
8921272340

push

github

web-flow
chore(deps): Bump @commitlint/cli from 18.6.1 to 19.3.0 (#2016)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

10531 of 15901 relevant lines covered (66.23%)

3880.04 hits per line

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

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

3
local lastshaper
4

5
SILE.registerCommand("font", function (options, content)
170✔
6
  if SU.hasContent(content) then SILE.settings:pushState() end
990✔
7
  if options.filename then SILE.settings:set("font.filename", options.filename) end
495✔
8
  if options.family then
495✔
9
    SILE.settings:set("font.family", options.family)
388✔
10
    SILE.settings:set("font.filename", "")
388✔
11
  end
12
  if options.size then
495✔
13
    local size = SU.cast("measurement", options.size)
453✔
14
    if not size then SU.error("Couldn't parse font size "..options.size) end
453✔
15
    SILE.settings:set("font.size", size:absolute())
906✔
16
  end
17
  if options.weight then SILE.settings:set("font.weight", 0+options.weight) end
495✔
18
  if options.style then SILE.settings:set("font.style", options.style) end
495✔
19
  if options.variant then SILE.settings:set("font.variant", options.variant) end
495✔
20
  if options.features then SILE.settings:set("font.features", options.features) end
495✔
21
  if options.variations then SILE.settings:set("font.variations", options.variations) end
495✔
22
  if options.direction then SILE.settings:set("font.direction", options.direction) end
495✔
23
  if options.language then
495✔
24
    if options.language ~= "und" and icu and icu.canonicalize_language then
361✔
25
      local newlang = icu.canonicalize_language(options.language)
361✔
26
      -- if newlang ~= options.language then
27
        -- SU.warn("Language '"..options.language.."' not canonical, '"..newlang.."' will be used instead.")
28
      -- end
29
      options.language = newlang
361✔
30
    end
31
    SILE.settings:set("document.language", options.language)
361✔
32
    fluent:set_locale(options.language)
361✔
33
    SILE.languageSupport.loadLanguage(options.language)
361✔
34
  end
35
  if options.script then SILE.settings:set("font.script", options.script)
850✔
36
  elseif SILE.settings:get("document.language") then
280✔
37
    local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
280✔
38
    if lang and lang.defaultScript then
140✔
39
      SILE.settings:set("font.script", lang.defaultScript)
1✔
40
    end
41
  end
42
  if options.hyphenchar then
495✔
43
    -- must be in the form of, for example, "-" or "U+2010" or "0x2010" (Unicode hex codepoint)
44
    SILE.settings:set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
710✔
45
  end
46

47
  -- We must *actually* load the font here, because by the time we're inside
48
  -- SILE.shaper.shapeToken, it's too late to respond appropriately to things
49
  -- that the post-load hook might want to do.
50
  SILE.font.cache(SILE.font.loadDefaults({}), SILE.shaper.getFace)
990✔
51

52
  if SU.hasContent(content) then
990✔
53
    SILE.process(content)
94✔
54
    SILE.settings:popState()
94✔
55
    if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
94✔
56
      SU.debug("color-fonts", "Switching from color fonts shaper back to previous shaper")
×
57
      SILE.typesetter:leaveHmode(true)
×
58
      lastshaper, SILE.shaper = nil, lastshaper
×
59
    end
60
  end
61
end, "Set current font family, size, weight, style, variant, script, direction and language", nil, true)
580✔
62

63
SILE.settings:declare({ parameter = "font.family", type = "string or nil", default = "Gentium Plus" })
85✔
64
SILE.settings:declare({ parameter = "font.size", type = "number or integer", default = 10 })
85✔
65
SILE.settings:declare({ parameter = "font.weight", type = "integer", default = 400 })
85✔
66
SILE.settings:declare({ parameter = "font.variant", type = "string", default = "normal" })
85✔
67
SILE.settings:declare({ parameter = "font.script", type = "string", default = "" })
85✔
68
SILE.settings:declare({ parameter = "font.style", type = "string", default = "" })
85✔
69
SILE.settings:declare({ parameter = "font.direction", type = "string", default = "" })
85✔
70
SILE.settings:declare({ parameter = "font.filename", type = "string or nil", default = "" })
85✔
71
SILE.settings:declare({ parameter = "font.features", type = "string", default = "" })
85✔
72
SILE.settings:declare({ parameter = "font.variations", type = "string", default = "" })
85✔
73
SILE.settings:declare({ parameter = "font.hyphenchar", type = "string", default = "-" })
85✔
74

75
SILE.fontCache = {}
85✔
76

77
local _key = function (options)
78
  return table.concat({
23,445✔
79
      options.family,
23,445✔
80
      ("%g"):format(SILE.measurement(options.size):tonumber()),
70,335✔
81
      ("%d"):format(options.weight or 0),
23,445✔
82
      options.style,
23,445✔
83
      options.variant,
23,445✔
84
      options.features,
23,445✔
85
      options.variations,
23,445✔
86
      options.direction,
23,445✔
87
      options.filename,
23,445✔
88
    }, ";")
23,445✔
89
end
90

91
local font = {
85✔
92

93
  loadDefaults = function (options)
94
    if not options.family then options.family = SILE.settings:get("font.family") end
7,410✔
95
    if not options.size then options.size = SILE.settings:get("font.size") end
7,410✔
96
    if not options.weight then options.weight = SILE.settings:get("font.weight") end
7,495✔
97
    if not options.style then options.style = SILE.settings:get("font.style") end
7,495✔
98
    if not options.variant then options.variant = SILE.settings:get("font.variant") end
9,244✔
99
    if SILE.settings:get("font.filename") ~= "" then
9,244✔
100
      options.filename = SILE.settings:get("font.filename")
×
101
      options.family = ""
×
102
    end
103
    if not options.language then options.language = SILE.settings:get("document.language") end
9,159✔
104
    if not options.script then options.script = SILE.settings:get("font.script") end
9,244✔
105
    if not options.direction then
4,622✔
106
      options.direction = SILE.settings:get("font.direction")
9,244✔
107
      if not options.direction or options.direction == "" then
4,622✔
108
        options.direction = SILE.typesetter and SILE.typesetter.frame and SILE.typesetter.frame:writingDirection() or "LTR"
8,427✔
109
      end
110
    end
111
    if not options.features then options.features = SILE.settings:get("font.features") end
9,244✔
112
    if not options.variations then options.variations = SILE.settings:get("font.variations") end
9,244✔
113
    if not options.hyphenchar then options.hyphenchar = SILE.settings:get("font.hyphenchar") end
9,244✔
114
    return options
4,622✔
115
  end,
116

117
  cache = function (options, callback)
118
    local key = _key(options)
3,832✔
119
    if not SILE.fontCache[key] then
3,832✔
120
      SU.debug("fonts", "Looking for", key)
173✔
121
      local face = callback(options)
173✔
122
      SILE.fontCache[key] = face
173✔
123
    end
124
    local cached = SILE.fontCache[key]
3,832✔
125
    SILE.font.postLoadHook(cached)
3,832✔
126
    return cached
3,832✔
127
  end,
128

129
  finish = function ()
130
    for key, font in pairs(SILE.fontCache) do
258✔
131
      if font.tempfilename ~= font.filename then
173✔
132
        SU.debug("fonts", "Removing temporary file of", key, ":", font.tempfilename)
×
133
        os.remove(font.tempfilename)
×
134
      end
135
    end
136
  end,
137

138
  postLoadHook = function(face)
139
    local ot = require("core.opentype-parser")
3,832✔
140
    local font = ot.parseFont(face)
3,832✔
141
    if font.cpal then
3,832✔
142
      SILE.require("packages.color-fonts")
×
143
      if SILE.shaper._name ~= "harfbuzzWithColor" then
×
144
        SU.debug("color-fonts", "Switching to color font Shaper")
×
145
        SILE.typesetter:leaveHmode(true)
×
146
        lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
147
      end
148
    end
149
  end,
150

151
  _key = _key
85✔
152
}
153

154
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