Class: PDF::Core::Stream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ Stream

Returns a new instance of Stream.



12
13
14
15
16
# File 'lib/pdf/core/stream.rb', line 12

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

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



10
11
12
# File 'lib/pdf/core/stream.rb', line 10

def filters
  @filters
end

Instance Method Details

#<<(io) ⇒ Object



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

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

#compress!Object



24
25
26
27
28
29
# File 'lib/pdf/core/stream.rb', line 24

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

#compressed?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pdf/core/stream.rb', line 31

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

#dataObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdf/core/stream.rb', line 69

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/pdf/core/stream.rb', line 35

def empty?
  @stream.nil?
end

#filtered_streamObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pdf/core/stream.rb', line 39

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
    # XXX Fillter stream
  end
end

#inspectObject



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

def inspect
  "#<#{self.class.name}:0x#{format '%014x', object_id} "\
    "@stream=#{@stream.inspect}, @filters=#{@filters.inspect}>"
end

#lengthObject



57
58
59
# File 'lib/pdf/core/stream.rb', line 57

def length
  @stream.length
end

#objectObject



61
62
63
64
65
66
67
# File 'lib/pdf/core/stream.rb', line 61

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