File: outline_item.rb

package info (click to toggle)
ruby-pdf-core 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: ruby: 2,270; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,312 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

module PDF
  module Core
    # Outline item.
    #
    # @api private
    # @see # PDF 1.7 spec, section 8.2.2 Document Outline
    class OutlineItem
      # The total number of its open descendants at all lower levels of the
      # outline hierarchy.
      # @return [Integer]
      attr_accessor :count

      # The first of this item’s immediate children in the outline hierarchy.
      # @return [Reference<PDF::Core::OutlineItem>]
      attr_accessor :first

      # The last of this item’s immediate children in the outline hierarchy.
      # @return [Reference<PDF::Core::OutlineItem>]
      attr_accessor :last

      # The next item at this outline level.
      # @return [Reference<PDF::Core::OutlineItem>]
      attr_accessor :next

      # The previous item at this outline level.
      # @return [Reference<PDF::Core::OutlineItem>]
      attr_accessor :prev

      # The parent of this item in the outline hierarchy.
      # @return [Reference<[PDF::Core::OutlineItem, PDF::Core::OutlineRoot]>]
      attr_accessor :parent

      # The text to be displayed on the screen for this item.
      # @return [String]
      attr_accessor :title

      # The destination to be displayed when this item is activated.
      # @return [String]
      # @return [Symbol]
      # @return [Array]
      # @see Destinations
      attr_accessor :dest

      # Is this item open or closed.
      # @return [Boolean]
      attr_accessor :closed

      # @param title [String]
      # @param parent [PDF::Core::OutlineRoot, PDF::Core::OutlineItem]
      # @param options [Hash]
      # @option options :closed [Boolean]
      def initialize(title, parent, options)
        @closed = options[:closed]
        @title = title
        @parent = parent
        @count = 0
      end

      # A hash representation of this outline item.
      #
      # @return [Hash]
      def to_hash
        hash = {
          Title: title,
          Parent: parent,
          Count: closed ? -count : count,
        }
        [
          { First: first }, { Last: last }, { Next: defined?(@next) && @next },
          { Prev: prev }, { Dest: dest },
        ].each do |h|
          unless h.values.first.nil?
            hash.merge!(h)
          end
        end
        hash
      end
    end
  end
end