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?)")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subsections = []) ⇒ Section

Creates a new XRef section.

subsections

An array of XRefSubsection.



242
243
244
# File 'lib/origami/xreftable.rb', line 242

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

Instance Attribute Details

#subsectionsObject (readonly)

Returns an Array of Subsection.



314
315
316
# File 'lib/origami/xreftable.rb', line 314

def subsections
  @subsections
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



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

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)
    subsections << XRef::Subsection.parse(scanner)
  end

  XRef::Section.new(subsections)
end

Instance Method Details

#<<(subsection) ⇒ Object

Appends a new subsection.

subsection

A XRefSubsection.



265
266
267
# File 'lib/origami/xreftable.rb', line 265

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

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

Returns a XRef associated with a given object.

no

The Object number.



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

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

  nil
end

#clearObject

Clear all the entries.



319
320
321
# File 'lib/origami/xreftable.rb', line 319

def clear
  @subsections.clear
end

#each(&b) ⇒ Object

Processes each XRef in each Subsection.



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

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

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

#each_subsection(&b) ⇒ Object

Processes each Subsection in this table.



307
308
309
# File 'lib/origami/xreftable.rb', line 307

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.



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

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

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

#sizeObject

The number of XRef entries in the Section.



326
327
328
# File 'lib/origami/xreftable.rb', line 326

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

#to_s(eol: $/) ⇒ Object

Outputs self into PDF code.



333
334
335
336
337
# File 'lib/origami/xreftable.rb', line 333

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