Class: TTFunk::Subset::CodePage
- Defined in:
- lib/ttfunk/subset/code_page.rb
Overview
A subset that uses standard code page encoding.
Direct Known Subclasses
Constant Summary
Constants inherited from Base
Base::MICROSOFT_PLATFORM_ID, Base::MS_SYMBOL_ENCODING_ID
Instance Attribute Summary collapse
-
#code_page ⇒ Integer
readonly
Code page used in this subset.
-
#encoding ⇒ Encoding, ...
readonly
Encoding used in this subset.
Attributes inherited from Base
Class Method Summary collapse
-
.unicode_mapping_for(encoding) ⇒ Hash{Integer => Integer}
Get a mapping from an encoding to Unicode.
Instance Method Summary collapse
-
#covers?(character) ⇒ Boolean
Can this subset include the character? This depends on the encoding used in this subset.
-
#from_unicode(character) ⇒ Integer?
Get character code for Unicode codepoint.
-
#includes?(character) ⇒ Boolean
Does this subset actually has the character?.
-
#initialize(original, code_page, encoding) ⇒ CodePage
constructor
A new instance of CodePage.
-
#new_cmap_table ⇒ TTFunk::Table::Cmap
Get
cmap
table for this subset. -
#original_glyph_ids ⇒ Array<Integer>
Get the list of Glyph IDs from the original font that are in this subset.
-
#space_char_code ⇒ Integer?
Get a chacter code for Space in this subset.
-
#to_unicode_map ⇒ Hash
Get a mapping from this subset to Unicode.
-
#use(character) ⇒ void
Add a character to subset.
Methods inherited from Base
#collect_glyphs, #encode, #encoder_klass, #glyphs, #microsoft_symbol?, #new_to_old_glyph, #old_to_new_glyph, #unicode?, #unicode_cmap
Constructor Details
#initialize(original, code_page, encoding) ⇒ CodePage
Returns a new instance of CodePage.
Source Code
47 | def initialize(original, code_page, encoding) |
48 | super(original) |
49 | @code_page = code_page |
50 | @encoding = encoding |
51 | @subset = Array.new(256) |
52 | @from_unicode_cache = {} |
53 | use(space_char_code) |
54 | end
|
Instance Attribute Details
#code_page ⇒ Integer (readonly)
Code page used in this subset.
This is used for proper OS/2
table encoding.
Source Code
38 | def code_page |
39 | @code_page
|
40 | end
|
#encoding ⇒ Encoding, ... (readonly)
Encoding used in this subset.
Source Code
42 | def encoding |
43 | @encoding
|
44 | end
|
Class Method Details
.unicode_mapping_for(encoding) ⇒ Hash{Integer => Integer}
Get a mapping from an encoding to Unicode
Source Code
16 | def unicode_mapping_for(encoding) |
17 | mapping_cache[encoding] ||= |
18 | (0..255).each_with_object({}) do |c, ret| |
19 | codepoint = |
20 | c.chr(encoding) |
21 | .encode(Encoding::UTF_8, undef: :replace, replace: '') |
22 | .codepoints |
23 | .first |
24 | ret[c] = codepoint if codepoint |
25 | end
|
26 | end
|
Instance Method Details
#covers?(character) ⇒ Boolean
Can this subset include the character? This depends on the encoding used in this subset.
Source Code
77 | def covers?(character) |
78 | !from_unicode(character).nil? |
79 | end
|
#from_unicode(character) ⇒ Integer?
Get character code for Unicode codepoint.
Source Code
94 | def from_unicode(character) |
95 | @from_unicode_cache[character] ||= (+'' << character).encode!(encoding).ord |
96 | rescue Encoding::UndefinedConversionError |
97 | nil
|
98 | end
|
#includes?(character) ⇒ Boolean
Does this subset actually has the character?
Source Code
85 | def includes?(character) |
86 | code = from_unicode(character) |
87 | code && @subset[code] |
88 | end
|
#new_cmap_table ⇒ TTFunk::Table::Cmap
Get cmap
table for this subset.
Source Code
103 | def new_cmap_table |
104 | @new_cmap_table ||= |
105 | begin
|
106 | mapping = {} |
107 | |
108 | @subset.each_with_index do |unicode, roman| |
109 | mapping[roman] = unicode_cmap[unicode] |
110 | end
|
111 | |
112 | TTFunk::Table::Cmap.encode(mapping, :mac_roman) |
113 | end
|
114 | end
|
#original_glyph_ids ⇒ Array<Integer>
Get the list of Glyph IDs from the original font that are in this subset.
Source Code
120 | def original_glyph_ids |
121 | ([0] + @subset.map { |unicode| unicode && unicode_cmap[unicode] }) |
122 | .compact.uniq.sort |
123 | end
|
#space_char_code ⇒ Integer?
Get a chacter code for Space in this subset
Source Code
128 | def space_char_code |
129 | @space_char_code ||= from_unicode(Unicode::SPACE_CHAR) |
130 | end
|
#to_unicode_map ⇒ Hash
Get a mapping from this subset to Unicode.
Source Code
59 | def to_unicode_map |
60 | self.class.unicode_mapping_for(encoding) |
61 | .select { |codepoint, _unicode| @subset[codepoint] } |
62 | end
|
#use(character) ⇒ void
This method returns an undefined value.
Add a character to subset.
Source Code
68 | def use(character) |
69 | @subset[from_unicode(character)] = character |
70 | end
|