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

Defined Under Namespace

Classes: Compound, 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, new2old, old2new) ⇒ Object

Accepts a hash mapping (old) glyph-ids to glyph objects, and a hash mapping old glyph-ids to new glyph-ids.

Returns a hash containing:

  • :table - a string representing the encoded ‘glyf’ table containing the given glyphs.

  • :offsets - an array of offsets for each glyph



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ttfunk/table/glyf.rb', line 14

def self.encode(glyphs, new2old, old2new)
  result = { :table => "", :offsets => [] }

  new2old.keys.sort.each do |new_id|
    glyph = glyphs[new2old[new_id]]
    result[:offsets] << result[:table].length
    result[:table] << glyph.recode(old2new) 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
  return result
end

Instance Method Details

#for(glyph_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ttfunk/table/glyf.rb', line 29

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 nil
  end

  parse_from(offset + index) do
    raw = io.read(size)
    number_of_contours, x_min, y_min, x_max, y_max = raw.unpack("n5").map { |i| to_signed(i) }

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