Class: TTFunk::Table::Cff::FdSelector

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

Overview

CFF FDSelect.

Constant Summary collapse

ARRAY_FORMAT =

Array format.

0
RANGE_FORMAT =

Range format.

3
RANGE_ENTRY_SIZE =

Range entry size.

3
ARRAY_ENTRY_SIZE =

Array entry size.

1

Instance Attribute Summary collapse

Attributes inherited from SubTable

#file, #length, #table_offset

Instance Method Summary collapse

Methods inherited from SubTable

#eot?, #read

Constructor Details

#initialize(top_dict, file, offset, length = nil) ⇒ FdSelector

Returns a new instance of FdSelector.

Parameters:

  • top_dict (TTFunk::Table:Cff::TopDict)
  • file (TTFunk::File)
  • offset (Integer)
  • length (Integer) (defaults to: nil)
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 43
43
def initialize(top_dict, file, offset, length = nil)
44
  @top_dict = top_dict
45
  super(file, offset, length)
46
end

Instance Attribute Details

#entriesArray<Integer>, Array<Array(Range, Integer)> (readonly)

Number of entries.

Returns:

  • (Array<Integer>)

    if format is array.

  • (Array<Array(Range, Integer)>)

    if format is range.

Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 33
33
def entries
34
  @entries
35
end

#items_countInteger (readonly)

Number of encoded items.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 28
28
def items_count
29
  @items_count
30
end

#n_glyphsInteger (readonly)

Number of glyphs.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 37
37
def n_glyphs
38
  @n_glyphs
39
end

#top_dictTTFunk::Table::Cff::TopDict (readonly)

Top dict.

Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 24
24
def top_dict
25
  @top_dict
26
end

Instance Method Details

#[](glyph_id) ⇒ Integer

Get font dict index for glyph ID.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 51
51
def [](glyph_id)
52
  case format_sym
53
  when :array_format
54
    entries[glyph_id]
55
56
  when :range_format
57
    if (entry = range_cache[glyph_id])
58
      return entry
59
    end
60
61
    range, entry =
62
      entries.bsearch { |rng, _|
63
        if rng.cover?(glyph_id)
64
          0
65
        elsif glyph_id < rng.first
66
          -1
67
        else
68
          1
69
        end
70
      }
71
72
    range.each { |i| range_cache[i] = entry }
73
    entry
74
  end
75
end

#each {|font| ... } ⇒ void

This method returns an undefined value.

Iterate over font dicts for each glyph ID.

Yield Parameters:

  • font (Integer)

    dict index.

Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 81
81
def each
82
  return to_enum(__method__) unless block_given?
83
84
  items_count.times { |i| yield(self[i]) }
85
end

#encode(charmap) ⇒ String

Encode Font dict selector.

Parameters:

  • charmap (Hash{Integer => Hash})

    keys are the charac codes, values are hashes:

    • :old (Integer) - glyph ID in the original font.
    • :new (Integer) - glyph ID in the subset font.

Returns:

  • (String)
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 94
94
def encode(charmap)
95
  # get list of [new_gid, fd_index] pairs
96
  new_indices =
97
    charmap
98
      .reject { |code, mapping| mapping[:new].zero? && !code.zero? }
99
      .sort_by { |_code, mapping| mapping[:new] }
100
      .map { |(_code, mapping)| [mapping[:new], self[mapping[:old]]] }
101
102
  ranges = rangify_gids(new_indices)
103
  total_range_size = ranges.size * RANGE_ENTRY_SIZE
104
  total_array_size = new_indices.size * ARRAY_ENTRY_SIZE
105
106
  ''.b.tap do |result|
107
    if total_array_size <= total_range_size
108
      result << [ARRAY_FORMAT].pack('C')
109
      result << new_indices.map(&:last).pack('C*')
110
    else
111
      result << [RANGE_FORMAT, ranges.size].pack('Cn')
112
      ranges.each { |range| result << range.pack('nC') }
113
114
      # "A sentinel GID follows the last range element and serves to
115
      # delimit the last range in the array. (The sentinel GID is set
116
      # equal to the number of glyphs in the font. That is, its value
117
      # is 1 greater than the last GID in the font)."
118
      result << [new_indices.size].pack('n')
119
    end
120
  end
121
end