Class: PDF::Reader::WidthCalculator::BuiltIn

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/width_calculator/built_in.rb

Overview

Type1 fonts can be one of 14 “built in” standard fonts. In these cases, the reader is expected to have it's own copy of the font metrics. see Section 9.6.2.2, PDF 32000-1:2008, pp 256

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ BuiltIn

Returns a new instance of BuiltIn


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pdf/reader/width_calculator/built_in.rb', line 64

def initialize(font)
  @font = font
  @@all_metrics ||= PDF::Reader::SynchronizedCache.new

  metrics_path = File.join(File.dirname(__FILE__), "..","afm","#{font.basefont}.afm")

  if File.file?(metrics_path)
    @metrics = @@all_metrics[metrics_path] ||= AFM::Font.new(metrics_path)
  else
    raise ArgumentError, "No built-in metrics for #{font.basefont}"
  end
end

Instance Method Details

#glyph_width(code_point) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pdf/reader/width_calculator/built_in.rb', line 77

def glyph_width(code_point)
  return 0 if code_point.nil? || code_point < 0

  m = @metrics.char_metrics_by_code[code_point]
  if m.nil?
    names = @font.encoding.int_to_name(code_point)
    m = names.map { |name|
      @metrics.char_metrics[name.to_s]
    }.compact.first
  end

  if m
    m[:wx]
  elsif @font.widths[code_point - 1]
    @font.widths[code_point - 1]
  elsif control_character?(code_point)
    0
  else
    raise MalformedPDFError, "Unknown glyph width for #{code_point} #{@font.basefont}"
  end
end