class HexaPDF::Font::TrueType::Table::Glyf::Glyph

Represents the definition of a glyph. Since the purpose of this implementation is not editing or rendering glyphs, the raw glyph data is only decoded so far as to get general information about the glyph.

Attributes

component_offsets[R]

The array with the component glyph offsets, or nil if this is not a compound glyph.

components[R]

The array with the component glyph IDs, or nil if this is not a compound glyph.

number_of_contours[R]

The number of contours in the glyph. A zero or positive number implies a simple glyph, a negative number a glyph made up from multiple components

raw_data[R]

Contains the raw byte data of the glyph.

x_max[R]

The maximum x value for coordinate data.

x_min[R]

The minimum x value for coordinate data.

y_max[R]

The maximum y value for coordinate data.

y_min[R]

The minimum y value for coordinate data.

Public Class Methods

new(raw_data) click to toggle source

Creates a new glyph from the given raw data.

# File lib/hexapdf/font/true_type/table/glyf.rb, line 83
def initialize(raw_data)
  @raw_data = raw_data
  @number_of_contours, @x_min, @y_min, @x_max, @y_max = @raw_data.unpack('s>5')
  @number_of_contours ||= 0
  @x_min ||= 0
  @y_min ||= 0
  @x_max ||= 0
  @y_max ||= 0
  @components = nil
  @component_offsets = nil
  parse_compound_glyph if compound?
end

Public Instance Methods

compound?() click to toggle source

Returns true if this a compound glyph.

# File lib/hexapdf/font/true_type/table/glyf.rb, line 97
def compound?
  number_of_contours < 0
end

Private Instance Methods

parse_compound_glyph() click to toggle source

Parses the raw data to get the component glyphs.

This is needed because the component glyphs are referenced by their glyph IDs and those may change when subsetting the font.

# File lib/hexapdf/font/true_type/table/glyf.rb, line 113
def parse_compound_glyph
  @components = []
  @component_offsets = []
  index = 10
  while true
    flags, glyph_id = raw_data[index, 4].unpack('n2')
    @components << glyph_id
    @component_offsets << (index + 2)
    break if flags & FLAG_MORE_COMPONENTS == 0

    index += 4 # fields flags and glyphIndex
    index += (flags & FLAG_ARG_1_AND_2_ARE_WORDS == 0 ? 2 : 4) # arguments
    if flags & FLAG_WE_HAVE_A_TWO_BY_TWO != 0 # transformation
      index += 8
    elsif flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE != 0
      index += 4
    elsif flags & FLAG_WE_HAVE_A_SCALE != 0
      index += 2
    end
  end
end