Class: Svgcode::SVG::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/svgcode/svg/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) ⇒ Command

Returns a new instance of Command.



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

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.



6
7
8
# File 'lib/svgcode/svg/command.rb', line 6

def absolute
  @absolute
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/svgcode/svg/command.rb', line 6

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



6
7
8
# File 'lib/svgcode/svg/command.rb', line 6

def points
  @points
end

Class Method Details

.name_str(sym, absolute) ⇒ Object



102
103
104
105
106
# File 'lib/svgcode/svg/command.rb', line 102

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

Instance Method Details

#==(other) ⇒ Object



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

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

#absolute!(pos) ⇒ Object



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

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

#absolute?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/svgcode/svg/command.rb', line 39

def absolute?
  @absolute
end

#apply_transforms!(transforms) ⇒ Object



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

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



69
70
71
72
# File 'lib/svgcode/svg/command.rb', line 69

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

#flip_points_y!(max_y) ⇒ Object



74
75
76
77
# File 'lib/svgcode/svg/command.rb', line 74

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

#name_strObject



87
88
89
# File 'lib/svgcode/svg/command.rb', line 87

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

#negate_points_yObject



59
60
61
62
# File 'lib/svgcode/svg/command.rb', line 59

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

#negate_points_y!Object



64
65
66
67
# File 'lib/svgcode/svg/command.rb', line 64

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

#relative?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/svgcode/svg/command.rb', line 43

def relative?
  !@absolute
end

#to_sObject



98
99
100
# File 'lib/svgcode/svg/command.rb', line 98

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