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.



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

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

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



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

def dictionary
  @dictionary
end

#documentObject

Returns the value of attribute document.



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

def document
  @document
end

#startxrefObject

Returns the value of attribute startxref.



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

def startxref
  @startxref
end

Class Method Details

.parse(stream, parser = nil) ⇒ Object

:nodoc:



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

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

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

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

  startxref = scanner['startxref'].to_i

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

  Trailer.new(startxref, dictionary)
end

Instance Method Details

#[](key) ⇒ Object

Access a key in the trailer dictionary if present.



145
146
147
# File 'lib/origami/trailer.rb', line 145

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

#[]=(key, value) ⇒ Object

Sets a value in the trailer dictionary.



152
153
154
155
# File 'lib/origami/trailer.rb', line 152

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

#dictionary?Boolean

Returns true if the Trailer contains a Dictionary.



168
169
170
# File 'lib/origami/trailer.rb', line 168

def dictionary?
  !@dictionary.nil?
end

#key?(key) ⇒ Boolean

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



138
139
140
# File 'lib/origami/trailer.rb', line 138

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

#to_obfuscated_strObject



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

def to_obfuscated_str
  content = ""
  if 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.



175
176
177
178
179
180
181
182
183
184
# File 'lib/origami/trailer.rb', line 175

def to_s(indent: 1, eol: $/)
  content = +""
  if 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