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

sile-typesetter / sile / 7232859119

16 Dec 2023 03:49PM UTC coverage: 66.878% (-7.7%) from 74.62%
7232859119

push

github

web-flow
Merge 05d75c2a3 into 8686730e4

0 of 4 new or added lines in 1 file covered. (0.0%)

1201 existing lines in 56 files now uncovered.

10550 of 15775 relevant lines covered (66.88%)

3347.52 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")
71✔
2

3
local lastshaper
4

5
SILE.registerCommand("font", function (options, content)
142✔
6
  if SU.hasContent(content) then SILE.settings:pushState() end
886✔
7
  if options.filename then SILE.settings:set("font.filename", options.filename) end
443✔
8
  if options.family then
443✔
9
    SILE.settings:set("font.family", options.family)
375✔
10
    SILE.settings:set("font.filename", "")
375✔
11
  end
12
  if options.size then
443✔
13
    local size = SU.cast("measurement", options.size)
421✔
14
    if not size then SU.error("Couldn't parse font size "..options.size) end
421✔
15
    SILE.settings:set("font.size", size:absolute())
842✔
16
  end
17
  if options.weight then SILE.settings:set("font.weight", 0+options.weight) end
443✔
18
  if options.style then SILE.settings:set("font.style", options.style) end
443✔
19
  if options.variant then SILE.settings:set("font.variant", options.variant) end
443✔
20
  if options.features then SILE.settings:set("font.features", options.features) end
443✔
21
  if options.variations then SILE.settings:set("font.variations", options.variations) end
443✔
22
  if options.direction then SILE.settings:set("font.direction", options.direction) end
443✔
23
  if options.language then
443✔
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)
798✔
36
  elseif SILE.settings:get("document.language") then
176✔
37
    local lang = SILE.languageSupport.languages[SILE.settings:get("document.language")]
176✔
38
    if lang and lang.defaultScript then
88✔
39
      SILE.settings:set("font.script", lang.defaultScript)
1✔
40
    end
41
  end
42
  if options.hyphenchar then
443✔
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)
886✔
51

52
  if SU.hasContent(content) then
886✔
53
    SILE.process(content)
63✔
54
    SILE.settings:popState()
63✔
55
    if SILE.shaper._name == "harfbuzzWithColor" and lastshaper then
63✔
UNCOV
56
      SU.debug("color-fonts", "Switching from color fonts shaper back to previous shaper")
×
UNCOV
57
      SILE.typesetter:leaveHmode(true)
×
UNCOV
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)
514✔
62

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

75
SILE.fontCache = {}
71✔
76

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

91
local font = {
71✔
92

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

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

129
  finish = function ()
130
    for key, font in pairs(SILE.fontCache) do
216✔
131
      if font.tempfilename ~= font.filename then
145✔
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,607✔
140
    local font = ot.parseFont(face)
3,607✔
141
    if font.cpal then
3,607✔
UNCOV
142
      SILE.require("packages.color-fonts")
×
UNCOV
143
      if SILE.shaper._name ~= "harfbuzzWithColor" then
×
UNCOV
144
        SU.debug("color-fonts", "Switching to color font Shaper")
×
UNCOV
145
        SILE.typesetter:leaveHmode(true)
×
UNCOV
146
        lastshaper, SILE.shaper = SILE.shaper, SILE.shapers.harfbuzzWithColor()
×
147
      end
148
    end
149
  end,
150

151
  _key = _key
71✔
152
}
153

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