class TTFunk::Subset::CodePage

Attributes

code_page[R]
encoding[R]

Public Class Methods

new(original, code_page, encoding) click to toggle source
Calls superclass method TTFunk::Subset::Base::new
# File lib/ttfunk/subset/code_page.rb, line 36
def initialize(original, code_page, encoding)
  super(original)
  @code_page = code_page
  @encoding = encoding
  @subset = Array.new(256)
  use(space_char_code)
end
unicode_mapping_for(encoding) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 11
def unicode_mapping_for(encoding)
  mapping_cache[encoding] ||= (0..255).each_with_object({}) do |c, ret|
    # rubocop:disable Lint/SuppressedException
    begin
      ret[c] = c.chr(encoding)
                .encode(Encoding::UTF_8)
                .codepoints
                .first
    rescue Encoding::UndefinedConversionError
      # There is not a strict 1:1 mapping between all code page
      # characters and unicode.
    end
    # rubocop:enable Lint/SuppressedException
  end
end

Private Class Methods

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

Public Instance Methods

covers?(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 52
def covers?(character)
  !from_unicode(character).nil?
end
from_unicode(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 61
def from_unicode(character)
  [character].pack('U*').encode(encoding).ord
rescue Encoding::UndefinedConversionError
  nil
end
includes?(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 56
def includes?(character)
  code = from_unicode(character)
  code && @subset[code]
end
new_cmap_table() click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 67
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
# File lib/ttfunk/subset/code_page.rb, line 79
def original_glyph_ids
  ([0] + @subset.map { |unicode| unicode && unicode_cmap[unicode] })
    .compact.uniq.sort
end
space_char_code() click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 84
def space_char_code
  @space_char_code ||= from_unicode(Unicode::SPACE_CHAR)
end
to_unicode_map() click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 44
def to_unicode_map
  self.class.unicode_mapping_for(encoding)
end
use(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 48
def use(character)
  @subset[from_unicode(character)] = character
end