[cairo-commit] cairo-java/test/org/freedesktop/cairo/test CairoSnippetsPDF.java, NONE, 1.1 Snippets.java, NONE, 1.1

Jeffrey Morgan commit at pdx.freedesktop.org
Fri Mar 4 12:34:49 PST 2005


Committed by: kuzman

Update of /cvs/cairo/cairo-java/test/org/freedesktop/cairo/test
In directory gabe:/tmp/cvs-serv13777/test/org/freedesktop/cairo/test

Added Files:
	CairoSnippetsPDF.java Snippets.java 
Log Message:
More updates and start of snippets tests.

--- NEW FILE: CairoSnippetsPDF.java ---
/*
 * Java-Gnome Bindings Library
 *
 * Copyright 1998-2005 the Java-Gnome Team, all rights reserved.
 *
 * The Java-Gnome bindings library is free software distributed under
 * the terms of the GNU Library General Public License version 2.
 */

package org.freedesktop.cairo.test;

import java.io.IOException;

import org.freedesktop.cairo.Cairo;
import org.freedesktop.cairo.PdfSurface;

/**
 */
public class CairoSnippetsPDF {

	public static int X_INCHES = 2;
	public static int Y_INCHES = 2;
	public static double X_PPI = 300.0;
	public static double Y_PPI = 300.0;

	public static double WIDTH  = X_INCHES * 96.0;
	public static double HEIGHT = Y_INCHES * 96.0;
	
	public static double M_PI = 3.14159265358979323846;
	

	public static void main(String[] args) throws Exception {
		// initialize
		Cairo cr = new Cairo();
		PdfSurface surface = new PdfSurface("./snippets.pdf", X_INCHES, Y_INCHES, X_PPI, Y_PPI);
		cr.setTargetSurface(surface);
		
		// call the snippets
		Snippets snip = new Snippets();
		for (int i = 0; i < Snippets.snippets.length; i++) {
			cr.save();
			Snippets.invokeSnippet(snip, i, cr, WIDTH, HEIGHT);
			cr.showPage();
			cr.restore();
		}
		
		// cleanup
		cr.dispose();
		surface.closeFile();
	}
}

--- NEW FILE: Snippets.java ---
/*
 * Java-Gnome Bindings Library
 *
 * Copyright 1998-2005 the Java-Gnome Team, all rights reserved.
 *
 * The Java-Gnome bindings library is free software distributed under
 * the terms of the GNU Library General Public License version 2.
 */

package org.freedesktop.cairo.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.freedesktop.cairo.Cairo;

/**
 */
public class Snippets {

	public static double M_PI = 3.14159265358979323846;
	
	public static String[] snippets = {
		"arc",
		"arc_negative",
		"clip",
		"curve_to",
		"curve_rectangle"
	};
	
