Class: HexaPDF::Type::Resources

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

Overview

Represents the resources needed by a content stream.

See: PDF2.0 s7.8.3

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_color_space(color_space) ⇒ Object

Adds the color space to the resources and returns the name under which it is stored.

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



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

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

#add_ext_gstate(object) ⇒ Object

Adds the graphics state parameter dictionary to the resources and returns the name under which it is stored.

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



138
139
140
# File 'lib/hexapdf/type/resources.rb', line 138

def add_ext_gstate(object)
  object_setter(:ExtGState, 'GS', object)
end

#add_font(object) ⇒ Object

Adds the font dictionary to the resources and returns the name under which it is stored.

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



153
154
155
# File 'lib/hexapdf/type/resources.rb', line 153

def add_font(object)
  object_setter(:Font, 'F', object)
end

#add_pattern(object) ⇒ Object

Adds the pattern dictionary to the resources and returns the name under which it is stored.

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



181
182
183
# File 'lib/hexapdf/type/resources.rb', line 181

def add_pattern(object)
  object_setter(:Pattern, 'P', object)
end

#add_property_list(dict) ⇒ Object

Adds the property list to the resources and returns the name under which it is stored.

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



167
168
169
# File 'lib/hexapdf/type/resources.rb', line 167

def add_property_list(dict)
  object_setter(:Properties, 'P', dict)
end

#add_xobject(object) ⇒ Object

Adds the XObject to the resources and returns the name under which it is stored.

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



122
123
124
# File 'lib/hexapdf/type/resources.rb', line 122

def add_xobject(object)
  object_setter(:XObject, 'XO', object)
end

#color_space(name) ⇒ Object

Returns the color space stored under the given name.

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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hexapdf/type/resources.rb', line 67

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

#ext_gstate(name) ⇒ Object

Returns the graphics state parameter dictionary (see Type::GraphicsStateParameter) stored under the given name.

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



130
131
132
# File 'lib/hexapdf/type/resources.rb', line 130

def ext_gstate(name)
  object_getter(:ExtGState, name)
end

#font(name) ⇒ Object

Returns the font dictionary stored under the given name.

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



145
146
147
148
# File 'lib/hexapdf/type/resources.rb', line 145

def font(name)
  font = object_getter(:Font, name)
  font.kind_of?(Hash) ? document.wrap(font) : font
end

#pattern(name) ⇒ Object

Returns the pattern dictionary stored under the given name.

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



174
175
176
# File 'lib/hexapdf/type/resources.rb', line 174

def pattern(name)
  object_getter(:Pattern, name)
end

#property_list(name) ⇒ Object

Returns the property list stored under the given name.

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



160
161
162
# File 'lib/hexapdf/type/resources.rb', line 160

def property_list(name)
  object_getter(:Properties, name)
end

#xobject(name) ⇒ Object

Returns the XObject stored under the given name.

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



115
116
117
# File 'lib/hexapdf/type/resources.rb', line 115

def xobject(name)
  object_getter(:XObject, name)
end