Class: Origami::Trailer

Inherits:
Object
  • Object
show all
Includes:
StandardObject
Defined in:
lib/origami/trailer.rb,
lib/origami/obfuscation.rb

Overview

Class representing a PDF file Trailer.

Constant Summary collapse

TOKENS =

:nodoc:

%w{ trailer %%EOF }
XREF_TOKEN =

:nodoc:

"startxref"
@@regexp_open =
Regexp.new(WHITESPACES + TOKENS.first + WHITESPACES)
@@regexp_xref =
Regexp.new(WHITESPACES + XREF_TOKEN + WHITESPACES + "(?<startxref>\\d+)")
@@regexp_close =
Regexp.new(WHITESPACES + TOKENS.last + WHITESPACES)

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StandardObject

included, #pre_build, #version_required

Constructor Details

#initialize(startxref = 0, dictionary = {}) ⇒ Trailer

Creates a new Trailer.

startxref

The file offset to the XRef::Section.

dictionary

A hash of attributes to set in the Trailer Dictionary.



112
113
114
# File 'lib/origami/trailer.rb', line 112

def initialize(startxref = 0, dictionary = {})
    @startxref, self.dictionary = startxref, dictionary && Dictionary.new(dictionary)
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



97
98
99
# File 'lib/origami/trailer.rb', line 97

def dictionary
  @dictionary
end

#documentObject

Returns the value of attribute document.



95
96
97
# File 'lib/origami/trailer.rb', line 95

def document
  @document
end

#startxrefObject

Returns the value of attribute startxref.



96
97
98
# File 'lib/origami/trailer.rb', line 96

def startxref
  @startxref
end

Class Method Details

.parse(stream, parser = nil) ⇒ Object

:nodoc:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/origami/trailer.rb', line 116

def self.parse(stream, parser = nil) #:nodoc:
    scanner = Parser.init_scanner(stream)

    if scanner.skip(@@regexp_open)
        dictionary = Dictionary.parse(scanner, parser)
    else
        dictionary = nil
    end

    if not scanner.scan(@@regexp_xref)
        raise InvalidTrailerError, "Cannot get startxref value"
    end

    startxref = scanner['startxref'].to_i

    if not scanner.scan(@@regexp_close)
        parser.warn("No %%EOF token found") if parser
    end

    Trailer.new(startxref, dictionary)
end

Instance Method Details

#[](key) ⇒ Object

Access a key in the trailer dictionary if present.



148
149
150
# File 'lib/origami/trailer.rb', line 148

def [](key)
    @dictionary[key] if dictionary?
end

#[]=(key, value) ⇒ Object

Sets a value in the trailer dictionary.



155
156
157
158
# File 'lib/origami/trailer.rb', line 155

def []=(key, value)
    self.dictionary = Dictionary.new unless dictionary?
    @dictionary[key] = value
end

#dictionary?Boolean

Returns true if the Trailer contains a Dictionary.

Returns:



171
172
173
# File 'lib/origami/trailer.rb', line 171

def dictionary?
    not @dictionary.nil?
end

#key?(key) ⇒ Boolean

Returns true if the specified key is present in the Trailer dictionary.

Returns:



141
142
143
# File 'lib/origami/trailer.rb', line 141

def key?(key)
    self.dictionary? and @dictionary.key?(key)
end

#to_obfuscated_strObject



233
234
235
236
237
238
239
240
241
242
# File 'lib/origami/obfuscation.rb', line 233

def to_obfuscated_str
    content = ""
    if self.dictionary?
        content << TOKENS.first << $/ << @dictionary.to_obfuscated_str << $/
    end

    content << XREF_TOKEN << $/ << @startxref.to_s << $/ << TOKENS.last << $/

    content
end

#to_s(indent: 1, eol: $/) ⇒ Object

Outputs self into PDF code.



178
179
180
181
182
183
184
185
186
187
# File 'lib/origami/trailer.rb', line 178

def to_s(indent: 1, eol: $/)
    content = ""
    if self.dictionary?
        content << TOKENS.first << eol << @dictionary.to_s(indent: indent, eol: eol) << eol
    end

    content << XREF_TOKEN << eol << @startxref.to_s << eol << TOKENS.last << eol

    content
end