Class: Pdfrb::Font::CFF::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/font/cff/index.rb

Overview

CFF INDEX structure (TN5176 s5): count (Card16), offSize (OffSize), offsets (count+1 × offSize bytes, 1-based), then the data items.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Index

Returns a new instance of Index.



38
39
40
# File 'lib/pdfrb/font/cff/index.rb', line 38

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



10
11
12
# File 'lib/pdfrb/font/cff/index.rb', line 10

def items
  @items
end

Class Method Details

.parse(data, offset) ⇒ Object

Parse an INDEX at offset in data; returns [index, next_offset].



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pdfrb/font/cff/index.rb', line 14

def self.parse(data, offset)
  count = data.byteslice(offset, 2).unpack1("n")
  return [new([]), offset + 2] if count.zero?

  off_size = data.getbyte(offset + 2)
  offsets_base = offset + 3
  offsets = (0..count).map do |i|
    read_off(data, offsets_base + (i * off_size), off_size)
  end
  data_base = offsets_base + ((count + 1) * off_size)
  items = (0...count).map do |i|
    start = data_base + offsets[i] - 1
    len = offsets[i + 1] - offsets[i]
    data.byteslice(start, len)
  end
  [new(items), data_base + offsets[count] - 1]
end

.read_off(data, pos, size) ⇒ Object



32
33
34
35
36
# File 'lib/pdfrb/font/cff/index.rb', line 32

def self.read_off(data, pos, size)
  value = 0
  size.times { |i| value = (value * 256) + data.getbyte(pos + i) }
  value
end

Instance Method Details

#[](i) ⇒ Object



50
51
52
# File 'lib/pdfrb/font/cff/index.rb', line 50

def [](i)
  @items[i]
end

#eachObject



42
43
44
# File 'lib/pdfrb/font/cff/index.rb', line 42

def each(&)
  @items.each(&)
end

#serializeObject

Serialize back to INDEX bytes.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pdfrb/font/cff/index.rb', line 55

def serialize
  return [0].pack("n") if @items.empty?

  offsets = [1]
  @items.each { |item| offsets << (offsets.last + item.bytesize) }
  max = offsets.last
  off_size = if max < 0x100
               1
             else
               (if max < 0x10000
                  2
                else
                  (max < 0x1000000 ? 3 : 4)
                end)
             end

  buf = +"".b
  buf << [@items.size].pack("n")
  buf << off_size.chr
  offsets.each do |o|
    off_size.downto(1) { |i| buf << ((o >> (8 * (i - 1))) & 0xFF).chr }
  end
  @items.each { |item| buf << item.b }
  buf
end

#sizeObject



46
47
48
# File 'lib/pdfrb/font/cff/index.rb', line 46

def size
  @items.size
end