Debian Codegraph @

third_party/spiro/ppedit/ppedit.c

raw symbols SCIP
1 /*
2 ppedit - A pattern plate editor for Spiro splines.
3 Copyright (C) 2007 Raph Levien
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19
20 */
21
22 #include "x3.h"
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <math.h>
27
28 #include "zmisc.h"
29 #include "bezctx.h"
30 #include "bezctx_x3.h"
31 #include "bezctx_ps.h"
32 #include "cornu.h"
33 #include "spiro.h"
34 #include "plate.h"
35 #include "image.h"
36
37 int n_iter = 10;
38
39 typedef struct {
40 const char *description;
41 plate *p;
42 } undo_record;
43
44 typedef struct {
45 x3widget *view;
46 const char *description;
47 plate *p;
48 int undo_n;
49 int undo_i;
50 undo_record undo_buf[16];
51 int undo_xn_state;
52
53 x3widget *undo_me;
54 x3widget *redo_me;
55
56 x3widget *show_knots_me;
57 x3widget *show_bg_me;
58 int show_knots;
59 int show_bg;
60
61 image *bg_image;
62 } plate_edit;
63
64 #define C1 0.55228
65 static void
66 draw_dot(x3dc *dc,
67 double x, double y, double r, guint32 rgba)
68 {
69 x3setrgba(dc, rgba);
70 x3moveto(dc, x + r, y);
71 x3curveto(dc, x + r, y - C1 * r, x + C1 * r, y - r, x, y - r);
72 x3curveto(dc, x - C1 * r, y - r, x - r, y - C1 * r, x - r, y);
73 x3curveto(dc, x - r, y + C1 * r, x - C1 * r, y + r, x, y + r);
74 x3curveto(dc, x + C1 * r, y + r, x + r, y + C1 * r, x + r, y);
75 x3fill(dc);
76 }
77
78 static void
79 draw_raw_rect(x3dc *dc,
80 double rx0, double ry0, double rx1, double ry1, guint32 rgba)
81 {
82 x3setrgba(dc, rgba);
83 x3rectangle(dc, rx0, ry0, rx1 - rx0, ry1 - ry0);
84 x3fill(dc);
85 }
86
87 static void
88 draw_rect(x3dc *dc,
89 double x, double y, double r, guint32 rgba)
90 {
91 draw_raw_rect(dc,
92 x - r, y - r, x + r, y + r, rgba);
93 }
94
95 static void
96 draw_half(x3dc *dc,
97 double x, double y, double r, double th, guint32 rgba)
98 {
99 double c = cos(th);
100 double s = sin(th);
101
102 x3setrgba(dc, rgba);
103 x3moveto(dc, x + c * r, y + s * r);
104 x3curveto(dc,
105 x + c * r + C1 * s * r,
106 y + s * r - C1 * c * r,
107 x + s * r + C1 * c * r,
108 y - c * r + C1 * s * r,
109 x + s * r,
110 y - c * r);
111 x3curveto(dc,
112 x + s * r - C1 * c * r,
113 y - c * r - C1 * s * r,
114 x - c * r + C1 * s * r,
115 y - s * r - C1 * c * r,
116 x - c * r,
117 y - s * r);
118 x3closepath(dc);
119 x3fill(dc);
120 }
121
122 static void
123 draw_plate(x3dc *dc,
124 plate_edit *pe)
125 {
126 plate *p = pe->p;
127 int i, j;
128
129 /* find an existing point to select, if any */
130 for (i = 0; i < p->n_sp; i++) {
131 bezctx *bc = new_bezctx_x3(dc);
132 subpath *sp = &p->sp[i];
133 spiro_seg *s = draw_subpath(sp, bc);
134
135 bezctx_x3_finish(bc);
136 x3setrgba(dc, 0x000000ff);
137 x3setlinewidth(dc, 1.5);
138 x3stroke(dc);
139
140 for (j = 0; j < sp->n_kt; j++) {
141 if (pe->show_knots) {
142 knot *kt = &sp->kt[j];
143 kt_flags kf = kt->flags;
144 if ((kf & KT_SELECTED) && (kf & KT_OPEN)) {
145 draw_dot(dc, kt->x, kt->y,
146 3, 0x000000ff);
147 draw_dot(dc, kt->x, kt->y,
148 1.5, 0xffffffff);
149 } else if ((kf & KT_SELECTED) && (kf & KT_CORNER)) {
150 draw_rect(dc, kt->x, kt->y,
151 3, 0x000000ff);
152 draw_rect(dc, kt->x, kt->y,
153 1.5, 0xffffffff);
154 } else if (!(kf & KT_SELECTED) && (kf & KT_CORNER)) {
155 draw_rect(dc, kt->x, kt->y,
156 2.5, 0x000080ff);
157 } else if ((kf & KT_SELECTED) && (kf & KT_CORNU)) {
158 draw_rect(dc, kt->x, kt->y,
159 3, 0xc000c0ff);
160 draw_rect(dc, kt->x, kt->y,
161 1.5, 0xffffffff);
162 } else if (!(kf & KT_SELECTED) && (kf & KT_CORNU)) {
163 draw_rect(dc, kt->x, kt->y,
164 2.5, 0x800080ff);
165 } else if ((kf & KT_LEFT) || (kf & KT_RIGHT)) {
166 double th = 1.5708 + (s ? get_knot_th(s, j) : 0);
167 if (kf & KT_LEFT)
168 th += 3.1415926;
169 if (kf & KT_SELECTED) {
170 draw_half(dc, kt->x, kt->y,
171 4, th, 0x000000ff);
172 draw_half(dc,
173 kt->x + sin(th), kt->y - cos(th),
174 2, th, 0xffffffff);
175 } else {
176 draw_half(dc, kt->x, kt->y,
177 3, th, 0x000080ff);
178 }
179 } else {
180 draw_dot(dc, kt->x, kt->y,
181 2, 0x000080ff);
182 }
183 }
184 }
185 free_spiro(s);
186 }
187 }
188
189 static void
190 draw_selection(x3dc *dc,
191 plate_edit *pe)
192 {
193 plate *p = pe->p;
194
195 if (p->motmode == MOTION_MODE_SELECT) {
196 double rx0 = p->sel_x0;
197 double ry0 = p->sel_y0;
198 double rx1 = p->x0;
199 double ry1 = p->y0;
200 if (rx0 > rx1) {
201 double tmp = rx1;
202 rx1 = rx0;
203 rx0 = tmp;
204 }
205 if (ry0 > ry1) {
206 double tmp = ry1;
207 ry1 = ry0;
208 ry0 = tmp;
209 }
210 if (rx1 > rx0 && ry1 > ry0)
211 draw_raw_rect(dc,
212 rx0, ry0, rx1, ry1, 0x0000ff20);
213 }
214 }
215
216 /* Make sure there's room for at least one more undo record. */
217 static void
218 makeroom_undo(plate_edit *pe)
219 {
220 const int undo_max = sizeof(pe->undo_buf) / sizeof(undo_record);
221
222 if (pe->undo_n == undo_max) {
223 free_plate(pe->undo_buf[0].p);
224 memmove(pe->undo_buf, pe->undo_buf + 1, (undo_max - 1) * sizeof(undo_record));
225 pe->undo_i--;
226 pe->undo_n--;
227 }
228 }
229
230 static void
231 set_undo_menuitem(x3widget *me, const char *name, const char *desc)
232 {
233 char str[256];
234
235 if (desc) {
236 sprintf(str, "%s %s", name, desc);
237 } else {
238 strcpy(str, name);
239 }
240 #if 0
241 gtk_container_foreach(GTK_CONTAINER(me),
242 (GtkCallback)gtk_label_set_text,
243 str);
244 #endif
245 x3setactive(me, desc != NULL);
246 }
247
248 static void
249 set_undo_state(plate_edit *pe, const char *undo_desc, const char *redo_desc)
250 {
251 set_undo_menuitem(pe->undo_me, "Undo", undo_desc);
252 set_undo_menuitem(pe->redo_me, "Redo", redo_desc);
253 }
254
255 static void
256 begin_undo_xn(plate_edit *pe)
257 {
258 int i;
259
260 if (pe->undo_xn_state != 1) {
261 for (i = pe->undo_i; i < pe->undo_n; i++)
262 free_plate(pe->undo_buf[i].p);
263 pe->undo_n = pe->undo_i;
264 makeroom_undo(pe);
265 i = pe->undo_i;
266 pe->undo_buf[i].description = pe->description;
267 pe->undo_buf[i].p = copy_plate(pe->p);
268 pe->undo_n = i + 1;
269 pe->undo_xn_state = 1;
270 }
271 }
272
273 static void
274 dirty_undo_xn(plate_edit *pe, const char *description)
275 {
276 if (pe->undo_xn_state == 0) {
277 g_warning("dirty_undo_xn: not in begin_undo_xn state");
278 begin_undo_xn(pe);
279 }
280 if (description == NULL)
281 description = pe->p->description;
282 if (pe->undo_xn_state == 1) {
283 pe->undo_i++;
284 pe->undo_xn_state = 2;
285 set_undo_state(pe, description, NULL);
286 }
287 pe->description = description;
288 }
289
290 static void
291 begindirty_undo_xn(plate_edit *pe, const char *description)
292 {
293 begin_undo_xn(pe);
294 dirty_undo_xn(pe, description);
295 }
296
297 static void
298 end_undo_xn(plate_edit *pe)
299 {
300 if (pe->undo_xn_state == 0) {
301 g_warning("end_undo_xn: not in undo xn");
302 }
303 pe->undo_xn_state = 0;
304 }
305
306 static int
307 undo(plate_edit *pe)
308 {
309 if (pe->undo_i == 0)
310 return 0;
311
312 if (pe->undo_i == pe->undo_n) {
313 makeroom_undo(pe);
314 pe->undo_buf[pe->undo_i].description = pe->description;
315 pe->undo_buf[pe->undo_i].p = pe->p;
316 pe->undo_n++;
317 } else {
318 free_plate(pe->p);
319 }
320 pe->undo_i--;
321 pe->description = pe->undo_buf[pe->undo_i].description;
322 set_undo_state(pe,
323 pe->undo_i > 0 ? pe->description : NULL,
324 pe->undo_buf[pe->undo_i + 1].description);
325 g_print("undo: %d of %d\n", pe->undo_i, pe->undo_n);
326 pe->p = copy_plate(pe->undo_buf[pe->undo_i].p);
327 return 1;
328 }
329
330 static int
331 redo(plate_edit *pe)
332 {
333 if (pe->undo_i >= pe->undo_n - 1)
334 return 0;
335 free_plate(pe->p);
336 pe->undo_i++;
337 set_undo_state(pe,
338 pe->undo_buf[pe->undo_i].description,
339 pe->undo_i < pe->undo_n - 1 ?
340 pe->undo_buf[pe->undo_i + 1].description : NULL);
341 pe->description = pe->undo_buf[pe->undo_i].description;
342 pe->p = copy_plate(pe->undo_buf[pe->undo_i].p);
343 g_print("redo: %d of %d\n", pe->undo_i, pe->undo_n);
344 return 1;
345 }
346
347 typedef struct {
348 x3viewclient base;
349
350 plate_edit *pe;
351 } x3vc_ppe;
352
353 static void
354 ppedit_viewclient_draw(x3viewclient *self, x3dc *dc)
355 {
356 plate_edit *pe = ((x3vc_ppe *)self)->pe;
357
358 #ifdef VERBOSE
359 printf("ppedit draw\n");
360 #endif
361 #if 1
362 x3setrgba(dc, 0xffffffff);
363 #else
364 x3setrgba(dc, rand() << 8 | 0xff);
365 #endif
366 x3rectangle(dc, dc->x, dc->y, dc->width, dc->height);
367 x3fill(dc);
368
369 draw_plate(dc, pe);
370 draw_selection(dc, pe);
371 }
372
373 static void
374 ppedit_viewclient_mouse(x3viewclient *self,
375 int button, int mods,
376 double x, double y)
377 {
378 x3vc_ppe *z = (x3vc_ppe *)self;
379 plate_edit *pe = z->pe;
380 plate *p = pe->p;
381
382 #ifdef VERBOSE
383 printf("ppedit mouse: %d %d %g %g\n", button, mods, x, y);
384 #endif
385 if (button == 1) {
386 press_mod pm = 0;
387
388 if (mods & GDK_SHIFT_MASK) pm |= PRESS_MOD_SHIFT;
389 if (mods & GDK_CONTROL_MASK) pm |= PRESS_MOD_CTRL;
390 if (mods == GDK_2BUTTON_PRESS) pm |= PRESS_MOD_DOUBLE;
391 if (mods == GDK_3BUTTON_PRESS) pm |= PRESS_MOD_TRIPLE;
392
393 begin_undo_xn(pe);
394 p->description = NULL;
395 plate_press(p, x, y, pm);
396 if (p->description) dirty_undo_xn(pe, NULL);
397 x3view_dirty(pe->view);
398 } else if (button == -1) {
399 int need_redraw = (pe->p->motmode == MOTION_MODE_SELECT);
400
401 plate_unpress(pe->p);
402
403 if (need_redraw) x3view_dirty(pe->view);
404 } else if (button == 0 && (mods & GDK_BUTTON1_MASK)) {
405 if (p->motmode == MOTION_MODE_MOVE) {
406 plate_motion_move(p, x, y);
407 dirty_undo_xn(pe, NULL);
408 } else if (p->motmode == MOTION_MODE_SELECT) {
409 plate_motion_select(p, x, y);
410 }
411 x3view_dirty(pe->view);
412 }
413 }
414
415 static int
416 ppedit_viewclient_key(x3viewclient *self, char *keyname, int mods, int key)
417 {
418 x3vc_ppe *z = (x3vc_ppe *)self;
419 plate_edit *pe = z->pe;
420 double dx = 0, dy = 0;
421 int did_something = 0;
422
423 if (!strcmp(keyname, "Left"))
424 dx = -1;
425 else if (!strcmp(keyname, "Right"))
426 dx = 1;
427 else if (!strcmp(keyname, "Up"))
428 dy = -1;
429 else if (!strcmp(keyname, "Down"))
430 dy = 1;
431 if (mods & X3_SHIFT_MASK) {
432 dx *= 10;
433 dy *= 10;
434 } else if (mods & X3_CONTROL_MASK) {
435 dx *= .1;
436 dy *= .1;
437 }
438 if (dx != 0 || dy != 0) {
439 begindirty_undo_xn(pe, "Keyboard move");
440 plate_motion_move(pe->p, pe->p->x0 + dx, pe->p->y0 + dy);
441 end_undo_xn(pe);
442 did_something = TRUE;
443 }
444 if (did_something) {
445 x3view_dirty(pe->view);
446 return 1;
447 } else
448 return 0;
449 }
450
451 x3viewclient *ppedit_viewclient(plate_edit *pe)
452 {
453 x3vc_ppe *result = (x3vc_ppe *)malloc(sizeof(x3vc_ppe));
454
455 x3viewclient_init(&result->base);
456 result->base.draw = ppedit_viewclient_draw;
457 result->base.mouse = ppedit_viewclient_mouse;
458 result->base.key = ppedit_viewclient_key;
459 result->pe = pe;
460
461 return &result->base;
462 }
463
464 int
465 print_func(plate_edit *pe)
466 {
467 plate *p = pe->p;
468 int i;
469 FILE *f = fopen("/tmp/foo.ps", "w");
470 bezctx *bc = new_bezctx_ps(f);
471
472 fputs(ps_prolog, f);
473 for (i = 0; i < p->n_sp; i++) {
474 subpath *sp = &p->sp[i];
475 free_spiro(draw_subpath(sp, bc));
476 }
477 bezctx_ps_close(bc);
478 fputs(ps_postlog, f);
479 fclose(f);
480 return TRUE;
481 }
482
483 int
484 ppedit_callback(x3widget *w, void *data,
485 char *cmd, char *what, char *arg, void *more)
486 {
487 plate_edit *pe = (plate_edit *)data;
488 printf("my callback: cmd=\"%s\", what=\"%s\", arg=\"%s\"\n",
489 cmd, what ? what : "(null)", arg ? arg : "(null)");
490
491 if (!strcmp(cmd, "quit")) {
492 gtk_main_quit();
493 } else if (!strcmp(cmd, "save")) {
494 file_write_plate("plate", pe->p);
495 } else if (!strcmp(cmd, "khid")) {
496 pe->show_knots = !pe->show_knots;
497 x3view_dirty(pe->view);
498 } else if (!strcmp(cmd, "bghd")) {
499 pe->show_bg = !pe->show_bg;
500 x3view_dirty(pe->view);
501 } else if (!strcmp(cmd, "prin")) {
502 return print_func(pe);
503 } else if (!strcmp(cmd, "undo")) {
504 undo(pe);
505 x3view_dirty(pe->view);
506 } else if (!strcmp(cmd, "redo")) {
507 redo(pe);
508 x3view_dirty(pe->view);
509 } else if (!strcmp(cmd, "togc")) {
510 begindirty_undo_xn(pe, "Toggle Corner");
511 plate_toggle_corner(pe->p);
512 end_undo_xn(pe);
513 x3view_dirty(pe->view);
514 } else if (!strcmp(cmd, "delp")) {
515 begindirty_undo_xn(pe, "Delete Point");
516 plate_delete_pt(pe->p);
517 end_undo_xn(pe);
518 x3view_dirty(pe->view);
519 } else if (!strcmp(cmd, "mods")) {
520 pe->p->mmode = MOUSE_MODE_SELECT;
521 } else if (!strcmp(cmd, "modo")) {
522 pe->p->mmode = MOUSE_MODE_ADD_CURVE;
523 } else if (!strcmp(cmd, "modv")) {
524 pe->p->mmode = MOUSE_MODE_ADD_CORNER;
525 } else if (!strcmp(cmd, "modl")) {
526 pe->p->mmode = MOUSE_MODE_ADD_LEFT;
527 } else if (!strcmp(cmd, "modr")) {
528 pe->p->mmode = MOUSE_MODE_ADD_RIGHT;
529 } else if (!strcmp(cmd, "modc")) {
530 pe->p->mmode = MOUSE_MODE_ADD_CORNU;
531 }
532 return 1;
533 }
534
535 static void
536 create_mainwin(plate_edit *pe)
537 {
538 x3widget *mainwin;
539 x3widget *view;
540 x3widget *menu;
541 void *data = pe;
542 x3viewflags viewflags = x3view_click | x3view_hover | x3view_key |
543 x3view_2d | x3view_scroll;
544
545 mainwin = x3window(x3window_main, "ppedit", ppedit_callback, data);
546
547 menu = x3menu(mainwin, "File");
548
549 x3menuitem(menu, "Save", "save", "<cmd>s");
550 x3menuitem(menu, "Print", "prin", "<cmd>p");
551 x3menuitem(menu, "Quit", "quit", "<cmd>q");
552
553 menu = x3menu(mainwin, "Edit");
554
555 pe->undo_me = x3menuitem(menu, "Undo", "undo", "<cmd>z");
556 pe->redo_me = x3menuitem(menu, "Redo", "redo", "<cmd>y");
557
558 //set_undo_state(p, NULL, NULL);
559 x3menuitem(menu, "Toggle Corner", "togc", "<cmd>t");
560 x3menuitem(menu, "Delete Point", "delp", "<cmd>d");
561 x3menuitem(menu, "Selection Mode", "mods", "1");
562 x3menuitem(menu, "Add Curve Mode", "modo", "2");
563 x3menuitem(menu, "Add Corner Mode", "modv", "3");
564 x3menuitem(menu, "Add Left Mode", "modl", "4");
565 x3menuitem(menu, "Add Right Mode", "modr", "5");
566 x3menuitem(menu, "Add G2 point Mode", "modc", "6");
567
568 menu = x3menu(mainwin, "View");
569
570 pe->show_knots_me = x3menuitem(menu, "Hide Knots", "khid", "<cmd>k");
571 pe->show_bg_me = x3menuitem(menu, "Hide BG", "bghd", "<cmd>b");
572
573 pe->view = x3view(mainwin, viewflags, ppedit_viewclient(pe));
574
575 #ifdef X3_GTK
576 gtk_window_set_default_size(GTK_WINDOW(mainwin->widget), 512, 512);
577 #endif
578
579 x3_window_show(mainwin);
580 }
581
582 int main(int argc, char **argv)
583 {
584 plate_edit pe;
585 plate *p = NULL;
586 x3init(&argc, &argv);
587 char *reason;
588
589 if (argc > 1)
590 p = file_read_plate(argv[1]);
591 if (p == NULL)
592 p = new_plate();
593 pe.p = p;
594 pe.undo_n = 0;
595 pe.undo_i = 0;
596 pe.undo_xn_state = 0;
597 pe.show_knots = 1;
598 pe.show_bg = 1;
599 pe.bg_image = load_image_file("/tmp/foo.ppm", &reason);
600 create_mainwin(&pe);
601 x3main();
602 return 0;
603 }