[Commit] CairoJava/src/org/cairographics/cairo CairoSurface.java, NONE, 1.1 Cairo.java, NONE, 1.1 CairoMatrix.java, NONE, 1.1 CairoImage.java, NONE, 1.1

Soorya Kuloor commit at keithp.com
Wed Aug 6 12:54:05 PDT 2003


Committed by: skuloor

Update of /local/src/CVS/CairoJava/src/org/cairographics/cairo
In directory home.keithp.com:/tmp/cvs-serv20525/src/org/cairographics/cairo

Added Files:
	CairoSurface.java Cairo.java CairoMatrix.java CairoImage.java 
Log Message:
Initial checkin

--- NEW FILE: CairoSurface.java ---
/*
 * $Id: CairoSurface.java,v 1.1 2003/08/06 18:54:03 skuloor Exp $
 * 
 * Copyright (C) 2003 Verano
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Verano not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Verano makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * VERANO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * VERANO BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
package org.cairographics.cairo;

import org.cairographics.cairo.internal.*;

import org.eclipse.swt.graphics.Image;

/**
 * The CairoSurface class represents an Cairo drawing surface. An Cairo object is used to draw on the drawing
 * surface.
 */
public class CairoSurface {
    
    /** Native handle for the drawing surface */
    long handle = 0;

    /**
     * Default constructor. This is private.
     *
     */
    private CairoSurface() {
    }

    /**
     * Creates an Cairo surface using a native surface handle.
     *  
     * @param surface The surface handle.
     */
    CairoSurface(long surface) {
        this.handle = surface;
    }

    /**
     * Constructs a surface using an SWT image object. Anything drawn on the surface will be stored in the 
     * image.
     * 
     * @param img The image to draw on.
     */
    public CairoSurface(CairoImage img) {
        handle = CairoAPI.surfaceCreateForImage(img.handle);
    }

    /**
     * Creates a new surface for the given image.
     * 
     * @param img The SWT image object
     */
    public CairoSurface(Image img) {
        handle = CairoAPI.surfaceCreateForPixmap(img.pixmap);    
    }
    
    /**
     * Creates a new surface similar to one given, but with slightly different specs.
     * 
     * @param neighbor The surface to use as a template
     * @param format The RGB format (one of Cairo.FORMAT_xxx constants)
     * @param width Width of the new surface
     * @param height Height of the new surface
     */
    public CairoSurface(CairoSurface neighbor, short format, int width, int height) {
        handle = CairoAPI.surfaceCreateSimilar(neighbor.handle, format, width, height);
    }

    /**
     * Creates a new surface similar to one given, but with slightly different specs.
     * 
     * @param neighbor The surface to use as a template
     * @param format The RGB format (one of Cairo.FORMAT_xxx constants)
     * @param width Width of the new surface
     * @param height Height of the new surface
     * @param red The red component of the background color
     * @param green The green component of the background color
     * @param blue The blue component of the background color
     * @param alpha The alpha component of the background color
     */
    public CairoSurface(
        CairoSurface neighbor,
        short format,
        int width,
        int height,
        double red,
        double green,
        double blue,
        double alpha) {
        handle =
            CairoAPI.surfaceCreateSimilarSolid(
                neighbor.handle,
                format,
                width,
                height,
                red,
                green,
                blue,
                alpha);
    }

    /**
     * Disposes native resources used by the surface. Do not use the surface once it is
     * disposed.
     *
     */
    public void dispose() {
        CairoAPI.surfaceDestroy(handle);
    }

    /**
     * Sets the transformation matrix for the surface.
     * 
     * @param matrix Transformation matrix
     */
    public void setMatrix(CairoMatrix matrix) {
        CairoAPI.surfaceSetMatrix(handle, matrix.handle);
    }
    
    /**
     * Returns the transformation matrix of the surface.
     * 
     * @return The transformation matrix.
     */
    public CairoMatrix getMatrix() {
        CairoMatrix mat = new CairoMatrix();
        CairoAPI.surfaceGetMatrix(handle, mat.handle);
        return mat;
    }

    /**
     * Sets the filter for the surface (???)
     * 
     * @param filter The filter specification
     */
    public void setFilter(short filter) {
        CairoAPI.surfaceSetFilter(handle, filter);
    }

    /**
     * Sets the repeat count for filling (//TODO: Figure out what this is)
     * 
     * @param repeat The repeat count.
     */
    public void setRepeat(int repeat) {
        CairoAPI.surfaceSetRepeat(handle, repeat);
    }

}

--- NEW FILE: Cairo.java ---
/*
 * $Id: Cairo.java,v 1.1 2003/08/06 18:54:03 skuloor Exp $
 * 
 * Copyright (C) 2003 Verano
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Verano not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Verano makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * VERANO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * VERANO BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package org.cairographics.cairo;

import org.cairographics.cairo.internal.*;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Widget;

/**
 * This class wraps the native cairo graphics state.
 */
