[cairo-commit] rcairo/src/lib/cairo/context circle.rb, NONE, 1.1 path.rb, NONE, 1.1 quad.rb, NONE, 1.1 rectangle.rb, NONE, 1.1

Kouhei Sutou commit at pdx.freedesktop.org
Tue Mar 6 04:17:41 PST 2007


Committed by: kou

Update of /cvs/cairo/rcairo/src/lib/cairo/context
In directory kemper:/tmp/cvs-serv14983/src/lib/cairo/context

Added Files:
	circle.rb path.rb quad.rb rectangle.rb 
Log Message:
* setup.rb: removed dependency on setup.rb. used Ruby-GNOME2 style.
* extconf.rb: moved from packages/cairo/ext/.

* packages/cairo/ext: move to ...
* src/: ... here.

* packages/cairo/lib: move to ...
* src/lib: ... here.

* README, samples/: followed the changes.


--- NEW FILE: circle.rb ---
module Cairo
  class Context
    module Circle
      def circle(x, y, radius)
        arc(x, y, radius, 0, 2 * Math::PI)
      end
    end
  end
end

--- NEW FILE: path.rb ---
module Cairo
  class Context
    module Path
      module Point
        module_function
        def distance(a, b)
          ax, ay = a
          bx, by = b
          Math.sqrt((bx - ax) ** 2 + (by - ay) ** 2)
        end
      end

      def transform_path(path, &block)
        save do
          new_path
          path.each do |type, points|
            case type
            when PATH_CURVE_TO
              curve_to(*points.collect(&block).flatten)
            when PATH_MOVE_TO
              move_to(*points.collect(&block).flatten)
            when PATH_LINE_TO
              line_to(*points.collect(&block).flatten)
            when PATH_CLOSE_PATH
              close_path
            end
          end
          copy_path
        end
      end

      def map_path_onto(path)
        parameterized_path = parameterize_path(path)
        transformed_path = transform_path(copy_path) do |x, y|
          current_point = nil
          d = x
          i = -1
          type = points = nil
          path.each do |type, points|
            i += 1
            break if d <= parameterized_path[i]
            d -= parameterized_path[i]
            case type
            when PATH_MOVE_TO
              current_point = points[0]
            when PATH_LINE_TO
              current_point = points[0]
            when PATH_CURVE_TO
              current_point = points[2]
            when PATH_CLOSE_PATH
            end
          end

          case type
          when PATH_MOVE_TO
            [x, y]
          when PATH_LINE_TO
            ratio = d / parameterized_path[i]
            current_x, current_y = current_point
            lx, ly = points[0]
            new_x = current_x * (1 - ratio) + lx * ratio
            new_y = current_y * (1 - ratio) + ly * ratio
            dx = -(current_x - lx)
            dy = -(current_y - ly)

            ratio = y / parameterized_path[i]
            [new_x + -dy * ratio, new_y + dx * ratio]
          when PATH_CURVE_TO
            ratio = d / parameterized_path[i]
            current_x, current_y = current_point
            cx0, cy0 = points[0]
            cx1, cy1 = points[1]
            cx2, cy2 = points[2]

            new_x = current_x * (1 - ratio) * (1 - ratio) * (1 - ratio) +
                    3 * cx0   * (1 - ratio) * (1 - ratio) * ratio +
                    3 * cx1   * (1 - ratio) *      ratio  * ratio +
                        cx2   *      ratio  *      ratio  * ratio
            new_y = current_y * (1 - ratio) * (1 - ratio) * (1 - ratio) +
                    3 * cy0   * (1 - ratio) * (1 - ratio) * ratio +
                    3 * cy1   * (1 - ratio) *      ratio  * ratio +
                        cy2   *      ratio  *      ratio  * ratio

            dx = -3 * current_x * (1 - ratio) * (1 - ratio) +
                  3 * cx0       * (1 - 4 * ratio + 3 * ratio * ratio) +
                  3 * cx1       * (    2 * ratio - 3 * ratio * ratio) +
                  3 * cx2       *      ratio  * ratio
            dy = -3 * current_y * (1 - ratio) * (1 - ratio) +
                  3 * cy0       * (1 - 4 * ratio + 3 * ratio * ratio) +
                  3 * cy1       * (    2 * ratio - 3 * ratio * ratio) +
                  3 * cy2       *      ratio  * ratio

            ratio = y / Math.sqrt(dx ** 2, dy ** 2)

            [new_x + -dy * ratio, new_y + dx * ratio]
          when PATH_CLOSE_PATH
            [x, y]
          end
        end
        new_path
        append_path(transformed_path)
      end

      private
      def parameterize_path(path)
        current_point = nil
        path.collect do |type, points|
          result = 0
          case type
          when PATH_MOVE_TO
            current_point = points[0]
          when PATH_LINE_TO
            result = Point.distance(current_point, points[0])
            current_point = points[0]
          when PATH_CURVE_TO
            result = Point.distance(current_point, points[0])
            result += Point.distance(points[0], points[1])
            result += Point.distance(points[1], points[2])
            current_point = points[2]
          when PATH_CLOSE_PATH
          end
          result
        end
      end
    end
  end
end

--- NEW FILE: quad.rb ---
module Cairo
  class Context
    module Quad
      def quad_to(x1, y1, x2, y2)
        x0, y0 = current_point
        cx1 = x0 + 2 * (x1 - x0) / 3.0
        cy1 = y0 + 2 * (y1 - y0) / 3.0
        cx2 = cx1 + (x2 - x0) / 3.0
        cy2 = cy1 + (y2 - y0) / 3.0
        curve_to(cx1, cy1, cx2, cy2, x2, y2)
      end

      def rel_quad_to(x1, y1, x2, y2)
        x0, y0 = current_point
        quad_to(x1 + x0, y1 + y0, x2 + x0, y2 + x0)
      end
    end
  end
end

--- NEW FILE: rectangle.rb ---
module Cairo
  class Context
    module Rectangle
      def rounded_rectangle(x, y, width, height, x_radius, y_radius=nil)
        x1 = x
        x2 = x1 + width
        y1 = y
        y2 = y1 + height

        y_radius ||= x_radius

        x_radius = [x_radius, width / 2.0].min
        y_radius = [y_radius, height / 2.0].min

        xr1 = x_radius
        xr2 = x_radius / 2.0
        yr1 = y_radius
        yr2 = y_radius / 2.0

        move_to(x1 + xr1, y1)
        line_to(x2 - xr1, y1)
        curve_to(x2 - xr2, y1, x2, y1 + yr2, x2, y1 + yr1)
        line_to(x2, y2 - yr1)
        curve_to(x2, y2 - yr2, x2 - xr2, y2, x2 - xr1, y2)
        line_to(x1 + xr1, y2)
        curve_to(x1 + xr2, y2, x1, y2 - yr2, x1, y2 - yr1)
        line_to(x1, y1 + yr1)
        curve_to(x1, y1 + yr2, x1 + xr2, y1, x1 + xr1, y1)
        close_path
      end
    end
  end
end



More information about the cairo-commit mailing list