	public static void invokeSnippet(Snippets snip, int num, Cairo cr, double width, double height) 
			throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		Method m = Snippets.class.getMethod(snippets[num], new Class[] {Cairo.class, double.class, double.class});
		m.invoke(snip, new Object[] {cr, new Double(width), new Double(height)});
	}

	public void normalize(Cairo cr, double width, double height) {
		cr.scale(width, height);
		cr.setLineWidth(0.04);
	}
	
	/**
	 * arc snippet
	 */
	public void arc(Cairo cr, double width, double height) {
		double xc = 0.5;
		double yc = 0.5;
		double radius = 0.4;
		double angle1 = 45.0  * (M_PI/180.0);  /* angles are specified */
		double angle2 = 180.0 * (M_PI/180.0);  /* in radians           */

		normalize(cr, width, height);

		cr.arc(xc, yc, radius, angle1, angle2);
		cr.stroke();

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.arc(xc, yc, 0.05, 0, 2*M_PI);
		cr.fill();
		cr.setLineWidth(0.03);
		cr.arc(xc, yc, radius, angle1, angle1);
		cr.lineTo(xc, yc);
		cr.arc(xc, yc, radius, angle2, angle2);
		cr.lineTo(xc, yc);
		cr.stroke();
	}
	
	/**
	 * arc_negative snippet
	 */
	public void arc_negative(Cairo cr, double width, double height) {
		double xc = 0.5;
		double yc = 0.5;
		double radius = 0.4;
		double angle1 = 45.0  * (M_PI/180.0);  /* angles are specified */
		double angle2 = 180.0 * (M_PI/180.0);  /* in radians           */

		normalize(cr, width, height);

		cr.arcNegative(xc, yc, radius, angle1, angle2);
		cr.stroke();

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.arc(xc, yc, 0.05, 0, 2*M_PI);
		cr.fill();
		cr.setLineWidth(0.03);
		cr.arc(xc, yc, radius, angle1, angle1);
		cr.lineTo(xc, yc);
		cr.arc(xc, yc, radius, angle2, angle2);
		cr.lineTo(xc, yc);
		cr.stroke();
	}
	
	/**
	 * clip snippet
	 */
	public void clip(Cairo cr, double width, double height) {
		normalize (cr, width, height);

		cr.arc(0.5, 0.5, 0.3, 0, 2 * M_PI);
		cr.clip();

		cr.newPath();  /* current path is not
		                         consumed by cairo_clip() */
		cr.rectangle(0, 0, 1, 1);
		cr.fill();
		cr.setRGBColor(0, 1, 0);
		cr.moveTo(0, 0);
		cr.lineTo(1, 1);
		cr.moveTo(1, 0);
		cr.lineTo(0, 1);
		cr.stroke();
	}

	/**
	 * curve_to snippet
	 */
	public void curve_to(Cairo cr, double width, double height) {
		double x=0.1,  y=0.5;
		double x1=0.4, y1=0.9,
		       x2=0.6, y2=0.1,
		       x3=0.9, y3=0.5;

		normalize (cr, width, height);

		cr.moveTo(x, y);
		cr.curveTo(x1, y1, x2, y2, x3, y3);

		cr.stroke();

		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.setLineWidth(0.03);
		cr.moveTo(x,y);   cr.lineTo(x1,y1);
		cr.moveTo(x2,y2); cr.lineTo(x3,y3);
		cr.stroke();
	}

	/**
	 * curve_rectangle snippet
	 */
	public void curve_rectangle(Cairo cr, double width, double height) {
		/* a custom shape, that could be wrapped in a function */
		double x0	   = 0.1,   /*< parameters like cairo_rectangle */
		       y0	   = 0.1,
		       rect_width  = 0.8,
		       rect_height = 0.8,
		       radius = 0.4;   /*< and an approximate curvature radius */

		double x1,y1;

		normalize(cr, width, height);

		x1=x0+rect_width;
		y1=y0+rect_height;

		if (rect_width/2<radius) {
		    if (rect_height/2<radius) {
		        cr.moveTo(x0, (y0 + y1)/2);
		        cr.curveTo(x0 ,y0, x0, y0, (x0 + x1)/2, y0);
		        cr.curveTo(x1, y0, x1, y0, x1, (y0 + y1)/2);
		        cr.curveTo(x1, y1, x1, y1, (x1 + x0)/2, y1);
		        cr.curveTo(x0, y1, x0, y1, x0, (y0 + y1)/2);
		    } else {
		        cr.moveTo(x0, y0 + radius);
		        cr.curveTo(x0 ,y0, x0, y0, (x0 + x1)/2, y0);
		        cr.curveTo(x1, y0, x1, y0, x1, y0 + radius);
		        cr.lineTo(x1 , y1 - radius);
		        cr.curveTo(x1, y1, x1, y1, (x1 + x0)/2, y1);
		        cr.curveTo(x0, y1, x0, y1, x0, y1- radius);
		    }
		} else {
		    if (rect_height/2<radius) {
		        cr.moveTo(x0, (y0 + y1)/2);
		        cr.curveTo(x0 , y0, x0 , y0, x0 + radius, y0);
		        cr.lineTo(x1 - radius, y0);
		        cr.curveTo(x1, y0, x1, y0, x1, (y0 + y1)/2);
		        cr.curveTo(x1, y1, x1, y1, x1 - radius, y1);
		        cr.lineTo(x0 + radius, y1);
		        cr.curveTo(x0, y1, x0, y1, x0, (y0 + y1)/2);
		    } else {
		        cr.moveTo(x0, y0 + radius);
		        cr.curveTo(x0 , y0, x0 , y0, x0 + radius, y0);
		        cr.lineTo(x1 - radius, y0);
		        cr.curveTo(x1, y0, x1, y0, x1, y0 + radius);
		        cr.lineTo(x1 , y1 - radius);
		        cr.curveTo(x1, y1, x1, y1, x1 - radius, y1);
		        cr.lineTo(x0 + radius, y1);
		        cr.curveTo(x0, y1, x0, y1, x0, y1- radius);
		    }
		}
		cr.closePath();

		/* and fill/stroke it */
		cr.save();
	    cr.setRGBColor(0.5,0.5,1);
	    cr.fill();
		cr.restore();
		cr.setAlpha(0.5);
		cr.setRGBColor(0.5, 0,0);
		cr.stroke();
	}
}




More information about the cairo-commit mailing list