public class Cairo {
    /*
     * Cairo Format:
     */
    public final static short FORMAT_ARGB32 = 0;
    public final static short FORMAT_RGB24 = 1;
    public final static short FORMAT_A8 = 2;
    public final static short FORMAT_A1 = 4;

    // TODO: Define Operator constants here

    /*
     * Fill Rule constants
     */
    public final static short FILLRULE_WINDING = 0;
    public final static short FILLRULE_EVENODD = 1;

    /*
     * Linecap rules: CairoLineCapButt, CairoLineCapRound, CairoLineCapSquare
     */
    public final static short LINECAP_BUTT = 0;
    public final static short LINECAP_ROUND = 1;
    public final static short LINECAP_SQUARE = 2;

    /*
     * Line join styles
     */
    public final static short LINEJOIN_MITER = 0;
    public final static short LINEJOIN_ROUND = 1;
    public final static short LINEJOIN_BEVEL = 2;

    /*
     * Cairo function call status constants
     */
    public final static short STATUS_SUCCESS = 0;
    public final static short STATUS_NO_MEMORY = 1;
    public final static short STATUS_INVALID_RESTORE = 2;
    public final static short STATUS_INVALID_POPGROUP = 3;
    public final static short STATUS_NO_CURRENT_POINT = 4;
    public final static short STATUS_INVALID_MATRIX = 5;

    /** native cairo state handle */
    long handle = 0;

    /**
     * This is the default XrState constructor. This kept private because unless you set some
     * drawing target, xrstate is useless and cairo library may dump core.
     */
    private Cairo() {
        handle = CairoAPI.create();
    }

    /**
     * Constructs a new state that draws on a SWT image.
     * 
     * @param img SWT image
     */
    public Cairo(Image img) {
        this();
        setTarget(img);
    }

    /**
     * Constructs a new cairo state that draws on an cairo surface.
     * 
     * @param surface cairo surface
     */
    public Cairo(CairoSurface surface) {
        this();
        setTarget(surface);
    }

    /**
     * Constructs a new cairo state that draws on a SWT widget.
     * 
     * @param widget SWT widget
     */
    public Cairo(Widget widget) {
        this();
        setTarget(widget);
    }

    /**
     * Disposes all the native resources used by the object.
     *
     */
    public void dispose() {
        CairoAPI.destroy(handle);
    }

    /**
     * Pushes the current state into the state stack.
     *
     */
    public void push() {
        save();
    }

    /**
     * Pops a state from the state stack and restores its parameters into current state.
     *
     */
    public void pop() {
        restore();
    }

    /**
     * Saves the current graphics state onto the state stack. This method does the same thing
     * as the push() method.
     *
     */
    public void save() {
        CairoAPI.save(handle);
    }

    /**
     * Restores the last state from the state stack. Does the same thing as the pop() method.
     *
     */
    public void restore() {
        CairoAPI.restore(handle);
    }

    /**
     * Sets the surface as the target surface for the state. The state uses this surface to draw shapes on.
     * 
     * @param surface
     */
    public void setTarget(CairoSurface surface) {
        CairoAPI.setTargetSurface(handle, surface.handle);
    }

    /**
     * Sets the image as the target for this state. All shapes will be drawn on this image.
     * 
     * @param img The target image.
     */
    public void setTarget(Image img) {
        CairoAPI.setTargetImage(handle, img.pixmap);
    }

    /**
     * Sets the widget as the target for this state. All the shapes will be drawn on this widget.
     * 
     * @param widget An SWT widget.
     */
    public void setTarget(Widget widget) {
        CairoAPI.setTargetDrawable(handle, widget.handle);
    }

    /**
     * Sets the color for drawing. Note that the RGB values must be within the range of 0.0 and 1.0.
     * 
     * @param red The red component of the color (range 0.0 - 1.0)
     * @param green The green component of the color (range 0.0 - 1.0).
     * @param blue  The blue component of the color (range 0.0 - 1.0).
     */
    public void setColor(double red, double green, double blue) {
        CairoAPI.setRGBColor(handle, red, green, blue);
    }

    /**
     * Sets the color using an SWT color.
     * 
     * @param color SWT color specification.
     */
    public void setColor(Color color) {
        double r = ((double) color.getRed()) / 255.0;
        double g = ((double) color.getGreen()) / 255.0;
        double b = ((double) color.getBlue()) / 255.0;
        CairoAPI.setRGBColor(handle, r, g, b);
    }

    /**
     * Sets the fill pattern.
     * 
     * @param surface The fill pattern surface.
     */
    public void setPattern(CairoSurface surface) {
        CairoAPI.setPattern(handle, surface.handle);
    }

