Sha256: f105868ba949da4e693dcd8a76ad4e6a4c2971e1c1262ab07f35266d02162758

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

#--
# PDF::Writer for Ruby.
#   http://rubyforge.org/projects/ruby-pdf/
#   Copyright 2003 - 2005 Austin Ziegler.
#
#   Licensed under a MIT-style licence. See LICENCE in the main distribution
#   for full licensing information.
#
# $Id$
#++
  # The contents objects hold all of the content which appears on pages
class PDF::Writer::Object::Contents < PDF::Writer::Object
  def initialize(parent, page = nil)
    super(parent)

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

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

  attr_reader   :on_page
  attr_accessor :data

  def size
    @data.size
  end

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

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

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

  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eventioz-pdf-writer-1.2.4 lib/pdf/writer/object/contents.rb
eventioz-pdf-writer-1.0 lib/pdf/writer/object/contents.rb