class TTFunk::Subset::CodePage

A subset that uses standard code page encoding.

Attributes

code_page[R]

Code page used in this subset. This is used for proper ‘OS/2` table encoding. @return [Integer]

encoding[R]

Encoding used in this subset. @return [Encoding, String, Symbol]

Public Class Methods

new(original, code_page, encoding) click to toggle source

@param original [TTFunk::File] @param code_page [Integer] @param encoding [Encoding, String, Symbol]

Calls superclass method TTFunk::Subset::Base::new
# 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
unicode_mapping_for(encoding) click to toggle source

Get a mapping from an encoding to Unicode

@param encoding [Encoding, String, Symbol] @return [Hash{Integer => Integer}]

# 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

Private Class Methods

mapping_cache() click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 30
def mapping_cache
  @mapping_cache ||= {}
end

Public Instance Methods

covers?(character) click to toggle source

Can this subset include the character? This depends on the encoding used in this subset.

@param character [Integer] Unicode codepoint @return [Boolean]

# File lib/ttfunk/subset/code_page.rb, line 77
def covers?(character)
  !from_unicode(character).nil?
end
from_unicode(character) click to toggle source

Get character code for Unicode codepoint.

@param character [Integer] Unicode codepoint @return [Integer, nil]

# 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) click to toggle source

Does this subset actually has the character?

@param character [Integer] Unicode codepoint @return [Boolean]

# File lib/ttfunk/subset/code_page.rb, line 85
def includes?(character)
  code = from_unicode(character)
  code && @subset[code]
end
new_cmap_table() click to toggle source

Get ‘cmap` table for this subset.

@return [TTFunk::Table::Cmap]

# 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() click to toggle source

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

@return [Array<Integer>]

# 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() click to toggle source

Get a chacter code for Space in this subset

@return [Integer, nil]

# 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() click to toggle source

Get a mapping from this subset to Unicode.

@return [Hash]

# 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) click to toggle source

Add a character to subset.

@param character [Integer] Unicode codepoint @return [void]

# File lib/ttfunk/subset/code_page.rb, line 68
def use(character)
  @subset[from_unicode(character)] = character
end