Module: Skrift::Color::COLR

Defined in:
lib/skrift/color/colr.rb

Overview

Parses a COLR (v0) table into a map of

base glyph id => [[layer glyph id, palette index], ...]   (bottom to top)

Returns nil if the font has no COLR table or a version we don’t support.

Class Method Summary collapse

Class Method Details

.parse(font) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/skrift/color/colr.rb', line 11

def parse(font)
  off = font.tables["COLR"]
  return nil unless off
  return nil unless font.getu16(off).zero? # v0 only

  num_base  = font.getu16(off + 2)
  base_off  = off + font.getu32(off + 4)
  layer_off = off + font.getu32(off + 8)

  layers = {}
  num_base.times do |i|
    rec   = base_off + i * 6
    gid   = font.getu16(rec)
    first = font.getu16(rec + 2)
    count = font.getu16(rec + 4)
    layers[gid] = count.times.map do |j|
      lr = layer_off + (first + j) * 4
      [font.getu16(lr), font.getu16(lr + 2)]
    end
  end
  layers
end