Class: TTFunk::Subset::Base Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/subset/base.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base subset.

Direct Known Subclasses

CodePage, Unicode, Unicode8Bit

Constant Summary collapse

MICROSOFT_PLATFORM_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Microsoft Platform ID

3
MS_SYMBOL_ENCODING_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Symbol Encoding ID for Microsoft Platform

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Base.

Parameters:



33
34
35
# File 'lib/ttfunk/subset/base.rb', line 33

def initialize(original)
  @original = original
end

Instance Attribute Details

#originalTTFunk::File (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Original font

Returns:



30
31
32
# File 'lib/ttfunk/subset/base.rb', line 30

def original
  @original
end

Instance Method Details

#collect_glyphs(glyph_ids) ⇒ Hash{Integer => TTFunk::Table::Glyf::Simple, TTFunk::Table::Glyf::Compound>, Hash{Integer => TTFunk::Table::Cff::Charstring}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get glyphs by their IDs in the original font.

Parameters:

  • glyph_ids (Array<Integer>)

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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 = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encode this subset into a binary font representation.

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


63
64
65
# File 'lib/ttfunk/subset/base.rb', line 63

def encode(options = {})
  encoder_klass.new(original, self, options).encode
end

#encoder_klassTTFunk::TTFEncoder, TTFunk::OTFEncoder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encoder class for this subset.



70
71
72
# File 'lib/ttfunk/subset/base.rb', line 70

def encoder_klass
  original.cff.exists? ? OTFEncoder : TTFEncoder
end

#glyphsHash{Integer => TTFunk::Table::Glyf::Simple, TTFunk::Table::Glyf::Compound}, Hash{Integer => TTFunk::Table::Cff::Charstring] if original is a CFF-based OpenType font

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get glyphs in this subset.

Returns:



87
88
89
# File 'lib/ttfunk/subset/base.rb', line 87

def glyphs
  @glyphs ||= collect_glyphs(original_glyph_ids)
end

#microsoft_symbol?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Does this subset use Microsoft Symbolic encoding?

Returns:

  • (Boolean)


47
48
49
50
# 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_glyphHash{Integer => Integer}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Glyph ID mapping from this subset to the original font.

Returns:

  • (Hash{Integer => Integer})


142
143
144
# 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_glyphHash{Integer => Integer}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Glyph ID mapping from the original font to this subset.

Returns:

  • (Hash{Integer => Integer})


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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_mapHash{Integer => Integer}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a mapping from this subset to Unicode.

Returns:

  • (Hash{Integer => Integer})


55
56
57
# File 'lib/ttfunk/subset/base.rb', line 55

def to_unicode_map
  {}
end

#unicode?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is this Unicode-based subset?

Returns:

  • (Boolean)


40
41
42
# File 'lib/ttfunk/subset/base.rb', line 40

def unicode?
  false
end

#unicode_cmapTTFunk::Table::Cmap::Subtable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the first Unicode cmap from the original font.



77
78
79
# File 'lib/ttfunk/subset/base.rb', line 77

def unicode_cmap
  @unicode_cmap ||= @original.cmap.unicode.first
end