Module: TTFunk::Table::Cmap::Format04

### Home Classes Methods

### In Files

- lib/ttfunk/table/cmap/format04.rb

### Methods

- ::encode
- #[]
- #supported?

### 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::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::Format25
- TTFunk::Table::Post::Format30
- TTFunk::Table::Post::Format40
- TTFunk::Table::Simple

No matching classes.

# TTFunk::Table::Cmap::Format04

### Attributes

code_map[R]
language[R]

### Public Class Methods

encode(charmap) click to toggle source

Expects a hash mapping character codes to glyph ids (where the glyph ids are from the original font). Returns a hash including a new map (:charmap) that maps the characters in charmap to a another hash containing both the old (:old) and new (:new) glyph ids. The returned hash also includes a :subtable key, which contains the encoded subtable for the given charmap.

    # File lib/ttfunk/table/cmap/format04.rb, line 15
    def self.encode(charmap)
      end_codes = []
      start_codes = []
      next_id = 0
      last = difference = nil

      glyph_map = { 0 => 0 }
      new_map = charmap.keys.sort.inject({}) do |map, code|
        old = charmap[code]
        glyph_map[old] ||= next_id += 1
        map[code] = { :old => old, :new => glyph_map[old] }

        delta = glyph_map[old] - code
        if last.nil? || delta != difference
          end_codes << last if last
          start_codes << code
          difference = delta
        end
        last = code

        map
      end

      end_codes << last if last
      end_codes << 0xFFFF
      start_codes << 0xFFFF
      segcount = start_codes.length

      # build the conversion tables
      deltas = []
      range_offsets = []
      glyph_indices = []

      offset = 0
      start_codes.zip(end_codes).each_with_index do |(a, b), segment|
        if a == 0xFFFF
          deltas << 0
          range_offsets << 0
          break
        end

        start_glyph_id = new_map[a][:new]
        if a - start_glyph_id >= 0x8000
          deltas << 0
          range_offsets << 2 * (glyph_indices.length + segcount - segment)
          a.upto(b) { |code| glyph_indices << new_map[code][:new] }
        else
          deltas << -a + start_glyph_id
          range_offsets << 0
        end
        offset += 2
      end

      # format, length, language
      subtable = [4, 16 + 8 * segcount + 2 * glyph_indices.length, 0].pack("nnn")

      search_range = 2 * 2 ** (Math.log(segcount) / Math.log(2)).to_i
      entry_selector = (Math.log(search_range / 2) / Math.log(2)).to_i
      range_shift = (2 * segcount) - search_range
      subtable << [segcount * 2, search_range, entry_selector, range_shift].pack("nnnn")

      subtable << end_codes.pack("n*") << "\00\\00"" << start_codes.pack("n*")
      subtable << deltas.pack("n*") << range_offsets.pack("n*") << glyph_indices.pack("n*")

      { :charmap => new_map, :subtable => subtable, :max_glyph_id => next_id+1 }
    end

### Public Instance Methods

[](code) click to toggle source

    # File lib/ttfunk/table/cmap/format04.rb, line 82
    def [](code)
      @code_map[code] || 0
    end

supported?() click to toggle source

    # File lib/ttfunk/table/cmap/format04.rb, line 86
    def supported?
      true
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.