Sha256: c09e283fc8741ae3b7a878be11e4fa9506efee48f47cefff3f3216ede2b1c020

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require 'svgcode/svg/point'

module Svgcode
  module SVG
    class BaseCommand
      attr_reader :name, :absolute, :points

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

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

      def absolute?
        @absolute
      end

      def relative?
        !@absolute
      end

      def absolute(pos)
        points = @points.collect { |p| p + pos }
        Command.new(name: @name, absolute: true, points: points)
      end

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

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

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

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

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

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

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
svgcode-0.6.2 lib/svgcode/svg/base_command.rb
svgcode-0.6.1 lib/svgcode/svg/base_command.rb
svgcode-0.6.0 lib/svgcode/svg/base_command.rb