Class: Sunniesnow::Tools::SvgPath::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/sscharter/tools/svg_path.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, parameters, follow_up = name) ⇒ Command

Returns a new instance of Command.

Parameters:

  • name (Symbol)
  • parameters (Array<Symbol>)
  • follow_up (Symbol) (defaults to: name)


318
319
320
321
322
# File 'lib/sscharter/tools/svg_path.rb', line 318

def initialize name, parameters, follow_up = name
  @name = name
  @parameters = parameters
  @follow_up = follow_up
end

Instance Method Details

#parse(path, tokens) ⇒ void

This method returns an undefined value.

Parameters:

  • path (Path)
  • tokens (Array<String>)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/sscharter/tools/svg_path.rb', line 327

def parse path, tokens
  relative = (?a..?z).include? tokens.shift
  name = @name.to_sym
  begin
    arguments = @parameters.map do |parameter|
      case parameter
      when :point
        point = Vector2D.new tokens.shift.to_f, tokens.shift.to_f
        point += path.current if relative
        point
      when :point_x
        x = tokens.shift.to_f
        Vector2D.new relative ? path.current.x + x : x, path.current.y
      when :point_y
        y = tokens.shift.to_f
        Vector2D.new path.current.x, relative ? path.current.y + y : y
      when :vector
        Vector2D.new tokens.shift.to_f, tokens.shift.to_f
      when :number
        tokens.shift.to_f
      when :boolean
        raise "Invalid boolean: #{tokens.first}" unless %w[0 1].include? tokens.first
        tokens.shift == ?1
      end
    end
    path.public_send name, *arguments
    name = @follow_up.to_sym
  end until tokens.empty? || tokens.first =~ /[a-zA-Z]/
end