Class: TTFunk::SubsetCollection

### Home Classes Methods

### In Files

- lib/ttfunk/subset_collection.rb

### Parent

Object

### Methods

- ::new
- #[]
- #encode
- #use

### Class/Module Index

Quicksearch

- TTFunk
- TTFunk::Directory
- TTFunk::Encoding
- TTFunk::Encoding::MacRoman
- TTFunk::Encoding::Windows1252
- TTFunk::File
- TTFunk::Reader
- TTFunk::ResourceFile
- TTFunk::Subset
- TTFunk::Subset::Base
- TTFunk::Subset::MacRoman
- TTFunk::Subset::Unicode
- TTFunk::Subset::Unicode8Bit
- TTFunk::Subset::Windows1252
- TTFunk::SubsetCollection
- TTFunk::Table
- TTFunk::Table::Cmap
- TTFunk::Table::Cmap::Format00
- TTFunk::Table::Cmap::Format04
- TTFunk::Table::Cmap::Format06
- TTFunk::Table::Cmap::Format10
- TTFunk::Table::Cmap::Format12
- TTFunk::Table::Cmap::Subtable
- TTFunk::Table::Glyf
- TTFunk::Table::Glyf::Compound
- TTFunk::Table::Glyf::Simple
- TTFunk::Table::Head
- TTFunk::Table::Hhea
- TTFunk::Table::Hmtx
- TTFunk::Table::Kern
- TTFunk::Table::Kern::Format0
- TTFunk::Table::Loca
- TTFunk::Table::Maxp
- TTFunk::Table::Name
- TTFunk::Table::Name::String
- TTFunk::Table::OS2
- TTFunk::Table::Post
- TTFunk::Table::Post::Format10
- TTFunk::Table::Post::Format20
- TTFunk::Table::Post::Format30
- TTFunk::Table::Post::Format40
- TTFunk::Table::Sbix
- TTFunk::Table::Simple

No matching classes.

# TTFunk::SubsetCollection

### Public Class Methods

new(original) click to toggle source

    # File lib/ttfunk/subset_collection.rb, line 5
    def initialize(original)
      @original = original
      @subsets = [Subset.for(@original, :mac_roman)]
    end

### Public Instance Methods

[](subset) click to toggle source

    # File lib/ttfunk/subset_collection.rb, line 10
    def [](subset)
      @subsets[subset]
    end

encode(characters) click to toggle source

`characters` should be an array of UTF-16 characters. Returns an array of subset chunks, where each chunk is another array of two elements. The first element is the subset number, and the second element is the string of characters to render with that font subset. The strings will be encoded for their subset font, and so may not look (in the raw) like what was passed in, but they will render correctly with the indicated subset font.

    # File lib/ttfunk/subset_collection.rb, line 40
    def encode(characters)
      return [] if characters.empty?

      # TODO: probably would be more optimal to nix the #use method,
      # and merge it into this one, so it can be done in a single
      # pass instead of two passes.
      use(characters)

      parts = []
      current_subset = 0
      current_char = 0
      char = characters[current_char]

      loop do
        while @subsets[current_subset].includes?(char)
          char = @subsets[current_subset].from_unicode(char)

          if parts.empty? || parts.last[0] != current_subset
            encoded_char = char.chr
            if encoded_char.respond_to?(:force_encoding)
              encoded_char.force_encoding("ASCII-8BIT")
            end
            parts << [current_subset, encoded_char]
          else
            parts.last[1] << char
          end

          current_char += 1
          return parts if current_char >= characters.length
          char = characters[current_char]
        end

        current_subset = (current_subset + 1) % @subsets.length
      end
    end

use(characters) click to toggle source

`characters` should be an array of UTF-16 characters

    # File lib/ttfunk/subset_collection.rb, line 15
    def use(characters)
      characters.each do |char|
        covered = false
        @subsets.each_with_index do |subset, i|
          if subset.covers?(char)
            subset.use(char)
            covered = true
            break
          end
        end

        if !covered
          @subsets << Subset.for(@original, :unicode_8bit)
          @subsets.last.use(char)
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.