Class: TTFunk::Table::Cff::Index

Inherits:
SubTable
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ttfunk/table/cff/index.rb

Overview

CFF Index.

Direct Known Subclasses

CharstringsIndex, FontIndex, SubrIndex, TopIndex

Instance Attribute Summary

Attributes inherited from SubTable

#file, #length, #table_offset

Instance Method Summary collapse

Methods inherited from SubTable

#eot?, #initialize, #read

Constructor Details

This class inherits a constructor from TTFunk::SubTable

Instance Method Details

#[](index) ⇒ any

Get value by index.

Parameters:

  • index (Integer)

Returns:

  • (any)


14
15
16
17
18
19
20
21
22
23
# File 'lib/ttfunk/table/cff/index.rb', line 14

def [](index)
  return if index >= items_count

  entry_cache[index] ||=
    decode_item(
      index,
      data_reference_offset + offsets[index],
      offsets[index + 1] - offsets[index],
    )
end

#each {|item| ... } ⇒ void #eachEnumerator

Iterate over index items.

Overloads:

  • #each {|item| ... } ⇒ void

    This method returns an undefined value.

    Yield Parameters:

    • item (any)
  • #eachEnumerator

    Returns:

    • (Enumerator)


32
33
34
35
36
37
38
# File 'lib/ttfunk/table/cff/index.rb', line 32

def each(&block)
  return to_enum(__method__) unless block

  items_count.times do |i|
    yield(self[i])
  end
end

#encode(*args) ⇒ TTFunk::EncodedString

Encode index.

Parameters:

  • args

    all arguments are passed to ‘encode_item` method.

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ttfunk/table/cff/index.rb', line 51

def encode(*args)
  new_items = encode_items(*args)

  if new_items.empty?
    return [0].pack('n')
  end

  if new_items.length > 0xffff
    raise Error, 'Too many items in a CFF index'
  end

  offsets_array =
    new_items
      .each_with_object([1]) { |item, offsets|
        offsets << (offsets.last + item.length)
      }

  offset_size = (offsets_array.last.bit_length / 8.0).ceil

  offsets_array.map! { |offset| encode_offset(offset, offset_size) }

  EncodedString.new.concat(
    [new_items.length, offset_size].pack('nC'),
    *offsets_array,
    *new_items,
  )
end

#items_countInteger

Numer of items in this index.

Returns:

  • (Integer)


43
44
45
# File 'lib/ttfunk/table/cff/index.rb', line 43

def items_count
  items.length
end