Class: HexaPDF::Type::OptionalContentConfiguration

Inherits:
Dictionary show all
Defined in:
lib/hexapdf/type/optional_content_configuration.rb

Overview

Represents an optional content configuration dictionary.

This dictionary is used for the /D and /Configs entries in the optional content properties dictionary. It configures the states of the OCGs as well as defines how those states may be changed by a PDF processor.

See: PDF2.0 s8.11.4.3

Defined Under Namespace

Classes: UsageApplication

Constant Summary

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_hash, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#add_ocg_to_ui(ocg, path: nil) ⇒ Object

Makes the given optional content group visible in an interactive PDF processor’s user interface.

The OCG is always added to the end of the specified path or, if path is not specified, the top level.

The optional argument path specifies the strings or OCGs under which the given OCG should hierarchically be nested. A string is used as a non-selectable label, an OCG reflects an actual nesting of the involved OCGs.

Examples:

configuration.add_ocg_to_ui(ocg)                   # Add the OCG as top-level item
configuration.add_ocg_to_ui(ocg, path: 'Debug')    # Add the OCG under the label 'Debug'
# Add the OCG under the label 'Page1' which is under the label 'Debug'
configuration.add_ocg_to_ui(ocg, path: ['Debug', 'Page1'])
configuration.add_ocg_to_ui(ocg, path: other_ocg)  # Add the OCG under the other OCG


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/hexapdf/type/optional_content_configuration.rb', line 134

def add_ocg_to_ui(ocg, path: nil)
  array = self[:Order] ||= []
  path = Array(path)
  until path.empty?
    item = path.shift
    index = array.index do |entry|
      if (entry.kind_of?(Array) || entry.kind_of?(PDFArray)) && item.kind_of?(String)
        entry.first == item
      else
        entry == item
      end
    end

    if item.kind_of?(String)
      unless index
        array << [item]
        index = -1
      end
      array = array[index]
    else
      unless index
        array << item << []
        index = -2
      end
      unless array[index + 1].kind_of?(Array) || array[index + 1].kind_of?(PDFArray)
        array.insert(index + 1, [])
      end
      array = array[index + 1]
    end
  end
  array << ocg
end

#ocg_on?(ocg) ⇒ Boolean

Returns true if the given optional content group is on.

Returns:



113
114
115
# File 'lib/hexapdf/type/optional_content_configuration.rb', line 113

def ocg_on?(ocg)
  ocg_state(ocg) == :on
end

#ocg_state(ocg, state = nil) ⇒ Object

:call-seq:

configuration.ocg_state(ocg)          -> state
configuration.ocg_state(ocg, state)   -> state

Returns the state (:on, :off or nil) of the optional content group if the state argument is not given. Otherwise sets the state of the OCG to the given state value (:on/:ON or :off/:OFF).

The value nil is only returned if the state is not defined by the configuration dictionary (which may only be the case if the configuration dictionary is not the default configuration dictionary).



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hexapdf/type/optional_content_configuration.rb', line 94

def ocg_state(ocg, state = nil)
  if state.nil?
    case self[:BaseState]
    when :ON then self[:OFF]&.include?(ocg) ? :off : :on
    when :OFF then self[:ON]&.include?(ocg) ? :on : :off
    else self[:OFF]&.include?(ocg) ? :off : (self[:ON]&.include?(ocg) ? :on : nil)
    end
  elsif state&.downcase == :on
    (self[:ON] ||= []) << ocg unless self[:ON]&.include?(ocg)
    self[:OFF].delete(ocg) if key?(:OFF)
  elsif state&.downcase == :off
    (self[:OFF] ||= []) << ocg unless self[:OFF]&.include?(ocg)
    self[:ON].delete(ocg) if key?(:ON)
  else
    raise ArgumentError, "Invalid value #{state.inspect} for state argument"
  end
end