Parent

Included Modules

Class/Module Index [+]

Quicksearch

Origami::Stream

Class representing a PDF Stream Object. Streams can be used to hold any kind of data, especially binary data.

Constants

Length

Attributes

dictionary[RW]

Public Class Methods

native_type() click to toggle source
# File lib/origami/stream.rb, line 368
def self.native_type ; Stream end
new(data = "", dictionary = {}) click to toggle source

Creates a new PDF Stream.

data

The Stream uncompressed data.

dictionary

A hash representing the Stream attributes.

# File lib/origami/stream.rb, line 89
def initialize(data = "", dictionary = {})
    super()
    
    set_indirect(true)
   
    @dictionary, @data = Dictionary.new(dictionary), data
    @dictionary.parent = self
end

Public Instance Methods

cast_to(type) click to toggle source
# File lib/origami/stream.rb, line 212
def cast_to(type)
  super(type)

  cast = type.new("", self.dictionary.to_h)
  cast.rawdata = @rawdata.dup
  cast.no, cast.generation = self.no, self.generation
  cast.set_indirect(true) 
  cast.set_pdf(self.pdf) 
  cast.file_offset = self.file_offset

  cast
end
data() click to toggle source

Returns the uncompressed stream content.

# File lib/origami/stream.rb, line 232
def data
  self.decode! if @data.nil?

  @data 
end
data=(str) click to toggle source

Sets the uncompressed stream content.

str

The new uncompressed data.

# File lib/origami/stream.rb, line 242
def data=(str)      
  @rawdata = nil
  @data = str
end
decode!() click to toggle source

Uncompress the stream data.

# File lib/origami/stream.rb, line 268
def decode!
  self.decrypt! if self.is_a?(Encryption::EncryptedStream)
  
  unless is_decoded?
    filters = self.Filter
    
    if filters.nil?
      @data = @rawdata.dup
    else
      case filters
        when Array, Name then
          dparams = self.DecodeParms || []

          dparams = [ dparams ] unless dparams.is_a?(::Array)
          filters = [ filters ] unless filters.is_a?(::Array)
      
          @data = @rawdata.dup
          @data.freeze

          filters.length.times do |layer|
            params = dparams[layer].is_a?(Dictionary) ? dparams[layer] : {}
            filter = filters[layer]

            begin
              @data = decode_data(@data, filter, params)
            rescue Filter::InvalidFilterDataError => e
              @data = e.decoded_data if e.decoded_data
              raise InvalidStreamObjectError, 
                "Error while decoding stream #{self.reference}\n\t-> [#{e.class}] #{e.message}"
            end
          end
        else
          raise InvalidStreamObjectError, "Invalid Filter type parameter"
      end
    end
  end
  
  self
end
encode!() click to toggle source

Compress the stream data.

# File lib/origami/stream.rb, line 311
def encode!

  unless is_encoded?
    filters = self.Filter
    
    if filters.nil?
      @rawdata = @data.dup
    else
      case filters
        when Array, Name then
          dparams = self.DecodeParms || []

          dparams = [ dparams ] unless dparams.is_a?(::Array)
          filters = [ filters ] unless filters.is_a?(::Array)
      
          @rawdata = @data.dup
          (filters.length - 1).downto(0) do |layer|
            params = dparams[layer].is_a?(Dictionary) ? dparams[layer] : {}
            filter = filters[layer]

            @rawdata = encode_data(@rawdata, filter, params)
          end
        else
          raise InvalidStreamObjectError, "Invalid filter type parameter"
      end
    end
    
    self.Length = @rawdata.length
  end
  
  self
end
eval_js() click to toggle source

Evaluates the current Stream as JavaScript.

# File lib/origami/javascript.rb, line 669
def eval_js
  self.pdf.eval_js(self.data)
end
post_build() click to toggle source
# File lib/origami/stream.rb, line 104
def post_build
  self.Length = @rawdata.length
 
  super
end
pre_build() click to toggle source
# File lib/origami/stream.rb, line 98
def pre_build
  encode!
 
  super
end
rawdata() click to toggle source

Returns the raw compressed stream content.

# File lib/origami/stream.rb, line 250
def rawdata
  self.encode! if @rawdata.nil? 

  @rawdata
end
rawdata=(str) click to toggle source

Sets the raw compressed stream content.

str

the new raw data.

# File lib/origami/stream.rb, line 260
def rawdata=(str)
  @rawdata = str
  @data = nil
end
set_predictor(predictor, colors = 1, bitspercomponent = 8, columns = 1) click to toggle source
# File lib/origami/stream.rb, line 190
def set_predictor(predictor, colors = 1, bitspercomponent = 8, columns = 1)
  
  filters = self.Filter
  filters = [ filters ] unless filters.is_a?(::Array)

  if not filters.include?(:FlateDecode) and not filters.include?(:LZWDecode)
    raise InvalidStreamObjectError, 'Predictor functions can only be used with Flate or LZW filters'
  end

  layer = filters.index(:FlateDecode) or filters.index(:LZWDecode)

  params = Filter::LZW::DecodeParms.new
  params[:Predictor] = predictor
  params[:Colors] = colors if colors != 1
  params[:BitsPerComponent] = bitspercomponent if bitspercomponent != 8
  params[:Columns] = columns if columns != 1

  set_decode_params(layer, params)
  
  self
end
to_obfuscated_str() click to toggle source
# File lib/origami/obfuscation.rb, line 206
def to_obfuscated_str
  content = ""
  
  content << @dictionary.to_obfuscated_str
  content << "stream" + EOL
  content << self.rawdata
  content << EOL << TOKENS.last
  
  super(content)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.