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


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

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

#add_extgstate(extgstate, name = nil) ⇒ Object


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

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

#add_font(font, name = nil) ⇒ Object


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

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

#add_pattern(pattern, name = nil) ⇒ Object


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

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

#add_properties(properties, name = nil) ⇒ Object


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

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.


156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/origami/page.rb', line 156

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


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

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

#add_xobject(xobject, name = nil) ⇒ Object


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

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

#colorspacesObject


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

def colorspaces; each_colorspace.to_h end

#each_colorspace(&block) ⇒ Object


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

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

#each_extgstate(&block) ⇒ Object


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

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

#each_font(&block) ⇒ Object


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

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

#each_pattern(&block) ⇒ Object


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

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

#each_property(&block) ⇒ Object


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

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

#each_resource(type) ⇒ Object

Iterates over the resources by type.


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

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


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

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

#each_xobject(&block) ⇒ Object


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

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

#extgstatesObject


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

def extgstates; each_extgstate.to_h end

#fontsObject


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

def fonts; each_font.to_h end

#patternsObject


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

def patterns; each_pattern.to_h end

#propertiesObject


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

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.


206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/origami/page.rb', line 206

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


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

def shadings; each_shading.to_h end

#xobjectsObject


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

def xobjects; each_xobject.to_h end