[cairo-commit] cairo-demo/X11 ChangeLog,NONE,1.1 Makefile,1.1,1.2 cairo-spline.c,1.5,1.6 cairo-freq.c,1.3,NONE
Carl Worth
commit at pdx.freedesktop.org
Fri Nov 7 12:09:16 PST 2003
Committed by: cworth
Update of /cvs/cairo/cairo-demo/X11
In directory pdx:/tmp/cvs-serv5270
Modified Files:
Makefile cairo-spline.c
Added Files:
ChangeLog
Removed Files:
cairo-freq.c
Log Message:
* cairo-spline.c: Added code to demonstrate use of cairo_in_stroke
and cairo_in_fill. Various code cleanups.
* Makefile (PROGS): Remove cairo-freq which wasn't adding anything.
--- NEW FILE: ChangeLog ---
2003-11-07 Carl Worth <cworth at isi.edu>
* cairo-spline.c: Added code to demonstrate use of cairo_in_stroke
and cairo_in_fill. Various code cleanups.
* Makefile (PROGS): Remove cairo-freq which wasn't adding anything.
Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/X11/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Makefile 18 Aug 2003 18:11:37 -0000 1.1
--- Makefile 7 Nov 2003 20:09:14 -0000 1.2
***************
*** 2,6 ****
LDFLAGS=`pkg-config --libs cairo`
! PROGS=cairo-demo cairo-spline cairo-knockout cairo-freq
all: $(PROGS)
--- 2,6 ----
LDFLAGS=`pkg-config --libs cairo`
! PROGS=cairo-demo cairo-spline cairo-knockout
all: $(PROGS)
Index: cairo-spline.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/X11/cairo-spline.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** cairo-spline.c 24 Oct 2003 16:32:31 -0000 1.5
--- cairo-spline.c 7 Nov 2003 20:09:14 -0000 1.6
***************
*** 47,55 ****
double xtrans;
double ytrans;
- int show_path;
! int drag;
pt_t drag_pt;
- pt_t *active_pt;
int active;
} win_t;
--- 47,53 ----
double xtrans;
double ytrans;
! int click;
pt_t drag_pt;
int active;
} win_t;
***************
*** 70,81 ****
} key_binding_t;
- typedef int (*button_callback_t)(win_t *win, int x, int y);
-
- typedef struct button_binding
- {
- int button;
- button_callback_t callback;
- } button_binding_t;
-
static void win_init(win_t *win, Display *dpy);
static void win_deinit(win_t *win);
--- 68,71 ----
***************
*** 85,97 ****
static void win_print_help(win_t *win);
- static int select_point_cb(win_t *win, int x, int y);
- static int snap_point_cb(win_t *win, int x, int y);
-
static int quit_cb(win_t *win);
- static int select_next_cb(win_t *win);
- static int left_epsilon_cb(win_t *win);
- static int right_epsilon_cb(win_t *win);
- static int up_epsilon_cb(win_t *win);
- static int down_epsilon_cb(win_t *win);
static int print_spline_cb(win_t *win);
static int zoom_in_cb(win_t *win);
--- 75,79 ----
***************
*** 103,107 ****
static int flatten_cb(win_t *win);
static int smooth_cb(win_t *win);
- static int toggle_path(win_t *win);
static int widen_line(win_t *win);
static int narrow_line(win_t *win);
--- 85,88 ----
***************
*** 111,276 ****
static const double DEFAULT_TOLERANCE = .1;
static const double DEFAULT_LINE_WIDTH = 10;
static const cairo_line_cap_t DEFAULT_LINE_CAP = CAIRO_LINE_CAP_ROUND;
! /* This was breaking the polygon tessellation code. All fixed now. */
! static const spline_t polygon_killer = {
! { { 613.125, 4643.06 }, { 21957.1, 3763.06 }, { 12906, 256 }, { 524, 8788 } }
! };
!
! static const spline_t intersection_killer = {
! { { 244.56999999999999318, 268.56999999999993634 }, { 364.56000000000000227, 747.55999999999994543 }, { 34.380000000000002558, 412.56000000000000227 }, { 576.37000000000000455, 203.56999999999999318 } }
! };
!
! /*
! #define DEFAULT_SPLINE intersection_killer
! #define DEFAULT_ZOOM 1.0
! */
!
! /* Showing off the problems with wide butt-capped splines that turn
! sharply at the end. */
! static const spline_t funky_fangs = {
! { { 69.25, 48.185 }, { 40.225, 43.06 }, { 59.5, 34.5 }, { 59.4998, 35.2514 } }
! };
!
! /* Adjust any point by an epsilon to see the fangs appear.
! (The fact that they are missing is a bug)
! -- Actually, it looks like this one is working now.
! */
! static const spline_t touchy_fangs = {
! { { 18.25, 21.875 }, { 18.25, 23.875 }, { 30, 30.375 }, { 30, 27 } }
! };
!
! /* Here's another one that starts off buggy, but small adjustments
! change it, (here you need to change a control point by a pixel or
! two in order to have an effect) */
! static const spline_t touchy_shell = {
! { { 8.9375, 6.32812 }, { 8.23434, 10.3594 }, { 8.5625, 4.25 }, { 11.9531, 7.6875 } }
! };
!
! /* A simple looping spline. No known problems. */
static const spline_t ribbon = {
{ {110, 20}, {310, 300}, {10, 310}, {210, 20} }
};
- static const spline_t simple = {
- { {10, 10}, {70, 10}, {100, 40}, {100, 100} }
- };
-
- /* This one's fixed with some proper sorting/duplicate elimination on the pen */
- static const spline_t non_uniform_width = {
- { { 18, 12 }, { 18.0001373291015625, 30.26085662841796875 }, { 99.9996185302734375, 48.72466278076171875 }, { 100, 100 } }
- };
-
- /* This one shows why my incremental trapexoid generation scheme was flawed */
- static const spline_t warts = {
- { { 14.75, 25.25 }, { 30.5001373291015625, 57.76085662841796875 }, { 127.4996185302734375, -17.77533721923828125 }, { 61.75, 33.75 } }
- };
-
- /* This one is designed to torture ghostscript */
- static const spline_t gstorture = {
- {{51.57, 51.57}, {412.56, 412.56}, {34.38, 412.56}, {395.37, 51.57}}
- };
-
- /*
- #define DEFAULT_SPLINE gstorture
- #define DEFAULT_ZOOM 1.0
- */
-
- /*
- #define DEFAULT_SPLINE simple
- #define DEFAULT_ZOOM 1.0
- */
-
- /* This was causing an infitie loop at one point.
- The bug is now fixed, (the while loop no longer exists) */
- static const spline_t infinite_loop = {
- { {32 * 4008192 / 65536.0, 32 * 10819706 / 65536.0},
- {32 * 44968140 / 65536.0, 32 * 7706746 / 65536.0},
- {32 * 26431488 / 65536.0, 32 * 524288 / 65536.0},
- {32 * 1073152 / 65536.0, 32 * 17997824 / 65536.0} }
- };
-
- /* This one shows a tiny sub-pixel trapezoid that was being rasterized
- with a fully-lit pixel. The fix for this has now been applied to
- the trap code in the server. */
- static const spline_t wart = {
- { { 179, 410 }, { 448, 475 }, { 33, 514 }, { 313, 399 } }
- };
-
- /* A new wart, (right on the tip of the spike). I haven't chased this
- one down yet. */
- static const spline_t another_wart = {
- { { 37.5, 22.125 }, { 40.875, 23.625 }, { 13.375, 57 }, { 34.25, 35.375 } }
- };
-
- /* What happens when the spline folds over on itself at the end? */
- static const spline_t overlap = {
- { { 3.03125, 5.1875 }, { 12.25, 11.1875 }, { 15.1875, 7.375 }, { 14.25, 7.45312 } }
- };
-
- /*
- #define DEFAULT_SPLINE funky_fangs
- #define DEFAULT_ZOOM 8.0
- */
-
- /*
- #define DEFAULT_SPLINE touchy_fangs
- #define DEFAULT_ZOOM 8.0
- */
-
- /*
- #define DEFAULT_SPLINE touchy_shell
- #define DEFAULT_ZOOM 16.0
- */
-
- /*
- #define DEFAULT_SPLINE polygon_killer
- #define DEFAULT_ZOOM (1 / 32.0)
- */
-
- /*
- #define DEFAULT_SPLINE infinite_loop
- #define DEFAULT_ZOOM 1 / 32.0
- */
-
#define DEFAULT_SPLINE ribbon
- #define DEFAULT_ZOOM 1.0
-
- /*
- #define DEFAULT_SPLINE wart
- #define DEFAULT_ZOOM 1.0
- */
-
- /*
- #define DEFAULT_SPLINE another_wart
- #define DEFAULT_ZOOM 8.0
- #define DEFAULT_WIDTH 256
- #define DEFAULT_TOLERANCE .001
- */
-
- /*
- #define DEFAULT_SPLINE overlap
- #define DEFAULT_ZOOM 64
- */
static const callback_doc_t callback_doc[] = {
{ quit_cb, "Exit the program" },
- { select_point_cb, "Activate closest control point" },
- { select_next_cb, "Activate next control point" },
- { snap_point_cb, "Snap active point to closest control point" },
- { left_epsilon_cb, "Move active point left by an epsilon" },
- { right_epsilon_cb, "Move active point right by an epsilon" },
- { up_epsilon_cb, "Move active point up by an epsilon" },
- { down_epsilon_cb, "Move active point down by an epsilon" },
{ print_spline_cb, "Print current spline coordinates on stdout" },
! { zoom_in_cb, "Zoom in (2X)" },
! { zoom_out_cb, "Zoom out (2X)" },
! { trans_left_cb, "Translate left (25%)" },
! { trans_right_cb, "Translate right (25%)" },
! { trans_up_cb, "Translate up (25%)" },
! { trans_down_cb, "Translate down (25%)" },
{ flatten_cb, "Decrease rendering accuracy, (tolerance *= 10)" },
{ smooth_cb, "Increase rendering accuracy, (tolerance /= 10)" },
- { toggle_path, "Toggle thin display of spline path" },
{ widen_line, "Widen line width" },
{ narrow_line, "Narrow line width" },
--- 92,116 ----
static const double DEFAULT_TOLERANCE = .1;
static const double DEFAULT_LINE_WIDTH = 10;
+ static const double DEFAULT_ZOOM = 1.0;
static const cairo_line_cap_t DEFAULT_LINE_CAP = CAIRO_LINE_CAP_ROUND;
! /* A simple looping spline. */
static const spline_t ribbon = {
{ {110, 20}, {310, 300}, {10, 310}, {210, 20} }
};
#define DEFAULT_SPLINE ribbon
static const callback_doc_t callback_doc[] = {
{ quit_cb, "Exit the program" },
{ print_spline_cb, "Print current spline coordinates on stdout" },
! { zoom_in_cb, "Zoom in" },
! { zoom_out_cb, "Zoom out" },
! { trans_left_cb, "Translate left" },
! { trans_right_cb, "Translate right" },
! { trans_up_cb, "Translate up" },
! { trans_down_cb, "Translate down" },
{ flatten_cb, "Decrease rendering accuracy, (tolerance *= 10)" },
{ smooth_cb, "Increase rendering accuracy, (tolerance /= 10)" },
{ widen_line, "Widen line width" },
{ narrow_line, "Narrow line width" },
***************
*** 280,289 ****
/* Keysym, Alias, Keycode, callback */
{ "Q", 0, 0, quit_cb },
! { "Left", 0, 0, left_epsilon_cb },
! { "Right", 0, 0, right_epsilon_cb },
! { "Up", 0, 0, up_epsilon_cb },
! { "Down", 0, 0, down_epsilon_cb },
{ "Return", 0, 0, print_spline_cb },
- { "space", 0, 0, select_next_cb },
{ "plus", 0, 0, zoom_in_cb },
{ "equal", 1, 0, zoom_in_cb },
--- 120,128 ----
/* Keysym, Alias, Keycode, callback */
{ "Q", 0, 0, quit_cb },
! { "Left", 0, 0, trans_left_cb },
! { "Right", 0, 0, trans_right_cb },
! { "Up", 0, 0, trans_up_cb },
! { "Down", 0, 0, trans_down_cb },
{ "Return", 0, 0, print_spline_cb },
{ "plus", 0, 0, zoom_in_cb },
{ "equal", 1, 0, zoom_in_cb },
***************
*** 293,306 ****
{ "less", 0, 0, flatten_cb },
{ "comma", 1, 0, flatten_cb },
- { "P", 0, 0, toggle_path },
{ "W", 0, 0, widen_line },
{ "N", 0, 0, narrow_line },
};
- static const button_binding_t button_binding[] = {
- { 1, select_point_cb },
- { 3, snap_point_cb }
- };
-
int
main(int argc, char *argv[])
--- 132,139 ----
***************
*** 344,376 ****
static void
- draw_handle(cairo_t *ct, pt_t *p, int is_active, double size, double width)
- {
- cairo_save(ct);
-
- if (is_active)
- cairo_set_rgb_color(ct, 0, 1, 0);
- else
- cairo_set_rgb_color(ct, 1, 0, 0);
-
- cairo_set_line_width(ct, width);
-
- cairo_move_to(ct, p->x - size / 2.0, p->y - size / 2.0);
- cairo_rel_line_to(ct, size, 0);
- cairo_rel_line_to(ct, 0, size);
- cairo_rel_line_to(ct, -size, 0);
- cairo_rel_line_to(ct, 0, -size);
- cairo_stroke(ct);
-
- cairo_restore(ct);
- }
-
- static void
draw_spline(cairo_t *ct, win_t *win)
{
spline_t *spline = &win->spline;
double zoom = win->zoom;
int i;
cairo_save(ct);
--- 177,191 ----
static void
draw_spline(cairo_t *ct, win_t *win)
{
spline_t *spline = &win->spline;
double zoom = win->zoom;
+ double drag_user_x = win->drag_pt.x;
+ double drag_user_y = win->drag_pt.y;
int i;
+ cairo_inverse_transform_point (ct, &drag_user_x, &drag_user_y);
+
cairo_save(ct);
***************
*** 380,401 ****
spline->pt[2].x, spline->pt[2].y,
spline->pt[3].x, spline->pt[3].y);
- cairo_stroke(ct);
! if (win->show_path) {
! cairo_set_line_width(ct, 1 / zoom);
! cairo_set_rgb_color(ct, 1, 1, 1);
! cairo_move_to(ct, spline->pt[0].x, spline->pt[0].y);
! cairo_curve_to(ct,
! spline->pt[1].x, spline->pt[1].y,
! spline->pt[2].x, spline->pt[2].y,
! spline->pt[3].x, spline->pt[3].y);
! cairo_stroke(ct);
}
draw_control_line(ct, &spline->pt[0], &spline->pt[1], 2.0 / zoom);
draw_control_line(ct, &spline->pt[3], &spline->pt[2], 2.0 / zoom);
for (i=0; i < 4; i++) {
! draw_handle(ct, &spline->pt[i], i == win->active, 5.0 / zoom, 1.0 / zoom);
}
--- 195,226 ----
spline->pt[2].x, spline->pt[2].y,
spline->pt[3].x, spline->pt[3].y);
! if (win->click && cairo_in_stroke (ct, drag_user_x, drag_user_y)) {
! win->active = 0xf;
}
+ cairo_stroke(ct);
+
draw_control_line(ct, &spline->pt[0], &spline->pt[1], 2.0 / zoom);
draw_control_line(ct, &spline->pt[3], &spline->pt[2], 2.0 / zoom);
for (i=0; i < 4; i++) {
! cairo_save(ct);
!
! cairo_set_rgb_color(ct, 1, 0, 0);
!
! cairo_set_alpha (ct, 0.5);
!
! cairo_new_path (ct);
! cairo_arc (ct,
! spline->pt[i].x, spline->pt[i].y,
! win->line_width / 1.25, 0, 2 * M_PI);
! if (win->click && cairo_in_fill (ct, drag_user_x, drag_user_y)) {
! win->active = (1<<i);
! win->click = 0;
! }
! cairo_fill (ct);
!
! cairo_restore(ct);
}
***************
*** 416,424 ****
ct = cairo_create();
- /*
- if (win->restrict_traps)
- cairo_restrict_spline_traps(ct, win->trap_start, win->trap_stop);
- */
-
cairo_set_target_drawable (ct, dpy, drawable);
--- 241,244 ----
***************
*** 478,482 ****
win->active = 0;
- win->active_pt = &win->spline.pt[win->active];
win->spline = DEFAULT_SPLINE;
win->tolerance = DEFAULT_TOLERANCE;
--- 298,301 ----
***************
*** 487,493 ****
win->ytrans = DEFAULT_YTRANS;
! win->show_path = 1;
!
! win->drag = 0;
win_refresh(win);
--- 306,310 ----
win->ytrans = DEFAULT_YTRANS;
! win->click = 0;
win_refresh(win);
***************
*** 538,550 ****
int i;
! printf("Cairo spline demonstration\n");
! printf("Click and drag to move spline endpoints and control points, or:\n\n");
!
! for (i=0; i < ARRAY_SIZE(button_binding); i++)
! printf("Button %d:\t%s\n",
! button_binding[i].button,
! get_callback_doc(button_binding[i].callback));
!
! printf("\n");
for (i=0; i < ARRAY_SIZE(key_binding); i++)
--- 355,360 ----
int i;
! printf("A cairo spline demonstration\n");
! printf("Click and drag to move the spline or adjust its controls. Or:\n\n");
for (i=0; i < ARRAY_SIZE(key_binding); i++)
***************
*** 555,591 ****
}
! static double
! distance_sq(pt_t *a, pt_t *b)
{
! double dx = b->x - a->x;
! double dy = b->y - a->y;
!
! return dx*dx + dy*dy;
}
! static int
! win_handle_button_press(win_t *win, XButtonEvent *bev)
{
int i;
! for (i=0; i < ARRAY_SIZE(button_binding); i++)
! if (button_binding[i].button == bev->button)
! return (button_binding[i].callback)(win, bev->x, bev->y);
! return 0;
! }
- static void
- win_handle_motion(win_t *win, XMotionEvent *mev)
- {
- if (win->drag) {
- win->active_pt->x += (mev->x - win->drag_pt.x) / win->zoom;
- win->active_pt->y += (mev->y - win->drag_pt.y) / win->zoom;
-
win->needs_refresh = 1;
-
- win->drag_pt.x = mev->x;
- win->drag_pt.y = mev->y;
}
}
--- 365,397 ----
}
! static void
! win_handle_button_press(win_t *win, XButtonEvent *bev)
{
! win->click = 1;
! win->drag_pt.x = bev->x;
! win->drag_pt.y = bev->y;
!
! win->needs_refresh = 1;
}
! static void
! win_handle_motion(win_t *win, XMotionEvent *mev)
{
int i;
! if (win->active == 0)
! return;
! for (i = 0; i < 4; i++) {
! if (((1<<i) & win->active) == 0)
! continue;
! win->spline.pt[i].x += (mev->x - win->drag_pt.x) / win->zoom;
! win->spline.pt[i].y += (mev->y - win->drag_pt.y) / win->zoom;
win->needs_refresh = 1;
}
+
+ win->drag_pt.x = mev->x;
+ win->drag_pt.y = mev->y;
}
***************
*** 663,667 ****
break;
case ButtonRelease:
! win->drag = 0;
break;
case KeyPress:
--- 469,474 ----
break;
case ButtonRelease:
! win->click = 0;
! win->active = 0;
break;
case KeyPress:
***************
*** 683,733 ****
/* Callbacks */
- /* nearest control point in spline to given point */
- static int
- find_nearest(spline_t *spline, double x, double y)
- {
- int i, nearest;
- double dist, min_dist;
- pt_t pt;
-
- pt.x = x;
- pt.y = y;
-
- for (i=0; i<4; i++) {
- dist = distance_sq(&spline->pt[i], &pt);
- if (i==0 || dist < min_dist) {
- nearest = i;
- min_dist = dist;
- }
- }
-
- return nearest;
- }
-
- static int
- select_point_cb(win_t *win, int x, int y)
- {
- win->drag = 1;
- win->drag_pt.x = x;
- win->drag_pt.y = y;
-
- win->active = find_nearest(&win->spline, x / win->zoom, y / win->zoom);
- win->active_pt = &win->spline.pt[win->active];
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
- snap_point_cb(win_t *win, int x, int y)
- {
- *win->active_pt = win->spline.pt[find_nearest(&win->spline, x / win->zoom, y / win->zoom)];
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
static int
quit_cb(win_t *win)
--- 490,493 ----
***************
*** 737,791 ****
static int
- select_next_cb(win_t *win)
- {
- win->active = (win->active + 1) % 4;
- win->active_pt = &win->spline.pt[win->active];
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
- left_epsilon_cb(win_t *win)
- {
- win->active_pt->x -= EPSILON;
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
- right_epsilon_cb(win_t *win)
- {
- win->active_pt->x += EPSILON;
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
- up_epsilon_cb(win_t *win)
- {
- win->active_pt->y -= EPSILON;
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
- down_epsilon_cb(win_t *win)
- {
- win->active_pt->y += EPSILON;
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
print_spline_cb(win_t *win)
{
--- 497,500 ----
***************
*** 804,808 ****
zoom_in_cb(win_t *win)
{
! win->zoom *= 2.0;
win->needs_refresh = 1;
--- 513,517 ----
zoom_in_cb(win_t *win)
{
! win->zoom *= 1.1;
win->needs_refresh = 1;
***************
*** 814,818 ****
trans_left_cb(win_t *win)
{
! win->xtrans -= win->width / 4.0;
win->needs_refresh = 1;
--- 523,527 ----
trans_left_cb(win_t *win)
{
! win->xtrans -= win->width / 16.0;
win->needs_refresh = 1;
***************
*** 824,828 ****
trans_right_cb(win_t *win)
{
! win->xtrans += win->width / 4.0;
win->needs_refresh = 1;
--- 533,537 ----
trans_right_cb(win_t *win)
{
! win->xtrans += win->width / 16.0;
win->needs_refresh = 1;
***************
*** 834,838 ****
trans_up_cb(win_t *win)
{
! win->ytrans -= win->height / 4.0;
win->needs_refresh = 1;
--- 543,547 ----
trans_up_cb(win_t *win)
{
! win->ytrans -= win->height / 16.0;
win->needs_refresh = 1;
***************
*** 844,848 ****
trans_down_cb(win_t *win)
{
! win->ytrans += win->height / 4.0;
win->needs_refresh = 1;
--- 553,557 ----
trans_down_cb(win_t *win)
{
! win->ytrans += win->height / 16.0;
win->needs_refresh = 1;
***************
*** 854,858 ****
zoom_out_cb(win_t *win)
{
! win->zoom /= 2.0;
win->needs_refresh = 1;
--- 563,567 ----
zoom_out_cb(win_t *win)
{
! win->zoom /= 1.1;
win->needs_refresh = 1;
***************
*** 882,895 ****
static int
- toggle_path(win_t *win)
- {
- win->show_path = ! win->show_path;
-
- win->needs_refresh = 1;
-
- return 0;
- }
-
- static int
widen_line(win_t *win)
{
--- 591,594 ----
--- cairo-freq.c DELETED ---
More information about the cairo-commit
mailing list