[cairo-commit] cairo-java/test/snippets Snippets.java, NONE, 1.1 CairoSnippetsPDF.java, NONE, 1.1 CairoSnippetsPNG.java, NONE, 1.1

Jeffrey Morgan commit at pdx.freedesktop.org
Tue Mar 8 03:58:35 PST 2005


Committed by: kuzman

Update of /cvs/cairo/cairo-java/test/snippets
In directory gabe:/tmp/cvs-serv4217/test/snippets

Added Files:
	Snippets.java CairoSnippetsPDF.java CairoSnippetsPNG.java 
Log Message:
Renamed package and started new example

--- 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 snippets;

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

import org.freedesktop.cairo.Cairo;
import org.freedesktop.cairo.FontSlant;
import org.freedesktop.cairo.FontWeight;
import org.freedesktop.cairo.LineCap;
import org.freedesktop.cairo.LineJoin;
import org.freedesktop.cairo.Pattern;
import org.freedesktop.cairo.RGBColor;
import org.freedesktop.cairo.TextExtents;

/**
 */
public class Snippets {

	public static double M_PI = 3.14159265358979323846;
	
	public static String[] snippets = {
		"arc",
		"arc_negative",
		"clip",
		"curve_to",
		"curve_rectangle",
		"fill_and_stroke",
		"fill_and_stroke2",
		"gradient",
		"path",
		"set_line_cap",
		"set_line_join",
		"text",
		"text_align_center",
		"text_extents",
		"xxx_clip_rectangle",
		"xxx_dash",
		"xxx_long_lines",
		"xxx_multi_segment_caps",
		"xxx_self_intersect"
	};
	
	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();
	}

	/**
	 * fill_and_stroke snippet
	 */
	public void fill_and_stroke(Cairo cr, double width, double height) {
		normalize(cr, width, height);

		cr.moveTo(0.5, 0.1);
		cr.lineTo(0.9, 0.9);
		cr.relLineTo(-0.4, 0.0);
		cr.curveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);

		cr.save();
		cr.setRGBColor(0, 0, 1);
		cr.fill();
		cr.restore();

		cr.closePath();
		cr.stroke();
	}

	/**
	 * fill_and_stroke2 snippet
	 */
	public void fill_and_stroke2(Cairo cr, double width, double height) {
		normalize (cr, width, height);

		cr.moveTo(0.5, 0.1);
		cr.lineTo(0.9, 0.9);
		cr.relLineTo(-0.4, 0.0);
		cr.curveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
		cr.closePath();

		cr.moveTo(0.25, 0.1);
		cr.relLineTo(0.2, 0.2);
		cr.relLineTo(-0.2, 0.2);
		cr.relLineTo(-0.2, -0.2);
		cr.closePath();


		cr.save();
	    cr.setRGBColor(0, 0, 1);
	    cr.fill();
		cr.restore();

		cr.stroke();
	}

	/**
	 * gradient snippet
	 */
	public void gradient(Cairo cr, double width, double height) {
		
		normalize (cr, width, height);

		Pattern pat = new Pattern(0.0, 0.0, 0.0, 1.0);
		pat.addColorStop(1, new RGBColor(0, 0, 0), 1);
		pat.addColorStop(0, new RGBColor(1, 1, 1), 1);
		cr.rectangle(0,0,1,1);
		cr.setPattern(pat);
		cr.fill();

		pat.dispose();
		pat = new Pattern(0.45, 0.4, 0.1, 0.4, 0.4, 0.5);
		pat.addColorStop(0, new RGBColor(1, 1, 1), 1);
		pat.addColorStop(1, new RGBColor(0, 0, 0), 1);
		cr.setPattern(pat);
		cr.arc(0.5, 0.5, 0.3, 0, 2 * M_PI);
		cr.fill();
		pat.dispose();
	}

	/**
	 * path snippet
	 */
	public void path(Cairo cr, double width, double height) {
		normalize(cr, width, height);
		cr.moveTo(0.5, 0.1);
		cr.lineTo(0.9, 0.9);
		cr.relLineTo(-0.4, 0.0);
		cr.curveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);

		cr.stroke();
	}

	/**
	 * set_line_cap snippet
	 */
	public void set_line_cap(Cairo cr, double width, double height) {
		normalize(cr, width, height);
		cr.setLineWidth(0.12);
		cr.setLineCap(LineCap.BUTT); /* default */
		cr.moveTo(0.25, 0.2); 
		cr.lineTo(0.25, 0.8);
		cr.stroke();
		cr.setLineCap(LineCap.ROUND);
		cr.moveTo(0.5, 0.2); 
		cr.lineTo(0.5, 0.8);
		cr.stroke();
		cr.setLineCap(LineCap.SQUARE);
		cr.moveTo(0.75, 0.2); 
		cr.lineTo(0.75, 0.8);
		cr.stroke();

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setLineWidth(0.01);
		cr.moveTo(0.25, 0.2); 
		cr.lineTo(0.25, 0.8);
		cr.moveTo(0.5, 0.2);  
		cr.lineTo(0.5, 0.8);
		cr.moveTo(0.75, 0.2); 
		cr.lineTo(0.75, 0.8);
		cr.stroke();
	}

	/**
	 * set_line_join snippet
	 */
	public void set_line_join(Cairo cr, double width, double height) {
		normalize(cr, width, height);
		cr.setLineWidth(0.16);
		cr.moveTo(0.3, 0.33);
		cr.relLineTo(0.2, -0.2);
		cr.relLineTo(0.2, 0.2);
		cr.setLineJoin(LineJoin.MITER); /* default */
		cr.stroke();

		cr.moveTo(0.3, 0.63);
		cr.relLineTo(0.2, -0.2);
		cr.relLineTo(0.2, 0.2);
		cr.setLineJoin(LineJoin.BEVEL);
		cr.stroke();

		cr.moveTo(0.3, 0.93);
		cr.relLineTo(0.2, -0.2);
		cr.relLineTo(0.2, 0.2);
		cr.setLineJoin(LineJoin.ROUND);
		cr.stroke();


	}

	/**
	 * text snippet
	 */
	public void text(Cairo cr, double width, double height) {
		normalize (cr, width, height);
		cr.selectFont("Sans", FontSlant.NORMAL, FontWeight.BOLD);
		cr.scaleFont(0.35);

		cr.moveTo(0.04, 0.53);
		cr.showText("Hello");

		cr.moveTo(0.27, 0.65);
		cr.textPath("void");
		cr.save();
		cr.setRGBColor(0.5,0.5,1);
		cr.fill();
		cr.restore();
		cr.setLineWidth(0.01);
		cr.stroke();

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.arc(0.04, 0.53, 0.02, 0, 2*M_PI);
		cr.arc(0.27, 0.65, 0.02, 0, 2*M_PI);
		cr.fill();
	}

	/**
	 * text_align_center snippet
	 */
	public void text_align_center(Cairo cr, double width, double height) {

		normalize (cr, width, height);

		cr.selectFont("Sans", FontSlant.NORMAL, FontWeight.NORMAL);
		cr.scaleFont(0.2);
		TextExtents extents = cr.getTextExtents("cairo");
		double x = 0.5 -((extents.getWidth()/2.0) + extents.getXBearing());
		double y = 0.5 -((extents.getHeight()/2.0) + extents.getYBearing());

		cr.moveTo(x, y);
		cr.showText("cairo");

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.arc(x, y, 0.05, 0, 2*M_PI);
		cr.fill();
		cr.moveTo(0.5, 0);
		cr.relLineTo(0, 1);
		cr.moveTo(0, 0.5);
		cr.relLineTo(1, 0);
		cr.stroke();
	}
	
	/**
	 * text_extents snippet
	 */
	public void text_extents(Cairo cr, double width, double height) {
		double x=0.1;
		double y=0.6;
		String utf8 = "cairo";
		normalize (cr, width, height);

		cr.selectFont("Sans", FontSlant.NORMAL, FontWeight.NORMAL);

		cr.scaleFont(0.4);
		TextExtents extents = cr.getTextExtents(utf8);

		cr.moveTo(x,y);
		cr.showText(utf8);

		/* draw helping lines */
		cr.setRGBColor(1,0.2,0.2);
		cr.setAlpha(0.6);
		cr.arc(x, y, 0.05, 0, 2*M_PI);
		cr.fill();
		cr.moveTo(x,y);
		cr.relLineTo(0, -extents.getHeight());
		cr.relLineTo(extents.getWidth(), 0);
		cr.relLineTo(extents.getXBearing(), -extents.getYBearing());
		cr.stroke();
	}

	/**
	 * xxx_clip_rectangle snippet
	 */
	public void xxx_clip_rectangle(Cairo cr, double width, double height) {
		normalize (cr, width, height);

		cr.newPath();
		cr.moveTo(.25, .25);
		cr.lineTo(.25, .75);
		cr.lineTo(.75, .75);
		cr.lineTo(.75, .25);
		cr.lineTo(.25, .25);
		cr.closePath();

		cr.clip();

		cr.moveTo(0, 0);
		cr.lineTo(1, 1);
		cr.stroke();
	}

	/**
	 * xxx_dash snippet
	 */
	public void xxx_dash(Cairo cr, double width, double height) {
		double dashes[] = {0.20,  /* ink */
		                   0.05,  /* skip */
		                   0.05,  /* ink */
		                   0.05   /* skip*/ 
		                  };
		double offset = -0.2; 

		normalize(cr, width, height);

		cr.setDash(dashes, offset);

		cr.moveTo(0.5, 0.1);
		cr.lineTo(0.9, 0.9);
		cr.relLineTo(-0.4, 0.0);
		cr.curveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
		cr.stroke();
	}

	/**
	 * xxx_long_lines snippet
	 */
	public void xxx_long_lines(Cairo cr, double width, double height) {
		normalize(cr, width, height);

		cr.moveTo(0.1, -50);
		cr.lineTo(0.1,  50);
		cr.setRGBColor(1, 0 ,0);
		cr.stroke();

		cr.moveTo(0.2, -60);
		cr.lineTo(0.2,  60);
		cr.setRGBColor(1, 1 ,0);
		cr.stroke();

		cr.moveTo(0.3, -70);
		cr.lineTo(0.3,  70);
		cr.setRGBColor(0, 1 ,0);
		cr.stroke();

		cr.moveTo(0.4, -80);
		cr.lineTo(0.4,  80);
		cr.setRGBColor(0, 0 ,1);
		cr.stroke();
	}

	/**
	 * xxx_multi_segment_caps snippet
	 */
	public void xxx_multi_segment_caps(Cairo cr, double width, double height) {
		normalize(cr, width, height);

		cr.moveTo(0.2, 0.3);
		cr.lineTo(0.8, 0.3);

		cr.moveTo(0.2, 0.5);
		cr.lineTo(0.8, 0.5);

		cr.moveTo(0.2, 0.7);
		cr.lineTo(0.8, 0.7);

		cr.setLineWidth(0.12);
		cr.setLineCap (LineCap.ROUND);
		cr.stroke();
	}

	/**
	 * xxx_self_intersect snippet
	 */
	public void xxx_self_intersect(Cairo cr, double width, double height) {
		normalize(cr, width, height);

		cr.moveTo(0.3, 0.3);
		cr.lineTo(0.7, 0.3);

		cr.lineTo(0.5, 0.3);
		cr.lineTo(0.5, 0.7);

		cr.setLineWidth(0.22);
		cr.setLineCap(LineCap.ROUND);
		cr.setLineJoin(LineJoin.ROUND);
		cr.stroke();
	}
}

--- 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 snippets;

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.close();
	}
}

--- NEW FILE: CairoSnippetsPNG.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 snippets;

import org.freedesktop.cairo.Cairo;
import org.freedesktop.cairo.Format;
import org.freedesktop.cairo.PngSurface;

/**
 */
public class CairoSnippetsPNG {

	public static int IMAGE_WIDTH = 256;
	public static int IMAGE_HEIGHT = 256;

	public static double LINE_WIDTH = 0.04;

	public static void main(String[] args) throws Exception {
		// call the snippets
		Snippets snip = new Snippets();
		for (int i = 0; i < Snippets.snippets.length; i++) {
			String filename = "./" + Snippets.snippets[i] + ".png";
			Cairo cr = new Cairo();
			PngSurface surface = new PngSurface(filename, Format.ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT);
			cr.setTargetSurface(surface);
			
			cr.save();
			Snippets.invokeSnippet(snip, i, cr, IMAGE_WIDTH, IMAGE_HEIGHT);
			cr.restore();
			cr.showPage();
			cr.dispose();
			surface.close();
		}
	}
}




More information about the cairo-commit mailing list