Class: PDF::Core::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/core/stream.rb

Overview

PDF Stream object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • io (String) (defaults to: nil)

    must be mutable



18
19
20
21
22
# File 'lib/pdf/core/stream.rb', line 18

def initialize(io = nil)
  @filtered_stream = ''
  @stream = io
  @filters = FilterList.new
end

Instance Attribute Details

#filtersPDF::Core::FilterList (readonly)

Stream filters



15
16
17
# File 'lib/pdf/core/stream.rb', line 15

def filters
  @filters
end

Instance Method Details

#<<(io) ⇒ self

Append data to stream.

Parameters:

  • io (String)

Returns:

  • (self)


28
29
30
31
32
# File 'lib/pdf/core/stream.rb', line 28

def <<(io)
  (@stream ||= +'') << io
  @filtered_stream = nil
  self
end

#compress!void

This method returns an undefined value.

Set up stream to be compressed when serialized.



37
38
39
40
41
42
# File 'lib/pdf/core/stream.rb', line 37

def compress!
  unless @filters.names.include?(:FlateDecode)
    @filtered_stream = nil
    @filters << :FlateDecode
  end
end

#compressed?Boolean

Is this stream compressed?

Returns:

  • (Boolean)


47
48
49
# File 'lib/pdf/core/stream.rb', line 47

def compressed?
  @filters.names.include?(:FlateDecode)
end

#dataHash

Stream dictionary

Returns:

  • (Hash)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pdf/core/stream.rb', line 99

def data
  if @stream
    filter_names = @filters.names
    filter_params = @filters.decode_params

    d = {
      Length: filtered_stream.length,
    }
    if filter_names.any?
      d[:Filter] = filter_names
    end
    if filter_params.any? { |f| !f.nil? }
      d[:DecodeParms] = filter_params
    end

    d
  else
    {}
  end
end

#empty?Boolean

Is there any data in this stream?

Returns:

  • (Boolean)


54
55
56
# File 'lib/pdf/core/stream.rb', line 54

def empty?
  @stream.nil?
end

#filtered_streamStream

Stream data with filters applied.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pdf/core/stream.rb', line 61

def filtered_stream
  if @stream
    if @filtered_stream.nil?
      @filtered_stream = @stream.dup

      @filters.each do |(filter_name, params)|
        filter = PDF::Core::Filters.const_get(filter_name)
        if filter
          @filtered_stream = filter.encode(@filtered_stream, params)
        end
      end
    end

    @filtered_stream
  end
end

#inspectString

String representation of the stream for debugging purposes.

Returns:

  • (String)


123
124
125
126
127
128
129
130
131
# File 'lib/pdf/core/stream.rb', line 123

def inspect
  format(
    '#<%<class>s:0x%<object_id>014x @stream=%<stream>s, @filters=%<filters>s>',
    class: self.class.name,
    object_id: object_id,
    stream: @stream.inspect,
    filters: @filters.inspect,
  )
end

#lengthInteger

Size of data in the stream

Returns:

  • (Integer)


81
82
83
# File 'lib/pdf/core/stream.rb', line 81

def length
  @stream.length
end

#objectString

Serialized stream data

Returns:

  • (String)


88
89
90
91
92
93
94
# File 'lib/pdf/core/stream.rb', line 88

def object
  if filtered_stream
    "stream\n#{filtered_stream}\nendstream\n"
  else
    ''
  end
end