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: PDF1.7 s7.8.3

Constant Summary

Constants included from DictionaryFields

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

Constants inherited from Object

Object::NOT_DUPLICATABLE_CLASSES

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_h, type, #type

Methods inherited from Object

#<=>, #==, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #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.



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

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.



136
137
138
# File 'lib/hexapdf/type/resources.rb', line 136

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.



150
151
152
# File 'lib/hexapdf/type/resources.rb', line 150

def add_font(object)
  object_setter(:Font, 'F', 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.



164
165
166
# File 'lib/hexapdf/type/resources.rb', line 164

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.



120
121
122
# File 'lib/hexapdf/type/resources.rb', line 120

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.



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

def color_space(name)
  case name
  when :DeviceRGB, :DeviceGray, :DeviceCMYK
    GlobalConfiguration.constantize('color_space.map', name).new
  else
    space_definition = self[:ColorSpace] && 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_definition.map! {|item| document.deref(item) }
      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.



128
129
130
# File 'lib/hexapdf/type/resources.rb', line 128

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.



143
144
145
# File 'lib/hexapdf/type/resources.rb', line 143

def font(name)
  object_getter(:Font, 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.



157
158
159
# File 'lib/hexapdf/type/resources.rb', line 157

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.



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

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