Class: TTFunk::Table::Glyf

Inherits:
TTFunk::Table show all
Defined in:
lib/ttfunk/table/glyf.rb,
lib/ttfunk/table/glyf/simple.rb,
lib/ttfunk/table/glyf/compound.rb,
lib/ttfunk/table/glyf/path_based.rb

Overview

Glyph Data (glyf) table.

Defined Under Namespace

Classes: Compound, PathBased, Simple

Instance Attribute Summary

Attributes inherited from TTFunk::Table

#file, #length, #offset

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TTFunk::Table

#exists?, #initialize, #raw, #tag

Constructor Details

This class inherits a constructor from TTFunk::Table

Class Method Details

.encode(glyphs, new_to_old, old_to_new) ⇒ Hash

Encode table.

Parameters:

  • glyphs (Hash)

    a hash mapping (old) glyph-ids to glyph objects

  • new_to_old (Hash{Integer => Integer})

    a hash mapping new glyph IDs to glyph IDs in the original font.

  • old_to_new (Hash{Integer => Integer})

    a hash mapping old glyph IDs to new glyph IDs.

Returns:

  • (Hash)
    • :table (String) - encoded table.

    • :offsets (Array<Integer>) - glyph offsets in the table.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ttfunk/table/glyf.rb', line 19

def self.encode(glyphs, new_to_old, old_to_new)
  result = { table: +'', offsets: [] }

  new_to_old.keys.sort.each do |new_id|
    glyph = glyphs[new_to_old[new_id]]
    result[:offsets] << result[:table].length
    result[:table] << glyph.recode(old_to_new) if glyph
  end

  # include an offset at the end of the table, for use in computing the
  # size of the last glyph
  result[:offsets] << result[:table].length
  result
end

Instance Method Details

#for(glyph_id) ⇒ TTFunk::Table::Glyf::Simple, ...

Get glyph by ID.

Parameters:

  • glyph_id (Integer)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ttfunk/table/glyf.rb', line 39

def for(glyph_id)
  return @cache[glyph_id] if @cache.key?(glyph_id)

  index = file.glyph_locations.index_of(glyph_id)
  size = file.glyph_locations.size_of(glyph_id)

  if size.zero? # blank glyph, e.g. space character
    @cache[glyph_id] = nil
    return
  end

  parse_from(offset + index) do
    raw = io.read(size)
    number_of_contours = to_signed(raw.unpack1('n'))

    @cache[glyph_id] =
      if number_of_contours == -1
        Compound.new(glyph_id, raw)
      else
        Simple.new(glyph_id, raw)
      end
  end
end