Class: Origami::XRef

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

Overview

Class representing a Cross-reference information.

Defined Under Namespace

Classes: InvalidXRefSectionError, InvalidXRefSubsectionError, Section, Subsection

Constant Summary collapse

FREE =
"f"
USED =
"n"
FIRSTFREE =
65535
@@regexp =
/(\d{10}) (\d{5}) (n|f)(\r\n| \r| \n)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset, generation, state) ⇒ XRef

Creates a new XRef.

offset

The file offset of the referenced Object.

generation

The generation number of the referenced Object.

state

The state of the referenced Object (FREE or USED).



82
83
84
# File 'lib/origami/xreftable.rb', line 82

def initialize(offset, generation, state)
  @offset, @generation, @state = offset, generation, state
end

Instance Attribute Details

#generationObject

Returns the value of attribute generation.



74
75
76
# File 'lib/origami/xreftable.rb', line 74

def generation
  @generation
end

#offsetObject

Returns the value of attribute offset.



74
75
76
# File 'lib/origami/xreftable.rb', line 74

def offset
  @offset
end

#stateObject

Returns the value of attribute state.



74
75
76
# File 'lib/origami/xreftable.rb', line 74

def state
  @state
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/origami/xreftable.rb', line 86

def self.parse(stream) #:nodoc:
  
  if stream.scan(@@regexp).nil?
    raise InvalidXRefError, "Invalid XRef format"
  end
  
  offset = stream[1].to_i
  generation = stream[2].to_i
  state = stream[3]
  
  XRef.new(offset, generation, state)
end

Instance Method Details

#to_sObject

Outputs self into PDF code.



102
103
104
105
106
107
# File 'lib/origami/xreftable.rb', line 102

def to_s
  off = ("0" * (10 - @offset.to_s.length)) + @offset.to_s
  gen = ("0" * (5 - @generation.to_s.length)) + @generation.to_s
  
  "#{off} #{gen} #{@state}" + EOL
end

#to_xrefstm_data(type_w, field1_w, field2_w) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/origami/xreftable.rb', line 109

def to_xrefstm_data(type_w, field1_w, field2_w)
  type_w <<= 3
  field1_w <<= 3
  field2_w <<= 3

  type = ((@state == FREE) ? "\000" : "\001").unpack("B#{type_w}")[0]

  offset = @offset.to_s(2)
  offset = '0' * (field1_w - offset.size) + offset
  generation = @generation.to_s(2)

  generation = '0' * (field2_w - generation.size) + generation

  [ type , offset, generation ].pack("B#{type_w}B#{field1_w}B#{field2_w}")
end