Method: HexaPDF::Type::OptionalContentConfiguration#ocg_state

Defined in:
lib/hexapdf/type/optional_content_configuration.rb

#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