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)


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_pageInteger (readonly)

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

Returns:

  • (Integer)


38
39
40
# File 'lib/ttfunk/subset/code_page.rb', line 38

def code_page
  @code_page
end

#encodingEncoding, ... (readonly)

Encoding used in this subset.

Returns:

  • (Encoding, String, Symbol)


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

Parameters:

  • encoding (Encoding, String, Symbol)

Returns:

  • (Hash{Integer => Integer})


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.

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)


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.

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Integer, nil)


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?

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)


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_tableTTFunk::Table::Cmap

Get ‘cmap` table for this subset.

Returns:



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_idsArray<Integer>

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

Returns:

  • (Array<Integer>)


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_codeInteger?

Get a chacter code for Space in this subset

Returns:

  • (Integer, nil)


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_mapHash

Get a mapping from this subset to Unicode.

Returns:

  • (Hash)


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.

Parameters:

  • character (Integer)

    Unicode codepoint



68
69
70
# File 'lib/ttfunk/subset/code_page.rb', line 68

def use(character)
  @subset[from_unicode(character)] = character
end