1
  E   3ˆl–Ü4ý('”Ò~êqA
ãm\ŠbÑ¿�þYnV–qÀ¶P²ÀùÙ„/_Ø_�uÐb&=LyZËcõÁÖ 5ô    3#--
# $Id: embellishable.rb,v 1.9 2009/02/28 23:52:13 rmagick Exp $
# Copyright (C) 2009 Timothy P. Hunter
#++
module Magick
  class RVG
    # Parent class of Circle, Ellipse, Text, etc.
    # @private
    class Shape
      include Stylable
      include Transformable
      include Duplicatable

      # Each shape can have its own set of transforms and styles.
      def add_primitives(gc)
        gc.push
        add_transform_primitives(gc)
        add_style_primitives(gc)
        gc.__send__(@primitive, *@args)
        gc.pop
      end
    end # class Shape

    class Circle < Shape
      # Define a circle with radius +r+ and centered at [<tt>cx</tt>, <tt>cy</tt>].
      # Use the RVG::ShapeConstructors#circle method to create Circle objects in a container.
      def initialize(r, cx = 0, cy = 0)
        super()
        r, cx, cy = Magick::RVG.convert_to_float(r, cx, cy)
        raise ArgumentError, "radius must be >= 0 (#{r} given)" if r < 0

        @primitive = :circle
        @args = [cx, cy, cx + r, cy]
      end
    end # class Circle

    class Ellipse < Shape
      # Define an ellipse with a center at [<tt>cx</tt>, <tt>cy</tt>], a horizontal radius +rx+
      # and a vertical radius +ry+.
      # Use the RVG::ShapeConstructors#ellipse method to create Ellipse objects in a container.
      def initialize(rx, ry, cx = 0, cy = 0)
        super()
        rx, ry, cx, cy = Magick::RVG.convert_to_float(rx, ry, cx, cy)
        raise ArgumentError, "radii must be >= 0 (#{rx}, #{ry} given)" if rx < 0 || ry < 0

        @primitive = :ellipse
        # Ellipses are always complete.
        @args = [cx, cy, rx, ry, 0, 360]
      end
    end # class Ellipse

    class Line < Shape
      # Define a line from [<tt>x1</tt>, <tt>y1</tt>] to [<tt>x2</tt>, <tt>y2</tt>].
      # Use the RVG::ShapeConstructors#line method to create Line objects in a container.
      def initialize(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
        super()
        @primitive = :line
        @args = [x1, y1, x2, y2]
      end
    end # class Line

    class Path < Shape
      # Define an SVG path. The argument can be either a path string
      # or a PathData object.
      # Use the RVG::ShapeConstructors#path method to create Path objects in a container.
      def initialize(path)
        super()
        @primitive = :path
        @args = [path.to_s]
      end
    end # class Path

    class Rect < Shape
      # Define a width x height rectangle. The upper-left corner is at [<tt>x</tt>, <tt>y</tt>].
      # If either <tt>width</tt> or <tt>height</tt> is 0, the rectangle is not rendered.
      # Use the RVG::ShapeConstructors#rect method to create Rect objects in a container.
      def initialize(width, height, x = 0, y = 0)
        super()
        width, height, x, y = Magick::RVG.convert_to_float(width, height, x, y)
        raise ArgumentError, "width, height must be >= 0 (#{width}, #{height} given)" if width < 0 || height < 0

        @args = [x, y, x + width, y + height]
        @primitive = :rectangle
      end

      # Specify optional rounded corners for a rectangle. The arguments
      # are the x- and y-axis radii. If y is omitted it defaults to x.
      def round(rx, ry = nil)
        rx, ry = Magick::RVG.convert_to_float(rx, ry || rx)
        raise ArgumentError, "rx, ry must be >= 0 (#{rx}, #{ry} given)" if rx < 0 || ry < 0

        @args << rx << ry
        @primitive = :roundrectangle
        self
      end
    end  # class Rect

    class PolyShape < Shape
      def polypoints(points)
        case points.length
        when 1
          points = Array(points[0])
        when 2
          x_coords = Array(points[0])
          y_coords = Array(points[1])
          raise ArgumentError, 'array arguments must contain at least one point' unless !x_coords.empty? && !y_coords.empty?

          n = x_coords.length - y_coords.length
          short = n > 0 ? y_coords : x_coords
          olen = short.length
          n.abs.times { |x| short << short[x % olen] }
          