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 32
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|
      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 25
def mapping_cache
  @mapping_cache ||= {}
end

Public Instance Methods

covers?(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 48
def covers?(character)
  !from_unicode(character).nil?
end
from_unicode(character) click to toggle source
# File lib/ttfunk/subset/code_page.rb, line 57
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 52
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 63
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 76
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 81
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 40
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 44
def use(character)
  @subset[from_unicode(character)] = character
end