Class: PDF::Writer::Object::Contents

Inherits:
PDF::Writer::Object show all
Defined in:
lib/pdf/writer/object/contents.rb

Overview

The contents objects hold all of the content which appears on pages

Instance Attribute Summary collapse

Attributes inherited from PDF::Writer::Object

#oid

Instance Method Summary collapse

Constructor Details

#initialize(parent, page = nil) ⇒ Contents


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pdf/writer/object/contents.rb', line 13

def initialize(parent, page = nil)
  super(parent)

  @data = RUBY_VERSION < '1.9' ? "" : "".force_encoding("BINARY")
  
  @info = {}
  @raw = false
  @on_page = nil

  if page.kind_of?(PDF::Writer::Object::Page)
    @on_page = page
  elsif page == :raw
    @raw = true
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data


30
31
32
# File 'lib/pdf/writer/object/contents.rb', line 30

def data
  @data
end

#on_pageObject (readonly)

Returns the value of attribute on_page


29
30
31
# File 'lib/pdf/writer/object/contents.rb', line 29

def on_page
  @on_page
end

Instance Method Details

#<<(v) ⇒ Object

Raises:

  • (TypeError)

40
41
42
43
44
45
46
47
# File 'lib/pdf/writer/object/contents.rb', line 40

def <<(v)
  raise TypeError unless v.kind_of?(PDF::Writer::Object) or v.kind_of?(String)
  if RUBY_VERSION >= '1.9'
    @data.force_encoding 'BINARY'
    v.force_encoding 'BINARY'
  end
  @data << v
end

#add(a) ⇒ Object


49
50
51
# File 'lib/pdf/writer/object/contents.rb', line 49

def add(a)
  a.each { |k, v| @info[k] = v }
end

#eachObject


36
37
38
# File 'lib/pdf/writer/object/contents.rb', line 36

def each
  @contents.each { |c| yield c }
end

#sizeObject


32
33
34
# File 'lib/pdf/writer/object/contents.rb', line 32

def size
  @data.size
end

#to_sObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdf/writer/object/contents.rb', line 53

def to_s
  tmp = @data.dup
  res = "\n#{@oid} 0 obj\n"
  if @raw
    res << tmp
  else
    res << "<<"
    if PDF::Writer::Compression and @parent.compressed?
      res << " /Filter /FlateDecode"
      tmp = Zlib::Deflate.deflate(tmp)
    end
    @info.each { |k, v| res << "\n/#{k} #{v}" }
    res << "\n/Length #{tmp.size} >>\nstream\n#{tmp}\nendstream"
  end
  res << "\nendobj\n"
  res
end