    /**
     * Sets the tessellation tolerance. The tolerance specifies how accurately curves must be represented.
     * A higher value of tolerance show jagged curves, whereas a lower value of tolerance shows smooth curves.
     * A value of around 0.5 gives really good results.
     * 
     * @param tolerance The tesselation tolerance. Must be >= 0.0.
     */
    public void setTolerance(double tolerance) {
        CairoAPI.setTolerance(handle, tolerance);
    }

    /**
     * Sets alpha (opacity) value for the state.
     * 
     * @param alpha Alpha (opacity) value. Must be between 0.0 and 1.0.
     */
    public void setAlpha(double alpha) {
        CairoAPI.setAlpha(handle, alpha);
    }

    /**
     * Sets the cairo operator for operation between surfaces.
     * 
     * @param operator Surface operator (see the constants in the cairo class).
     */
    public void setOperator(short operator) {
        CairoAPI.setOperator(handle, operator);
    }

    /**
     * Sets the fill rule to one of the cairo.FILLRULE_XXX rules.
     * 
     * @param fillRule The fill rule, one of FILLRULE_WINDING, FILLRULE_EVENODD.
     */
    public void setFillRule(short fillRule) {
        CairoAPI.setFillRule(handle, fillRule);
    }

    /**
     * Sets the line width for drawing (stroking) shapes.
     * 
     * @param width Line width
     */
    public void setLineWidth(double width) {
        CairoAPI.setLineWidth(handle, width);
    }

    /**
     * Sets the line cap style for lines. Must be one of cairo.LINECAP_xxxx constants.
     * 
     * @param linecap The line cap style.
     */
    public void setLineCap(short linecap) {
        CairoAPI.setLineCap(handle, linecap);
    }

    /**
     * Sets the line join style for joining lines. Must be one of cairo.LINEJOIN_xxx constants.
     * 
     * @param lineJoin
     */
    public void setLineJoin(short lineJoin) {
        CairoAPI.setLineJoin(handle, lineJoin);
    }

    /**
     * Sets dashes for stroking shapes.
     * 
     * @param dashes The dashes array. This array contains the lengths of dashes and spaces for stroking.
     * @param offset The offset where the first dash must be drawn.
     */
    public void setDash(double[] dashes, double offset) {
        CairoAPI.setDash(handle, dashes, offset);
    }

    /**
     * Sets the miter limit for LINEJOIN_MITER style of line join
     * 
     * @param limit The miter limit.
     */
    public void setMiterLimit(double limit) {
        CairoAPI.setMiterLimit(handle, limit);
    }

    /**
     * Append translation transformation to this state.
     * 
     * @param tx X translation
     * @param ty Y translation
     */
    public void translate(double tx, double ty) {
        CairoAPI.translate(handle, tx, ty);
    }

    /**
     * Append non-uniform scaling to this state.
     * 
     * @param sx X axis scaling
     * @param sy Y axis scaling
     */
    public void scale(double sx, double sy) {
        CairoAPI.scale(handle, sx, sy);
    }

    /**
     * Append rotational transformation to this state.
     * 
     * @param angle Angle of rotation in radians.
     */
    public void rotate(double angle) {
        CairoAPI.rotate(handle, angle);
    }

    /**
     * Sets the transformation matrix for this state.
     * 
     * @param matrix The transformation matrix
     */
    public void setMatrix(CairoMatrix matrix) {
        CairoAPI.setMatrix(handle, matrix.handle);
    }

    /**
     * Appends the given transformation matrix to the current transformation of the state.
     * 
     * @param matrix The transformation matrix to append
     */
    public void concatMatrix(CairoMatrix matrix) {
        CairoAPI.concatMatrix(handle, matrix.handle);
    }

    /**
     * Set the transformation matrix for the state to default (identity) matrix.
     *
     */
    public void defaultMatrix() {
        CairoAPI.defaultMatrix(handle);
    }

    /**
     * Set the transformation matrix for the state to identity matrix.
     *
     */
    public void identityMatrix() {
        CairoAPI.identityMatrix(handle);
    }

    /**
     * Starts a new path. You can add path segments to this path using the path extension methods
     * (xxxxTo()).
     */
    public void newPath() {
        CairoAPI.newPath(handle);
    }

    /**
     * Moves the current point in the path to the given co-ordinates.
     * 
     * @param x The x co-ordinate of the point to move to
     * @param y The y co-ordinate of the point to move to
     */
    public void moveTo(double x, double y) {
        CairoAPI.moveTo(handle, x, y);
    }

    /**
     * Moves to the current path to a new point. The co-ordinates of the new point are given in relation
     * to the current point of the state.
     * 
     * @param tx Relative x distance between current point and the new point
     * @param ty Relative y distance between current point and the new point
     */
    public void relMoveTo(double tx, double ty) {
        CairoAPI.relMoveTo(handle, tx, ty);
    }

