[cairo-commit] cairo-demo/kapow ChangeLog, 1.3, 1.4 Makefile, 1.1, 1.2 kapow.c, 1.4, 1.5

Carl Worth commit at pdx.freedesktop.org
Tue May 17 08:19:41 PDT 2005


Committed by: cworth

Update of /cvs/cairo/cairo-demo/kapow
In directory gabe:/tmp/cvs-serv7984

Modified Files:
	ChangeLog Makefile kapow.c 
Log Message:

        * Makefile:
        * kapow.c: (bend_it), (make_text_path), (stdio_write), (main):
        Update to all the latest cairo API changes.


Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/kapow/ChangeLog,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ChangeLog	2 Apr 2005 02:06:37 -0000	1.3
+++ ChangeLog	17 May 2005 15:19:38 -0000	1.4
@@ -1,3 +1,9 @@
+2005-05-17  Carl Worth  <cworth at cworth.org>
+
+	* Makefile:
+	* kapow.c: (bend_it), (make_text_path), (stdio_write), (main):
+	Update to all the latest cairo API changes.
+
 2005-04-01  Carl Worth  <cworth at cworth.org>
 
 	* kapow.c: (main): Track removal of cairo_set_target_png.

Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/kapow/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Makefile	24 Jan 2005 16:16:51 -0000	1.1
+++ Makefile	17 May 2005 15:19:39 -0000	1.2
@@ -1,6 +1,11 @@
-CFLAGS	= $(shell pkg-config --cflags cairo libpng) -Wall -g
+MYCFLAGS= $(shell pkg-config --cflags cairo libpng) -Wall -g -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -fno-strict-aliasing
 LDLIBS	= $(shell pkg-config --libs cairo libpng) -g
 
+all: kapow
+
+%.o:%.c
+	$(CC) $(CFLAGS) $(MYCFLAGS) -c $^
+
 kapow : kapow.o
 
 clean :

Index: kapow.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/kapow/kapow.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- kapow.c	2 Apr 2005 02:06:37 -0000	1.4
+++ kapow.c	17 May 2005 15:19:39 -0000	1.5
@@ -1,11 +1,10 @@
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <libgen.h>
 #include <cairo.h>
-#ifdef CAIRO_HAS_PNG_FUNCTIONS
-#include <cairo-png.h>
-#else
+#ifndef CAIRO_HAS_PNG_FUNCTIONS
 #error This program requires cairo with PNG support
 #endif
 #include <math.h>
@@ -29,7 +28,7 @@
 #define X_INNER_RADIUS (X_OUTER_RADIUS * 0.7)
 #define Y_INNER_RADIUS (Y_OUTER_RADIUS * 0.7)
 
-void
+static void
 make_star_path (cairo_t *cr)
 {
     double x;
@@ -68,8 +67,8 @@
     cairo_t *cr;
 };
 
-void
-bend_it (double x, double y, double *new_x, double *new_y)
+static void
+bend_it (double *x, double *y)
 {
     const double cx = IMAGE_WIDTH / 2, cy = 500;
     double angle, radius, t;
@@ -79,59 +78,63 @@
      * On top of that, we'll scale up the letters going left to right.
      */
 
-    angle = M_PI / 2 - (double) (x - cx) / IMAGE_WIDTH;
+    angle = M_PI / 2 - (double) (*x - cx) / IMAGE_WIDTH;
     t = 3 * M_PI / 4 - angle + 0.05;
     angle = 3 * M_PI / 4 - pow (t, 1.8);
-    radius = cy - (IMAGE_HEIGHT / 2 + (y - IMAGE_HEIGHT / 2) * t * 2);
+    radius = cy - (IMAGE_HEIGHT / 2 + (*y - IMAGE_HEIGHT / 2) * t * 2);
 
-    *new_x = cx + cos (angle) * radius;
-    *new_y = cy - sin (angle) * radius;
+    *x = cx + cos (angle) * radius;
+    *y = cy - sin (angle) * radius;
 }
 
-void
-move_to_callback(void *data, double x, double y)
+static void
+make_text_path (cairo_t *cr, double x, double y, const char *text)
 {
-    struct ctx *ctx = data;
-    double new_x, new_y;
-
-    if (ctx->first) {
-	cairo_new_path (ctx->cr);
-	ctx->first = 0;
-    }
+    cairo_path_t *path;
+    cairo_path_data_t *data;
+    int i;
 
-    bend_it (x, y, &new_x, &new_y);
-    cairo_move_to (ctx->cr, new_x, new_y);
-}
+    cairo_move_to (cr, x, y);
+    cairo_text_path (cr, text);
 
-void
-line_to_callback(void *data, double x, double y)
-{
-    struct ctx *ctx = data;
-    double new_x, new_y;
+    path = cairo_copy_path_flat (cr);
 
-    bend_it (x, y, &new_x, &new_y);
-    cairo_line_to (ctx->cr, new_x, new_y);
-}
+    cairo_new_path (cr);
 
