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

sile-typesetter / sile / 6713098919

31 Oct 2023 10:21PM UTC coverage: 52.831% (-21.8%) from 74.636%
6713098919

push

github

web-flow
Merge d0a2a1ee9 into b185d4972

45 of 45 new or added lines in 3 files covered. (100.0%)

8173 of 15470 relevant lines covered (52.83%)

6562.28 hits per line

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

0.0
/packages/color-fonts/init.lua
1
local base = require("packages.base")
×
2

3
local package = pl.class(base)
×
4
package._name = "color-fonts"
×
5

6
function package:_init ()
×
7
  base._init(self)
×
8

9
  local harfbuzz = require("shapers.harfbuzz")
×
10

11
  SILE.shapers.harfbuzzWithColor = pl.class(harfbuzz)
×
12
  SILE.shapers.harfbuzzWithColor._name = "harfbuzzWithColor"
×
13

14
  function SILE.shapers.harfbuzzWithColor.shapeToken (self_, str, options)
×
15
    local ot = require("core.opentype-parser")
×
16
    if not options.family then return {} end
×
17
    local face = SILE.font.cache(options, SILE.shaper.getFace)
×
18
    local font = ot.parseFont(face)
×
19
    local items = self_._base.shapeToken(self_, str, options)
×
20
    if font.colr and font.cpal then
×
21
      local newItems = {}
×
22
      for i = 1, #items do
×
23
        local layers = font.colr[items[i].gid]
×
24
        if layers then
×
25
          for j = 1, #layers do
×
26
            local item = items[i]
×
27
            local layer = layers[j]
×
28
            local width = 0
×
29
            local height = 0
×
30
            local text = ""
×
31
            if j == #layers then
×
32
              width = item.width
×
33
              height = item.height
×
34
              text = item.text
×
35
            end
36
            -- XXX: handle multiple palette, add a font option?
37
            local color = font.cpal[1][layer.paletteIndex]
×
38
            local newItem = {
×
39
              gid = layer.gid,
40
              glyphAdvance = item.glyphAdvance,
41
              width = width,
42
              height = height,
43
              depth = item.depth,
44
              index = item.index,
45
              x_offset = item.x_offset,
46
              y_offset = item.y_offset,
47
              text = text,
48
              color = color,
49
            }
50
            newItems[#newItems+1] = newItem
×
51
          end
52
        else
53
          newItems[#newItems+1] = items[i]
×
54
        end
55
      end
56
      return newItems
×
57
    end
58
    return items
×
59
  end
60

61
  function SILE.shapers.harfbuzzWithColor.createNnodes (self_, token, options)
×
62
    local items, _ = self_:shapeToken(token, options)
×
63
    if #items < 1 then return {} end
×
64
    local lang = options.language
×
65
    SILE.languageSupport.loadLanguage(lang)
×
66
    local nodeMaker = SILE.nodeMakers[lang] or SILE.nodeMakers.unicode
×
67
    local run = { [1] = { slice = {}, color = items[1].color, chunk = "" } }
×
68
    for i = 1, #items do
×
69
      if items[i].color ~= run[#run].color then
×
70
        run[#run+1] = { slice = {}, chunk = "", color = items[i].color }
×
71
        if i <#items then
×
72
          run[#run].color = items[i].color
×
73
        end
74
      end
75
      run[#run].chunk = run[#run].chunk .. items[i].text
×
76
      run[#run].slice[#(run[#run].slice)+1] = items[i]
×
77
    end
78
    local nodes = {}
×
79
    for i=1, #run do
×
80
      options = pl.tablex.deepcopy(options)
×
81
      if run[i].color then
×
82
        nodes[#nodes+1] = SILE.nodefactory.hbox({
×
83
          outputYourself = function () SILE.outputter:pushColor(run[i].color) end
×
84
        })
85
      end
86
      for node in nodeMaker(options):iterator(run[i].slice, run[i].chunk) do
×
87
        nodes[#nodes+1] = node
×
88
      end
89
      if run[i].color then
×
90
        nodes[#nodes+1] = SILE.nodefactory.hbox({
×
91
          outputYourself = function () SILE.outputter:popColor() end
×
92
        })
93
      end
94
    end
95
    return nodes
×
96
  end
97

98
end
99

100
package.documentation = [[
101
\begin{document}
102
The \autodoc:package{color-fonts} package adds support for fonts with multi-colored glyphs (that is,
103
OpenType fonts with \code{COLR} and \code{CPAL} tables).
104
This package is automatically loaded when such a font is detected.
105
\end{document}
106
]]
×
107

108
return package
×
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

© 2025 Coveralls, Inc