Sha256: db9255cd19f0b4abc40a5f46520e6407763cc1fa9e8f66f55824b75cac9bc435

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

module Savage
  class SubPath
    include Utils
    include DirectionProxy
    include Transformable

    define_proxies do |sym,const|
      define_method(sym) do |*args|
        raise TypeError if const == "QuadraticCurveTo" && @directions.last.class != Directions::QuadraticCurveTo && [2,3].include?(args.length)
        raise TypeError if const == "CubicCurveTo" && @directions.last.class != Directions::CubicCurveTo && [4,5].include?(args.length)
        (@directions << Savage::Directions.const_get(const).new(*args)).last
      end
    end

    attr_accessor :directions

    def move_to(*args)
      return nil unless @directions.empty?
      (@directions << Directions::MoveTo.new(*args)).last
    end

    def initialize(*args)
      @directions = []
      move_to(*args) if (2..3).include?(args.length)
      yield self if block_given?
    end

    def to_command
      @directions.to_enum(:each_with_index).collect { |dir, i|
        command_string = dir.to_command
        if i > 0
          prev_command_code = @directions[i-1].command_code
          if dir.command_code == prev_command_code || (prev_command_code.match(/^[Mm]$/) && dir.command_code == 'L')
            command_string.gsub!(/^[A-Za-z]/,'')
            command_string.insert(0,' ') unless command_string.match(/^-/)
          end
        end
        command_string
      }.join
    end

    def commands
      @directions
    end

    def closed?
      @directions.last.kind_of? Directions::ClosePath
    end

    def transform(*args)
      directions.each { |dir| dir.transform *args }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
text2path-0.0.2 lib/ext/savage/lib/savage/sub_path.rb
text2path-0.0.1 lib/ext/savage/lib/savage/sub_path.rb