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

sile-typesetter / sile / 6915845768

18 Nov 2023 07:23PM UTC coverage: 63.161% (-5.6%) from 68.751%
6915845768

push

github

web-flow
Merge 0f5c09a66 into f64e235fa

9802 of 15519 relevant lines covered (63.16%)

2045.54 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")
96✔
2

3
local lastshaper
4

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

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

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

75
SILE.fontCache = {}
96✔
76

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

91
local font = {
96✔
92

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

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

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

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