class HexaPDF::Type::Resources

See: PDF2.0 s7.8.3
Represents the resources needed by a content stream.

def add_color_space(color_space)

respective name is just returned.
color spaces +:DeviceGray+, +:DeviceRGB+ and +:DeviceCMYK+ are never stored, their
If there already exists a color space with the same definition, it is reused. The device

Adds the color space to the resources and returns the name under which it is stored.
def add_color_space(color_space)
  family = color_space.family
  return family if family == :DeviceRGB || family == :DeviceGray || family == :DeviceCMYK
  definition = color_space.definition
  self[:ColorSpace] = {} unless key?(:ColorSpace)
  color_space_dict = self[:ColorSpace]
  name, _value = color_space_dict.value.find do |_k, v|
    v.map! {|item| document.deref(item) }
    v == definition
  end
  unless name
    name = create_resource_name(color_space_dict.value, 'CS')
    color_space_dict[name] = definition
  end
  name
end

def add_ext_gstate(object)

If there already exists a name for the given dictionary, it is just returned.

which it is stored.
Adds the graphics state parameter dictionary to the resources and returns the name under
def add_ext_gstate(object)
  object_setter(:ExtGState, 'GS', object)
end

def add_font(object)

If there already exists a name for the given dictionary, it is just returned.

Adds the font dictionary to the resources and returns the name under which it is stored.
def add_font(object)
  object_setter(:Font, 'F', object)
end

def add_pattern(object)

If there already exists a name for the given dictionary, it is just returned.

Adds the pattern dictionary to the resources and returns the name under which it is stored.
def add_pattern(object)
  object_setter(:Pattern, 'P', object)
end

def add_property_list(dict)

If there already exists a name for the given property list, it is just returned.

Adds the property list to the resources and returns the name under which it is stored.
def add_property_list(dict)
  object_setter(:Properties, 'P', dict)
end

def add_xobject(object)

If there already exists a name for the given XObject, it is just returned.

Adds the XObject to the resources and returns the name under which it is stored.
def add_xobject(object)
  object_setter(:XObject, 'XO', object)
end

def color_space(name)

lookup since they are fixed.
Note: The color spaces :DeviceGray, :DeviceRGB and :DeviceCMYK are returned without a

If the color space is not found, an error is raised.

Returns the color space stored under the given name.
def color_space(name)
  case name
  when :DeviceRGB, :DeviceGray, :DeviceCMYK
    GlobalConfiguration.constantize('color_space.map', name).new
  else
    space_definition = (name == :Pattern ? name : self[:ColorSpace]&.[](name))
    if space_definition.nil?
      raise HexaPDF::Error, "Color space '#{name}' not found in the resources"
    elsif space_definition.kind_of?(Array)
      space_family = space_definition[0]
    else
      space_family = space_definition
      space_definition = [space_definition]
    end
    GlobalConfiguration.constantize('color_space.map', space_family) do
      HexaPDF::Content::ColorSpace::Universal
    end.new(space_definition)
  end
end

def create_resource_name(hash, prefix)

Returns a unique name that can be used to store a resource in the given hash.
def create_resource_name(hash, prefix)
  n = hash.size + 1
  while true
    name = :"#{prefix}#{n}"
    return name unless hash.key?(name)
    n += 1
  end
end

def ext_gstate(name)

If the dictionary is not found, an error is raised.

under the given name.
Returns the graphics state parameter dictionary (see Type::GraphicsStateParameter) stored
def ext_gstate(name)
  object_getter(:ExtGState, name)
end

def font(name)

If the dictionary is not found, an error is raised.

Returns the font dictionary stored under the given name.
def font(name)
  font = object_getter(:Font, name)
  font.kind_of?(Hash) ? document.wrap(font) : font
end

def object_getter(dict_name, name)

Helper method for returning an entry of a subdictionary.
def object_getter(dict_name, name)
  obj = self[dict_name] && self[dict_name][name]
  if obj.nil?
    raise HexaPDF::Error, "No object called '#{name}' stored under /#{dict_name}"
  end
  obj
end

def object_setter(dict_name, prefix, object)

Helper method for setting an entry of a subdictionary.
def object_setter(dict_name, prefix, object)
  self[dict_name] = {} unless key?(dict_name)
  dict = self[dict_name]
  name, _value = dict.each.find {|_, dict_obj| dict_obj == object }
  unless name
    name = create_resource_name(dict.value, prefix)
    dict[name] = object
  end
  name
end

def pattern(name)

If the dictionary is not found, an error is raised.

Returns the pattern dictionary stored under the given name.
def pattern(name)
  object_getter(:Pattern, name)
end

def perform_validation

Ensures that a valid procedure set is available.
def perform_validation
  val = self[:ProcSet]
  if val.kind_of?(Symbol)
    yield("Procedure set is a single value instead of an Array", true)
    val = value[:ProcSet] = [val]
  end
  super
  return unless val
  val.reject! do |name|
    case name
    when :PDF, :Text, :ImageB, :ImageC, :ImageI
      false
    else
      yield("Invalid page procedure set name /#{name}", true)
      true
    end
  end
end

def property_list(name)

If the property list is not found, an error is raised.

Returns the property list stored under the given name.
def property_list(name)
  object_getter(:Properties, name)
end

def xobject(name)

If the XObject is not found, an error is raised.

Returns the XObject stored under the given name.
def xobject(name)
  object_getter(:XObject, name)
end