Class: TTFunk::Table::Cff::FdSelector
- 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
-
#entries ⇒ Array<Integer>, Array<Array(Range, Integer)>
readonly
Number of entries.
-
#items_count ⇒ Integer
readonly
Number of encoded items.
-
#n_glyphs ⇒ Integer
readonly
Number of glyphs.
-
#top_dict ⇒ TTFunk::Table::Cff::TopDict
readonly
Top dict.
Attributes inherited from SubTable
Instance Method Summary collapse
-
#[](glyph_id) ⇒ Integer
Get font dict index for glyph ID.
-
#each {|font| ... } ⇒ void
Iterate over font dicts for each glyph ID.
-
#encode(charmap) ⇒ String
Encode Font dict selector.
-
#initialize(top_dict, file, offset, length = nil) ⇒ FdSelector
constructor
A new instance of FdSelector.
Methods inherited from SubTable
Constructor Details
#initialize(top_dict, file, offset, length = nil) ⇒ FdSelector
Returns a new instance of FdSelector.
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
#entries ⇒ Array<Integer>, Array<Array(Range, Integer)> (readonly)
Number of entries.
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 33
33 | def entries |
34 | @entries
|
35 | end
|
#items_count ⇒ Integer (readonly)
Number of encoded items.
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 28
28 | def items_count |
29 | @items_count
|
30 | end
|
#n_glyphs ⇒ Integer (readonly)
Number of glyphs.
Source Code
lib/ttfunk/table/cff/fd_selector.rb, line 37
37 | def n_glyphs |
38 | @n_glyphs
|
39 | end
|
#top_dict ⇒ TTFunk::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.
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.
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.
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
|