class TTFunk::Table::Cff::Encoding

Constants

DEFAULT_ENCODING_ID
EXPERT_ENCODING_ID
STANDARD_ENCODING_ID

Attributes

count[R]
format[R]
offset_or_id[R]
top_dict[R]

Public Class Methods

codes_for_encoding_id(encoding_id) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 15
def codes_for_encoding_id(encoding_id)
  case encoding_id
  when STANDARD_ENCODING_ID
    Encodings::STANDARD
  when EXPERT_ENCODING_ID
    Encodings::EXPERT
  end
end
new(top_dict, file, offset_or_id = nil, length = nil) click to toggle source
Calls superclass method TTFunk::SubTable::new
# File lib/ttfunk/table/cff/encoding.rb, line 27
def initialize(top_dict, file, offset_or_id = nil, length = nil)
  @top_dict = top_dict
  @offset_or_id = offset_or_id || DEFAULT_ENCODING_ID

  if offset
    super(file, offset, length)
  else
    @count = self.class.codes_for_encoding_id(offset_or_id).size
  end
end

Public Instance Methods

[](glyph_id) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 45
def [](glyph_id)
  return 0 if glyph_id.zero?
  return code_for(glyph_id) if offset

  self.class.codes_for_encoding_id(offset_or_id)[glyph_id]
end
each() { |self| ... } click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 38
def each
  return to_enum(__method__) unless block_given?

  # +1 adjusts for the implicit .notdef glyph
  (count + 1).times { |i| yield self[i] }
end
encode(new_to_old, old_to_new) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 65
def encode(new_to_old, old_to_new)
  # no offset means no encoding was specified (i.e. we're supposed to
  # use a predefined encoding) so there's nothing to encode
  return '' unless offset
  return encode_supplemental(new_to_old, old_to_new) if supplemental?

  codes =
    new_to_old.keys.sort.map do |new_gid|
      code_for(new_to_old[new_gid])
    end

  ranges = TTFunk::BinUtils.rangify(codes)

  # calculate whether storing the charset as a series of ranges is
  # more efficient (i.e. takes up less space) vs storing it as an
  # array of SID values
  total_range_size = (2 * ranges.size) +
    (element_width(:range_format) * ranges.size)

  total_array_size = codes.size * element_width(:array_format)

  if total_array_size <= total_range_size
    ([format_int(:array_format), codes.size] + codes).pack('C*')
  else
    element_fmt = element_format(:range_format)
    result = [format_int(:range_format), ranges.size].pack('CC')
    ranges.each { |range| result << range.pack(element_fmt) }
    result
  end
end
offset() click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 52
def offset
  # Numbers from 0..1 mean encoding IDs instead of offsets. IDs are
  # pre-defined, generic encodings that define the characters present
  # in the font.
  #
  # In the case of an offset, add the CFF table's offset since the
  # charset offset is relative to the start of the CFF table. Otherwise
  # return nil (no offset).
  if offset_or_id > 1
    offset_or_id + top_dict.cff_offset
  end
end
supplemental?() click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 96
def supplemental?
  # high-order bit set to 1 indicates supplemental encoding
  @format >> 7 == 1
end

Private Instance Methods

code_for(glyph_id) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 121
def code_for(glyph_id)
  return 0 if glyph_id.zero?

  # rather than validating the glyph as part of one of the predefined
  # encodings, just pass it through
  return glyph_id unless offset

  case format_sym
  when :array_format, :supplemental
    @entries[glyph_id]

  when :range_format
    remaining = glyph_id

    @entries.each do |range|
      if range.size >= remaining
        return (range.first + remaining) - 1
      end

      remaining -= range.size
    end

    0
  end
end
element_format(fmt = format_sym) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 177
def element_format(fmt = format_sym)
  {
    array_format: 'C',
    range_format: 'CC',
    supplemental: 'Cn'
  }[fmt]
end
element_width(fmt = format_sym) click to toggle source

@TODO: handle supplemental encoding (necessary?)

# File lib/ttfunk/table/cff/encoding.rb, line 186
def element_width(fmt = format_sym)
  case fmt
  when :array_format then 1
  when :range_format then 2
  when :supplemental then 3
  else
    raise Error, "'#{fmt}' is an unsupported encoding format"
  end
end
encode_supplemental(_new_to_old, old_to_new) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 103
def encode_supplemental(_new_to_old, old_to_new)
  new_entries =
    @entries.each_with_object({}) do |(code, old_gid), ret|
      if (new_gid = old_to_new[old_gid])
        ret[code] = new_gid
      end
    end

  result = [format_int(:supplemental), new_entries.size].pack('CC')
  fmt = element_format(:supplemental)

  new_entries.each do |code, new_gid|
    result << [code, new_gid].pack(fmt)
  end

  result
end
format_int(sym = format_sym) click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 207
def format_int(sym = format_sym)
  case sym
  when :array_format then 0
  when :range_format then 1
  when :supplemental then 129
  else
    raise Error, "unsupported charset format '#{sym}'"
  end
end
format_sym() click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 196
def format_sym
  return :supplemental if supplemental?

  case @format
  when 0 then :array_format
  when 1 then :range_format
  else
    raise Error, "unsupported charset format '#{fmt}'"
  end
end
parse!() click to toggle source
# File lib/ttfunk/table/cff/encoding.rb, line 147
def parse!
  @format, entry_count = read(2, 'C*')
  @length = entry_count * element_width

  case format_sym
  when :array_format
    @count = entry_count
    @entries = OneBasedArray.new(read(length, 'C*'))

  when :range_format
    @entries = []
    @count = 0

    entry_count.times do
      code, num_left = read(element_width, element_format)
      @entries << (code..(code + num_left))
      @count += num_left + 1
    end

  when :supplemental
    @entries = {}
    @count = entry_count

    entry_count.times do
      code, glyph = read(element_width, element_format)
      @entries[code] = glyph
    end
  end
end