class HexaPDF::Font::Type1::Font

Represents a Type1 font.

This class abstracts from the specifics of the Type1 font and allows working with it in a standardized way.

The following method calls are forwarded to the contained FontMetrics object:

Attributes

metrics[R]

The associated FontMetrics object.

Public Class Methods

from_afm(source) click to toggle source

Creates a Type1 font object from an AFM source.

# File lib/hexapdf/font/type1/font.rb, line 70
def self.from_afm(source)
  new(AFMParser.parse(source))
end
new(metrics) click to toggle source

Creates a new Type1 font object with the given font metrics.

# File lib/hexapdf/font/type1/font.rb, line 84
def initialize(metrics)
  @metrics = metrics
end

Public Instance Methods

encoding() click to toggle source

Returns the built-in encoding of the font.

# File lib/hexapdf/font/type1/font.rb, line 89
def encoding
  @encoding ||=
    begin
      if @metrics.encoding_scheme == 'AdobeStandardEncoding'
        Encoding.for_name(:StandardEncoding)
      elsif font_name == 'ZapfDingbats' || font_name == 'Symbol'
        Encoding.for_name((font_name + "Encoding").to_sym)
      else
        encoding = Encoding::Base.new
        @metrics.character_metrics.each do |key, char_metric|
          next unless key.kind_of?(Integer) && key >= 0
          encoding.code_to_name[key] = char_metric.name
        end
        encoding
      end
    end
end
features() click to toggle source

Returns a set of features this font supports.

For Type1 fonts, the features that may be available :kern and :liga.

# File lib/hexapdf/font/type1/font.rb, line 127
def features
  @features ||= Set.new.tap do |set|
    set << :kern unless metrics.kerning_pairs.empty?
    set << :liga unless metrics.ligature_pairs.empty?
  end
end
missing_glyph_id() click to toggle source

Returns the name/id of the missing glyph, i.e. .notdef.

# File lib/hexapdf/font/type1/font.rb, line 120
def missing_glyph_id
  :'.notdef'
end
strikeout_position() click to toggle source

Returns the distance from the baseline to the top of the strikeout line.

# File lib/hexapdf/font/type1/font.rb, line 140
def strikeout_position
  # We use the suggested value from the OpenType spec.
  225
end
strikeout_thickness() click to toggle source

Returns the thickness of the strikeout line.

# File lib/hexapdf/font/type1/font.rb, line 146
def strikeout_thickness
  # The OpenType spec suggests the width of an em-dash or 50, so we use that as
  # approximation since the AFM files don't contain this information.
  if (bbox = @metrics.character_metrics[:emdash]&.bbox)
    bbox[3] - bbox[1]
  else
    50
  end
end
underline_position() click to toggle source

Returns the distance from the baseline to the top of the underline.

# File lib/hexapdf/font/type1/font.rb, line 135
def underline_position
  @metrics.underline_position + @metrics.underline_thickness / 2.0
end
width(glyph_name) → width or nil click to toggle source
width(glyph_code) → width or nil

Returns the width of the glyph which can either be specified by glyph name or by an integer that is interpreted according to the built-in encoding.

If there is no glyph found for the name or code, nil is returned.

# File lib/hexapdf/font/type1/font.rb, line 115
def width(glyph)
  (metric = @metrics.character_metrics[glyph]) && metric.width
end