Sha256: c4a55ebfa025dc73350a36bb525fdcd1ccb4f7b2e8366f8e1aaf37fcadbc10a1

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# coding: utf-8

class PDF::Reader

  class AbstractStrategy # :nodoc:

    def initialize(ohash, receivers, options = {})
      @ohash, @options = ohash, options
      if receivers.is_a?(Array)
        @receivers = receivers
      else
        @receivers = [receivers]
      end
    end

    private

    def options
      @options || {}
    end

    # calls the name callback method on the receiver class with params as the arguments
    #
    def callback (name, params=[])
      @receivers.each do |receiver|
        receiver.send(name, *params) if receiver.respond_to?(name)
      end
    end

    # strings outside of page content should be in either PDFDocEncoding or UTF-16.
    def decode_strings(obj)
      case obj
      when String then
        if obj[0,2].unpack("C*").slice(0,2) == [254,255]
          PDF::Reader::Encoding.new(:UTF16Encoding).to_utf8(obj[2, obj.size])
        else
          PDF::Reader::Encoding.new(:PDFDocEncoding).to_utf8(obj)
        end
      when Hash   then obj.each { |key,val| obj[key] = decode_strings(val) }
      when Array  then obj.collect { |item| decode_strings(item) }
      else
        obj
      end
    end

    def info
      ohash.object(trailer[:Info])
    end

    def info?
      info ? true : false
    end

    def ohash
      @ohash
    end

    def pages
      ohash.object(root[:Pages])
    end

    def pages?
      pages ? true : false
    end

    def root
      ohash.object(trailer[:Root])
    end

    def root?
      root ? true : false
    end

    def trailer
      ohash.trailer
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pdf-reader-0.10.1 lib/pdf/reader/abstract_strategy.rb
pdf-reader-0.10.0 lib/pdf/reader/abstract_strategy.rb