[cairo-commit] cairo-demo/kapow ChangeLog,1.1,1.2 kapow.c,1.1,1.2

Kristian Hogsberg commit at pdx.freedesktop.org
Mon Jan 24 12:02:28 PST 2005


Committed by: krh

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

Modified Files:
	ChangeLog kapow.c 
Log Message:
2005-01-24  Kristian Høgsberg  <krh at redhat.com>

        * kapow.c (main): Add support for runing as a CGI script.



Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/kapow/ChangeLog,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ChangeLog	24 Jan 2005 16:16:51 -0000	1.1
+++ ChangeLog	24 Jan 2005 20:02:25 -0000	1.2
@@ -1,3 +1,7 @@
+2005-01-24  Kristian Høgsberg  <krh at redhat.com>
+
+	* kapow.c (main): Add support for runing as a CGI script.
+
 2005-01-24  Carl Worth  <cworth at cworth.org>
 
 	* kapow.c (main): Print usage message if not argument is

Index: kapow.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/kapow/kapow.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- kapow.c	24 Jan 2005 16:16:51 -0000	1.1
+++ kapow.c	24 Jan 2005 20:02:25 -0000	1.2
@@ -1,24 +1,27 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <libgen.h>
 #include <cairo.h>
 #include <math.h>
 
 const char filename[] = "kapow.png";
 const char fontname[] = "Sans";
-const char TEXT_DEFAULT[] = "kapow";
+const char default_text[] = "KAPOW";
 
 #define IMAGE_WIDTH 384
 #define IMAGE_HEIGHT 256
 
+#define SPIKES 10
+#define SHADOW_OFFSET 10
+
 #define X_FUZZ 16
 #define Y_FUZZ 16
 
-#define X_INNER_RADIUS 140.0
-#define Y_INNER_RADIUS 80.0
-
-#define X_OUTER_RADIUS 190.0
-#define Y_OUTER_RADIUS 110.0
+#define X_OUTER_RADIUS (IMAGE_WIDTH / 2 - X_FUZZ - SHADOW_OFFSET)
+#define Y_OUTER_RADIUS (IMAGE_HEIGHT / 2 - Y_FUZZ - SHADOW_OFFSET)
 
-#define SPIKES 10
+#define X_INNER_RADIUS (X_OUTER_RADIUS * 0.7)
+#define Y_INNER_RADIUS (Y_OUTER_RADIUS * 0.7)
 
 void
 make_star_path (cairo_t *cr)
@@ -62,22 +65,21 @@
 void
 bend_it (double x, double y, double *new_x, double *new_y)
 {
-    const double dist = 500;
+    const double cx = IMAGE_WIDTH / 2, cy = 500;
     double angle, radius, t;
 
     /* We're going to wrap the points around a big circle with center
-     * at (IMAGE_WIDTH / 2, dist), dist being somewhere well below the
-     * visible area.  On top of that, we'll scale up the letters going
-     * left to right.
+     * at (cx, cy), with cy being somewhere well below the visible area.
+     * On top of that, we'll scale up the letters going left to right.
      */
 
-    angle = M_PI / 2 - (double) (x - IMAGE_WIDTH / 2) / 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 = dist - (IMAGE_HEIGHT / 2 + (y - IMAGE_HEIGHT / 2) * t * 2);
+    radius = cy - (IMAGE_HEIGHT / 2 + (y - IMAGE_HEIGHT / 2) * t * 2);
 
-    *new_x = (IMAGE_WIDTH / 2) + cos (angle) * radius;
-    *new_y = dist - sin (angle) * radius;
+    *new_x = cx + cos (angle) * radius;
+    *new_y = cy - sin (angle) * radius;
 }
 
 void
@@ -113,27 +115,50 @@
     cairo_close_path (ctx->cr);
 }
 
+void
+make_text_path (cairo_t *cr, double x, double y, const char *text)
+{
+    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);
+}
+
 int
 main (int argc, char *argv[])
 {
     FILE *fp;
     double x;
     double y;
-    const char *text;
+    char text[20];
     cairo_text_extents_t extents;
     cairo_pattern_t *pattern;
     cairo_t *cr;
-    struct ctx ctx;
+    char *query_string;
 
-    if (argc > 1) {
-	text = argv[1];
-    } else {
-	text = TEXT_DEFAULT;
-	fprintf (stderr, "Usage: %s <exclamation>\n", argv[0]);
-	fprintf (stderr, "No exclamation provided, using \"%s\"\n", text);
+    query_string = getenv ("QUERY_STRING");
+    if (query_string != NULL) {
+	fp = stdout;
+	sscanf (query_string, "text=%19s", text);
+    }
+    else {
+	fp = fopen (filename, "w");
+
+	if (argc == 2) {
+	    strncpy (text, argv[1], sizeof text - 1);
+	    text [sizeof text - 1] = '\0';
+	}
+	else {
+	    fprintf (stderr, "Usage: %s <exclamation>\n", basename (argv[0]));
+	    strcpy (text, default_text);
+	    fprintf (stderr, "No exclamation provided, using \"%s\"\n", text);
+	}
     }
 
-    fp = fopen (filename, "w");
 
     cr = cairo_create ();
 
@@ -142,13 +167,13 @@
 
     cairo_set_line_width (cr, 2);
 
-    cairo_translate (cr, 10, 10);
+    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_fill (cr);
-
-    cairo_translate (cr, -10, -10);
+    cairo_restore (cr);
 
     make_star_path (cr);
     cairo_set_alpha (cr, 1);
@@ -173,31 +198,24 @@
     x = IMAGE_WIDTH / 2 - (extents.width / 2 + extents.x_bearing);
     y = IMAGE_HEIGHT / 2 - (extents.height / 2 + extents.y_bearing);
 
-    cairo_move_to (cr, x, y);
-    cairo_text_path (cr, text);
+    make_text_path (cr, x, y, text);
 
-    srand (10);
-    ctx.first = 1;
-    ctx.cr = cr;
-    cairo_current_path_flat (cr, move_to_callback, line_to_callback,
-			     close_path_callback, &ctx);
+    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_set_rgb_color (cr, 1, 0, 0);
     cairo_fill (cr);
 
-    cairo_move_to (cr, x, y);
-    cairo_text_path (cr, text);
+    make_text_path (cr, x, y, text);
     cairo_set_rgb_color (cr, 0, 0, 0);
-
-    srand (10);
-    ctx.first = 1;
-    ctx.cr = cr;
-    cairo_current_path_flat (cr, move_to_callback, line_to_callback,
-			     close_path_callback, &ctx);
-
-
     cairo_stroke (cr);
 
+    if (query_string != NULL)
+	printf ("Content-Type: image/png\n\n");
+
     cairo_show_page (cr);
 
     cairo_destroy (cr);




More information about the cairo-commit mailing list