    /**
     * Draws a line segment as part of the current path. The line is drawn from the current point of the
     * path to the new co-ordinates.
     * 
     * @param x The x coordinate for the end point for the line segment
     * @param y The y co-ordinate for the end point for the line segment
     */
    public void lineTo(double x, double y) {
        CairoAPI.lineTo(handle, x, y);
    }

    /**
     * Draws a line segment as part of the current path. The line is drawn from the current point of the
     * path to the new co-ordinates. The new co-ordinates are given relative to the current point.
     * 
     * @param tx The relative x coordinate for the end point for the line segment
     * @param ty The relative y co-ordinate for the end point for the line segment
     */
    public void relLineTo(double tx, double ty) {
        CairoAPI.relLineTo(handle, tx, ty);
    }

    /**
     * Draws a cubic bezier curve from the current point to (x3, y3) using 2 control points (x1, y1) and
     * (x2, y2).
     * 
     * @param x1 x co-ordinate of the first control point
     * @param y1 y co-ordinate of the first control point
     * @param x2 x co-ordinate of the second control point
     * @param y2 y co-ordinate of the second control point
     * @param x3 x co-ordinate of the end point
     * @param y3 y co-ordinate of the end point
     */
    public void curveTo(
        double x1,
        double y1,
        double x2,
        double y2,
        double x3,
        double y3) {
        CairoAPI.curveTo(handle, x1, y1, x2, y2, x3, y3);
    }

    /**
     * Draws a cubic bezier curve from the current point to (dx3, dy3) using 2 control points (dx1, dy1) and
     * (dx2, dy2). The co-ordinates are specified relative to current point in the path.
     * 
     * @param dx1 relative x co-ordinate of the first control point
     * @param dy1 relative y co-ordinate of the first control point
     * @param dx2 relative x co-ordinate of the second control point
     * @param dy2 relative y co-ordinate of the second control point
     * @param dx3 relative x co-ordinate of the end point
     * @param dy3 relative y co-ordinate of the end point
     */
    public void relCurveTo(
        double dx1,
        double dy1,
        double dx2,
        double dy2,
        double dx3,
        double dy3) {
        CairoAPI.relCurveTo(handle, dx1, dy1, dx2, dy2, dx3, dy3);
    }

    /**
     * Draws a quadratic bezier curve from the current point to (x2, y2) using a control point (x1, y1).
     * 
     * @param x1 x co-ordinate of the control point
     * @param y1 y co-ordinate of the control point
     * @param x2 x co-ordinate of the end point
     * @param y2 y co-ordinate of the end point
     */
    public void quadTo(double x1, double y1, double x2, double y2) {
        CairoAPI.quadTo(handle, x1, y1, x2, y2);
    }

    /**
     * Draws a quadratic bezier curve from the current point to (x2, y2) using a control point (x1, y1).
     * The co-ordinates are specified relative to current point in the path.
     * 
     * @param dx1 relative x co-ordinate of the control point
     * @param dy1 relative y co-ordinate of the control point
     * @param dx2 relative x co-ordinate of the end point
     * @param dy2 relative y co-ordinate of the end point
     */
    public void relQuadTo(double dx1, double dy1, double dx2, double dy2) {
        CairoAPI.relQuadTo(handle, dx1, dy1, dx2, dy2);
    }

    /**
     * Draws an elliptic arc from the current point to (end_x, end_y) with the given parameters.
     * 
     * @param rx The x radius of the arc.
     * @param ry The y radius of the arc.
     * @param x_axis_rotation The x axis rotation for the arc
     * @param large_arc_flag If 1 a large arc is drawn, else a short arc
     * @param sweep_flag If 1 clockwise sweep is used, else anti-clockwise
     * @param end_x x coodinate of the end point
     * @param end_y y co-ordinate of the end point
     */
    public void arcTo(
        double rx,
        double ry,
        double x_axis_rotation,
        int large_arc_flag,
        int sweep_flag,
        double end_x,
        double end_y) {
        CairoAPI.arcTo(
            handle,
            rx,
            ry,
            x_axis_rotation,
            large_arc_flag,
            sweep_flag,
            end_x,
            end_y);
    }

    /**
     * Draws an elliptic arc from the current point to (end_dx, end_dy) with the given parameters.
     * The co-ordinates of the end point are relative to the current point.
     * 
     * @param rx The x radius of the arc.
     * @param ry The y radius of the arc.
     * @param x_axis_rotation The x axis rotation for the arc
     * @param large_arc_flag If 1 a large arc is drawn, else a short arc
     * @param sweep_flag If 1 clockwise sweep is used, else anti-clockwise
     * @param end_dx relative x coodinate of the end point
     * @param end_dy relative y co-ordinate of the end point
     */
    public void relArcTo(
        double rx,
        double ry,
        double x_axis_rotation,
        int large_arc_flag,
        int sweep_flag,
        double end_dx,
        double end_dy) {
        CairoAPI.relArcTo(
            handle,
            rx,
            ry,
            x_axis_rotation,
            large_arc_flag,
            sweep_flag,
            end_dx,
            end_dy);
    }

