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

sile-typesetter / sile / 6915746301

18 Nov 2023 07:02PM UTC coverage: 56.433% (-12.3%) from 68.751%
6915746301

push

github

web-flow
Merge 8b3fdc301 into f64e235fa

8729 of 15468 relevant lines covered (56.43%)

932.75 hits per line

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

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

3
local lastshaper
4

5
SILE.registerCommand("font", function (options, content)
68✔
6
  if SU.hasContent(content) then SILE.settings:pushState() end
32✔
7
  if options.filename then SILE.settings:set("font.filename", options.filename) end
16✔
8
  if options.family then
16✔
9
    SILE.settings:set("font.family", options.family)
7✔
10
    SILE.settings:set("font.filename", "")
7✔
11
  end
12
  if options.size then
16✔
13
    local size = SU.cast("measurement", options.size)
5✔
14
    if not size then SU.error("Couldn't parse font size "..options.size) end
5✔
15
    SILE.settings:set("font.size", size:absolute())
10✔
16
  end
17
  if options.weight then SILE.settings:set("font.weight", 0+options.weight) end
16✔
18
  if options.style then SILE.settings:set("font.style", options.style) end
16✔
19
  if options.variant then SILE.settings:set("font.variant", options.variant) end
16✔
20
  if options.features then SILE.settings:set("font.features", options.features) end
16✔
21
  if options.variations then SILE.settings:set("font.variations", options.variations) end
16✔
22
  if options.direction then SILE.settings:set("font.direction", options.direction) end
16✔
23
  if options.language then
16✔
24
    if options.language ~= "und" and icu and icu.canonicalize_language then
×
25
      local newlang = icu.canonicalize_language(options.language)
×
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
×
30
    end
31
    SILE.settings:set("document.language", options.language)
×
32
    fluent:set_locale(options.language)
×
33
    SILE.languageSupport.loadLanguage(options.language)
×
34
  end
35
  if options.script then SILE.settings:set("font.script", options.script)
16✔
36
  elseif SILE.settings:get("document.language") then
32✔
37
    local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
32✔
38
    if lang and lang.defaultScript then
16✔
39
      SILE.settings:set("font.script", lang.defaultScript)
×
40
    end
41
  end
42
  if options.hyphenchar then
16✔
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))
×
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)
32✔
51

52
  if SU.hasContent(content) then
32✔
53
    SILE.process(content)
12✔
54
    SILE.settings:popState()
12✔
55
    if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
12✔
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)
50✔
62

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

75
SILE.fontCache = {}
34✔
76

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

91
local font = {
34✔
92

93
  loadDefaults = function (options)
94
    if not options.family then options.family = SILE.settings:get("font.family") end
2,530✔
95
    if not options.size then options.size = SILE.settings:get("font.size") end
2,530✔
96
    if not options.weight then options.weight = SILE.settings:get("font.weight") end
2,548✔
97
    if not options.style then options.style = SILE.settings:get("font.style") end
2,548✔
98
    if not options.variant then options.variant = SILE.settings:get("font.variant") end
3,770✔
99
    if SILE.settings:get("font.filename") ~= "" then
3,770✔
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
3,752✔
104
    if not options.script then options.script = SILE.settings:get("font.script") end
3,770✔
105
    if not options.direction then
1,885✔
106
      options.direction = SILE.settings:get("font.direction")
3,770✔
107
      if not options.direction or options.direction == "" then
1,885✔
108
        options.direction = SILE.typesetter and SILE.typesetter.frame and SILE.typesetter.frame:writingDirection() or "LTR"
3,748✔
109
      end
110
    end
111
    if not options.features then options.features = SILE.settings:get("font.features") end
3,770✔
112
    if not options.variations then options.variations = SILE.settings:get("font.variations") end
3,770✔
113
    if not options.hyphenchar then options.hyphenchar = SILE.settings:get("font.hyphenchar") end
3,770✔
114
    return options
1,885✔
115
  end,
116

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

129
  finish = function ()
130
    for key, font in pairs(SILE.fontCache) do
62✔
131
      if font.tempfilename ~= font.filename then
44✔
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")
1,583✔
140
    local font = ot.parseFont(face)
1,583✔
141
    if font.cpal then
1,583✔
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
34✔
152
}
153

154
return font
34✔
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