Class: PSD::Layer
Overview
Represents a single layer and all of the data associated with that layer.
Constant Summary collapse
- LAYER_INFO =
All of the extra layer info sections that we know how to parse.
{ type: TypeTool, legacy_type: LegacyTypeTool, layer_name_source: LayerNameSource, object_effects: ObjectEffects, name: UnicodeName, section_divider: LayerSectionDivider, reference_point: ReferencePoint, layer_id: LayerID, fill_opacity: FillOpacity, placed_layer: PlacedLayer, vector_mask: VectorMask }
Instance Attribute Summary collapse
-
#adjustments ⇒ Object
(also: #info)
readonly
Returns the value of attribute adjustments.
-
#blend_mode ⇒ Object
readonly
Returns the value of attribute blend_mode.
-
#blending_mode ⇒ Object
readonly
Returns the value of attribute blending_mode.
-
#blending_ranges ⇒ Object
readonly
Returns the value of attribute blending_ranges.
-
#bottom ⇒ Object
Returns the value of attribute bottom.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#channels_info ⇒ Object
readonly
Returns the value of attribute channels_info.
-
#cols ⇒ Object
(also: #width)
Returns the value of attribute cols.
-
#file ⇒ Object
Returns the value of attribute file.
-
#fill_opacity ⇒ Object
readonly
Returns the value of attribute fill_opacity.
-
#group_layer ⇒ Object
Returns the value of attribute group_layer.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#layer_type ⇒ Object
readonly
Returns the value of attribute layer_type.
-
#left ⇒ Object
Returns the value of attribute left.
-
#mask ⇒ Object
readonly
Returns the value of attribute mask.
-
#node ⇒ Object
Returns the value of attribute node.
-
#opacity ⇒ Object
readonly
Returns the value of attribute opacity.
-
#ref_x ⇒ Object
Returns the value of attribute ref_x.
-
#ref_y ⇒ Object
Returns the value of attribute ref_y.
-
#right ⇒ Object
Returns the value of attribute right.
-
#rows ⇒ Object
(also: #height)
Returns the value of attribute rows.
-
#top ⇒ Object
Returns the value of attribute top.
Attributes included from Section
Instance Method Summary collapse
-
#[](val) ⇒ Object
We just delegate this to a normal method call.
-
#export(outfile) ⇒ Object
Export the layer to file.
-
#folder? ⇒ Boolean
Does this layer represent the start of a folder section?.
-
#folder_end? ⇒ Boolean
Does this layer represent the end of a folder section?.
-
#hidden? ⇒ Boolean
Is this layer hidden?.
-
#initialize(file) ⇒ Layer
constructor
Initializes all of the defaults for the layer.
-
#method_missing(method, *args, &block) ⇒ Object
We delegate all missing method calls to the extra layer info to make it easier to access that data.
-
#name ⇒ Object
Gets the name of this layer.
-
#parse(index = nil) ⇒ Object
Parse the layer and all of it’s sub-sections.
-
#parse_channel_image!(header) ⇒ Object
:nodoc:.
-
#scale_path_components(xr, yr) ⇒ Object
Attempt to scale the path components within this layer.
-
#text ⇒ Object
Helper that exports the text data in this layer, if any.
-
#translate(x = 0, y = 0) ⇒ Object
Attempt to translate this layer and modify the document.
-
#visible? ⇒ Boolean
Is this layer visible?.
Methods included from Section
#end_of_section, #end_section, #start_of_section, #start_section
Constructor Details
#initialize(file) ⇒ Layer
Initializes all of the defaults for the layer.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/psd/layer.rb', line 34 def initialize(file) @file = file @image = nil @mask = {} @blending_ranges = {} @adjustments = {} @channels_info = [] @blend_mode = {} @group_layer = nil @layer_type = 'normal' @blending_mode = 'normal' @opacity = 255 @fill_opacity = 255 # Just used for tracking which layer adjustments we're parsing. # Not essential. @info_keys = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
We delegate all missing method calls to the extra layer info to make it easier to access that data.
161 162 163 164 |
# File 'lib/psd/layer.rb', line 161 def method_missing(method, *args, &block) return @adjustments[method] if @adjustments.has_key?(method) super end |
Instance Attribute Details
#adjustments ⇒ Object (readonly) Also known as: info
Returns the value of attribute adjustments.
7 8 9 |
# File 'lib/psd/layer.rb', line 7 def adjustments @adjustments end |
#blend_mode ⇒ Object (readonly)
Returns the value of attribute blend_mode.
8 9 10 |
# File 'lib/psd/layer.rb', line 8 def blend_mode @blend_mode end |
#blending_mode ⇒ Object (readonly)
Returns the value of attribute blending_mode.
8 9 10 |
# File 'lib/psd/layer.rb', line 8 def blending_mode @blending_mode end |
#blending_ranges ⇒ Object (readonly)
Returns the value of attribute blending_ranges.
7 8 9 |
# File 'lib/psd/layer.rb', line 7 def blending_ranges @blending_ranges end |
#bottom ⇒ Object
Returns the value of attribute bottom.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def bottom @bottom end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
9 10 11 |
# File 'lib/psd/layer.rb', line 9 def channels @channels end |
#channels_info ⇒ Object (readonly)
Returns the value of attribute channels_info.
7 8 9 |
# File 'lib/psd/layer.rb', line 7 def channels_info @channels_info end |
#cols ⇒ Object Also known as: width
Returns the value of attribute cols.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def cols @cols end |
#file ⇒ Object
Returns the value of attribute file.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def file @file end |
#fill_opacity ⇒ Object (readonly)
Returns the value of attribute fill_opacity.
8 9 10 |
# File 'lib/psd/layer.rb', line 8 def fill_opacity @fill_opacity end |
#group_layer ⇒ Object
Returns the value of attribute group_layer.
11 12 13 |
# File 'lib/psd/layer.rb', line 11 def group_layer @group_layer end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/psd/layer.rb', line 7 def id @id end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
9 10 11 |
# File 'lib/psd/layer.rb', line 9 def image @image end |
#layer_type ⇒ Object (readonly)
Returns the value of attribute layer_type.
8 9 10 |
# File 'lib/psd/layer.rb', line 8 def layer_type @layer_type end |
#left ⇒ Object
Returns the value of attribute left.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def left @left end |
#mask ⇒ Object (readonly)
Returns the value of attribute mask.
7 8 9 |
# File 'lib/psd/layer.rb', line 7 def mask @mask end |
#node ⇒ Object
Returns the value of attribute node.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def node @node end |
#opacity ⇒ Object (readonly)
Returns the value of attribute opacity.
8 9 10 |
# File 'lib/psd/layer.rb', line 8 def opacity @opacity end |
#ref_x ⇒ Object
Returns the value of attribute ref_x.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def ref_x @ref_x end |
#ref_y ⇒ Object
Returns the value of attribute ref_y.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def ref_y @ref_y end |
#right ⇒ Object
Returns the value of attribute right.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def right @right end |
#rows ⇒ Object Also known as: height
Returns the value of attribute rows.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def rows @rows end |
#top ⇒ Object
Returns the value of attribute top.
12 13 14 |
# File 'lib/psd/layer.rb', line 12 def top @top end |
Instance Method Details
#[](val) ⇒ Object
We just delegate this to a normal method call.
95 96 97 |
# File 'lib/psd/layer.rb', line 95 def [](val) self.send(val) end |
#export(outfile) ⇒ Object
Export the layer to file. May or may not work.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/psd/layer.rb', line 80 def export(outfile) export_info(outfile) @blend_mode.write(outfile) @file.seek(@blend_mode.num_bytes, IO::SEEK_CUR) export_mask_data(outfile) export_blending_ranges(outfile) export_legacy_layer_name(outfile) export_extra_data(outfile) outfile.write @file.read(end_of_section - @file.tell) end |
#folder? ⇒ Boolean
Does this layer represent the start of a folder section?
104 105 106 107 |
# File 'lib/psd/layer.rb', line 104 def folder? return false unless @adjustments.has_key?(:section_divider) @adjustments[:section_divider].is_folder end |
#folder_end? ⇒ Boolean
Does this layer represent the end of a folder section?
110 111 112 113 |
# File 'lib/psd/layer.rb', line 110 def folder_end? return false unless @adjustments.has_key?(:section_divider) @adjustments[:section_divider].is_hidden end |
#hidden? ⇒ Boolean
Is this layer hidden?
121 122 123 |
# File 'lib/psd/layer.rb', line 121 def hidden? !@visible end |
#name ⇒ Object
Gets the name of this layer. If the PSD file is from an even remotely recent version of Photoshop, this data is stored as extra layer info and as a UTF-16 name. Otherwise, it’s stored in a legacy block.
151 152 153 154 155 156 157 |
# File 'lib/psd/layer.rb', line 151 def name if @adjustments.has_key?(:name) return @adjustments[:name].data end return @legacy_name end |
#parse(index = nil) ⇒ Object
Parse the layer and all of it’s sub-sections.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/psd/layer.rb', line 55 def parse(index=nil) start_section @idx = index parse_info parse_blend_modes extra_len = @file.read_int @layer_end = @file.tell + extra_len parse_mask_data parse_blending_ranges parse_legacy_layer_name parse_extra_data PSD.logger.debug "Layer name = #{name}" @file.seek @layer_end # Skip over any filler zeros end_section return self end |
#parse_channel_image!(header) ⇒ Object
:nodoc:
99 100 101 |
# File 'lib/psd/layer.rb', line 99 def parse_channel_image!(header) #:nodoc: # @image = ChannelImage.new(@file, header, self) end |
#scale_path_components(xr, yr) ⇒ Object
Attempt to scale the path components within this layer.
136 137 138 139 140 |
# File 'lib/psd/layer.rb', line 136 def scale_path_components(xr, yr) return unless @path_components @path_components.each{ |p| p.scale(xr, yr) } end |
#text ⇒ Object
Helper that exports the text data in this layer, if any.
143 144 145 146 |
# File 'lib/psd/layer.rb', line 143 def text return nil unless @adjustments[:type] @adjustments[:type].to_hash end |
#translate(x = 0, y = 0) ⇒ Object
Attempt to translate this layer and modify the document.
126 127 128 129 130 131 132 133 |
# File 'lib/psd/layer.rb', line 126 def translate(x=0, y=0) @left += x @right += x @top += y @bottom += y @path_components.each{ |p| p.translate(x,y) } if @path_components end |
#visible? ⇒ Boolean
Is this layer visible?
116 117 118 |
# File 'lib/psd/layer.rb', line 116 def visible? @visible end |