Class: Origami::XRef::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/xreftable.rb

Overview

Class representing a Cross-reference table. A section contains a set of XRefSubsection.

Constant Summary collapse

TOKEN =
"xref"
@@regexp_open =
Regexp.new(WHITESPACES + TOKEN + WHITESPACES + "(\\r?\\n|\\r\\n?)")
@@regexp_sub =
Regexp.new("(\\d+) (\\d+)" + WHITESPACES + "(\\r?\\n|\\r\\n?)")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subsections = []) ⇒ Section

Creates a new XRef section.

subsections

An array of XRefSubsection.



240
241
242
# File 'lib/origami/xreftable.rb', line 240

def initialize(subsections = [])
    @subsections = subsections
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/origami/xreftable.rb', line 244

def self.parse(stream) #:nodoc:
    if stream.skip(@@regexp_open).nil?
        raise InvalidXRefSectionError, "No xref token found"
    end

    subsections = []
    while stream.match?(@@regexp_sub) do
        subsections << XRef::Subsection.parse(stream)
    end

    XRef::Section.new(subsections)
end

Instance Method Details

#<<(subsection) ⇒ Object

Appends a new subsection.

subsection

A XRefSubsection.



261
262
263
# File 'lib/origami/xreftable.rb', line 261

def <<(subsection)
    @subsections << subsection
end

#[](no) ⇒ Object Also known as: find

Returns a XRef associated with a given object.

no

The Object number.



269
270
271
272
273
274
275
# File 'lib/origami/xreftable.rb', line 269

def [](no)
    @subsections.each do |s|
        return s[no] if s.has_object?(no)
    end

    nil
end

#clearObject

Clear all the entries.



317
318
319
# File 'lib/origami/xreftable.rb', line 317

def clear
    @subsections.clear
end

#each(&b) ⇒ Object

Processes each XRef in each Subsection.



281
282
283
284
285
286
287
# File 'lib/origami/xreftable.rb', line 281

def each(&b)
    return enum_for(__method__) { self.size } unless block_given?

    @subsections.each do |subsection|
        subsection.each(&b)
    end
end

#each_subsection(&b) ⇒ Object

Processes each Subsection in this table.



303
304
305
# File 'lib/origami/xreftable.rb', line 303

def each_subsection(&b)
    @subsections.each(&b)
end

#each_with_number(&b) ⇒ Object

Processes each XRef in each Subsection, passing the XRef and the object number.



292
293
294
295
296
297
298
# File 'lib/origami/xreftable.rb', line 292

def each_with_number(&b)
    return enum_for(__method__) { self.size } unless block_given?

    @subsections.each do |subsection|
        subsection.each_with_number(&b)
    end
end

#sizeObject

The number of XRef entries in the Section.



324
325
326
# File 'lib/origami/xreftable.rb', line 324

def size
    @subsections.reduce(0) { |total, subsection| total + subsection.size }
end

#subsectionsObject

Returns an Array of Subsection.



310
311
312
# File 'lib/origami/xreftable.rb', line 310

def subsections
    @subsections
end

#to_sObject

Outputs self into PDF code.



331
332
333
# File 'lib/origami/xreftable.rb', line 331

def to_s
    "xref" << EOL << @subsections.join
end