Class: PDF::Reader::Filter::AsciiHex

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/filter/ascii_hex.rb

Overview

implementation of the AsciiHex stream filter

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AsciiHex

Returns a new instance of AsciiHex.



11
12
13
# File 'lib/pdf/reader/filter/ascii_hex.rb', line 11

def initialize(options = {})
  @options = options
end

Instance Method Details

#filter(data) ⇒ Object

Decode the specified data using the AsciiHex algorithm.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pdf/reader/filter/ascii_hex.rb', line 18

def filter(data)
  data.chop! if data[-1,1] == ">"
  data = data[1,data.size] if data[0,1] == "<"

  return "" if data.nil?

  data.gsub!(/[^A-Fa-f0-9]/,"")
  data << "0" if data.size % 2 == 1
  data.scan(/.{2}/).flatten.map { |s| s.hex.chr }.join("")
rescue Exception => e
  # Oops, there was a problem decoding the stream
  raise MalformedPDFError,
      "Error occured while decoding an ASCIIHex stream (#{e.class.to_s}: #{e.to_s})"
end