    /**
     * Draws a horizontal line from the current point to the new x co-ordinates.
     * 
     * @param x x co-ordinate of the end point.
     */
    public void hLineTo(double x) {
        CairoAPI.hLineTo(handle, x);
    }

    /**
     * Draws a horizontal line from the current point to the new x co-ordinates.
     * 
     * @param dx relative x co-ordinate of the end point.
     */
    public void relHLineTo(double dx) {
        CairoAPI.relHLineTo(handle, dx);
    }

    /**
     * Draws a vertical line from the current point to the new y co-ordinates.
     * 
     * @param y y co-ordinate of the end point.
     */
    public void vLineTo(double y) {
        CairoAPI.vLineTo(handle, y);
    }

    /**
     * Draws a vertical line from the current point to the new y co-ordinates.
     * 
     * @param dy relative y co-ordinate of the end point.
     */
    public void relVLineTo(double dy) {
        CairoAPI.relVLineTo(handle, dy);
    }

    /**
     * Closes the current path by connecting current point to the starting point with a line segment.
     *
     */
    public void closePath() {
        CairoAPI.closePath(handle);
    }

    /**
     * Strokes the current path.
     *
     */
    public void stroke() {
        CairoAPI.stroke(handle);
    }

    /**
     * Fills the current path
     *
     */
    public void fill() {
        CairoAPI.fill(handle);
    }

    /**
     * Uses current path as a clip to fill or stroke next paths.
     *
     */
    public void clip() {
        CairoAPI.clip(handle);
    }

    /**
     * Sets a rectangular clip for the given state.
     * 
     * CAUTION: If the current has transformation matrix, then this rectangle is transformed into
     * drawing co-ordinates and then the clip is set to the bounding rectangle of the transformed rectangle.
     * 
     * @param rect The rectangular clip region
     */
    public void clipRegion(int x, int y, int width, int height) {
        CairoAPI.clipSurface(handle, x, y, width, height);
    }
    
    /**
     * Sets the font for drawing text. The font names must be of the form "family:weight:style".
     * e.g. "Verdana:bold:italic", "arial:normal:normal"
     *  
     * @param key The font specification as a string.
     */
    public void selectFont(String key) {
        CairoAPI.selectFont(handle, key);
    }

    /**
     * Uniformly scale the current font by the given amount. You can set font size using this method.
     * 
     * @param scale The scaling factor.
     */
    public void scaleFont(double scale) {
        CairoAPI.scaleFont(handle, scale);
    }

    /**
     * Transforms the current font using the given affine parameters.
     * Note: tx, ty are not required.
     * 
     * @param a affine parameter a
     * @param b affine parameter b
     * @param c affine parameter c
     * @param d affine parameter d
     */
    public void transformFont(double a, double b, double c, double d) {
        CairoAPI.transformFont(handle, a, b, c, d);
    }

    /**
     * Returns the text extents of the given string using current font and size settings.
     * 
     * @param str The text string.
     * @return The extents of the given string
     */
    public double[] getTextExtents(String str) {
        return CairoAPI.getTextExtents(handle, str);
    }

    /**
     * Draws the given text on the screen.
     * 
     * @param text String to draw on the screen.
     */
    public void showText(String text) {
        CairoAPI.showText(handle, text);
    }

    /**
     * Shows an image at a given location.
     * 
     * @param img The SWT image object
     * @param x The x co-ordinate of the location
     * @param y The y co-ordinate of the location
     */    
    public void showImage(Image img, double x, double y) {
        
        /*
         * Create a surface from the given image and then show the surface
         */
        CairoSurface surface = new CairoSurface(img);
        translate(x, y);
        CairoAPI.showSurface(handle, surface.handle, img.getImageData().width, img.getImageData().height);
        translate(-x, -y);
        surface.dispose();
    }
    
    /**
     * Shows an XrSurface on the screen. All the shapes and images drawn onto this surface will be
     * shown.
     * 
     * @param surface The surface to display
     * @param width Width of the surface
     * @param height Height of the surface
     */
    public void showSurface(CairoSurface surface, int width, int height) {
        CairoAPI.showSurface(handle, surface.handle, width, height);
    }

    /**
     * Returns the target surface (the surface used to draw shapes on) of this state.
     * 
     * @return The target surface of this state.
     */
    public CairoSurface getTargetSurface() {
        return new CairoSurface(CairoAPI.getTargetSurface(handle));
    }

    /*
     * Functions to return error status
     *
     */
     
    /**
     * Returns the error status of the last cairo call. The return value is one of
     * cairo.STATUS_xxx constants.
     * 
     * @return The error status of the last call.
     */
    public short getStatus() {
        return CairoAPI.getStatus(handle);
    }

