Class: Fontisan::Tables::Cff2::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fontisan/tables/cff2/index.rb

Overview

Reads a CFF2 INDEX structure.

A CFF2 INDEX uses a 4-byte count (Card32) instead of the 2-byte count (Card16) used by CFF1. Otherwise the layout is the same: count, offSize, offset array (1-based), then data.

An empty INDEX is just the 4-byte count field (= 0).

Examples:

idx = Cff2::Index.read(raw_cff2_bytes, charstrings_offset)
idx[0]  # first item
idx.count
idx.total_size

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, start_offset = 0) ⇒ Index

Returns a new instance of Index.

Parameters:

  • io (IO, StringIO)

    positioned at the INDEX start

  • start_offset (Integer) (defaults to: 0)

    byte offset (for diagnostics)



47
48
49
50
51
# File 'lib/fontisan/tables/cff2/index.rb', line 47

def initialize(io, start_offset = 0)
  @io = io
  @start_offset = start_offset
  parse!
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



24
25
26
# File 'lib/fontisan/tables/cff2/index.rb', line 24

def count
  @count
end

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/fontisan/tables/cff2/index.rb', line 24

def data
  @data
end

#off_sizeObject (readonly)

Returns the value of attribute off_size.



24
25
26
# File 'lib/fontisan/tables/cff2/index.rb', line 24

def off_size
  @off_size
end

#offsetsObject (readonly)

Returns the value of attribute offsets.



24
25
26
# File 'lib/fontisan/tables/cff2/index.rb', line 24

def offsets
  @offsets
end

#start_offsetObject (readonly)

Returns the value of attribute start_offset.



24
25
26
# File 'lib/fontisan/tables/cff2/index.rb', line 24

def start_offset
  @start_offset
end

Class Method Details

.build(items) ⇒ String

Build an INDEX from a list of data items. Delegates to Fontisan::Tables::Cff2::IndexBuilder.

Parameters:

  • items (Array<String>)

Returns:

  • (String)

    binary INDEX bytes



41
42
43
# File 'lib/fontisan/tables/cff2/index.rb', line 41

def self.build(items)
  IndexBuilder.build(items)
end

.read(raw_data, offset) ⇒ Object

Read a CFF2 INDEX at offset within raw_data.

Parameters:

  • raw_data (String)

    full CFF2 table bytes

  • offset (Integer)

    byte offset of the INDEX



30
31
32
33
34
# File 'lib/fontisan/tables/cff2/index.rb', line 30

def self.read(raw_data, offset)
  io = StringIO.new(raw_data)
  io.seek(offset)
  new(io, offset)
end

Instance Method Details

#[](index) ⇒ String?

Fetch the item at index (0-based).

Returns:

  • (String, nil)

    binary data, or nil if out of range



56
57
58
59
60
61
62
63
# File 'lib/fontisan/tables/cff2/index.rb', line 56

def [](index)
  return nil if index.negative? || index >= count
  return "".b if count.zero?

  start_pos = offsets[index] - 1
  end_pos = offsets[index + 1] - 1
  data[start_pos, end_pos - start_pos]
end

#eachObject

Iterate over all items.



66
67
68
69
70
# File 'lib/fontisan/tables/cff2/index.rb', line 66

def each
  return enum_for(:each) unless block_given?

  count.times { |i| yield self[i] }
end

#empty?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/fontisan/tables/cff2/index.rb', line 77

def empty?
  count.zero?
end

#to_aObject

All items as an array.



73
74
75
# File 'lib/fontisan/tables/cff2/index.rb', line 73

def to_a
  Array.new(count) { |i| self[i] }
end

#total_sizeObject

Total size of the INDEX in bytes (count + offSize + offsets + data).



82
83
84
85
86
# File 'lib/fontisan/tables/cff2/index.rb', line 82

def total_size
  return 4 if count.zero?

  4 + 1 + ((count + 1) * off_size) + data.bytesize
end