Skip to content

Commit

Permalink
Allow resource sections to be accessed by a human readable type. Also…
Browse files Browse the repository at this point in the history
… fix tests.
  • Loading branch information
meltingice committed Aug 5, 2013
1 parent 4a35c8b commit 45c5cd6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/psd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ def header

# Get the Resources section, parsing if needed.
def resources
return @resources.data unless @resources.nil?
return @resources unless @resources.nil?

ensure_header

@resources = Resources.new(@file)
@resources.parse

PSD.logger.debug @resources.inspect
PSD.logger.debug @resources.data.inspect

return @resources.data
return @resources
end

# Get the LayerMask section. Ensures the header and resources
Expand Down
5 changes: 3 additions & 2 deletions lib/psd/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ class PSD
# Most of the resources are options/preferences set by the user
# or automatically by Photoshop.
class Resource
attr_reader :id, :name, :size
attr_reader :type, :id, :name, :size
attr_accessor :data

def initialize(file)
@file = file
@data = {}
@type = nil
end

def parse
@file.seek 4, IO::SEEK_CUR # Type, always 8BIM
@type = @file.read_string(4) # Always 8BIM
@id = @file.read_short

name_length = Util.pad2(@file.read(1).bytes.to_a[0] + 1) - 1
Expand Down
5 changes: 3 additions & 2 deletions lib/psd/resource_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ def self.factory(file, resource)
section = Section.const_get(c)
next unless section.id == resource.id

return section.new(file, resource).parse
section.new(file, resource).parse
return section.name
end

return nil
end

def initialize(file, resource)
@file = file
@resource = resource
Expand Down
15 changes: 12 additions & 3 deletions lib/psd/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Resources
def initialize(file)
@file = file
@resources = {}
@type_index = {}
@length = nil
end

Expand All @@ -27,8 +28,9 @@ def parse

resource_end = @file.tell + resource.size

Resource::Section.factory(@file, resource)
name = Resource::Section.factory(@file, resource)
@resources[resource.id] = resource
@type_index[name] = resource.id unless name.nil?

@file.seek resource_end
n -= @file.tell - pos
Expand All @@ -39,15 +41,22 @@ def parse
end

end_section
return @resources
end

def skip
@file.seek length, IO::SEEK_CUR
end

def [](id)
@resources[id]
if id.is_a?(Symbol)
by_type(id)
else
@resources[id]
end
end

def by_type(id)
@resources[@type_index[id]]
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/psd/resources/layer_comps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Resource
class Section
class LayerComps < Section
def self.id; 1065; end
def self.name; :layer_comps; end

def parse
# Descriptor version
Expand Down
8 changes: 4 additions & 4 deletions spec/parsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@

it "should contain data" do
@psd.resources.should_not be_nil
@psd.resources.is_a?(Array).should be_true
@psd.resources.size.should >= 1
@psd.resources.data.is_a?(Hash).should be_true
@psd.resources.data.size.should >= 1
end

it "should be of type 8BIM" do
@psd.resources.each { |r| r.type.should == '8BIM' }
@psd.resources.data.each { |id, r| r.type.should == '8BIM' }
end

it "should have an ID" do
@psd.resources.each do |r|
@psd.resources.data.each do |id, r|
r.id.should_not be_nil
end
end
Expand Down

0 comments on commit 45c5cd6

Please sign in to comment.