    /**
     * Returns the error message for the last cairo call as string.
     * 
     * @return Error message.
     */
    public String getStatusString() {
        return CairoAPI.getStatusString(handle);
    }

    /**
     * Returns the current surface operator
     * 
     * @return The surface operator.
     */
    public short getOperator() {
        return CairoAPI.getOperator(handle);
    }
    
    /**
     * Returns the tesselation tolerance of the current state.
     * 
     * @return tesselation tolerance
     */
    public double getTolerance() {
        return CairoAPI.getTolerance(handle);
    }

    /**
     * Returns the current point of the surface.
     * 
     * @return Current point for drawing
     */
    public double[] getCurrentPoint() {
        return CairoAPI.getCurrentPoint(handle);
    }

    /**
     * Returns the stroke line width.
     * 
     * @return The stroke line width
     */
    public double getLineWidth() {
        return CairoAPI.getLineWidth(handle);
    }

    /**
     * Returns current linecap style.
     * 
     * @return The line cap style
     */
    public short getLineCap() {
        return CairoAPI.getLineCap(handle);
    }

    /**
     * Return current line join style.
     * 
     * @return Line join style
     */
    public short getLineJoin() {
        return CairoAPI.getLineJoin(handle);
    }

    /**
     * Returns the miter limit for miter style line joins
     * 
     * @return The miter limit
     */
    public double getMiterLimit() {
        return CairoAPI.getMiterLimit(handle);
    }

    /** Constant use for drawing ellipse */
    private static final double SVG_ARC_MAGIC = 0.5522847498;
    
    /**
     * Creates an ellipse path.
     * 
     * @param cx X co-ordinate of the center of ellipse
     * @param cy Y co-ordinate of the center of ellipse
     * @param rx X radius of the ellipse
     * @param ry Y radius of the ellipse
     */
    public void ellipse(double cx, double cy, double rx, double ry) {

        /* approximate an ellipse using 4 bezier curves */
        CairoAPI.newPath(handle);
        CairoAPI.moveTo(handle, cx + rx, cy);
        CairoAPI.curveTo(
            handle,
            cx + rx,
            cy + ry * SVG_ARC_MAGIC,
            cx + rx * SVG_ARC_MAGIC,
            cy + ry,
            cx,
            cy + ry);
        CairoAPI.curveTo(
            handle,
            cx - rx * SVG_ARC_MAGIC,
            cy + ry,
            cx - rx,
            cy + ry * SVG_ARC_MAGIC,
            cx - rx,
            cy);
        CairoAPI.curveTo(
            handle,
            cx - rx,
            cy - ry * SVG_ARC_MAGIC,
            cx - rx * SVG_ARC_MAGIC,
            cy - ry,
            cx,
            cy - ry);
        CairoAPI.curveTo(
            handle,
            cx + rx * SVG_ARC_MAGIC,
            cy - ry,
            cx + rx,
            cy - ry * SVG_ARC_MAGIC,
            cx + rx,
            cy);
        CairoAPI.closePath(handle);
    }

    /**
     * Draws a rounder rectangle.
     * 
     * @param x X co-ordinate of the top left corner
     * @param y Y co-ordinate of the top left corner
     * @param width Width of the rectangle
     * @param height Height of the rectangle
     * @param rx X radius of rounded corners
     * @param ry Y radius of rounded corners
     */
    public void rect(
        double x,
        double y,
        double width,
        double height,
        double rx,
        double ry) {

        /*
         * Check whether we really need rounded corners
         */
        CairoAPI.newPath(handle);
        if (rx > 0.0 || ry > 0.0) {
            CairoAPI.moveTo(handle, x + rx, y);
            CairoAPI.lineTo(handle, x + width - rx, y);
            CairoAPI.arcTo(handle, rx, ry, 0, 0, 1, x + width, y + ry);
            CairoAPI.lineTo(handle, x + width, y + height - ry);
            CairoAPI.arcTo(handle, rx, ry, 0, 0, 1, x + width - rx, y + height);
            CairoAPI.lineTo(handle, x + rx, y + height);
            CairoAPI.arcTo(handle, rx, ry, 0, 0, 1, x, y + height - ry);
            CairoAPI.lineTo(handle, x, y + ry);
            CairoAPI.arcTo(handle, rx, ry, 0, 0, 1, x + rx, y);
        } else {
            CairoAPI.moveTo(handle, x, y);
            CairoAPI.relLineTo(handle, 0, height);
            CairoAPI.relLineTo(handle, width, 0);
            CairoAPI.relLineTo(handle, 0, -height);
        }
        CairoAPI.closePath(handle);
    }
    
    /**
     * Transforms a point to the global co-ordinates and returns the transformed point. The original
     * point remains unchanged.
     * 
     * @param pt The point in local co-ordinates
     * @return The transformed point
     */
    public double[] getTransformedPoint(double x, double y) {
        return CairoAPI.transformPoint(handle, x, y);
    }
}

