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

sile-typesetter / sile / 9409557472

07 Jun 2024 12:09AM UTC coverage: 69.448% (-4.5%) from 73.988%
9409557472

push

github

alerque
fix(build): Distribute vendored compat-5.3.c source file

12025 of 17315 relevant lines covered (69.45%)

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

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

108
package.documentation = [[
109
\begin{document}
110
The \autodoc:package{color-fonts} package adds support for fonts with multi-colored glyphs (that is,
111
OpenType fonts with \code{COLR} and \code{CPAL} tables).
112
This package is automatically loaded when such a font is detected.
113
\end{document}
114
]]
×
115

116
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