Class: Origami::XRef::Subsection

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

Overview

Class representing a cross-reference subsection. A subsection contains a continute set of XRef.

Constant Summary collapse

@@regexp =
Regexp.new("(?<start>\\d+) (?<size>\\d+)" + WHITESPACES + "(\\r?\\n|\\r\\n?)")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, entries = []) ⇒ Subsection

Creates a new XRef subsection.

start

The number of the first object referenced in the subsection.

entries

An array of XRef.



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

def initialize(start, entries = [])
    @entries = entries.dup
    @range = Range.new(start, start + entries.size - 1)
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



140
141
142
# File 'lib/origami/xreftable.rb', line 140

def range
  @range
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/origami/xreftable.rb', line 152

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

    if scanner.scan(@@regexp).nil?
        raise InvalidXRefSubsectionError, "Bad subsection format"
    end

    start = scanner['start'].to_i
    size = scanner['size'].to_i

    xrefs = []
    size.times do
        xrefs << XRef.parse(scanner)
    end

    XRef::Subsection.new(start, xrefs)
end

Instance Method Details

#[](no) ⇒ Object

Returns XRef associated with a given object.

no

The Object number.



182
183
184
# File 'lib/origami/xreftable.rb', line 182

def [](no)
    @entries[no - @range.begin]
end

#each(&b) ⇒ Object

Processes each XRef in the subsection.



189
190
191
# File 'lib/origami/xreftable.rb', line 189

def each(&b)
    @entries.each(&b)
end

#each_with_numberObject

Processes each XRef in the subsection, passing the XRef and the object number to the block.



196
197
198
199
200
201
202
203
# File 'lib/origami/xreftable.rb', line 196

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

    counter = @range.to_enum
    @entries.each do |entry|
        yield(entry, counter.next)
    end
end

#has_object?(no) ⇒ Boolean

Returns whether this subsection contains information about a particular object.

no

The Object number.

Returns:



174
175
176
# File 'lib/origami/xreftable.rb', line 174

def has_object?(no)
    @range.include?(no)
end

#sizeObject

The number of entries in the subsection.



208
209
210
# File 'lib/origami/xreftable.rb', line 208

def size
    @entries.size
end

#to_s(eol: $/) ⇒ Object

Outputs self into PDF code.



215
216
217
218
219
220
221
222
# File 'lib/origami/xreftable.rb', line 215

def to_s(eol: $/)
    section = "#{@range.begin} #{@range.end - @range.begin + 1}" + eol
    @entries.each do |xref|
        section << xref.to_s(eol: eol)
    end

    section
end