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



144
145
146
# File 'lib/origami/page.rb', line 144

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

#add_extgstate(extgstate, name = nil) ⇒ Object



141
142
143
# File 'lib/origami/page.rb', line 141

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

#add_font(font, name = nil) ⇒ Object



160
161
162
# File 'lib/origami/page.rb', line 160

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

#add_pattern(pattern, name = nil) ⇒ Object



148
149
150
# File 'lib/origami/page.rb', line 148

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

#add_properties(properties, name = nil) ⇒ Object



164
165
166
# File 'lib/origami/page.rb', line 164

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.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/origami/page.rb', line 172

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

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

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

    name
end

#add_shading(shading, name = nil) ⇒ Object



152
153
154
# File 'lib/origami/page.rb', line 152

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

#add_xobject(xobject, name = nil) ⇒ Object



156
157
158
# File 'lib/origami/page.rb', line 156

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

#colorspacesObject



212
# File 'lib/origami/page.rb', line 212

def colorspaces; each_colorspace.to_h end

#each_colorspace(&block) ⇒ Object



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

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

#each_extgstate(&block) ⇒ Object



204
# File 'lib/origami/page.rb', line 204

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

#each_font(&block) ⇒ Object



208
# File 'lib/origami/page.rb', line 208

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

#each_pattern(&block) ⇒ Object



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

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

#each_property(&block) ⇒ Object



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

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

#each_resource(type) ⇒ Object

Iterates over the resources by type.



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/origami/page.rb', line 190

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

    rsrc = (target[type] and 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



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

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

#each_xobject(&block) ⇒ Object



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

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

#extgstatesObject



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

def extgstates; each_extgstate.to_h end

#fontsObject



216
# File 'lib/origami/page.rb', line 216

def fonts; each_font.to_h end

#patternsObject



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

def patterns; each_pattern.to_h end

#propertiesObject



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

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/origami/page.rb', line 222

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

#shadingsObject



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

def shadings; each_shading.to_h end

#xobjectsObject



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

def xobjects; each_xobject.to_h end