Class: PSD::PathRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/path_record.rb

Overview

Parses a vector path

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PathRecord

Reads the record type and begins parsing accordingly.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/psd/path_record.rb', line 14

def initialize(file)
  @file = file

  @record_type = @file.read_short

  case @record_type
  when 0, 3 then read_path_record
  when 1, 2, 4, 5 then read_bezier_point
  when 7 then read_clipboard_record
  when 8 then read_initial_fill
  else @file.seek(24, IO::SEEK_CUR)
  end
end

Instance Attribute Details

#layerObject

Returns the value of attribute layer.



4
5
6
# File 'lib/psd/path_record.rb', line 4

def layer
  @layer
end

Class Method Details

.read(layer) ⇒ Object

Facade to make it easier to parse the path record.



7
8
9
10
11
# File 'lib/psd/path_record.rb', line 7

def self.read(layer)
  pr = PSD::PathRecord.new(layer.file)
  pr.layer = layer
  pr
end

Instance Method Details

#is_bezier_point?Boolean

Is this record a bezier point?

Returns:

  • (Boolean)


115
116
117
# File 'lib/psd/path_record.rb', line 115

def is_bezier_point?
  [1,2,4,5].include? @record_type
end

#scale(xr, yr) ⇒ Object

Attempts to scale the path



103
104
105
106
107
108
109
110
111
112
# File 'lib/psd/path_record.rb', line 103

def scale(xr, yr)
  return unless is_bezier_point?

  @preceding_vert *= yr
  @preceding_horiz *= xr
  @anchor_vert *= yr
  @anchor_horiz *= xr
  @leaving_vert *= yr
  @leaving_horiz *= xr
end

#to_hashObject

Exports the path record to an easier to work with hash.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/psd/path_record.rb', line 45

def to_hash
  case @record_type
  when 0, 3
    {
      num_points: @num_points
    }
  when 1, 2, 4, 5
    {
      linked: @linked,
      preceding: {
        vert: @preceding_vert,
        horiz: @preceding_horiz
      },
      anchor: {
        vert: @anchor_vert,
        horiz: @anchor_horiz
      },
      leaving: {
        vert: @leaving_vert,
        horiz: @leaving_horiz
      }
    }
  when 7
    {
      clipboard: {
        top: @clipboard_top,
        left: @clipboard_left,
        bottom: @clipboard_bottom,
        right: @clipboard_right,
        resolution: @clipboard_resolution
      }
    }
  when 8
    {
      initial_fill: @initial_fill
    }
  else
    {}
  end.merge({ record_type: @record_type })
end

#translate(x = 0, y = 0) ⇒ Object

Attempts to translate the path



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/psd/path_record.rb', line 87

def translate(x=0, y=0)
  return unless is_bezier_point?

  document_width, document_height = @layer.document_dimensions
  translate_x_ratio = x.to_f / document_width.to_f
  translate_y_ratio = y.to_f / document_height.to_f

  @preceding_vert += translate_y_ratio
  @preceding_horiz += translate_x_ratio
  @anchor_vert += translate_y_ratio
  @anchor_horiz += translate_x_ratio
  @leaving_vert += translate_y_ratio
  @leaving_horiz += translate_x_ratio
end

#write(outfile) ⇒ Object

Writes out the path to file.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/psd/path_record.rb', line 29

def write(outfile)
  outfile.write_short @record_type
  case @record_type
  when 0 then write_path_record(outfile)
  when 3 then write_path_record(outfile)
  when 1 then write_bezier_point(outfile)
  when 2 then write_bezier_point(outfile)
  when 4 then write_bezier_point(outfile)
  when 5 then write_bezier_point(outfile)
  when 7 then write_clipboard_record(outfile)
  when 8 then write_initial_fill(outfile)
  else outfile.seek(24, IO::SEEK_CUR)
  end
end