--- NEW FILE: CairoMatrix.java ---
/*
 * $Id: CairoMatrix.java,v 1.1 2003/08/06 18:54:03 skuloor Exp $
 * 
 * Copyright (C) 2003 Verano
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Verano not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Verano makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * VERANO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * VERANO BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
package org.cairographics.cairo;

import org.cairographics.cairo.internal.*;

/**
 * This class wraps the Cairo transformation matrix.
 */
public class CairoMatrix {
    
    /**
     * Native matrix handle.
     */
    long handle = 0;

    /**
     * Default constructor. Creates a default identity matrix.
     *
     */
    public CairoMatrix() {
        handle = CairoAPI.matrixCreate();
    }

    /**
     * Constructs a matrix using the affine parameters.
     * 
     * @param a Parameter a
     * @param b Parameter b
     * @param c Parameter c
     * @param d Parameter d
     * @param tx Parameter tx
     * @param ty Parameter ty
     */
    public CairoMatrix(
        double a,
        double b,
        double c,
        double d,
        double tx,
        double ty) {
        handle = CairoAPI.matrixCreate();
        CairoAPI.matrixSetAffine(handle, a, b, c, d, tx, ty);
    }

    /**
     * Constructs a new matrix by copying another matrix.
     * 
     * @param other The matrix to copy from.
     */
    public CairoMatrix(CairoMatrix other) {
        handle = CairoAPI.matrixCreate();
        CairoAPI.matrixCopy(handle, other.handle);
    }

    /**
     * Disposes all the native resources used by the matrix.
     */
    public void dispose() {
        CairoAPI.matrixDestroy(handle);
    }

    /**
     * Returns a copy of this matrix.
     * 
     * @return A new matrix that is a copy of this matrix.
     */
    public CairoMatrix copy() {
        CairoMatrix other = new CairoMatrix();
        CairoAPI.matrixCopy(other.handle, handle);
        return other;
    }

    /**
     * Sets the matrix to identity matrix (no transformations).
     */
    public void setIdentity() {
        CairoAPI.matrixSetIdentity(handle);
    }

    /**
     * Returns all the affine parameters in a 6 element array.
     * 
     * @return 6 element array with affine parameters as contents.
     */
    public double[] getAffine() {
        return CairoAPI.matrixGetAffine(this.handle);
    }

    /**
     * Sets the transformation matrix elements (see the class documentation above).
     * 
     * @param a The a element
     * @param b The b element
     * @param c The c element
     * @param d The d element
     * @param tx The tx element
     * @param ty The ty element
     */
    public void setAffine(
        double a,
        double b,
        double c,
        double d,
        double tx,
        double ty) {
        CairoAPI.matrixSetAffine(handle, a, b, c, d, tx, ty);
    }

    /**
     * Appends a transaltion transformation to this matrix.
     * 
     * @param x X axis translation
     * @param y Y axis translation
     */
    public void translate(double tx, double ty) {
        CairoAPI.matrixTranslate(handle, tx, ty);
    }

    /**
     * Appends non-uniform scaling to this matrix.
     * 
     * @param sx X axis scaling factor
     * @param sy Y axis scaling factor
     */
    public void scale(double sx, double sy) {
        CairoAPI.matrixScale(handle, sx, sy);
    }

    /**
     * Appends rotation transformation to this matrix.
     * 
     * @param angle The rotation angle in radians.
     */
    public void rotate(double radians) {
        CairoAPI.matrixRotate(handle, radians);
    }

    /**
     * Appends rotation around a given point, to this transformation.
     * 
     * @param radians Rotation angle in radians.
     * @param cx The X co-ordinate of the center of rotation
     * @param cy The Y co-ordinate of the center of rotation
     */
    public void rotate(double radians, double cx, double cy) {
        CairoAPI.matrixTranslate(handle, cx, cy);
        CairoAPI.matrixRotate(handle, radians);
        CairoAPI.matrixTranslate(handle, -cx, -cy);
    }

    /**
     * Inverts this matrix.
     *
     */
    public void invert() {
        CairoAPI.matrixInvert(handle);
    }

    /**
     * Multiplies 2 matrices and returns the result.
     * 
     * @param a first matrix
     * @param b second matrix
     * @return The product
     */
    public static CairoMatrix multiply(CairoMatrix a, CairoMatrix b) {
        CairoMatrix product = new CairoMatrix();
        CairoAPI.matrixMultiply(product.handle, a.handle, b.handle);
        return product;
    }

    /**
     * Returns a copy of the matrix.
     * 
     * @return A new matrix that is copy of this matrix.
     */
    public CairoMatrix getCopy() {
        return copy();
    }

    /**
     * Multiples this matrix with another matrix.
     * 
     * @param secondMatrix The second matrix to multiply with.
     */
    public void multiply(CairoMatrix secondMatrix) {
        CairoMatrix other = (CairoMatrix) secondMatrix;
        CairoMatrix tmp = copy();
        CairoAPI.matrixMultiply(handle, tmp.handle, other.handle);
        tmp.dispose();
    }

