Class: Svgcode::SVG::PathCommand

Inherits:
BaseCommand show all
Defined in:
lib/svgcode/svg/path_command.rb

Constant Summary collapse

NAMES =
{
  'm' => :move,
  'l' => :line,
  'c' => :cubic,
  'z' => :close
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_or_opts) ⇒ PathCommand



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/svgcode/svg/path_command.rb', line 16

def initialize(str_or_opts)
  if str_or_opts.is_a?(Hash)
    @name = str_or_opts[:name]
    @absolute = !!str_or_opts[:absolute]
    @points = str_or_opts[:points]
  else
    str = str_or_opts
    @name = NAMES[str[0].to_s.downcase]

    @absolute =
    if @name == :close
      true
    else
      !!str[0].match(/[A-Z]/)
    end

    if @name != :close && str.length > 1
      @points = Point.parse(str[1..(str.length - 1)])
    end
  end

  @points = [] if @points.nil?
end

Instance Attribute Details

#absolute(pos) ⇒ Object (readonly)

Returns the value of attribute absolute.



7
8
9
# File 'lib/svgcode/svg/path_command.rb', line 7

def absolute
  @absolute
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/svgcode/svg/path_command.rb', line 7

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



7
8
9
# File 'lib/svgcode/svg/path_command.rb', line 7

def points
  @points
end

Class Method Details

.name_str(sym, absolute) ⇒ Object



103
104
105
106
107
# File 'lib/svgcode/svg/path_command.rb', line 103

def self.name_str(sym, absolute)
  str = NAMES.key(sym).dup
  str.upcase! if absolute
  str
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
95
96
97
# File 'lib/svgcode/svg/path_command.rb', line 92

def ==(other)
  other.is_a?(self.class) &&
    other.name == @name &&
    other.absolute? == @absolute &&
    other.points == @points
end

#absolute!(pos) ⇒ Object



53
54
55
56
57
58
# File 'lib/svgcode/svg/path_command.rb', line 53

def absolute!(pos)
  if relative?
    @points.collect! { |p| p + pos }
    @absolute = true
  end
end

#absolute?Boolean



40
41
42
# File 'lib/svgcode/svg/path_command.rb', line 40

def absolute?
  @absolute
end

#apply_transforms!(transforms) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/svgcode/svg/path_command.rb', line 80

def apply_transforms!(transforms)
  unless transforms.empty?
    transforms.reverse.each do |transform|
      @points.collect! { |point| transform.apply(point) }
    end
  end
end

#divide_points_by!(amount) ⇒ Object



70
71
72
73
# File 'lib/svgcode/svg/path_command.rb', line 70

def divide_points_by!(amount)
  @points.each { |point| point.divide_by!(amount) }
  nil
end

#flip_points_y!(max_y) ⇒ Object



75
76
77
78
# File 'lib/svgcode/svg/path_command.rb', line 75

def flip_points_y!(max_y)
  @points.each { |point| point.flip_y!(max_y) }
  nil
end

#name_strObject



88
89
90
# File 'lib/svgcode/svg/path_command.rb', line 88

def name_str
  PathCommand.name_str(@name, absolute?)
end

#negate_points_yObject



60
61
62
63
# File 'lib/svgcode/svg/path_command.rb', line 60

def negate_points_y
  points = @points.collect { |point| point.negate_y }
  PathCommand.new(name: @name, absolute: @absolute, points: points)
end

#negate_points_y!Object



65
66
67
68
# File 'lib/svgcode/svg/path_command.rb', line 65

def negate_points_y!
  @points.each { |point| point.negate_y! }
  nil
end

#relative?Boolean



44
45
46
# File 'lib/svgcode/svg/path_command.rb', line 44

def relative?
  !@absolute
end

#to_sObject



99
100
101
# File 'lib/svgcode/svg/path_command.rb', line 99

def to_s
  name_str + @points.collect { |p| p.to_s }.join(' ')
end