1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 static void parse_args (gint
*p_argc
,
29 duplicate_and_stroke (cairo_t
*cr
);
30 static void stroke_and_destroy (cairo_t
*cr
,
33 static void browsing (GtkWidget
*widget
,
35 static void browsing_segment (GtkToggleButton
*togglebutton
,
37 static void browsing_primitive (GtkToggleButton
*togglebutton
,
39 static void browsing_reset (GtkButton
*button
,
41 static void browsing_next (GtkButton
*button
,
44 static gboolean
arcs (GtkWidget
*widget
,
46 static void arc3p (cairo_t
*cr
,
54 static gboolean
intersections (GtkWidget
*widget
,
56 static gboolean
offset_curves (GtkWidget
*widget
,
58 static gboolean
offset_segments (GtkWidget
*widget
,
61 static void circle_callback (cairo_t
*cr
);
62 static void piston_callback (cairo_t
*cr
);
63 static void curve1_callback (cairo_t
*cr
);
64 static void line1_callback (cairo_t
*cr
);
68 cairo_path_t
*cairo_path
;
71 CpmlPrimitive primitive
;
76 static CpmlPair bezier_samples
[][4] = {
77 { { 0, 0 }, { 0, 40 }, { 120, 40 }, { 120, 0 } }, /* Simmetric low */
78 { { 40, 0 }, { 40, 160 }, { 80, 160 }, { 80, 0 } }, /* Simmetric high */
79 { { 0, 0 }, { 33.1371, 33.1371 }, { 86.8629, 33.1371 }, { 120, 0 } },
80 /* Arc approximation */
81 { { 0, 0 }, { 70, 120 }, { 50, 120 }, { 120, 0 } }, /* Twisted controls */
83 { { 0, 0 }, { 0, 120 }, { 60, 120 }, { 120, 0 } }, /* Vertical p1-p2 */
84 { { 0, 0 }, { 60, 120 }, { 120, 120 }, { 120, 0 } },/* Vertical p3-p4 */
85 { { 0, 120 }, { 120, 120 }, { 120, 60 }, { 0, 0 } },/* Horizontal p1-p2 */
86 { { 0, 120 }, { 120, 60 }, { 120, 0 }, { 0, 0 } }, /* Horizontal p3-p4 */
88 { { 0, 0 }, { 0, 120 }, { 120, 120 }, { 120, 0 } }, /* Down */
89 { { 0, 120 }, { 120, 120 }, { 120, 0 }, { 0, 0 } }, /* Right */
90 { { 0, 120 }, { 0, 0 }, { 120, 0 }, { 120, 120 } }, /* Up */
91 { { 120, 120 }, { 0, 120 }, { 0, 0 }, { 120, 0 } }, /* Left */
93 { { 0, 60 }, { 60, 120 }, { 120, 60 }, { 60, 0 } }, /* Down-right */
94 { { 60, 120 }, { 120, 60 }, { 60, 0 }, { 0, 60 } }, /* Up-right */
95 { { 120, 60 }, { 60, 0 }, { 0, 60 }, { 60, 120 } }, /* Up-left */
96 { { 60, 0 }, { 0, 60 }, { 60, 120 }, { 120, 60 } }, /* Down-left*/
98 { { 0, 0 }, { 60, 0 }, { 60, 120 }, { 120, 120 } }, /* Step left */
99 { { 120, 0 }, { 60, 0 }, { 60, 120 }, { 0, 120 } }, /* Step right */
100 { { 0, 0 }, { 60, 90 }, { 90, 120 }, { 120, 90 } }, /* Unbalanced opened */
101 { { 0, 0 }, { 40, 120 }, { 120, 120 }, { 60, 80 } } /* Unbalanced closed */
104 static void (*path_samples
[]) (cairo_t
*cr
) = {
113 main(gint argc
, gchar
**argv
)
120 _demo_init(argc
, argv
);
121 parse_args(&argc
, &argv
);
123 path
= _demo_file("cpml-demo.ui");
125 g_printerr(_("cpml-demo.ui not found!\n"));
129 builder
= gtk_builder_new();
132 gtk_builder_set_translation_domain(builder
, GETTEXT_PACKAGE
);
133 gtk_builder_add_from_file(builder
, path
, &error
);
137 g_print("%s\n", error
->message
);
141 window
= (GtkWidget
*) gtk_builder_get_object(builder
, "wndMain");
143 /* Connect signals */
144 g_signal_connect(window
, "delete-event", G_CALLBACK(gtk_main_quit
), NULL
);
145 g_signal_connect(gtk_builder_get_object(builder
, "areaBrowsing"),
146 "draw", G_CALLBACK(browsing
), NULL
);
147 g_signal_connect(gtk_builder_get_object(builder
, "optBrowsingSegment"),
148 "toggled", G_CALLBACK(browsing_segment
), NULL
);
149 g_signal_connect(gtk_builder_get_object(builder
, "optBrowsingPrimitive"),
150 "toggled", G_CALLBACK(browsing_primitive
), NULL
);
151 g_signal_connect(gtk_builder_get_object(builder
, "btnBrowsingReset"),
152 "clicked", G_CALLBACK(browsing_reset
), NULL
);
153 g_signal_connect(gtk_builder_get_object(builder
, "btnBrowsingNext"),
154 "clicked", G_CALLBACK(browsing_next
), NULL
);
155 g_signal_connect(gtk_builder_get_object(builder
, "areaArcs"),
156 "draw", G_CALLBACK(arcs
), NULL
);
157 g_signal_connect(gtk_builder_get_object(builder
, "areaIntersections"),
158 "draw", G_CALLBACK(intersections
), NULL
);
159 g_signal_connect(gtk_builder_get_object(builder
, "areaOffsetCurves"),
160 "draw", G_CALLBACK(offset_curves
), NULL
);
161 g_signal_connect(gtk_builder_get_object(builder
, "areaOffsetSegments"),
162 "draw", G_CALLBACK(offset_segments
), NULL
);
163 g_signal_connect(gtk_builder_get_object(builder
, "btnQuit"),
164 "clicked", G_CALLBACK(gtk_main_quit
), NULL
);
166 g_object_unref(builder
);
168 gtk_widget_show_all(window
);
175 /**********************************************
176 * Command line options parser
177 **********************************************/
182 g_print("cpml-demo " PACKAGE_VERSION
"\n");
187 parse_args(gint
*p_argc
, gchar
**p_argv
[])
189 GError
*error
= NULL
;
190 GOptionEntry entries
[] = {
191 {"version", 'V', G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
192 (gpointer
) version
, _("Display version information"), NULL
},
196 gtk_init_with_args(p_argc
, p_argv
, _("- CPML demonstration program"),
197 entries
, GETTEXT_PACKAGE
, &error
);
200 gint error_code
= error
->code
;
202 g_printerr("%s\n", error
->message
);
210 static cairo_path_t
*
211 duplicate_and_stroke(cairo_t
*cr
)
213 cairo_path_t
*path
= cairo_copy_path(cr
);
215 cairo_set_line_width(cr
, 2.);
222 stroke_and_destroy(cairo_t
*cr
, cairo_path_t
*path
)
224 cairo_append_path(cr
, path
);
225 cairo_path_destroy(path
);
227 cairo_set_line_width(cr
, 1.);
233 browsing(GtkWidget
*widget
, cairo_t
*cr
)
235 if (browsing_data
.area
== NULL
) {
238 /* Initialize browsing_data */
239 browsing_data
.area
= widget
;
240 browsing_data
.use_segment
= TRUE
;
242 /* Append all the path samples to the cairo context */
244 cairo_translate(cr
, 270.5, -120.5);
245 for (n
= 0; n
< G_N_ELEMENTS(path_samples
); ++n
) {
247 cairo_translate(cr
, -270., 240.);
249 cairo_translate(cr
, 270., 0.);
252 (path_samples
[n
]) (cr
);
256 browsing_data
.cairo_path
= cairo_copy_path(cr
);
257 cpml_segment_from_cairo(&browsing_data
.segment
,
258 browsing_data
.cairo_path
);
259 cpml_primitive_from_segment(&browsing_data
.primitive
,
260 &browsing_data
.segment
);
262 cairo_append_path(cr
, browsing_data
.cairo_path
);
265 cairo_set_line_width(cr
, 2.);
268 cairo_set_source_rgb(cr
, 1., 0.4, 0.);
269 cairo_set_line_width(cr
, 5.);
271 if (browsing_data
.use_segment
)
272 cpml_segment_to_cairo(&browsing_data
.segment
, cr
);
274 cpml_primitive_to_cairo(&browsing_data
.primitive
, cr
);
280 browsing_segment(GtkToggleButton
*togglebutton
, gpointer user_data
)
282 if (!gtk_toggle_button_get_active(togglebutton
))
285 browsing_data
.use_segment
= TRUE
;
286 gtk_widget_queue_draw(browsing_data
.area
);
290 browsing_primitive(GtkToggleButton
*togglebutton
, gpointer user_data
)
292 if (!gtk_toggle_button_get_active(togglebutton
))
295 browsing_data
.use_segment
= FALSE
;
296 gtk_widget_queue_draw(browsing_data
.area
);
300 browsing_reset(GtkButton
*button
, gpointer user_data
)
302 if (browsing_data
.use_segment
) {
303 cpml_segment_reset(&browsing_data
.segment
);
304 cpml_primitive_from_segment(&browsing_data
.primitive
,
305 &browsing_data
.segment
);
307 cpml_primitive_reset(&browsing_data
.primitive
);
310 gtk_widget_queue_draw(browsing_data
.area
);
314 browsing_next(GtkButton
*button
, gpointer user_data
)
316 if (browsing_data
.use_segment
) {
317 cpml_segment_next(&browsing_data
.segment
);
318 cpml_primitive_from_segment(&browsing_data
.primitive
,
319 &browsing_data
.segment
);
321 cpml_primitive_next(&browsing_data
.primitive
);
324 gtk_widget_queue_draw(browsing_data
.area
);
329 arcs(GtkWidget
*widget
, cairo_t
*cr
)
331 cairo_translate(cr
, 100.5, 100.5);
332 arc3p(cr
, 0, 0, 0, 120, 120, 120);
334 cairo_translate(cr
, 200, 0.);
335 arc3p(cr
, 0, 0, 120, 0, 120, 120);
337 cairo_translate(cr
, 200, 0.);
338 arc3p(cr
, 60, 0, 0, 120, 120, 120);
340 cairo_translate(cr
, -400, 200);
341 arc3p(cr
, 0, 50, -2, 85, 120, 0);
343 cairo_translate(cr
, 200, 0);
344 arc3p(cr
, -2, 85, 0, 50, 120, 0);
350 arc3p(cairo_t
*cr
, double x1
, double y1
,
351 double x2
, double y2
, double x3
, double y3
)
354 cairo_path_data_t p
[4];
356 double r
, start
, end
;
362 p
[1].header
.type
= CPML_ARC
;
363 p
[1].header
.length
= 3;
372 cpml_primitive_to_cairo(&arc
, cr
);
374 cairo_set_line_width(cr
, 1.);
377 /* Add an arc generated by cairo, just for reference */
378 if (!cpml_arc_info(&arc
, ¢er
, &r
, &start
, &end
)) {
379 g_warning(_("Unable to get arc info (%lf, %lf) (%lf, %lf) (%lf, %lf)\n"),
380 x1
, y1
, x2
, y2
, x3
, y3
);
385 cairo_arc(cr
, center
.x
, center
.y
, r
-5., start
, end
);
387 cairo_arc_negative(cr
, center
.x
, center
.y
, r
-5., start
, end
);
389 /* Show the inscribed triangle */
390 cairo_move_to(cr
, x1
, y1
);
391 cairo_line_to(cr
, x2
, y2
);
392 cairo_line_to(cr
, x3
, y3
);
394 cairo_set_line_width(cr
, 0.5);
400 intersections(GtkWidget
*widget
, cairo_t
*cr
)
403 CpmlSegment segment1
, segment2
;
404 CpmlPair intersection
;
406 cairo_translate(cr
, 10.5, 120.5);
410 path
= cairo_copy_path(cr
);
412 cairo_set_line_width(cr
, 1.);
415 cpml_segment_from_cairo(&segment1
, path
);
416 cpml_segment_from_cairo(&segment2
, path
);
418 while (cpml_segment_next(&segment2
)) {
419 cpml_segment_put_intersections(&segment1
, &segment2
, 1, &intersection
);
421 cairo_arc(cr
, intersection
.x
, intersection
.y
, 2.5, 0, 2 * M_PI
);
424 cpml_segment_next(&segment1
);
427 cairo_path_destroy(path
);
433 offset_curves(GtkWidget
*widget
, cairo_t
*cr
)
437 cairo_path_t
*path
, *path_copy
;
439 CpmlPrimitive primitive
;
444 /* Add the Bézier curve samples */
445 for (n
= 0; n
< G_N_ELEMENTS(bezier_samples
); ++n
) {
446 bezier
= &bezier_samples
[n
][0];
448 /* The samples are arranged in a 4x? matrix of 200x150 cells */
450 cairo_translate(cr
, 25., 25.);
452 cairo_translate(cr
, -600., 150.);
454 cairo_translate(cr
, 200., 0.);
456 /* Draw the Bézier curve */
457 cairo_move_to(cr
, bezier
[0].x
, bezier
[0].y
);
458 cairo_curve_to(cr
, bezier
[1].x
, bezier
[1].y
,
459 bezier
[2].x
, bezier
[2].y
, bezier
[3].x
, bezier
[3].y
);
461 /* Create a copy, to be used after */
462 path_copy
= cairo_copy_path(cr
);
464 path
= duplicate_and_stroke(cr
);
465 cpml_segment_from_cairo(&segment
, path
);
466 cpml_segment_offset(&segment
, 20.);
467 stroke_and_destroy(cr
, path
);
469 cpml_segment_from_cairo(&segment
, path_copy
);
470 cpml_primitive_from_segment(&primitive
, &segment
);
472 /* Checking cpml_curve_put_pair_at_time and cpml_curve_put_vector_at_time */
473 cairo_set_line_width(cr
, 1.);
474 for (t
= 0; t
< 1; t
+= 0.1) {
475 cpml_curve_put_pair_at_time(&primitive
, t
, &pair
);
476 cpml_curve_put_vector_at_time(&primitive
, t
, &vector
);
477 cpml_vector_set_length(&vector
, 20.);
478 cpml_vector_normal(&vector
);
480 cairo_new_sub_path(cr
);
481 cairo_arc(cr
, pair
.x
, pair
.y
, 2.5, 0, M_PI
*2);
484 cairo_move_to(cr
, pair
.x
, pair
.y
);
485 cairo_line_to(cr
, pair
.x
+ vector
.x
, pair
.y
+ vector
.y
);
489 cairo_path_destroy(path_copy
);
497 offset_segments(GtkWidget
*widget
, cairo_t
*cr
)
503 cairo_translate(cr
, 270.5, -120.5);
505 /* Offset the path samples */
506 for (n
= 0; n
< G_N_ELEMENTS(path_samples
); ++n
) {
508 cairo_translate(cr
, -270., 240.);
510 cairo_translate(cr
, 270., 0.);
513 /* Call the path callback */
514 (path_samples
[n
]) (cr
);
516 path
= duplicate_and_stroke(cr
);
517 cpml_segment_from_cairo(&segment
, path
);
518 cpml_segment_offset(&segment
, 15.);
519 stroke_and_destroy(cr
, path
);
527 circle_callback(cairo_t
*cr
)
529 cairo_new_sub_path(cr
);
530 cairo_arc(cr
, 120., 0., 100., 0., M_PI
*2);
534 piston_callback(cairo_t
*cr
)
536 cairo_path_t
*old_path
, *path
;
537 cairo_matrix_t matrix
;
540 /* Save the previous path, if any */
541 old_path
= cairo_copy_path(cr
);
544 cairo_move_to(cr
, 0., 46.5);
545 cairo_line_to(cr
, 210., 46.5);
546 cairo_line_to(cr
, 222.5, 35.);
547 cairo_line_to(cr
, 270., 35.);
548 cairo_line_to(cr
, 270., 56.);
549 cairo_line_to(cr
, 273., 59.);
550 cairo_line_to(cr
, 302., 59.);
551 cairo_line_to(cr
, 305., 56.);
552 cairo_arc(cr
, 325., 52.5, 20., G_PI
, 3. * G_PI_2
);
553 cairo_line_to(cr
, 400., 32.5);
554 cairo_line_to(cr
, 410., 22.5);
555 cairo_line_to(cr
, 450., 22.5);
556 cairo_arc_negative(cr
,
557 452., 34., 2., G_PI
, G_PI_2
);
558 cairo_line_to(cr
, 460., 36.);
559 cairo_line_to(cr
, 470., 30.);
560 cairo_line_to(cr
, 472., 12.5);
562 /* Mirror a reversed copy of the current path on the y = 0 axis */
563 path
= cairo_copy_path(cr
);
564 cpml_segment_from_cairo(&segment
, path
);
566 cpml_segment_reverse(&segment
);
567 cairo_matrix_init_scale(&matrix
, 1., -1.);
568 cpml_segment_transform(&segment
, &matrix
);
570 /* Join the mirrored path to the old path... */
571 path
->data
[0].header
.type
= CPML_LINE
;
572 cairo_append_path(cr
, path
);
573 cairo_path_destroy(path
);
575 /* ...and close the shape */
576 cairo_close_path(cr
);
578 /* Save the resulting path and clear the path memory */
579 path
= cairo_copy_path(cr
);
582 /* Restore the previous path and reappend the new path */
583 cairo_append_path(cr
, old_path
);
584 cairo_path_destroy(old_path
);
585 cairo_append_path(cr
, path
);
586 cairo_path_destroy(path
);
590 curve1_callback(cairo_t
*cr
)
592 cairo_move_to(cr
, 30., 0.);
593 cairo_curve_to(cr
, 120., 120., 180., 100., 180., 20.);
594 cairo_curve_to(cr
, 180., -20., 50., 40., 150., 40.);
595 cairo_curve_to(cr
, 220., 40., 190., -60., 150., -60.);
596 cairo_curve_to(cr
, 100., -60., 80., -40., 60., -60.);
600 line1_callback(cairo_t
*cr
)
602 cairo_move_to(cr
, 0, -50);
603 cairo_line_to(cr
, 100, 50);
605 cairo_move_to(cr
, 100, -50);
606 cairo_line_to(cr
, 0, 50);
608 cairo_move_to(cr
, 120, -50);
609 cairo_line_to(cr
, 200, -10);
611 cairo_move_to(cr
, 120, 50);
612 cairo_line_to(cr
, 200, 10);
614 cairo_move_to(cr
, 220, 0);
615 cairo_line_to(cr
, 280, 0);
617 cairo_move_to(cr
, 270, -40);
618 cairo_line_to(cr
, 270, 20);
620 cairo_move_to(cr
, 320, 60);
621 cairo_line_to(cr
, 380, 60);
623 cairo_move_to(cr
, 300, -40);
624 cairo_line_to(cr
, 340, 0);
626 cairo_move_to(cr
, 480, 10);
627 cairo_line_to(cr
, 400, 40);
629 cairo_move_to(cr
, 400, 40);
630 cairo_line_to(cr
, 450, -40);