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



120
121
122
# File 'lib/origami/page.rb', line 120

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

#add_extgstate(extgstate, name = nil) ⇒ Object



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

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

#add_font(font, name = nil) ⇒ Object



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

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

#add_pattern(pattern, name = nil) ⇒ Object



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

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

#add_properties(properties, name = nil) ⇒ Object



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

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.



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

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



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

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

#add_xobject(xobject, name = nil) ⇒ Object



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

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

#colorspacesObject



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

def colorspaces; each_colorspace.to_h end

#each_colorspace(&block) ⇒ Object



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

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

#each_extgstate(&block) ⇒ Object



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

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

#each_font(&block) ⇒ Object



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

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

#each_pattern(&block) ⇒ Object



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

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

#each_property(&block) ⇒ Object



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

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

#each_resource(type) ⇒ Object

Iterates over the resources by type.



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

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



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

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

#each_xobject(&block) ⇒ Object



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

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

#extgstatesObject



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

def extgstates; each_extgstate.to_h end

#fontsObject



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

def fonts; each_font.to_h end

#patternsObject



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

def patterns; each_pattern.to_h end

#propertiesObject



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

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.



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/origami/page.rb', line 198

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



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

def shadings; each_shading.to_h end

#xobjectsObject



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

def xobjects; each_xobject.to_h end