    /**
     * Rotate from the given vector.
     * 
     * @param x
     * @param y
     */
    public void rotateFromVector(double x, double y) {
        // TODO Implement this
    }

    /**
     * Appends uniform scaling transformation to this matrix.
     * 
     * @param scaleFactor The uniform scaling factor.
     */
    public void scale(double scaleFactor) {
        scale(scaleFactor, scaleFactor);
    }

    /**
     * Transforms the given point and returns transformed co-ordinates
     * 
     * @param x The x co-ordinate of the point to be transformed.
     * @param y The y co-ordinate of the point to be transformed.
     * @return A 2 element array with transformed x and y co-ordinate values.
     */
    public double[] getTransformedPoint(double x, double y) {
        return CairoAPI.matrixTransformPoint(this.handle, x, y);
    }

    /**
     * Transforms the given distance and returns transformed co-ordinates
     * 
     * @param dx The x distance
     * @param dy The y distance
     * @return A 2 element array with transformed x and y distances
     */
    public double[] getTransformedDistance(double dx, double dy) {
        return CairoAPI.matrixTransformDistance(this.handle, dx, dy);
    }
}

--- NEW FILE: CairoImage.java ---
/*
 * $Id: CairoImage.java,v 1.1 2003/08/06 18:54:03 skuloor Exp $
 * 
 * Copyright (C) 2003 Verano
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Verano not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Verano makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * VERANO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * VERANO BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 */
package org.cairographics.cairo;

import org.cairographics.cairo.internal.*;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Widget;

/**
 * This class is a wrapper around a local image buffer. This buffer can be used to render into using Cairo
 * when the X server does not support Render extension.
 */
public class CairoImage {

    /** Native handle */
    long handle = 0;
    
    /** Image width in number of pixels */
    int width;
    
    /** Image height in number of pixels */
    int height;

    /**
     * Default constructor. A image must be constructed with width and height arguments.
     * Due to this reason the default ctor is protecte.
     *
     */
    protected CairoImage() {
        width = height = 0;
    }

    /**
     * Constructs an image with given format, width and height
     * 
     * @param format Image format (one of Cairo.FORMAT_xxx options).
     * @param width The image width in number of pixels
     * @param height The image height in number of pixels
     */
    public CairoImage(short format, int width, int height) {
        this.width = width;
        this.height = height;

        handle = CairoAPI.createImage(format, width, height);
    }

    /**
     * Creates an image in FORMAT_ARGB32 format, with given width and height.
     * 
     * @param width The image width in number of pixels
     * @param height The image height in number of pixels
     */
    public CairoImage(int width, int height) {
        this.width = width;
        this.height = height;

        handle = CairoAPI.createImage(Cairo.FORMAT_ARGB32, width, height);
    }

    /**
     * Disposes the image by releasing its native memory and resources.
     *
     */
    public void dispose() {
        CairoAPI.destroyImage(handle);
        handle = 0;
    }

    /**
     * Draws the image on an SWT graphics context object. The image is drawn at (0,0) location
     * of the graphics context.
     * 
     * @param w The widget to draw on
     * @param gc The graphics context
     */
    public void drawOn(Widget w, GC gc) {
        CairoAPI.drawImage(handle, w.handle, gc.handle, 0, 0, 0, 0, width, height);
    }

    /**
     * Draws the image on an SWT graphics context object at the given location
     * 
     * @param w The widget to draw on
     * @param gc The graphics context
     * @param dest_x The x co-ordinate of the location to draw the image
     * @param dest_y The y co-ordinate of the location to draw the image
     */
    public void drawOn(Widget w, GC gc, int dest_x, int dest_y) {
        CairoAPI.drawImage(
            handle,
            w.handle,
            gc.handle,
            0,
            0,
            dest_x,
            dest_y,
            this.width,
            this.height);
    }

    /**
     * Draws a rectangular part of the the image on an SWT graphics context object at the given location
     * 
     * @param w The widget to draw on
     * @param gc The graphics context
     * @param src_x the image x co-ordinate of the starting point
     * @param src_y the image y co-ordinate of the starting point
     * @param dest_x the x co-ordinate of the destination point
     * @param dest_y the y co-ordinate of the destination point
     * @param wdth The width of the section of the image to draw
     * @param ht The height of the section of the image to draw 
     */
    public void drawOn(
        Widget w,
        GC gc,
        int src_x,
        int src_y,
        int dest_x,
        int dest_y,
        int wdth,
        int ht) {
        CairoAPI.drawImage(
            handle,
            w.handle,
            gc.handle,
            src_x,
            src_y,
            dest_x,
            dest_y,
            wdth,
            ht);
    }
}




More information about the Commit mailing list