[cairo-commit] cairo-demo/path_paint ChangeLog, 1.3, 1.4 Makefile,
1.1, 1.2 path_paint.c, 1.3, 1.4
OEyvind Kolaas
commit at pdx.freedesktop.org
Thu Nov 11 07:21:00 PST 2004
Committed by: pippin
Update of /cvs/cairo/cairo-demo/path_paint
In directory gabe:/tmp/cvs-serv12724
Modified Files:
ChangeLog Makefile path_paint.c
Log Message:
reorganization of code
Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/path_paint/ChangeLog,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ChangeLog 20 Sep 2004 16:45:11 -0000 1.3
+++ ChangeLog 11 Nov 2004 15:20:56 -0000 1.4
@@ -1,11 +1,19 @@
-2004-09-20 OEyvind Kolaas <pippin at freedesktop.org>
- *path_paint.c: signal in gtkcairo was renamed from redraw to paint
+2004-11-11 Ãyvind KolÃ¥s <pippin at freedesktop.org>
-2004-05-28 OEyvind Kolaas <pippin at freedesktop.org>
- *path_paint.c: added X keybinding for toggling between black/white
- color
+ * path_paint.c: reindentation and general code cleanup to better adhere
+ with GNU coding standards.
+ * Makefile.c: cleanup, added clean target
-2004-05-27 OEyvind Kolaas <pippin at freedesktop.org>
+2004-09-20 Ãyvind KolÃ¥s <pippin at freedesktop.org>
+
+ * path_paint.c: signal in gtkcairo was renamed from redraw to paint
+
+2004-05-28 Ãyvind KolÃ¥s <pippin at freedesktop.org>
+
+ * path_paint.c: added X keybinding for toggling between black/white
+ color
+
+2004-05-27 Ãyvind KolÃ¥s <pippin at freedesktop.org>
Initial import
Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/path_paint/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Makefile 27 May 2004 19:54:25 -0000 1.1
+++ Makefile 11 Nov 2004 15:20:56 -0000 1.2
@@ -1,11 +1,12 @@
-CFLAGS= -Wall
+APPS = path_paint
+
+CFLAGS = -Wall
LDFLAGS = -lm
-CFLAGS+= `pkg-config gtkcairo --cflags`
-LDFLAGS+= `pkg-config gtkcairo --libs`
-CC = gcc
+CFLAGS += `pkg-config gtkcairo --cflags`
+LDFLAGS += `pkg-config gtkcairo --libs`
-all: path_paint
+all: $(APPS)
-test: path_paint
- ./path_paint
+clean:
+ rm -f $(APPS)
Index: path_paint.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/path_paint/path_paint.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- path_paint.c 20 Sep 2004 16:45:11 -0000 1.3
+++ path_paint.c 11 Nov 2004 15:20:56 -0000 1.4
@@ -5,37 +5,38 @@
#define WIDTH 800
#define HEIGHT 600
+
#define STRIDE WIDTH*4
#define MAX_COORDS 1024
/* uncomment this to decrease the density of coordinate samples used for
* the current stroke, this is not an optimal solution, a better solution
- * would be to use actuall smoothing on the coordinate data
+ * would be to use actual smoothing on the coordinate data
*/
#define USE_HINT
GtkWidget *gtkcairo = NULL;
-int pen_color_is_black=1;
-int pen_radius=8;
+gboolean pen_color_is_black = TRUE;
+gint pen_radius = 8;
-double coord_x [MAX_COORDS];
-double coord_y [MAX_COORDS];
-int coord_count = 0;
+gdouble coord_x [MAX_COORDS];
+gdouble coord_y [MAX_COORDS];
+gint coord_count = 0;
-unsigned char buffer [WIDTH*HEIGHT*4];
-unsigned char temp_buffer [WIDTH*HEIGHT*4];
+guchar buffer [WIDTH*HEIGHT*4];
+guchar temp_buffer [WIDTH*HEIGHT*4];
cairo_surface_t *backbuffer = NULL;
-cairo_t *cr_save = NULL;
+cairo_t *cr_save = NULL;
-int pen_is_down=0;
+gboolean pen_is_down = FALSE;
void
destroy ()
{
- cairo_destroy (cr_save);
+ cairo_destroy (cr_save);
}
@@ -44,68 +45,73 @@
*/
void
points_to_linear_path (cairo_t *cr,
- double coord_x[],
- double coord_y[],
- int n_coords);
+ gdouble coord_x[],
+ gdouble coord_y[],
+ gint n_coords);
void
points_to_bezier_path (cairo_t *cr,
- double coord_x[],
- double coord_y[],
- int n_coords);
+ gdouble coord_x[],
+ gdouble coord_y[],
+ gint n_coords);
void
drawapp_render (cairo_t *cr,
- unsigned char *buffer)
+ guchar *buffer)
{
- cairo_save (cr);
+ cairo_save (cr);
- cairo_rectangle (cr, 0,0, WIDTH, HEIGHT);
- cairo_set_rgb_color (cr, 1,1,1);
- cairo_fill (cr);
+ cairo_rectangle (cr, 0,0, WIDTH, HEIGHT);
+ cairo_set_rgb_color (cr, 1,1,1);
+ cairo_fill (cr);
- cairo_move_to (cr, 0,0);
- cairo_show_surface (cr, backbuffer, WIDTH, HEIGHT);
+ cairo_move_to (cr, 0,0);
+ cairo_show_surface (cr, backbuffer, WIDTH, HEIGHT);
- cairo_set_line_width (cr, pen_radius*2);
- cairo_set_alpha (cr, 0.5);
+ cairo_set_line_width (cr, pen_radius*2);
+ cairo_set_alpha (cr, 0.5);
- if (pen_color_is_black) {
- cairo_set_rgb_color (cr, 0,0,0);
- } else {
- cairo_set_rgb_color (cr, 1,1,1);
+ if (pen_color_is_black)
+ {
+ cairo_set_rgb_color (cr, 0,0,0);
+ }
+ else
+ {
+ cairo_set_rgb_color (cr, 1,1,1);
}
- cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
- cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+ cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+ cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
- points_to_bezier_path (cr, coord_x, coord_y, coord_count);
- cairo_stroke (cr);
+ points_to_bezier_path (cr, coord_x, coord_y, coord_count);
+ cairo_stroke (cr);
- cairo_restore (cr);
+ cairo_restore (cr);
}
void
-apply_coords (char *buffer)
+apply_coords (guchar *buffer)
{
- drawapp_render (cr_save, buffer);
- memcpy (buffer, temp_buffer, HEIGHT*STRIDE);
+ drawapp_render (cr_save, buffer);
+ memcpy (buffer, temp_buffer, HEIGHT*STRIDE);
}
void
coords_clear (void)
{
- coord_count = 0;
+ coord_count = 0;
}
void
-coords_add (double x, double y)
+coords_add (gdouble x,
+ gdouble y)
{
- if (coord_count< MAX_COORDS-3) {
- coord_x [coord_count]=x;
- coord_y [coord_count]=y;
- coord_count++;
- }
+ if (coord_count< MAX_COORDS-3)
+ {
+ coord_x [coord_count]=x;
+ coord_y [coord_count]=y;
+ coord_count++;
+ }
}
void
@@ -113,71 +119,74 @@
cairo_t *cr,
gpointer data)
{
- drawapp_render (cr, buffer);
+ drawapp_render (cr, buffer);
}
void
-pen_motion (double x,
- double y,
- int pen_is_down)
+pen_motion (gdouble x,
+ gdouble y,
+ gboolean pen_is_down)
{
- if (pen_is_down) {
- coords_add (x,y);
- gtk_widget_queue_draw (gtkcairo);
+ if (pen_is_down)
+ {
+ coords_add (x,y);
+ gtk_widget_queue_draw (gtkcairo);
}
}
void
-pen_down (double x,
- double y)
+pen_down (gdouble x,
+ gdouble y)
{
- coords_add (x,y);
- gtk_widget_queue_draw (gtkcairo);
+ pen_is_down = TRUE;
+ coords_add (x,y);
+ gtk_widget_queue_draw (gtkcairo);
}
-void pen_up (double x,
- double y)
+void
+pen_up (gdouble x,
+ gdouble y)
{
- apply_coords (buffer);
- coords_clear ();
- gtk_widget_queue_draw (gtkcairo);
+ pen_is_down = FALSE;
+ apply_coords (buffer);
+ coords_clear ();
+ gtk_widget_queue_draw (gtkcairo);
}
-
void
-init ()
+init (void)
{
- coords_clear ();
- cr_save = cairo_create ();
- memset (buffer, sizeof(buffer), 0);
- memset (temp_buffer, sizeof(temp_buffer), 0);
+ coords_clear ();
+ cr_save = cairo_create ();
+ memset (buffer, sizeof(buffer), 0);
+ memset (temp_buffer, sizeof(temp_buffer), 0);
- cairo_set_target_image (
- cr_save, temp_buffer, CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE);
+ cairo_set_target_image (cr_save, temp_buffer,
+ CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE);
- backbuffer = cairo_surface_create_for_image (
- buffer, CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE);
+ backbuffer = cairo_surface_create_for_image (buffer,
+ CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE);
}
+/* just wrapping the gtk events */
+
static gboolean
event_press (GtkWidget *widget,
GdkEventButton *bev,
gpointer user_data)
{
- pen_down (bev->x, bev->y);
- pen_is_down = 1;
- return TRUE;
+ pen_down (bev->x, bev->y);
+ return TRUE;
}
static gboolean
event_release (GtkWidget *widget,
GdkEventButton *bev,
- gpointer user_data)
+ gpointer user_data)
{
- pen_up (bev->x, bev->y);
- pen_is_down = 0;
- return TRUE;
+ pen_up (bev->x, bev->y);
+ return TRUE;
}
static gboolean
@@ -185,9 +194,10 @@
GdkEventMotion *mev,
gpointer user_data)
{
- pen_motion (mev->x, mev->y, pen_is_down);
- gdk_window_get_pointer (widget->window, NULL, NULL, NULL);
- return TRUE;
+ pen_motion (mev->x, mev->y, pen_is_down);
+ gdk_window_get_pointer (widget->window, NULL, NULL, NULL);
+
+ return TRUE;
}
static gboolean
@@ -195,60 +205,64 @@
GdkEventKey *kev,
gpointer user_data)
{
- switch (kev->keyval) {
- case GDK_x:
- case GDK_X:
- if (pen_color_is_black)
- pen_color_is_black=0;
- else
- pen_color_is_black=1;
- default:
- break;
- }
- return TRUE;
+ switch (kev->keyval)
+ {
+ case GDK_x:
+ case GDK_X:
+ if (pen_color_is_black)
+ pen_color_is_black = FALSE;
+ else
+ pen_color_is_black = TRUE;
+ default:
+ break;
+ }
+ return TRUE;
}
-int main (int argc,
- char **argv)
+gint
+main (gint argc,
+ gchar **argv)
{
- GtkWidget *mainwin;
+ GtkWidget *mainwin;
- gtk_init (&argc, &argv);
+ gtk_init (&argc, &argv);
- mainwin= gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtkcairo = gtk_cairo_new ();
+ gtkcairo = gtk_cairo_new ();
- gtk_widget_set_events (gtkcairo,
- GDK_EXPOSURE_MASK |
+ gtk_widget_set_events (gtkcairo,
+ GDK_EXPOSURE_MASK |
#ifdef USE_HINT
- GDK_POINTER_MOTION_HINT_MASK |
+ GDK_POINTER_MOTION_HINT_MASK |
#endif
- GDK_BUTTON1_MOTION_MASK |
- GDK_BUTTON_PRESS_MASK |
- GDK_KEY_PRESS_MASK |
- GDK_BUTTON_RELEASE_MASK);
- gtk_widget_set_size_request (gtkcairo, WIDTH, HEIGHT);
+ GDK_BUTTON1_MOTION_MASK |
+ GDK_BUTTON_PRESS_MASK |
+ GDK_KEY_PRESS_MASK |
+ GDK_BUTTON_RELEASE_MASK);
+ gtk_widget_set_size_request (gtkcairo, WIDTH, HEIGHT);
- g_signal_connect (G_OBJECT (gtkcairo), "paint",
- G_CALLBACK (paint), NULL);
- g_signal_connect (G_OBJECT (gtkcairo), "motion_notify_event",
- G_CALLBACK (event_motion), NULL);
- g_signal_connect (G_OBJECT (gtkcairo), "button_press_event",
- G_CALLBACK (event_press), NULL);
- g_signal_connect (G_OBJECT (gtkcairo), "button_release_event",
- G_CALLBACK (event_release), NULL);
- g_signal_connect (G_OBJECT (gtkcairo), "key_press_event",
- G_CALLBACK (event_keypress), NULL);
- g_object_set (G_OBJECT (gtkcairo), "can_focus", 1, NULL);
+ g_signal_connect (G_OBJECT (gtkcairo), "paint",
+ G_CALLBACK (paint), NULL);
+ g_signal_connect (G_OBJECT (gtkcairo), "motion_notify_event",
+ G_CALLBACK (event_motion), NULL);
+ g_signal_connect (G_OBJECT (gtkcairo), "button_press_event",
+ G_CALLBACK (event_press), NULL);
+ g_signal_connect (G_OBJECT (gtkcairo), "button_release_event",
+ G_CALLBACK (event_release), NULL);
+ g_signal_connect (G_OBJECT (gtkcairo), "key_press_event",
+ G_CALLBACK (event_keypress), NULL);
+ g_object_set (G_OBJECT (gtkcairo), "can_focus", 1, NULL);
- gtk_container_add (GTK_CONTAINER (mainwin), gtkcairo);
+ gtk_container_add (GTK_CONTAINER (mainwin), gtkcairo);
- init ();
- gtk_widget_show_all (mainwin);
+ init ();
+ gtk_widget_show_all (mainwin);
- gtk_main ();
- return 0;
+ g_print ("press X to change between black and white ink\n");
+
+ gtk_main ();
+ return 0;
}
@@ -257,79 +271,85 @@
*/
void
points_to_bezier_path (cairo_t *cr,
- double coord_x[],
- double coord_y[],
- int n_coords)
+ gdouble coord_x[],
+ gdouble coord_y[],
+ gint n_coords)
{
- int i;
- double smooth_value = 1;
+ gint i;
+ gdouble smooth_value;
+
+ smooth_value = 1;
- cairo_new_path (cr);
- if (!n_coords)
- return;
+ cairo_new_path (cr);
- cairo_move_to (cr, coord_x[0], coord_y[0]);
+ if (!n_coords)
+ return;
- for (i=1;i<n_coords;i++){
- double x2 = coord_x[i];
- double y2 = coord_y[i];
+ cairo_move_to (cr, coord_x[0], coord_y[0]);
- double x0,y0,x1,y1,x3,y3;
+ for (i=1;i<n_coords;i++)
+ {
+ gdouble x2 = coord_x[i];
+ gdouble y2 = coord_y[i];
+ gdouble x0,y0,x1,y1,x3,y3;
- switch (i){
- case 1:
- x0=coord_x[i-1];
- y0=coord_y[i-1];
- x1 = coord_x[i-1];
- y1 = coord_y[i-1];
- break;
- case 2:
- default:
- x0=coord_x[i-2];
- y0=coord_y[i-2];
- x1 = coord_x[i-1];
- y1 = coord_y[i-1];
+ if (i==1)
+ {
+ x0=coord_x[i-1];
+ y0=coord_y[i-1];
+ x1 = coord_x[i-1];
+ y1 = coord_y[i-1];
+ }
+ else
+ {
+ x0=coord_x[i-2];
+ y0=coord_y[i-2];
+ x1 = coord_x[i-1];
+ y1 = coord_y[i-1];
}
- if (i<n_coords+1) {
- x3 = coord_x[i+1];
- y3 = coord_y[i+1];
- } else {
- x3 = coord_x[i];
- y3 = coord_y[i];
+ if (i<n_coords+1)
+ {
+ x3 = coord_x[i+1];
+ y3 = coord_y[i+1];
}
+ else
{
+ x3 = coord_x[i];
+ y3 = coord_y[i];
+ }
- double xc1 = (x0 + x1) / 2.0;
- double yc1 = (y0 + y1) / 2.0;
- double xc2 = (x1 + x2) / 2.0;
- double yc2 = (y1 + y2) / 2.0;
- double xc3 = (x2 + x3) / 2.0;
- double yc3 = (y2 + y3) / 2.0;
+ {
+ gdouble xc1 = (x0 + x1) / 2.0;
+ gdouble yc1 = (y0 + y1) / 2.0;
+ gdouble xc2 = (x1 + x2) / 2.0;
+ gdouble yc2 = (y1 + y2) / 2.0;
+ gdouble xc3 = (x2 + x3) / 2.0;
+ gdouble yc3 = (y2 + y3) / 2.0;
- double len1 = sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
- double len2 = sqrt( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) );
- double len3 = sqrt( (x3-x2) * (x3-x2) + (y3-y2) * (y3-y2) );
+ gdouble len1 = sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
+ gdouble len2 = sqrt( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) );
+ gdouble len3 = sqrt( (x3-x2) * (x3-x2) + (y3-y2) * (y3-y2) );
- double k1 = len1 / (len1 + len2);
- double k2 = len2 / (len2 + len3);
+ gdouble k1 = len1 / (len1 + len2);
+ gdouble k2 = len2 / (len2 + len3);
- double xm1 = xc1 + (xc2 - xc1) * k1;
- double ym1 = yc1 + (yc2 - yc1) * k1;
+ gdouble xm1 = xc1 + (xc2 - xc1) * k1;
+ gdouble ym1 = yc1 + (yc2 - yc1) * k1;
- double xm2 = xc2 + (xc3 - xc2) * k2;
- double ym2 = yc2 + (yc3 - yc2) * k2;
+ gdouble xm2 = xc2 + (xc3 - xc2) * k2;
+ gdouble ym2 = yc2 + (yc3 - yc2) * k2;
- double ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
- double ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
+ gdouble ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
+ gdouble ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
- double ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
- double ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
+ gdouble ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
+ gdouble ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
- cairo_curve_to (cr, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, x2,y2);
- }
- }
+ cairo_curve_to (cr, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, x2,y2);
+ }
+ }
}
@@ -338,16 +358,17 @@
*/
void
points_to_linear_path (cairo_t *cr,
- double coord_x[],
- double coord_y[],
- int n_coords)
+ gdouble coord_x[],
+ gdouble coord_y[],
+ gint n_coords)
{
- int i;
- if (!n_coords)
- return;
- cairo_move_to (cr, coord_x[0], coord_y[0]);
- for (i=1;i<n_coords;i++)
- cairo_line_to (cr, coord_x[i], coord_y[i]);
-}
+ gint i;
+
+ if (!n_coords)
+ return;
+ cairo_move_to (cr, coord_x[0], coord_y[0]);
+ for (i = 1; i < n_coords; i++)
+ cairo_line_to (cr, coord_x[i], coord_y[i]);
+}
More information about the cairo-commit
mailing list