Class: TTFunk::Subset::CodePage

Inherits:
Base
  • Object
show all
Defined in:
lib/ttfunk/subset/code_page.rb

Overview

A subset that uses standard code page encoding.

Direct Known Subclasses

MacRoman, Windows1252

Constant Summary

Constants inherited from Base

Base::MICROSOFT_PLATFORM_ID, Base::MS_SYMBOL_ENCODING_ID

Instance Attribute Summary collapse

Attributes inherited from Base

#original

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • original (TTFunk::File)
  • code_page (Integer)
  • encoding (Encoding, String, Symbol)
Source Code
lib/ttfunk/subset/code_page.rb, line 47
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_pageInteger (readonly)

Code page used in this subset. This is used for proper OS/2 table encoding.

Returns:

  • (Integer)
Source Code
lib/ttfunk/subset/code_page.rb, line 38
38
def code_page
39
  @code_page
40
end

#encodingEncoding, ... (readonly)

Encoding used in this subset.

Returns:

  • (Encoding, String, Symbol)
Source Code
lib/ttfunk/subset/code_page.rb, line 42
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

Parameters:

  • encoding (Encoding, String, Symbol)

Returns:

  • (Hash{Integer => Integer})
Source Code
lib/ttfunk/subset/code_page.rb, line 16
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.

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)
Source Code
lib/ttfunk/subset/code_page.rb, line 77
77
def covers?(character)
78
  !from_unicode(character).nil?
79
end

#from_unicode(character) ⇒ Integer?

Get character code for Unicode codepoint.

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Integer, nil)
Source Code
lib/ttfunk/subset/code_page.rb, line 94
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?

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)
Source Code
lib/ttfunk/subset/code_page.rb, line 85
85
def includes?(character)
86
  code = from_unicode(character)
87
  code && @subset[code]
88
end

#new_cmap_tableTTFunk::Table::Cmap

Get cmap table for this subset.

Returns:

Source Code
lib/ttfunk/subset/code_page.rb, line 103
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_idsArray<Integer>

Get the list of Glyph IDs from the original font that are in this subset.

Returns:

  • (Array<Integer>)
Source Code
lib/ttfunk/subset/code_page.rb, line 120
120
def original_glyph_ids
121
  ([0] + @subset.map { |unicode| unicode && unicode_cmap[unicode] })
122
    .compact.uniq.sort
123
end

#space_char_codeInteger?

Get a chacter code for Space in this subset

Returns:

  • (Integer, nil)
Source Code
lib/ttfunk/subset/code_page.rb, line 128
128
def space_char_code
129
  @space_char_code ||= from_unicode(Unicode::SPACE_CHAR)
130
end

#to_unicode_mapHash

Get a mapping from this subset to Unicode.

Returns:

  • (Hash)
Source Code
lib/ttfunk/subset/code_page.rb, line 59
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.

Parameters:

  • character (Integer)

    Unicode codepoint

Source Code
lib/ttfunk/subset/code_page.rb, line 68
68
def use(character)
69
  @subset[from_unicode(character)] = character
70
end