class TTFunk::Subset::Base

Base subset.

@api private

Constants

MICROSOFT_PLATFORM_ID

Microsoft Platform ID

MS_SYMBOL_ENCODING_ID

Symbol Encoding ID for Microsoft Platform

Attributes

original[R]

Original font

@return [TTFunk::File]

Public Class Methods

new(original) click to toggle source

@param original [TTFunk::File]

# File lib/ttfunk/subset/base.rb, line 33
def initialize(original)
  @original = original
end

Public Instance Methods

collect_glyphs(glyph_ids) click to toggle source

Get glyphs by their IDs in the original font.

@param glyph_ids [Array<Integer>] @return [Hash{Integer => TTFunk::Table::Glyf::Simple,

TTFunk::Table::Glyf::Compound>] if original is a TrueType font

@return [Hash{Integer => TTFunk::Table::Cff::Charstring}] if original is

a CFF-based OpenType font
# File lib/ttfunk/subset/base.rb, line 98
def collect_glyphs(glyph_ids)
  collected =
    glyph_ids.each_with_object({}) do |id, h|
      h[id] = glyph_for(id)
    end

  additional_ids = collected.values
    .select { |g| g && g.compound? }
    .map(&:glyph_ids)
    .flatten

  collected.update(collect_glyphs(additional_ids)) if additional_ids.any?

  collected
end
encode(options = {}) click to toggle source

Encode this subset into a binary font representation.

@param options [Hash] @return [String]

# File lib/ttfunk/subset/base.rb, line 63
def encode(options = {})
  encoder_klass.new(original, self, options).encode
end
encoder_klass() click to toggle source

Encoder class for this subset.

@return [TTFunk::TTFEncoder, TTFunk::OTFEncoder]

# File lib/ttfunk/subset/base.rb, line 70
def encoder_klass
  original.cff.exists? ? OTFEncoder : TTFEncoder
end
glyphs() click to toggle source

Get glyphs in this subset.

@return [Hash{Integer => TTFunk::Table::Glyf::Simple,

TTFunk::Table::Glyf::Compound}] if original is a TrueType font

@return [Hash{Integer => TTFunk::Table::Cff::Charstring] if original is

a CFF-based OpenType font
# File lib/ttfunk/subset/base.rb, line 87
def glyphs
  @glyphs ||= collect_glyphs(original_glyph_ids)
end
microsoft_symbol?() click to toggle source

Does this subset use Microsoft Symbolic encoding?

@return [Boolean]

# File lib/ttfunk/subset/base.rb, line 47
def microsoft_symbol?
  new_cmap_table[:platform_id] == MICROSOFT_PLATFORM_ID &&
    new_cmap_table[:encoding_id] == MS_SYMBOL_ENCODING_ID
end
new_to_old_glyph() click to toggle source

Glyph ID mapping from this subset to the original font.

@return [Hash{Integer => Integer}]

# File lib/ttfunk/subset/base.rb, line 142
def new_to_old_glyph
  @new_to_old_glyph ||= old_to_new_glyph.invert
end
old_to_new_glyph() click to toggle source

Glyph ID mapping from the original font to this subset.

@return [Hash{Integer => Integer}]

# File lib/ttfunk/subset/base.rb, line 117
def old_to_new_glyph
  @old_to_new_glyph ||=
    begin
      charmap = new_cmap_table[:charmap]
      old_to_new =
        charmap.each_with_object(0 => 0) do |(_, ids), map|
          map[ids[:old]] = ids[:new]
        end

      next_glyph_id = new_cmap_table[:max_glyph_id]

      glyphs.each_key do |old_id|
        unless old_to_new.key?(old_id)
          old_to_new[old_id] = next_glyph_id
          next_glyph_id += 1
        end
      end

      old_to_new
    end
end
to_unicode_map() click to toggle source

Get a mapping from this subset to Unicode.

@return [Hash{Integer => Integer}]

# File lib/ttfunk/subset/base.rb, line 55
def to_unicode_map
  {}
end
unicode?() click to toggle source

Is this Unicode-based subset?

@return [Boolean]

# File lib/ttfunk/subset/base.rb, line 40
def unicode?
  false
end
unicode_cmap() click to toggle source

Get the first Unicode cmap from the original font.

@return [TTFunk::Table::Cmap::Subtable]

# File lib/ttfunk/subset/base.rb, line 77
def unicode_cmap
  @unicode_cmap ||= @original.cmap.unicode.first
end

Private Instance Methods

glyph_for(glyph_id) click to toggle source
# File lib/ttfunk/subset/base.rb, line 148
def glyph_for(glyph_id)
  if original.cff.exists?
    original
      .cff
      .top_index[0]
      .charstrings_index[glyph_id]
      .glyph
  else
    original.glyph_outlines.for(glyph_id)
  end
end