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



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

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

#add_extgstate(extgstate, name = nil) ⇒ Object



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

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

#add_font(font, name = nil) ⇒ Object



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

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

#add_pattern(pattern, name = nil) ⇒ Object



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

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

#add_properties(properties, name = nil) ⇒ Object



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

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/origami/page.rb', line 153

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

    name = new_id(type) unless name
    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] = rsrc

    name
end

#add_shading(shading, name = nil) ⇒ Object



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

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

#add_xobject(xobject, name = nil) ⇒ Object



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

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

#colorspacesObject



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

def colorspaces; each_colorspace.to_h end

#each_colorspace(&block) ⇒ Object



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

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

#each_extgstate(&block) ⇒ Object



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

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

#each_font(&block) ⇒ Object



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

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

#each_pattern(&block) ⇒ Object



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

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

#each_property(&block) ⇒ Object



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

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

#each_resource(type) ⇒ Object

Iterates over the resources by type.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/origami/page.rb', line 170

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



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

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

#each_xobject(&block) ⇒ Object



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

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

#extgstatesObject



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

def extgstates; each_extgstate.to_h end

#fontsObject



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

def fonts; each_font.to_h end

#patternsObject



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

def patterns; each_pattern.to_h end

#propertiesObject



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

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.



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/origami/page.rb', line 202

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



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

def shadings; each_shading.to_h end

#xobjectsObject



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

def xobjects; each_xobject.to_h end