Class: Pdfrb::Document::OutlineEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/outline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, dest:, document:) ⇒ OutlineEntry

Returns a new instance of OutlineEntry.



71
72
73
74
75
76
77
78
# File 'lib/pdfrb/document/outline.rb', line 71

def initialize(title:, dest:, document:)
  @title = title
  @dest = dest
  @document = document
  @children = []
  @value = {}
  @oid = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



68
69
70
# File 'lib/pdfrb/document/outline.rb', line 68

def children
  @children
end

#destObject (readonly)

Returns the value of attribute dest.



68
69
70
# File 'lib/pdfrb/document/outline.rb', line 68

def dest
  @dest
end

#oidObject

Returns the value of attribute oid.



69
70
71
# File 'lib/pdfrb/document/outline.rb', line 69

def oid
  @oid
end

#titleObject (readonly)

Returns the value of attribute title.



68
69
70
# File 'lib/pdfrb/document/outline.rb', line 68

def title
  @title
end

#valueObject (readonly)

Returns the value of attribute value.



68
69
70
# File 'lib/pdfrb/document/outline.rb', line 68

def value
  @value
end

Instance Method Details

#add_child(entry) ⇒ Object



80
81
82
83
# File 'lib/pdfrb/document/outline.rb', line 80

def add_child(entry)
  @children << entry
  entry
end

#build!(document) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pdfrb/document/outline.rb', line 85

def build!(document)
  dict = document.add(
    {
      Title: @title,
      Parent: nil,
    },
    type: Pdfrb::Model::Cos::Dictionary
  )
  dict.value[:Dest] = @dest if @dest

  @oid = dict.oid
  @value = dict.value

  if @children.any?
    first_child = nil
    prev_child = nil
    @children.each do |child|
      child_ref = child.build!(document)
      child.value[:Parent] =
        Pdfrb::Model::Reference.new(dict.oid, dict.gen)
      child.value[:Prev] = prev_child.value ? Pdfrb::Model::Reference.new(prev_child.oid, 0) : nil if prev_child
      prev_child&.value&.[]=(:Next, Pdfrb::Model::Reference.new(child_ref.oid, 0))
      first_child ||= child_ref
      prev_child = child
    end
    dict.value[:First] = Pdfrb::Model::Reference.new(first_child.oid, 0)
    dict.value[:Last] = Pdfrb::Model::Reference.new(prev_child.oid, 0)
    dict.value[:Count] = @children.length
  end

  dict
end