Class: TTFunk::Table::Cff::Encoding
- Includes:
- Enumerable
- Defined in:
- lib/ttfunk/table/cff/encoding.rb
Overview
CFF Encoding.
Constant Summary collapse
- STANDARD_ENCODING_ID =
Predefined Standard Encoding ID.
0
- EXPERT_ENCODING_ID =
Predefined Expert Encoding ID.
1
- DEFAULT_ENCODING_ID =
Default encoding ID.
STANDARD_ENCODING_ID
Instance Attribute Summary collapse
-
#format ⇒ Integer
readonly
Encodign format.
-
#items_count ⇒ Integer
readonly
Number of encoded items.
-
#offset_or_id ⇒ Integer
readonly
Offset or encoding ID.
-
#top_dict ⇒ TTFunk::Table::Cff::TopDict
readonly
Top dict.
Attributes inherited from SubTable
Class Method Summary collapse
-
.codes_for_encoding_id(encoding_id) ⇒ TTFunk::OneBasedArray<Integer>
Get predefined encoding by ID.
Instance Method Summary collapse
-
#[](glyph_id) ⇒ Integer?
Get character code for glyph index.
-
#each {|code| ... } ⇒ void
#each ⇒ Enumerator
Iterate over character codes.
-
#encode(charmap) ⇒ String
Encode encoding.
-
#initialize(top_dict, file, offset = nil, length = nil) ⇒ Encoding
#initialize(top_dict, file, charset_id) ⇒ Encoding
constructor
A new instance of Encoding.
-
#offset ⇒ Integer?
Encoding offset in the file.
-
#supplemental? ⇒ Boolean
Is this a supplemental encoding?.
Methods inherited from SubTable
Constructor Details
#initialize(top_dict, file, offset = nil, length = nil) ⇒ Encoding #initialize(top_dict, file, charset_id) ⇒ Encoding
Returns a new instance of Encoding.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 59
59 | def initialize(top_dict, file, offset_or_id = nil, length = nil) |
60 | @top_dict = top_dict |
61 | @offset_or_id = offset_or_id || DEFAULT_ENCODING_ID |
62 | |
63 | if offset |
64 | super(file, offset, length) |
65 | @supplemental = format >> 7 == 1 |
66 | else
|
67 | @items_count = self.class.codes_for_encoding_id(offset_or_id).size |
68 | @supplemental = false |
69 | end
|
70 | end
|
Instance Attribute Details
#format ⇒ Integer (readonly)
Encodign format.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 40
40 | def format |
41 | @format
|
42 | end
|
#items_count ⇒ Integer (readonly)
Number of encoded items.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 44
44 | def items_count |
45 | @items_count
|
46 | end
|
#offset_or_id ⇒ Integer (readonly)
Offset or encoding ID.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 48
48 | def offset_or_id |
49 | @offset_or_id
|
50 | end
|
#top_dict ⇒ TTFunk::Table::Cff::TopDict (readonly)
Top dict.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 36
36 | def top_dict |
37 | @top_dict
|
38 | end
|
Class Method Details
.codes_for_encoding_id(encoding_id) ⇒ TTFunk::OneBasedArray<Integer>
Get predefined encoding by ID.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 24
24 | def codes_for_encoding_id(encoding_id) |
25 | case encoding_id |
26 | when STANDARD_ENCODING_ID |
27 | Encodings::STANDARD |
28 | when EXPERT_ENCODING_ID |
29 | Encodings::EXPERT |
30 | end
|
31 | end
|
Instance Method Details
#[](glyph_id) ⇒ Integer?
Get character code for glyph index.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 90
90 | def [](glyph_id) |
91 | return 0 if glyph_id.zero? |
92 | return code_for(glyph_id) if offset |
93 | |
94 | self.class.codes_for_encoding_id(offset_or_id)[glyph_id] |
95 | end
|
#each {|code| ... } ⇒ void #each ⇒ Enumerator
Iterate over character codes.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 79
79 | def each |
80 | return to_enum(__method__) unless block_given? |
81 | |
82 | # +1 adjusts for the implicit .notdef glyph
|
83 | (items_count + 1).times { |i| yield(self[i]) } |
84 | end
|
#encode(charmap) ⇒ String
Encode encoding.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 120
120 | def encode(charmap) |
121 | # Any subset encoding is all but guaranteed to be different from the
|
122 | # standard encoding so we don't even attempt to see if it matches. We
|
123 | # assume it's different and just encode it anew.
|
124 | |
125 | return encode_supplemental(charmap) if supplemental? |
126 | |
127 | codes = |
128 | charmap
|
129 | .reject { |_code, mapping| mapping[:new].zero? } |
130 | .sort_by { |_code, mapping| mapping[:new] } |
131 | .map { |(code, _m)| code } |
132 | |
133 | ranges = TTFunk::BinUtils.rangify(codes) |
134 | |
135 | # calculate whether storing the charset as a series of ranges is
|
136 | # more efficient (i.e. takes up less space) vs storing it as an
|
137 | # array of SID values
|
138 | total_range_size = (2 * ranges.size) + |
139 | (element_width(:range_format) * ranges.size) |
140 | |
141 | total_array_size = codes.size * element_width(:array_format) |
142 | |
143 | if total_array_size <= total_range_size |
144 | ([format_int(:array_format), codes.size] + codes).pack('C*') |
145 | else
|
146 | element_fmt = element_format(:range_format) |
147 | result = [format_int(:range_format), ranges.size].pack('CC') |
148 | ranges.each { |range| result << range.pack(element_fmt) } |
149 | result
|
150 | end
|
151 | end
|
#offset ⇒ Integer?
Encoding offset in the file.
Source Code
lib/ttfunk/table/cff/encoding.rb, line 100
100 | def offset |
101 | # Numbers from 0..1 mean encoding IDs instead of offsets. IDs are
|
102 | # pre-defined, generic encodings that define the characters present
|
103 | # in the font.
|
104 | #
|
105 | # In the case of an offset, add the CFF table's offset since the
|
106 | # charset offset is relative to the start of the CFF table. Otherwise
|
107 | # return nil (no offset).
|
108 | if offset_or_id > 1 |
109 | offset_or_id + top_dict.cff_offset |
110 | end
|
111 | end
|
#supplemental? ⇒ Boolean
Is this a supplemental encoding?
Source Code
lib/ttfunk/table/cff/encoding.rb, line 156
156 | def supplemental? |
157 | # high-order bit set to 1 indicates supplemental encoding
|
158 | @supplemental
|
159 | end
|