-void
-close_path_callback(void *data)
-{
-    struct ctx *ctx = data;
+    for (i=0; i < path->num_data; i += path->data[i].header.length) {
+	data = &path->data[i];
+	switch (data->header.type) {
+	case CAIRO_PATH_MOVE_TO:
+	    x = data[1].point.x;
+	    y = data[1].point.y;
+	    bend_it (&x, &y);
+	    cairo_move_to (cr, x, y);
+	    break;
+	case CAIRO_PATH_LINE_TO:
+	    x = data[1].point.x;
+	    y = data[1].point.y;
+	    bend_it (&x, &y);
+	    cairo_line_to (cr, x, y);
+	    break;
+	case CAIRO_PATH_CLOSE_PATH:
+	    cairo_close_path (cr);
+	    break;
+	default:
+	    assert(0);
+	}
+    }
 
-    cairo_close_path (ctx->cr);
+    free (path);
 }
 
-void
-make_text_path (cairo_t *cr, double x, double y, const char *text)
+static cairo_status_t
+stdio_write (void *closure, const unsigned char *data, unsigned int length)
 {
-    struct ctx ctx;
-
-    cairo_move_to (cr, x, y);
-    cairo_text_path (cr, text);
-    ctx.first = 1;
-    ctx.cr = cr;
-    cairo_current_path_flat (cr, move_to_callback, line_to_callback,
-			     close_path_callback, &ctx);
+    FILE *file = closure;
+    if (fwrite (data, 1, length, file) == length)
+	return CAIRO_STATUS_SUCCESS;
+    else
+	return CAIRO_STATUS_WRITE_ERROR;
 }
 
 int
@@ -143,6 +146,7 @@
     char text[20];
     cairo_text_extents_t extents;
     cairo_pattern_t *pattern;
+    cairo_surface_t *surface;
     cairo_t *cr;
     char *query_string;
 
@@ -166,40 +170,38 @@
     }
 
 
-    cr = cairo_create ();
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+					  IMAGE_WIDTH, IMAGE_HEIGHT);
 
-    cairo_set_target_image_no_data (cr, CAIRO_FORMAT_ARGB32,
-				    IMAGE_WIDTH, IMAGE_HEIGHT);
+    cr = cairo_create (surface);
 
     cairo_set_line_width (cr, 2);
 
     cairo_save (cr);
     cairo_translate (cr, SHADOW_OFFSET, SHADOW_OFFSET);
     make_star_path (cr);
-    cairo_set_alpha (cr, 0.5);
-    cairo_set_rgb_color (cr, 0, 0, 0);
+    cairo_set_source_rgba (cr, 0., 0., 0., 0.5);
     cairo_fill (cr);
     cairo_restore (cr);
 
     make_star_path (cr);
-    cairo_set_alpha (cr, 1);
     pattern =
 	cairo_pattern_create_radial (IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 10,
 				     IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 230);
-    cairo_pattern_add_color_stop (pattern, 0, 1, 1, 0.2, 1);
-    cairo_pattern_add_color_stop (pattern, 1, 1, 0, 0, 1);
-    cairo_set_pattern (cr, pattern);
+    cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 0.2, 1);
+    cairo_pattern_add_color_stop_rgba (pattern, 1, 1, 0, 0, 1);
+    cairo_set_source (cr, pattern);
     cairo_fill (cr);
 
     make_star_path (cr);
-    cairo_set_rgb_color (cr, 0, 0, 0);
+    cairo_set_source_rgb (cr, 0, 0, 0);
     cairo_stroke (cr);
 
-    cairo_select_font (cr, fontname,
+    cairo_select_font_face (cr, fontname,
 		       CAIRO_FONT_SLANT_NORMAL,
 		       CAIRO_FONT_WEIGHT_BOLD);
 
-    cairo_scale_font (cr, 50);
+    cairo_set_font_size (cr, 50);
     cairo_text_extents (cr, text, &extents);
     x = IMAGE_WIDTH / 2 - (extents.width / 2 + extents.x_bearing);
     y = IMAGE_HEIGHT / 2 - (extents.height / 2 + extents.y_bearing);
@@ -209,22 +211,23 @@
     pattern =
 	cairo_pattern_create_linear (IMAGE_WIDTH / 2 - 10, IMAGE_HEIGHT / 4,
 				     IMAGE_WIDTH / 2 + 10, 3 * IMAGE_HEIGHT / 4);
-    cairo_pattern_add_color_stop (pattern, 0, 1, 1, 1, 1);
-    cairo_pattern_add_color_stop (pattern, 1, 0, 0, 0.4, 1);
-    cairo_set_pattern (cr, pattern);
+    cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 1, 1);
+    cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0.4, 1);
+    cairo_set_source (cr, pattern);
 
     cairo_fill (cr);
 
     make_text_path (cr, x, y, text);
-    cairo_set_rgb_color (cr, 0, 0, 0);
+    cairo_set_source_rgb (cr, 0, 0, 0);
     cairo_stroke (cr);
 
     if (query_string != NULL)
 	printf ("Content-Type: image/png\n\n");
 
-    cairo_surface_write_png (cairo_get_target_surface (cr), fp);
+    cairo_surface_write_to_png_stream (surface, stdio_write, fp);
 
     cairo_destroy (cr);
+    cairo_surface_destroy (surface);
     fclose (fp);
 
     return 0;




More information about the cairo-commit mailing list