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