Class: HexaPDF::Font::Type1::Font

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hexapdf/font/type1/font.rb

Overview

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:

  • font_name

  • full_name

  • family_name

  • weight

  • weight_class

  • font_bbox

  • italic_angle

  • ascender

  • descender

  • cap_height

  • x_height

  • horizontal_dominant_width

  • vertical_dominant_width

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metrics) ⇒ Font

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



81
82
83
# File 'lib/hexapdf/font/type1/font.rb', line 81

def initialize(metrics)
  @metrics = metrics
end

Instance Attribute Details

#metricsObject (readonly)

The associated FontMetrics object.



73
74
75
# File 'lib/hexapdf/font/type1/font.rb', line 73

def metrics
  @metrics
end

Class Method Details

.from_afm(source) ⇒ Object

Creates a Type1 font object from an AFM source.



68
69
70
# File 'lib/hexapdf/font/type1/font.rb', line 68

def self.from_afm(source)
  new(AFMParser.parse(source))
end

Instance Method Details

#encodingObject

Returns the built-in encoding of the font.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hexapdf/font/type1/font.rb', line 86

def encoding
  @encoding ||=
    begin
      if @metrics.encoding_scheme == 'AdobeStandardEncoding'.freeze
        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

#featuresObject

Returns a set of features this font supports.

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



124
125
126
127
128
129
# File 'lib/hexapdf/font/type1/font.rb', line 124

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_idObject

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



117
118
119
# File 'lib/hexapdf/font/type1/font.rb', line 117

def missing_glyph_id
  :'.notdef'
end

#width(glyph) ⇒ Object

:call-seq:

font.width(glyph_name)        ->  width or nil
font.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.



112
113
114
# File 'lib/hexapdf/font/type1/font.rb', line 112

def width(glyph)
  (metric = @metrics.character_metrics[glyph]) && metric.width
end