class PDF::Reader::Font

Attributes

ascent[R]
basefont[R]
bbox[R]
descendantfonts[RW]
descent[R]
encoding[RW]
first_char[R]
label[RW]
missing_width[R]
subtype[RW]
tounicode[RW]
widths[R]

Public Class Methods

new(ohash = nil, obj = nil) click to toggle source
# File lib/pdf/reader/font.rb, line 32
def initialize(ohash = nil, obj = nil)
  if ohash.nil? || obj.nil?
    $stderr.puts "DEPREACTION WARNING - PDF::Reader::Font.new should be called with 2 args"
    return
  end
  @ohash = ohash
  @tounicode = nil

  extract_base_info(obj)
  extract_descriptor(obj)
  extract_descendants(obj)

  @encoding ||= PDF::Reader::Encoding.new(:StandardEncoding)
end

Public Instance Methods

basefont=(font) click to toggle source
# File lib/pdf/reader/font.rb, line 47
def basefont=(font)
  # setup a default encoding for the selected font. It can always be overridden
  # with encoding= if required
  case font
  when "Symbol" then
    @encoding = PDF::Reader::Encoding.new("SymbolEncoding")
  when "ZapfDingbats" then
    @encoding = PDF::Reader::Encoding.new("ZapfDingbatsEncoding")
  else
    @encoding = nil
  end
  @basefont = font
end
glyph_width(c) click to toggle source
# File lib/pdf/reader/font.rb, line 69
def glyph_width(c)
  @missing_width ||= 0
  @widths        ||= []
  @widths.fetch(c - @first_char, @missing_width)
end
to_utf8(params) click to toggle source
# File lib/pdf/reader/font.rb, line 61
def to_utf8(params)
  if @tounicode
    to_utf8_via_cmap(params)
  else
    to_utf8_via_encoding(params)
  end
end

Private Instance Methods

extract_base_info(obj) click to toggle source
# File lib/pdf/reader/font.rb, line 77
def extract_base_info(obj)
  @subtype  = @ohash.object(obj[:Subtype])
  @basefont = @ohash.object(obj[:BaseFont])
  @encoding = PDF::Reader::Encoding.new(@ohash.object(obj[:Encoding]))
  @widths   = @ohash.object(obj[:Widths]) || []
  @first_char = @ohash.object(obj[:FirstChar])
  if obj[:ToUnicode]
    stream = @ohash.object(obj[:ToUnicode])
    @tounicode = PDF::Reader::CMap.new(stream.unfiltered_data)
  end
end
extract_descendants(obj) click to toggle source
# File lib/pdf/reader/font.rb, line 99
def extract_descendants(obj)
  return unless obj[:DescendantFonts]

  descendants = @ohash.object(obj[:DescendantFonts])
  @descendantfonts = descendants.map { |desc|
    PDF::Reader::Font.new(@ohash, @ohash.object(desc))
  }
end
extract_descriptor(obj) click to toggle source
# File lib/pdf/reader/font.rb, line 89
def extract_descriptor(obj)
  return unless obj[:FontDescriptor]

  fd       = @ohash.object(obj[:FontDescriptor])
  @ascent  = @ohash.object(fd[:Ascent])
  @descent = @ohash.object(fd[:Descent])
  @missing_width = @ohash.object(fd[:MissingWidth])
  @bbox    = @ohash.object(fd[:FontBBox])
end
to_utf8_via_cmap(params) click to toggle source
# File lib/pdf/reader/font.rb, line 108
def to_utf8_via_cmap(params)
  if params.class == String
    params.unpack(encoding.unpack).map { |c|
      @tounicode.decode(c) || PDF::Reader::Encoding::UNKNOWN_CHAR
    }.flatten.pack("U*")
  elsif params.class == Array
    params.collect { |param| to_utf8_via_cmap(param) }
  else
    params
  end
end
to_utf8_via_encoding(params) click to toggle source
# File lib/pdf/reader/font.rb, line 120
def to_utf8_via_encoding(params)
  raise UnsupportedFeatureError, "font encoding '#{encoding}' currently unsupported" if encoding.kind_of?(String)

  if params.class == String
    encoding.to_utf8(params)
  elsif params.class == Array
    params.collect { |param| to_utf8_via_encoding(param) }
  else
    params
  end
end