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

Constant Summary collapse

BUILTINS =
[
  :Courier, :"Courier-Bold", :"Courier-BoldOblique", :"Courier-Oblique",
  :Helvetica, :"Helvetica-Bold", :"Helvetica-BoldOblique", :"Helvetica-Oblique",
  :Symbol,
  :"Times-Roman", :"Times-Bold", :"Times-BoldItalic", :"Times-Italic",
  :ZapfDingbats
]
@@all_metrics =

: PDF::Reader::SynchronizedCache | nil

nil

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ BuiltIn

: (PDF::Reader::Font) -> void



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pdf/reader/width_calculator/built_in.rb', line 26

def initialize(font)
  @font = font
  @@all_metrics ||= PDF::Reader::SynchronizedCache.new
  @metrics = nil #: AFM::Font?

  basefont = extract_basefont(font.basefont)
  metrics_path = File.join(File.dirname(__FILE__), "..","afm","#{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

: (Integer?) -> Numeric



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pdf/reader/width_calculator/built_in.rb', line 42

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


  names = @font.encoding.int_to_name(code_point)
  metrics = names.map { |name|
    @metrics.char_metrics[name.to_s]
  }.compact.first

  if metrics
    metrics[:wx]
  else
    @font.widths[code_point - 1] || 0
  end
end