Class: Svgcode::SVG::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/svgcode/svg/base_command.rb

Direct Known Subclasses

Circle, PathCommand

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(opts) ⇒ BaseCommand

Returns a new instance of BaseCommand.



15
16
17
18
19
# File 'lib/svgcode/svg/base_command.rb', line 15

def initialize(opts)
  @name = opts[:name]
  @absolute = !!opts[:absolute]
  @points = opts[:points] || []
end

Instance Attribute Details

#absolute(pos) ⇒ Object (readonly)

Returns the value of attribute absolute.



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

def absolute
  @absolute
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

Class Method Details

.name_str(sym, absolute) ⇒ Object



84
85
86
87
88
# File 'lib/svgcode/svg/base_command.rb', line 84

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

Instance Method Details

#==(other) ⇒ Object



73
74
75
76
77
78
# File 'lib/svgcode/svg/base_command.rb', line 73

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

#absolute!(pos) ⇒ Object



34
35
36
37
38
39
# File 'lib/svgcode/svg/base_command.rb', line 34

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

#absolute?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/svgcode/svg/base_command.rb', line 21

def absolute?
  @absolute
end

#apply_transforms!(transforms) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/svgcode/svg/base_command.rb', line 61

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



51
52
53
54
# File 'lib/svgcode/svg/base_command.rb', line 51

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

#flip_points_y!(max_y) ⇒ Object



56
57
58
59
# File 'lib/svgcode/svg/base_command.rb', line 56

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

#name_strObject



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

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

#negate_points_yObject



41
42
43
44
# File 'lib/svgcode/svg/base_command.rb', line 41

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

#negate_points_y!Object



46
47
48
49
# File 'lib/svgcode/svg/base_command.rb', line 46

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

#relative?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/svgcode/svg/base_command.rb', line 25

def relative?
  !@absolute
end

#to_sObject



80
81
82
# File 'lib/svgcode/svg/base_command.rb', line 80

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