Class: PSD::Layer

Inherits:
Object
  • Object
show all
Includes:
Section
Defined in:
lib/psd/layer.rb

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

Attributes included from Section

#section_end, #section_start

Instance Method Summary collapse

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.



162
163
164
165
# File 'lib/psd/layer.rb', line 162

def method_missing(method, *args, &block)
  return @adjustments[method] if @adjustments.has_key?(method)
  super
end

Instance Attribute Details

#adjustmentsObject (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_modeObject (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_modeObject (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_rangesObject (readonly)

Returns the value of attribute blending_ranges.



7
8
9
# File 'lib/psd/layer.rb', line 7

def blending_ranges
  @blending_ranges
end

#bottomObject

Returns the value of attribute bottom.



12
13
14
# File 'lib/psd/layer.rb', line 12

def bottom
  @bottom
end

#channelsObject (readonly)

Returns the value of attribute channels.



9
10
11
# File 'lib/psd/layer.rb', line 9

def channels
  @channels
end

#channels_infoObject (readonly)

Returns the value of attribute channels_info.



7
8
9
# File 'lib/psd/layer.rb', line 7

def channels_info
  @channels_info
end

#colsObject Also known as: width

Returns the value of attribute cols.



12
13
14
# File 'lib/psd/layer.rb', line 12

def cols
  @cols
end

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/psd/layer.rb', line 12

def file
  @file
end

#fill_opacityObject (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_layerObject

Returns the value of attribute group_layer.



11
12
13
# File 'lib/psd/layer.rb', line 11

def group_layer
  @group_layer
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/psd/layer.rb', line 7

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



9
10
11
# File 'lib/psd/layer.rb', line 9

def image
  @image
end

#layer_typeObject (readonly)

Returns the value of attribute layer_type.



8
9
10
# File 'lib/psd/layer.rb', line 8

def layer_type
  @layer_type
end

#leftObject

Returns the value of attribute left.



12
13
14
# File 'lib/psd/layer.rb', line 12

def left
  @left
end

#maskObject (readonly)

Returns the value of attribute mask.



7
8
9
# File 'lib/psd/layer.rb', line 7

def mask
  @mask
end

#nodeObject

Returns the value of attribute node.



12
13
14
# File 'lib/psd/layer.rb', line 12

def node
  @node
end

#opacityObject (readonly)

Returns the value of attribute opacity.



8
9
10
# File 'lib/psd/layer.rb', line 8

def opacity
  @opacity
end

#ref_xObject

Returns the value of attribute ref_x.



12
13
14
# File 'lib/psd/layer.rb', line 12

def ref_x
  @ref_x
end

#ref_yObject

Returns the value of attribute ref_y.



12
13
14
# File 'lib/psd/layer.rb', line 12

def ref_y
  @ref_y
end

#rightObject

Returns the value of attribute right.



12
13
14
# File 'lib/psd/layer.rb', line 12

def right
  @right
end

#rowsObject Also known as: height

Returns the value of attribute rows.



12
13
14
# File 'lib/psd/layer.rb', line 12

def rows
  @rows
end

#topObject

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?

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/psd/layer.rb', line 105

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?

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/psd/layer.rb', line 111

def folder_end?
  return false unless @adjustments.has_key?(:section_divider)
  @adjustments[:section_divider].is_hidden
end

#hidden?Boolean

Is this layer hidden?

Returns:

  • (Boolean)


122
123
124
# File 'lib/psd/layer.rb', line 122

def hidden?
  !@visible
end

#nameObject

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.



152
153
154
155
156
157
158
# File 'lib/psd/layer.rb', line 152

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, parse) ⇒ Object



99
100
101
102
# File 'lib/psd/layer.rb', line 99

def parse_channel_image!(header, parse)
  @image = ChannelImage.new(@file, header, self)
  parse ? @image.parse : @image.skip
end

#scale_path_components(xr, yr) ⇒ Object

Attempt to scale the path components within this layer.



137
138
139
140
141
# File 'lib/psd/layer.rb', line 137

def scale_path_components(xr, yr)
  return unless @path_components

  @path_components.each{ |p| p.scale(xr, yr) }
end

#textObject

Helper that exports the text data in this layer, if any.



144
145
146
147
# File 'lib/psd/layer.rb', line 144

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.



127
128
129
130
131
132
133
134
# File 'lib/psd/layer.rb', line 127

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?

Returns:

  • (Boolean)


117
118
119
# File 'lib/psd/layer.rb', line 117

def visible?
  @visible
end