Class: Origami::ObjectStream

Inherits:
Stream
  • Object
show all
Includes:
Enumerable
Defined in:
lib/origami/stream.rb

Overview

Class representing a Stream containing other Objects.

Constant Summary collapse

NUM =

:nodoc:

0
OBJ =

:nodoc:

1

Constants inherited from Stream

Stream::TOKENS

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Constants included from Object

Origami::Object::TOKENS

Instance Attribute Summary

Attributes inherited from Stream

#dictionary

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Instance Method Summary collapse

Methods inherited from Stream

#[], #[]=, add_type_info, #cast_to, #data, #data=, #decode!, #each_filter, #each_key, #encode!, #encoded_data, #encoded_data=, #filters, guess_type, #key?, native_type, parse, #post_build, #set_predictor, #to_obfuscated_str, #to_s, #value

Methods included from StandardObject

#do_type_check, #has_field?, included, #set_default_value, #set_default_values, #version_required

Methods included from Object

#<=>, #cast_to, #copy, #document, #export, #indirect?, #indirect_parent, #logicalize, #logicalize!, native_type, #native_type, parse, #post_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(raw_data = "", dictionary = {}) ⇒ ObjectStream

Creates a new Object Stream.

dictionary

A hash of attributes to set to the Stream.

raw_data

The Stream data.



530
531
532
533
534
# File 'lib/origami/stream.rb', line 530

def initialize(raw_data = "", dictionary = {})
    super

    @objects = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Origami::Stream

Instance Method Details

#<<(object) ⇒ Object Also known as: insert

Adds a new Object to this Stream.

object

The Object to append.



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/origami/stream.rb', line 568

def <<(object)
    unless object.generation == 0
        raise InvalidObjectError, "Cannot store an object with generation > 0 in an ObjectStream"
    end

    if object.is_a?(Stream)
        raise InvalidObjectError, "Cannot store a Stream in an ObjectStream"
    end

    # We must have an associated document to generate new object numbers.
    if @document.nil?
        raise InvalidObjectError, "The ObjectStream must be added to a document before inserting objects"
    end

    # The object already belongs to a document.
    unless (obj_doc = object.document).nil?
        # Remove the previous instance if the object is indirect to avoid duplicates.
        if obj_doc.equal?(@document)
            @document.delete_object(object.reference) if object.indirect?
        else
            object = object.export
        end
    end

    load! if @objects.nil?

    object.no, object.generation = @document.allocate_new_object_number if object.no == 0

    object.set_indirect(true) # object is indirect
    object.parent = self      # set this stream as the parent
    object.set_document(@document)      # indirect objects need pdf information
    @objects[object.no] = object

    Reference.new(object.no, 0)
end

#delete(no) ⇒ Object

Deletes Object no.



608
609
610
611
612
# File 'lib/origami/stream.rb', line 608

def delete(no)
    load! if @objects.nil?

    @objects.delete(no)
end

#each(&b) ⇒ Object Also known as: each_object

Iterates over each object in the stream.



664
665
666
667
668
# File 'lib/origami/stream.rb', line 664

def each(&b)
    load! if @objects.nil?

    @objects.values.each(&b)
end

#extract(no) ⇒ Object

Returns a given decompressed object contained in the Stream.

no

The Object number.



632
633
634
635
636
# File 'lib/origami/stream.rb', line 632

def extract(no)
    load! if @objects.nil?

    @objects[no]
end

#extract_by_index(index) ⇒ Object

Returns a given decompressed object by index.

index

The Object index in the ObjectStream.

Raises:

  • (TypeError)


642
643
644
645
646
647
648
649
# File 'lib/origami/stream.rb', line 642

def extract_by_index(index)
    load! if @objects.nil?

    raise TypeError, "index must be an integer" unless index.is_a?(::Integer)
    raise IndexError, "index #{index} out of range" if index < 0 or index >= @objects.size

    @objects.to_a.sort[index][1]
end

#include?(no) ⇒ Boolean

Returns whether a specific object is contained in this stream.

no

The Object number.

Returns:



655
656
657
658
659
# File 'lib/origami/stream.rb', line 655

def include?(no)
    load! if @objects.nil?

    @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.



617
618
619
620
621
622
623
624
625
626
# File 'lib/origami/stream.rb', line 617

def index(no)
    ind = 0
    @objects.to_a.sort.each do |num, obj|
        return ind if num == no

        ind = ind + 1
    end

    nil
end

#lengthObject

Returns the number of objects contained in the stream.



674
675
676
677
678
# File 'lib/origami/stream.rb', line 674

def length
    raise InvalidObjectStreamObjectError, "Invalid number of objects" unless self.N.is_a?(Integer)

    self.N.to_i
end

#objectsObject

Returns the array of inner objects.



683
684
685
686
687
# File 'lib/origami/stream.rb', line 683

def objects
    load! if @objects.nil?

    @objects.values
end

#pre_buildObject

:nodoc:



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/origami/stream.rb', line 536

def pre_build #:nodoc:
    load! if @objects.nil?

    prolog = ""
    data = ""
    objoff = 0
    @objects.to_a.sort.each do |num,obj|

        obj.set_indirect(false)
        obj.objstm_offset = objoff

        prolog << "#{num} #{objoff} "
        objdata = "#{obj.to_s} "

        objoff += objdata.size
        data << objdata
        obj.set_indirect(true)
        obj.no = num
    end

    self.data = prolog + data

    @dictionary[:N] = @objects.size
    @dictionary[:First] = prolog.size

    super
end