Class: Origami::XRef::Section

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

Overview

Class representing a Cross-reference table. A section contains a set of XRef::Subsection.

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.



244
245
246
# File 'lib/origami/xreftable.rb', line 244

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

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/origami/xreftable.rb', line 248

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

    if scanner.skip(@@regexp_open).nil?
        raise InvalidXRefSectionError, "No xref token found"
    end

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

    XRef::Section.new(subsections)
end

Instance Method Details

#<<(subsection) ⇒ Object

Appends a new subsection.

subsection

A XRefSubsection.



267
268
269
# File 'lib/origami/xreftable.rb', line 267

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

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

Returns a XRef associated with a given object.

no

The Object number.



275
276
277
278
279
280
281
# File 'lib/origami/xreftable.rb', line 275

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

    nil
end

#clearObject

Clear all the entries.



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

def clear
    @subsections.clear
end

#each(&b) ⇒ Object

Processes each XRef in each Subsection.



287
288
289
290
291
292
293
# File 'lib/origami/xreftable.rb', line 287

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.



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

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.



298
299
300
301
302
303
304
# File 'lib/origami/xreftable.rb', line 298

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.



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

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

#subsectionsObject

Returns an Array of Subsection.



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

def subsections
    @subsections
end

#to_s(eol: $/) ⇒ Object

Outputs self into PDF code.



337
338
339
# File 'lib/origami/xreftable.rb', line 337

def to_s(eol: $/)
    "xref" << eol << @subsections.map{|sub| sub.to_s(eol: eol)}.join
end