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.



85
86
87
# File 'lib/hexapdf/font/type1/font.rb', line 85

def initialize(metrics)
  @metrics = metrics
end

Instance Attribute Details

#metricsObject (readonly)

The associated FontMetrics object.



76
77
78
# File 'lib/hexapdf/font/type1/font.rb', line 76

def metrics
  @metrics
end

Class Method Details

.from_afm(source) ⇒ Object

Creates a Type1 font object from an AFM source.



71
72
73
# File 'lib/hexapdf/font/type1/font.rb', line 71

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

Instance Method Details

#encodingObject

Returns the built-in encoding of the font.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hexapdf/font/type1/font.rb', line 90

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

#featuresObject

Returns a set of features this font supports.

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



128
129
130
131
132
133
# File 'lib/hexapdf/font/type1/font.rb', line 128

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.



121
122
123
# File 'lib/hexapdf/font/type1/font.rb', line 121

def missing_glyph_id
  :'.notdef'
end

#strikeout_positionObject

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



141
142
143
144
# File 'lib/hexapdf/font/type1/font.rb', line 141

def strikeout_position
  # We use the suggested value from the OpenType spec.
  225
end

#strikeout_thicknessObject

Returns the thickness of the strikeout line.



147
148
149
150
151
152
153
154
155
# File 'lib/hexapdf/font/type1/font.rb', line 147

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_positionObject

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



136
137
138
# File 'lib/hexapdf/font/type1/font.rb', line 136

def underline_position
  @metrics.underline_position + @metrics.underline_thickness / 2.0
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.



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

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