Class: Podoff::Obj

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

Constant Summary collapse

ATTRIBUTES =
{ type: 'Type', contents: 'Contents', kids: 'Kids' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, ref, opts = {}) ⇒ Obj

Returns a new instance of Obj.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/podoff.rb', line 359

def initialize(doc, ref, opts={})

  @document = doc
  @ref = ref

  @start_index = opts[:start_index]
  @end_index = opts[:end_index]
  @attributes = nil
  @source = opts[:source]

  @stream = opts[:stream]
  @stream.obj = self if @stream

  recompute_attributes
  #@source.obj = self if @source.is_a?(Podoff::Stream)

  @document.scanner.pos = @end_index if @document.scanner && @end_index
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



357
358
359
# File 'lib/podoff.rb', line 357

def attributes
  @attributes
end

#documentObject (readonly)

Returns the value of attribute document.



353
354
355
# File 'lib/podoff.rb', line 353

def document
  @document
end

#end_indexObject (readonly)

Returns the value of attribute end_index.



355
356
357
# File 'lib/podoff.rb', line 355

def end_index
  @end_index
end

#refObject (readonly)

Returns the value of attribute ref.



354
355
356
# File 'lib/podoff.rb', line 354

def ref
  @ref
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



355
356
357
# File 'lib/podoff.rb', line 355

def start_index
  @start_index
end

#streamObject (readonly)

Returns the value of attribute stream.



356
357
358
# File 'lib/podoff.rb', line 356

def stream
  @stream
end

Class Method Details

.extract(doc) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/podoff.rb', line 340

def self.extract(doc)

  sca = doc.scanner

  re = sca.matched[0..-4].strip
  st = sca.pos - sca.matched.length

  i = sca.skip_until(/endobj/); return nil unless i
  en = sca.pos - 1

  Podoff::Obj.new(doc, re, start_index: st, end_index: en)
end

Instance Method Details

#dup(new_doc) ⇒ Object



378
379
380
381
382
383
# File 'lib/podoff.rb', line 378

def dup(new_doc)

  self.class.new(
    new_doc, ref,
    start_index: start_index, end_index: end_index)
end

#insert_contents(obj_or_ref) ⇒ Object Also known as: insert_content



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/podoff.rb', line 427

def insert_contents(obj_or_ref)

  fail ArgumentError.new("target '#{ref}' not a replica") \
    unless @source
  fail ArgumentError.new("target '#{ref}' doesn't have /Contents") \
    unless @attributes[:contents]

  re = obj_or_ref
  re = re.obj if re.respond_to?(:obj) # Stream
  re = re.ref if re.respond_to?(:ref)

  add_to_attribute(:contents, re)
end

#insert_font(nick, obj_or_ref) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/podoff.rb', line 414

def insert_font(nick, obj_or_ref)

  fail ArgumentError.new("target '#{ref}' not a replica") \
    unless @source

  nick = nick[1..-1] if nick[0] == '/'

  re = obj_or_ref
  re = re.ref if re.respond_to?(:ref)

  @source = @source.gsub(/\/Font\s*<</, "/Font\n<<\n/#{nick} #{re} R")
end

#replica?Boolean

Returns:

  • (Boolean)


404
405
406
407
# File 'lib/podoff.rb', line 404

def replica?

  @source != nil
end

#replicateObject

def self.create(doc, ref, source)

self.new(doc, ref, nil, nil, nil, source)

end



389
390
391
392
# File 'lib/podoff.rb', line 389

def replicate

  self.class.new(document, ref, source: source.dup)
end

#sourceObject



399
400
401
402
# File 'lib/podoff.rb', line 399

def source

  @source || (@start_index && @document.source[@start_index..@end_index])
end

#to_aObject



394
395
396
397
# File 'lib/podoff.rb', line 394

def to_a

  [ @ref, @start_index, @end_index, @attributes ]
end

#to_sObject



442
443
444
445
# File 'lib/podoff.rb', line 442

def to_s

  source || stream.to_s
end

#typeObject



409
410
411
412
# File 'lib/podoff.rb', line 409

def type

  @attributes && @attributes[:type]
end