Module: Origami::ResourcesHolder

Included in:
Font::Type3, Graphics::FormXObject, Graphics::Pattern::Tiling, Page, Resources
Defined in:
lib/origami/page.rb

Instance Method Summary collapse

Instance Method Details

#add_colorspace(colorspace, name = nil) ⇒ Object



118
119
120
# File 'lib/origami/page.rb', line 118

def add_colorspace(colorspace, name = nil)
  add_resource(Resources::COLORSPACE, colorspace, name)
end

#add_extgstate(extgstate, name = nil) ⇒ Object



114
115
116
# File 'lib/origami/page.rb', line 114

def add_extgstate(extgstate, name = nil)
  add_resource(Resources::EXTGSTATE, extgstate, name)
end

#add_font(font, name = nil) ⇒ Object



134
135
136
# File 'lib/origami/page.rb', line 134

def add_font(font, name = nil)
  add_resource(Resources::FONT, font, name)
end

#add_pattern(pattern, name = nil) ⇒ Object



122
123
124
# File 'lib/origami/page.rb', line 122

def add_pattern(pattern, name = nil)
  add_resource(Resources::PATTERN, pattern, name)
end

#add_properties(properties, name = nil) ⇒ Object



138
139
140
# File 'lib/origami/page.rb', line 138

def add_properties(properties, name = nil)
  add_resource(Resources::PROPERTIES, properties, name)
end

#add_resource(type, rsrc, name = nil) ⇒ Object

Adds a resource of the specified type in the current object. If name is not specified, a new name will be automatically generated.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/origami/page.rb', line 146

def add_resource(type, rsrc, name = nil)
  if name.nil?
    rsrc_name = resources(type).key(rsrc)
    return rsrc_name if rsrc_name
  end

  name ||= new_id(type)
  target = is_a?(Resources) ? self : (self.Resources ||= Resources.new)

  rsrc_dict = target[type]&.solve || (target[type] = Dictionary.new)
  rsrc_dict[name.to_sym] = rsrc

  name
end

#add_shading(shading, name = nil) ⇒ Object



126
127
128
# File 'lib/origami/page.rb', line 126

def add_shading(shading, name = nil)
  add_resource(Resources::SHADING, shading, name)
end

#add_xobject(xobject, name = nil) ⇒ Object



130
131
132
# File 'lib/origami/page.rb', line 130

def add_xobject(xobject, name = nil)
  add_resource(Resources::XOBJECT, xobject, name)
end

#colorspacesObject



209
210
211
# File 'lib/origami/page.rb', line 209

def colorspaces
  each_colorspace.to_h
end

#each_colorspace(&block) ⇒ Object



177
178
179
# File 'lib/origami/page.rb', line 177

def each_colorspace(&block)
  each_resource(Resources::COLORSPACE, &block)
end

#each_extgstate(&block) ⇒ Object



181
182
183
# File 'lib/origami/page.rb', line 181

def each_extgstate(&block)
  each_resource(Resources::EXTGSTATE, &block)
end

#each_font(&block) ⇒ Object



197
198
199
# File 'lib/origami/page.rb', line 197

def each_font(&block)
  each_resource(Resources::FONT, &block)
end

#each_pattern(&block) ⇒ Object



185
186
187
# File 'lib/origami/page.rb', line 185

def each_pattern(&block)
  each_resource(Resources::PATTERN, &block)
end

#each_property(&block) ⇒ Object



201
202
203
# File 'lib/origami/page.rb', line 201

def each_property(&block)
  each_resource(Resources::PROPERTIES, &block)
end

#each_resource(type) ⇒ Object

Iterates over the resources by type.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/origami/page.rb', line 164

def each_resource(type)
  target = is_a?(Resources) ? self : (self.Resources ||= Resources.new)

  rsrc = target[type]&.solve

  return enum_for(__method__, type) { rsrc.is_a?(Dictionary) ? rsrc.length : 0 } unless block_given?
  return unless rsrc.is_a?(Dictionary)

  rsrc.each_pair do |name, obj|
    yield(name.value, obj.solve)
  end
end

#each_shading(&block) ⇒ Object



189
190
191
# File 'lib/origami/page.rb', line 189

def each_shading(&block)
  each_resource(Resources::SHADING, &block)
end

#each_xobject(&block) ⇒ Object



193
194
195
# File 'lib/origami/page.rb', line 193

def each_xobject(&block)
  each_resource(Resources::XOBJECT, &block)
end

#extgstatesObject



205
206
207
# File 'lib/origami/page.rb', line 205

def extgstates
  each_extgstate.to_h
end

#fontsObject



225
226
227
# File 'lib/origami/page.rb', line 225

def fonts
  each_font.to_h
end

#patternsObject



213
214
215
# File 'lib/origami/page.rb', line 213

def patterns
  each_pattern.to_h
end

#propertiesObject



229
230
231
# File 'lib/origami/page.rb', line 229

def properties
  each_property.to_h
end

#resources(type = nil) ⇒ Object

Returns a Hash of all resources in the object or only the specified type.



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/origami/page.rb', line 236

def resources(type = nil)
  if type.nil?
    extgstates
      .merge colorspaces
      .merge patterns
      .merge shadings
      .merge xobjects
      .merge fonts
      .merge properties
  else
    each_resource(type).to_h
  end
end

#shadingsObject



217
218
219
# File 'lib/origami/page.rb', line 217

def shadings
  each_shading.to_h
end

#xobjectsObject



221
222
223
# File 'lib/origami/page.rb', line 221

def xobjects
  each_xobject.to_h
end