1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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.
27 _cpml_behavior_browsing(void)
30 CpmlPrimitive primitive
, primitive_copy
;
32 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
34 cpml_primitive_from_segment(&primitive
, &segment
);
35 g_assert_cmpfloat((primitive
.org
)->point
.x
, ==, 0);
36 g_assert_cmpfloat((primitive
.org
)->point
.y
, ==, 1);
37 g_assert_cmpint((primitive
.data
)->header
.type
, ==, CPML_LINE
);
38 g_assert_true(cpml_primitive_next(&primitive
));
39 g_assert_cmpfloat((primitive
.org
)->point
.x
, ==, 3);
40 g_assert_cmpfloat((primitive
.org
)->point
.y
, ==, 1);
41 g_assert_cmpint((primitive
.data
)->header
.type
, ==, CPML_ARC
);
42 g_assert_true(cpml_primitive_next(&primitive
));
43 g_assert_cmpfloat((primitive
.org
)->point
.x
, ==, 6);
44 g_assert_cmpfloat((primitive
.org
)->point
.y
, ==, 7);
45 g_assert_cmpint((primitive
.data
)->header
.type
, ==, CPML_CURVE
);
46 g_assert_true(cpml_primitive_next(&primitive
));
47 g_assert_cmpfloat((primitive
.org
)->point
.x
, ==, -2);
48 g_assert_cmpfloat((primitive
.org
)->point
.y
, ==, 2);
49 g_assert_cmpint((primitive
.data
)->header
.type
, ==, CPML_CLOSE
);
50 g_assert_false(cpml_primitive_next(&primitive
));
52 cpml_primitive_reset(&primitive
);
53 g_assert_true(cpml_primitive_next(&primitive
));
54 cpml_primitive_reset(&primitive
);
55 cpml_primitive_reset(&primitive
);
56 g_assert_true(cpml_primitive_next(&primitive
));
57 g_assert_true(cpml_primitive_next(&primitive
));
58 g_assert_true(cpml_primitive_next(&primitive
));
59 g_assert_false(cpml_primitive_next(&primitive
));
61 cpml_primitive_copy(&primitive_copy
, &primitive
);
62 g_assert_false(cpml_primitive_next(&primitive_copy
));
63 cpml_primitive_reset(&primitive
);
64 g_assert_false(cpml_primitive_next(&primitive_copy
));
65 cpml_primitive_reset(&primitive_copy
);
66 g_assert_true(cpml_primitive_next(&primitive_copy
));
70 _cpml_sanity_from_segment(gint i
)
72 CpmlPrimitive primitive
;
75 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
79 cpml_primitive_from_segment(NULL
, &segment
);
82 cpml_primitive_from_segment(&primitive
, NULL
);
85 g_test_trap_assert_failed();
91 _cpml_sanity_copy(gint i
)
93 CpmlPrimitive primitive
;
96 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
97 cpml_primitive_from_segment(&primitive
, &segment
);
101 cpml_primitive_copy(NULL
, &primitive
);
104 cpml_primitive_copy(&primitive
, NULL
);
107 g_test_trap_assert_failed();
113 _cpml_sanity_copy_data(gint i
)
115 CpmlPrimitive primitive
;
118 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
119 cpml_primitive_from_segment(&primitive
, &segment
);
123 cpml_primitive_copy_data(NULL
, &primitive
);
126 cpml_primitive_copy_data(&primitive
, NULL
);
129 g_test_trap_assert_failed();
135 _cpml_sanity_get_n_points(gint i
)
139 cpml_primitive_get_n_points(NULL
);
142 g_test_trap_assert_failed();
148 _cpml_sanity_get_length(gint i
)
152 cpml_primitive_get_length(NULL
);
155 g_test_trap_assert_failed();
161 _cpml_sanity_put_extents(gint i
)
164 CpmlPrimitive primitive
;
167 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
168 cpml_primitive_from_segment(&primitive
, &segment
);
172 cpml_primitive_put_extents(NULL
, &extents
);
175 cpml_primitive_put_extents(&primitive
, NULL
);
178 g_test_trap_assert_failed();
184 _cpml_sanity_put_pair_at(gint i
)
187 CpmlPrimitive primitive
;
190 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
191 cpml_primitive_from_segment(&primitive
, &segment
);
195 cpml_primitive_put_pair_at(NULL
, 1, &pair
);
198 cpml_primitive_put_pair_at(&primitive
, 1, NULL
);
201 g_test_trap_assert_failed();
207 _cpml_sanity_put_vector_at(gint i
)
210 CpmlPrimitive primitive
;
213 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
214 cpml_primitive_from_segment(&primitive
, &segment
);
218 cpml_primitive_put_vector_at(NULL
, 1, &vector
);
221 cpml_primitive_put_vector_at(&primitive
, 1, NULL
);
224 g_test_trap_assert_failed();
230 _cpml_sanity_get_closest_pos(gint i
)
233 CpmlPrimitive primitive
;
234 CpmlPair pair
= { 1, 1 };
236 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
237 cpml_primitive_from_segment(&primitive
, &segment
);
241 cpml_primitive_get_closest_pos(NULL
, &pair
);
244 cpml_primitive_get_closest_pos(&primitive
, NULL
);
247 g_test_trap_assert_failed();
253 _cpml_sanity_set_point(gint i
)
256 CpmlPrimitive primitive
;
257 CpmlPair pair
= { 1, 1 };
259 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
260 cpml_primitive_from_segment(&primitive
, &segment
);
264 cpml_primitive_set_point(NULL
, 1, &pair
);
267 cpml_primitive_set_point(&primitive
, 1, NULL
);
270 g_test_trap_assert_failed();
276 _cpml_sanity_put_point(gint i
)
279 CpmlPrimitive primitive
;
282 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
283 cpml_primitive_from_segment(&primitive
, &segment
);
287 cpml_primitive_put_point(NULL
, 0, &pair
);
290 cpml_primitive_put_point(&primitive
, 0, NULL
);
293 g_test_trap_assert_failed();
299 _cpml_sanity_put_intersections(gint i
)
302 CpmlPrimitive primitive1
, primitive2
;
305 /* Set primitive1 to 1.1 and primitive 2 to 2.2,
306 * so there is an intersection point in (1, 1) */
307 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
308 cpml_primitive_from_segment(&primitive1
, &segment
);
310 cpml_segment_next(&segment
);
311 cpml_primitive_from_segment(&primitive2
, &segment
);
312 cpml_primitive_next(&primitive2
);
316 cpml_primitive_put_intersections(NULL
, &primitive2
, 2, &pair
);
319 cpml_primitive_put_intersections(&primitive1
, NULL
, 2, &pair
);
322 cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, NULL
);
325 g_test_trap_assert_failed();
331 _cpml_sanity_put_intersections_with_segment(gint i
)
333 CpmlPrimitive primitive
;
337 /* Set primitive 1.1 and segment to 2
338 * so there is an intersection point in (1,1) */
339 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
340 cpml_primitive_from_segment(&primitive
, &segment
);
342 cpml_segment_next(&segment
);
346 cpml_primitive_put_intersections_with_segment(NULL
, &segment
, 2, &pair
);
349 cpml_primitive_put_intersections_with_segment(&primitive
, NULL
, 2, &pair
);
352 cpml_primitive_put_intersections_with_segment(&primitive
, &segment
, 2, NULL
);
355 g_test_trap_assert_failed();
361 _cpml_sanity_join(gint i
)
364 CpmlPrimitive primitive
;
366 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
367 cpml_primitive_from_segment(&primitive
, &segment
);
371 cpml_primitive_join(NULL
, &primitive
);
374 cpml_primitive_join(&primitive
, NULL
);
377 g_test_trap_assert_failed();
383 _cpml_sanity_offset(gint i
)
387 cpml_primitive_offset(NULL
, 1);
390 g_test_trap_assert_failed();
396 _cpml_sanity_dump(gint i
)
400 cpml_segment_dump(NULL
);
403 g_test_trap_assert_failed();
409 _cpml_sanity_to_cairo(gint i
)
412 CpmlPrimitive primitive
;
414 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
415 cpml_primitive_from_segment(&primitive
, &segment
);
419 cpml_primitive_to_cairo(NULL
, adg_test_cairo_context());
422 cpml_primitive_to_cairo(&primitive
, NULL
);
425 g_test_trap_assert_failed();
431 _cpml_method_from_segment(void)
434 CpmlPrimitive primitive
;
436 g_assert_true(cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path()));
438 cpml_primitive_from_segment(&primitive
, &segment
);
439 g_assert_nonnull(primitive
.segment
);
440 g_assert_nonnull(primitive
.org
);
441 g_assert_nonnull(primitive
.data
);
445 _cpml_method_copy(void)
448 CpmlPrimitive original
;
449 CpmlPrimitive primitive
= { NULL
, NULL
, NULL
};
451 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
452 cpml_primitive_from_segment(&original
, &segment
);
454 g_assert_false(original
.segment
== primitive
.segment
);
455 g_assert_false(original
.org
== primitive
.org
);
456 g_assert_false(original
.data
== primitive
.data
);
458 cpml_primitive_copy(&primitive
, &original
);
460 g_assert_true(original
.segment
== primitive
.segment
);
461 g_assert_true(original
.org
== primitive
.org
);
462 g_assert_true(original
.data
== primitive
.data
);
466 _cpml_method_copy_data(void)
469 CpmlPrimitive original
, *primitive
;
472 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
473 cpml_primitive_from_segment(&original
, &segment
);
475 /* Check incompatible primitives are not copied */
476 primitive
= cpml_primitive_deep_dup(&original
);
477 ++primitive
->data
[0].header
.length
;
478 g_assert_cmpint(cpml_primitive_copy_data(primitive
, &original
), ==, 0);
479 --primitive
->data
[0].header
.length
;
480 ++primitive
->data
[0].header
.type
;
481 g_assert_cmpint(cpml_primitive_copy_data(primitive
, &original
), ==, 0);
482 --primitive
->data
[0].header
.type
;
485 primitive
= cpml_primitive_deep_dup(&original
);
486 ++primitive
->org
->point
.x
;
487 ++primitive
->org
->point
.y
;
488 for (n
= 1; n
< primitive
->data
[0].header
.length
; ++n
) {
489 ++primitive
->data
[n
].point
.x
;
490 ++primitive
->data
[n
].point
.y
;
492 g_assert_cmpfloat(primitive
->org
->point
.x
, !=, original
.org
->point
.x
);
493 g_assert_cmpfloat(primitive
->org
->point
.y
, !=, original
.org
->point
.y
);
494 for (n
= 1; n
< primitive
->data
[0].header
.length
; ++n
) {
495 g_assert_cmpfloat(primitive
->data
[n
].point
.x
, !=, original
.data
[n
].point
.x
);
496 g_assert_cmpfloat(primitive
->data
[n
].point
.y
, !=, original
.data
[n
].point
.y
);
498 g_assert_cmpint(cpml_primitive_copy_data(primitive
, &original
), ==, 1);
499 g_assert_cmpfloat(primitive
->org
->point
.x
, ==, original
.org
->point
.x
);
500 g_assert_cmpfloat(primitive
->org
->point
.y
, ==, original
.org
->point
.y
);
501 for (n
= 1; n
< primitive
->data
[0].header
.length
; ++n
) {
502 g_assert_cmpfloat(primitive
->data
[n
].point
.x
, ==, original
.data
[n
].point
.x
);
503 g_assert_cmpfloat(primitive
->data
[n
].point
.y
, ==, original
.data
[n
].point
.y
);
506 } while (cpml_primitive_next(&original
));
510 _cpml_method_type_get_n_points(void)
514 n_points
= cpml_primitive_type_get_n_points(CPML_MOVE
);
515 g_assert_cmpuint(n_points
, ==, 0);
517 n_points
= cpml_primitive_type_get_n_points(CPML_LINE
);
518 g_assert_cmpuint(n_points
, ==, 2);
520 n_points
= cpml_primitive_type_get_n_points(CPML_ARC
);
521 g_assert_cmpuint(n_points
, ==, 3);
523 n_points
= cpml_primitive_type_get_n_points(CPML_CURVE
);
524 g_assert_cmpuint(n_points
, ==, 4);
526 n_points
= cpml_primitive_type_get_n_points(CPML_CLOSE
);
527 g_assert_cmpuint(n_points
, ==, 2);
531 _cpml_method_get_n_points(void)
534 CpmlPrimitive primitive
;
537 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
540 cpml_primitive_from_segment(&primitive
, &segment
);
541 n_points
= cpml_primitive_get_n_points(&primitive
);
542 g_assert_cmpuint(n_points
, ==, 2);
545 cpml_primitive_next(&primitive
);
546 n_points
= cpml_primitive_get_n_points(&primitive
);
547 g_assert_cmpuint(n_points
, ==, 3);
550 cpml_primitive_next(&primitive
);
551 n_points
= cpml_primitive_get_n_points(&primitive
);
552 g_assert_cmpuint(n_points
, ==, 4);
554 /* Close: although the end point is not needed, the CPML API
555 * returns 2 points to treat this primitive as a CPML_LINE */
556 cpml_primitive_next(&primitive
);
557 n_points
= cpml_primitive_get_n_points(&primitive
);
558 g_assert_cmpuint(n_points
, ==, 2);
562 _cpml_method_get_length(void)
565 CpmlPrimitive primitive
;
567 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
568 cpml_segment_next(&segment
);
569 cpml_primitive_from_segment(&primitive
, &segment
);
571 g_assert_cmpfloat(cpml_primitive_get_length(&primitive
), ==, 1);
573 cpml_primitive_next(&primitive
);
574 g_assert_cmpfloat(cpml_primitive_get_length(&primitive
), ==, 2);
578 _cpml_method_put_extents(void)
581 CpmlPrimitive primitive
;
584 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
587 cpml_primitive_from_segment(&primitive
, &segment
);
588 cpml_primitive_put_extents(&primitive
, &extents
);
589 g_assert_true(extents
.is_defined
);
590 g_assert_cmpfloat(extents
.org
.x
, ==, 0);
591 g_assert_cmpfloat(extents
.org
.y
, ==, 1);
592 g_assert_cmpfloat(extents
.size
.x
, ==, 3);
593 g_assert_cmpfloat(extents
.size
.y
, ==, 0);
595 /* Arc: the extents are computed precisely... let's ensure
596 * at least all the 3 points are included */
597 cpml_primitive_next(&primitive
);
598 cpml_primitive_put_extents(&primitive
, &extents
);
599 g_assert_true(extents
.is_defined
);
600 g_assert_cmpfloat(extents
.org
.x
, <=, 3);
601 g_assert_cmpfloat(extents
.org
.y
, <=, 1);
602 g_assert_cmpfloat(extents
.size
.x
, >=, 3);
603 g_assert_cmpfloat(extents
.size
.y
, >=, 6);
605 /* Curve: actually the extents are computed by using the
606 * convex hull (hence the exact coordinates of the points) */
607 cpml_primitive_next(&primitive
);
608 cpml_primitive_put_extents(&primitive
, &extents
);
609 g_assert_true(extents
.is_defined
);
610 g_assert_cmpfloat(extents
.org
.x
, ==, -2);
611 g_assert_cmpfloat(extents
.org
.y
, ==, 2);
612 g_assert_cmpfloat(extents
.size
.x
, ==, 12);
613 g_assert_cmpfloat(extents
.size
.y
, ==, 9);
616 cpml_primitive_next(&primitive
);
617 cpml_primitive_put_extents(&primitive
, &extents
);
618 g_assert_true(extents
.is_defined
);
619 g_assert_cmpfloat(extents
.org
.x
, ==, -2);
620 g_assert_cmpfloat(extents
.org
.y
, ==, 1);
621 g_assert_cmpfloat(extents
.size
.x
, ==, 2);
622 g_assert_cmpfloat(extents
.size
.y
, ==, 1);
626 _cpml_method_put_pair_at(void)
629 CpmlPrimitive primitive
;
632 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
635 cpml_primitive_from_segment(&primitive
, &segment
);
636 cpml_primitive_put_pair_at(&primitive
, 0, &pair
);
637 g_assert_cmpfloat(pair
.x
, ==, 0);
638 g_assert_cmpfloat(pair
.y
, ==, 1);
639 cpml_primitive_put_pair_at(&primitive
, 1, &pair
);
640 g_assert_cmpfloat(pair
.x
, ==, 3);
641 g_assert_cmpfloat(pair
.y
, ==, 1);
642 cpml_primitive_put_pair_at(&primitive
, 0.5, &pair
);
643 g_assert_cmpfloat(pair
.x
, ==, 1.5);
644 g_assert_cmpfloat(pair
.y
, ==, 1);
647 cpml_primitive_next(&primitive
);
648 cpml_primitive_put_pair_at(&primitive
, 0, &pair
);
649 g_assert_cmpfloat(pair
.x
, ==, 3);
650 g_assert_cmpfloat(pair
.y
, ==, 1);
651 cpml_primitive_put_pair_at(&primitive
, 1, &pair
);
652 g_assert_cmpfloat(pair
.x
, ==, 6);
653 g_assert_cmpfloat(pair
.y
, ==, 7);
654 cpml_primitive_put_pair_at(&primitive
, 0.5, &pair
);
655 adg_assert_isapprox(pair
.x
, 3.669);
656 adg_assert_isapprox(pair
.y
, 4.415);
659 cpml_primitive_next(&primitive
);
660 /* TODO: not yet implemented
661 * cpml_primitive_put_pair_at(&primitive, 0, &pair);
662 * g_assert_cmpfloat(pair.x, ==, 6);
663 * g_assert_cmpfloat(pair.y, ==, 7);
664 * cpml_primitive_put_pair_at(&primitive, 1, &pair);
665 * g_assert_cmpfloat(pair.x, ==, -2);
666 * g_assert_cmpfloat(pair.y, ==, 2);
667 * cpml_primitive_put_pair_at(&primitive, 0.5, &pair);
668 * g_assert_cmpfloat(pair.x, ==, 1);
669 * g_assert_cmpfloat(pair.y, ==, 1); */
672 cpml_primitive_next(&primitive
);
673 cpml_primitive_put_pair_at(&primitive
, 0, &pair
);
674 g_assert_cmpfloat(pair
.x
, ==, -2);
675 g_assert_cmpfloat(pair
.y
, ==, 2);
676 cpml_primitive_put_pair_at(&primitive
, 1, &pair
);
677 g_assert_cmpfloat(pair
.x
, ==, 0);
678 g_assert_cmpfloat(pair
.y
, ==, 1);
679 cpml_primitive_put_pair_at(&primitive
, 0.5, &pair
);
680 g_assert_cmpfloat(pair
.x
, ==, -1);
681 g_assert_cmpfloat(pair
.y
, ==, 1.5);
685 _cpml_method_put_vector_at(void)
688 CpmlPrimitive primitive
;
691 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
694 cpml_primitive_from_segment(&primitive
, &segment
);
695 cpml_primitive_put_vector_at(&primitive
, 0, &vector
);
696 g_assert_cmpfloat(vector
.x
, ==, 3);
697 g_assert_cmpfloat(vector
.y
, ==, 0);
698 cpml_primitive_put_vector_at(&primitive
, 1, &vector
);
699 g_assert_cmpfloat(vector
.x
, ==, 3);
700 g_assert_cmpfloat(vector
.y
, ==, 0);
701 cpml_primitive_put_vector_at(&primitive
, 0.5, &vector
);
702 g_assert_cmpfloat(vector
.x
, ==, 3);
703 g_assert_cmpfloat(vector
.y
, ==, 0);
706 cpml_primitive_next(&primitive
);
707 cpml_primitive_put_vector_at(&primitive
, 0, &vector
);
708 adg_assert_isapprox(vector
.x
, -0.077);
709 adg_assert_isapprox(vector
.y
, 0.997);
710 cpml_primitive_put_vector_at(&primitive
, 1, &vector
);
711 adg_assert_isapprox(vector
.x
, 0.844);
712 adg_assert_isapprox(vector
.y
, 0.537);
713 cpml_primitive_put_vector_at(&primitive
, 0.5, &vector
);
714 adg_assert_isapprox(vector
.x
, 0.447);
715 adg_assert_isapprox(vector
.y
, 0.894);
718 cpml_primitive_next(&primitive
);
719 /* TODO: not yet implemented
720 * cpml_primitive_put_vector_at(&primitive, 0, &vector);
721 * g_assert_cmpfloat(vector.x, ==, 6);
722 * g_assert_cmpfloat(vector.y, ==, 7);
723 * cpml_primitive_put_vector_at(&primitive, 1, &vector);
724 * g_assert_cmpfloat(vector.x, ==, -2);
725 * g_assert_cmpfloat(vector.y, ==, 2);
726 * cpml_primitive_put_vector_at(&primitive, 0.5, &vector);
727 * g_assert_cmpfloat(vector.x, ==, 1);
728 * g_assert_cmpfloat(vector.y, ==, 1); */
731 cpml_primitive_next(&primitive
);
732 cpml_primitive_put_vector_at(&primitive
, 0, &vector
);
733 g_assert_cmpfloat(vector
.x
, ==, 2);
734 g_assert_cmpfloat(vector
.y
, ==, -1);
735 cpml_primitive_put_vector_at(&primitive
, 1, &vector
);
736 g_assert_cmpfloat(vector
.x
, ==, 2);
737 g_assert_cmpfloat(vector
.y
, ==, -1);
738 cpml_primitive_put_vector_at(&primitive
, 0.5, &vector
);
739 g_assert_cmpfloat(vector
.x
, ==, 2);
740 g_assert_cmpfloat(vector
.y
, ==, -1);
744 _cpml_method_get_closest_pos(void)
747 CpmlPrimitive primitive
;
750 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
751 cpml_primitive_from_segment(&primitive
, &segment
);
754 pair
.x
= 0; pair
.y
= 1;
755 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0);
756 pair
.x
= 3; pair
.y
= 1;
757 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 1);
758 pair
.x
= -1; pair
.y
= -1;
759 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0);
760 pair
.x
= 4; pair
.y
= 2;
761 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 1);
762 pair
.x
= 1.5; pair
.y
= 0;
763 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0.5);
766 cpml_primitive_next(&primitive
);
767 /* TODO: not yet implemented
768 * pair.x = 3; pair.y = 1;
769 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 0);
770 * pair.x = 6; pair.y = 7;
771 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 1);
772 * pair.x = 0; pair.y = 0;
773 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 0);
774 * pair.x = 10; pair.y = 10;
775 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 1);
779 cpml_primitive_next(&primitive
);
780 /* TODO: not yet implemented
781 * pair.x = 6; pair.y = 7;
782 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 0);
783 * pair.x = -2; pair.y = 2;
784 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 1);
785 * pair.x = 10; pair.y = 10;
786 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 0);
787 * pair.x = 0; pair.y = 0;
788 * g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive, &pair), ==, 1);
792 cpml_primitive_next(&primitive
);
793 pair
.x
= -2; pair
.y
= 2;
794 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0);
795 pair
.x
= 0; pair
.y
= 1;
796 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 1);
797 pair
.x
= -3; pair
.y
= 3;
798 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0);
799 pair
.x
= 1; pair
.y
= 0;
800 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 1);
801 pair
.x
= -1; pair
.y
= 1.5;
802 g_assert_cmpfloat(cpml_primitive_get_closest_pos(&primitive
, &pair
), ==, 0.5);
806 _cpml_method_set_point(void)
809 CpmlSegment original
, *segment
;
810 CpmlPrimitive primitive
;
811 CpmlPair pair
, pair2
;
814 /* Work on a copy to not modify the original path data */
815 cpml_segment_from_cairo(&original
, (cairo_path_t
*) adg_test_path());
816 data_size
= original
.num_data
* sizeof(cairo_path_data_t
);
817 segment
= cpml_segment_deep_dup(&original
);
820 cpml_primitive_from_segment(&primitive
, segment
);
822 equality
= memcmp(original
.data
, segment
->data
, data_size
);
823 g_assert_cmpint(equality
, ==, 0);
824 cpml_primitive_put_point(&primitive
, 0, &pair
);
826 cpml_primitive_set_point(&primitive
, 0, &pair
);
827 equality
= memcmp(original
.data
, segment
->data
, data_size
);
828 g_assert_cmpint(equality
, !=, 0);
830 cpml_primitive_set_point(&primitive
, 0, &pair
);
831 equality
= memcmp(original
.data
, segment
->data
, data_size
);
832 g_assert_cmpint(equality
, ==, 0);
833 cpml_primitive_put_point(&primitive
, 1, &pair
);
835 cpml_primitive_set_point(&primitive
, 1, &pair
);
836 equality
= memcmp(original
.data
, segment
->data
, data_size
);
837 g_assert_cmpint(equality
, !=, 0);
838 /* On a CPML_LINE primitives, -1 and 1 indices are equals */
839 cpml_primitive_put_point(&primitive
, -1, &pair2
);
840 g_assert_cmpfloat(pair
.x
, ==, pair2
.x
);
841 g_assert_cmpfloat(pair
.y
, ==, pair2
.y
);
842 memcpy(segment
->data
, original
.data
, data_size
);
843 equality
= memcmp(original
.data
, segment
->data
, data_size
);
844 g_assert_cmpint(equality
, ==, 0);
845 cpml_primitive_put_point(&primitive
, 2, &pair
);
848 /* This should be a NOP without segfaults */
849 cpml_primitive_set_point(&primitive
, 2, &pair
);
850 equality
= memcmp(original
.data
, segment
->data
, data_size
);
851 g_assert_cmpint(equality
, ==, 0);
853 /* From now on, memcpy() is assumed to force equality (as already
854 * proved by the previous assertions) and pair2 is used as a
855 * different-from-everything pair, that is setting pair2 on any
856 * point will break the equality between segment->data and
863 cpml_primitive_next(&primitive
);
865 cpml_primitive_set_point(&primitive
, 0, &pair2
);
866 equality
= memcmp(original
.data
, segment
->data
, data_size
);
867 g_assert_cmpint(equality
, !=, 0);
868 memcpy(segment
->data
, original
.data
, data_size
);
869 cpml_primitive_set_point(&primitive
, 1, &pair2
);
870 equality
= memcmp(original
.data
, segment
->data
, data_size
);
871 g_assert_cmpint(equality
, !=, 0);
872 memcpy(segment
->data
, original
.data
, data_size
);
873 cpml_primitive_set_point(&primitive
, 2, &pair2
);
874 equality
= memcmp(original
.data
, segment
->data
, data_size
);
875 g_assert_cmpint(equality
, !=, 0);
876 memcpy(segment
->data
, original
.data
, data_size
);
877 cpml_primitive_set_point(&primitive
, 3, &pair2
);
878 equality
= memcmp(original
.data
, segment
->data
, data_size
);
879 g_assert_cmpint(equality
, ==, 0);
882 cpml_primitive_next(&primitive
);
884 cpml_primitive_set_point(&primitive
, 0, &pair2
);
885 equality
= memcmp(original
.data
, segment
->data
, data_size
);
886 g_assert_cmpint(equality
, !=, 0);
887 memcpy(segment
->data
, original
.data
, data_size
);
888 cpml_primitive_set_point(&primitive
, 1, &pair2
);
889 equality
= memcmp(original
.data
, segment
->data
, data_size
);
890 g_assert_cmpint(equality
, !=, 0);
891 memcpy(segment
->data
, original
.data
, data_size
);
892 cpml_primitive_set_point(&primitive
, 2, &pair2
);
893 equality
= memcmp(original
.data
, segment
->data
, data_size
);
894 g_assert_cmpint(equality
, !=, 0);
895 memcpy(segment
->data
, original
.data
, data_size
);
896 cpml_primitive_set_point(&primitive
, 3, &pair2
);
897 equality
= memcmp(original
.data
, segment
->data
, data_size
);
898 g_assert_cmpint(equality
, !=, 0);
899 memcpy(segment
->data
, original
.data
, data_size
);
900 cpml_primitive_set_point(&primitive
, 4, &pair2
);
901 equality
= memcmp(original
.data
, segment
->data
, data_size
);
902 g_assert_cmpint(equality
, ==, 0);
905 cpml_primitive_next(&primitive
);
907 cpml_primitive_set_point(&primitive
, 0, &pair2
);
908 equality
= memcmp(original
.data
, segment
->data
, data_size
);
909 g_assert_cmpint(equality
, !=, 0);
910 memcpy(segment
->data
, original
.data
, data_size
);
911 cpml_primitive_set_point(&primitive
, 1, &pair2
);
912 equality
= memcmp(original
.data
, segment
->data
, data_size
);
913 g_assert_cmpint(equality
, !=, 0);
914 memcpy(segment
->data
, original
.data
, data_size
);
915 cpml_primitive_set_point(&primitive
, 2, &pair2
);
916 equality
= memcmp(original
.data
, segment
->data
, data_size
);
917 g_assert_cmpint(equality
, ==, 0);
923 _cpml_method_put_point(void)
926 CpmlPrimitive primitive
;
929 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
932 cpml_primitive_from_segment(&primitive
, &segment
);
934 cpml_primitive_put_point(&primitive
, 0, &pair
);
935 g_assert_cmpfloat(pair
.x
, ==, 0);
936 g_assert_cmpfloat(pair
.y
, ==, 1);
937 cpml_primitive_put_point(&primitive
, 1, &pair
);
938 g_assert_cmpfloat(pair
.x
, ==, 3);
939 g_assert_cmpfloat(pair
.y
, ==, 1);
940 cpml_primitive_put_point(&primitive
, 2, &pair
);
941 g_assert_cmpfloat(pair
.x
, ==, 3);
942 g_assert_cmpfloat(pair
.y
, ==, 1);
943 /* The negative indices are checked only against CPML_LINE */
944 cpml_primitive_put_point(&primitive
, -1, &pair
);
945 g_assert_cmpfloat(pair
.x
, ==, 3);
946 g_assert_cmpfloat(pair
.y
, ==, 1);
947 cpml_primitive_put_point(&primitive
, -2, &pair
);
948 g_assert_cmpfloat(pair
.x
, ==, 0);
949 g_assert_cmpfloat(pair
.y
, ==, 1);
950 cpml_primitive_put_point(&primitive
, -3, &pair
);
951 g_assert_cmpfloat(pair
.x
, ==, 0);
952 g_assert_cmpfloat(pair
.y
, ==, 1);
955 cpml_primitive_next(&primitive
);
957 cpml_primitive_put_point(&primitive
, 0, &pair
);
958 g_assert_cmpfloat(pair
.x
, ==, 3);
959 g_assert_cmpfloat(pair
.y
, ==, 1);
960 cpml_primitive_put_point(&primitive
, 1, &pair
);
961 g_assert_cmpfloat(pair
.x
, ==, 4);
962 g_assert_cmpfloat(pair
.y
, ==, 5);
963 cpml_primitive_put_point(&primitive
, 2, &pair
);
964 g_assert_cmpfloat(pair
.x
, ==, 6);
965 g_assert_cmpfloat(pair
.y
, ==, 7);
966 cpml_primitive_put_point(&primitive
, 3, &pair
);
967 g_assert_cmpfloat(pair
.x
, ==, 6);
968 g_assert_cmpfloat(pair
.y
, ==, 7);
971 cpml_primitive_next(&primitive
);
973 cpml_primitive_put_point(&primitive
, 0, &pair
);
974 g_assert_cmpfloat(pair
.x
, ==, 6);
975 g_assert_cmpfloat(pair
.y
, ==, 7);
976 cpml_primitive_put_point(&primitive
, 1, &pair
);
977 g_assert_cmpfloat(pair
.x
, ==, 8);
978 g_assert_cmpfloat(pair
.y
, ==, 9);
979 cpml_primitive_put_point(&primitive
, 2, &pair
);
980 g_assert_cmpfloat(pair
.x
, ==, 10);
981 g_assert_cmpfloat(pair
.y
, ==, 11);
982 cpml_primitive_put_point(&primitive
, 3, &pair
);
983 g_assert_cmpfloat(pair
.x
, ==, -2);
984 g_assert_cmpfloat(pair
.y
, ==, 2);
985 cpml_primitive_put_point(&primitive
, 4, &pair
);
986 g_assert_cmpfloat(pair
.x
, ==, -2);
987 g_assert_cmpfloat(pair
.y
, ==, 2);
990 cpml_primitive_next(&primitive
);
992 cpml_primitive_put_point(&primitive
, 0, &pair
);
993 g_assert_cmpfloat(pair
.x
, ==, -2);
994 g_assert_cmpfloat(pair
.y
, ==, 2);
995 cpml_primitive_put_point(&primitive
, 1, &pair
);
996 g_assert_cmpfloat(pair
.x
, ==, 0);
997 g_assert_cmpfloat(pair
.y
, ==, 1);
998 cpml_primitive_put_point(&primitive
, 2, &pair
);
999 g_assert_cmpfloat(pair
.x
, ==, 0);
1000 g_assert_cmpfloat(pair
.y
, ==, 1);
1004 _cpml_method_put_intersections(void)
1006 CpmlSegment segment
;
1007 CpmlPrimitive primitive1
, primitive2
;
1010 /* Set primitive1 to 1.1 (first segment, first primitive) */
1011 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
1012 cpml_primitive_from_segment(&primitive1
, &segment
);
1014 /* Set primitive2 to 2.1 (second segment, first primitive) */
1015 cpml_segment_next(&segment
);
1016 cpml_primitive_from_segment(&primitive2
, &segment
);
1018 /* primitive1 (1.1) does not intersect primitive2 (2.1) */
1019 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, pair
), ==, 0);
1021 cpml_primitive_next(&primitive2
);
1023 /* primitive1 (1.1) intersects primitive2 (2.2) in (1, 1) */
1024 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, pair
), ==, 1);
1025 g_assert_cmpfloat(pair
[0].x
, ==, 1);
1026 g_assert_cmpfloat(pair
[0].y
, ==, 1);
1027 g_assert_cmpint(cpml_primitive_is_inside(&primitive1
, pair
), ==, 1);
1028 g_assert_cmpint(cpml_primitive_is_inside(&primitive2
, pair
), ==, 1);
1030 /* Check the intersection is not returned when not requested */
1031 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 0, pair
), ==, 0);
1033 cpml_primitive_next(&primitive1
);
1035 /* primitive1 (1.2) does not intersect primitive2 (2.2) */
1036 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, pair
), ==, 0);
1038 cpml_primitive_next(&primitive1
);
1040 /* primitive1 (1.3) does not intersect primitive2 (2.2) */
1041 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, pair
), ==, 0);
1043 cpml_primitive_next(&primitive1
);
1045 /* primitive1 (1.4) intersects primitive2 (2.2), but outside their boundaries */
1046 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1
, &primitive2
, 2, pair
), ==, 1);
1047 g_assert_cmpfloat(pair
[0].x
, ==, 1);
1048 g_assert_cmpfloat(pair
[0].y
, ==, -1);
1049 g_assert_cmpint(cpml_primitive_is_inside(&primitive1
, pair
), ==, 0);
1050 g_assert_cmpint(cpml_primitive_is_inside(&primitive2
, pair
), ==, 0);
1054 _cpml_method_put_intersections_with_segment(void)
1056 CpmlSegment segment
;
1057 CpmlPrimitive primitive
;
1060 /* Set primitive to first segment, first primitive */
1061 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
1062 cpml_primitive_from_segment(&primitive
, &segment
);
1064 /* Set segment to the second segment */
1065 cpml_segment_next(&segment
);
1067 /* primitive (1.1) intersects segment (2) in (1, 1) */
1068 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive
, &segment
, 4, pair
), ==, 1);
1069 g_assert_cmpfloat(pair
[0].x
, ==, 1);
1070 g_assert_cmpfloat(pair
[0].y
, ==, 1);
1072 cpml_primitive_next(&primitive
);
1074 /* primitive (1.1) does not intersect segment (2) */
1075 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive
, &segment
, 4, pair
), ==, 0);
1077 /* Set primitive to second segment, first primitive */
1078 cpml_primitive_from_segment(&primitive
, &segment
);
1080 /* Set segment to the first segment */
1081 cpml_segment_reset(&segment
);
1083 /* primitive (2.1) intersects segment (1) in extrapolation.
1084 * TODO: change this behavior! They must not intersect. */
1085 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive
, &segment
, 4, pair
), ==, 1);
1086 g_assert_cmpfloat(pair
[0].x
, ==, 2);
1087 g_assert_cmpfloat(pair
[0].y
, ==, 0);
1089 cpml_primitive_next(&primitive
);
1091 /* primitive (2.2) wrongly intersects segment (1) */
1092 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive
, &segment
, 4, pair
), ==, 1);
1093 g_assert_cmpfloat(pair
[0].x
, ==, 2);
1094 g_assert_cmpfloat(pair
[0].y
, ==, 0);
1098 _cpml_method_offset(void)
1100 CpmlSegment original
, *segment
;
1101 CpmlPrimitive primitive
, line
, curve
;
1102 CpmlPrimitive
*backup
;
1104 /* Work on a copy to avoid modifying the original cairo path */
1105 cpml_segment_from_cairo(&original
, (cairo_path_t
*) adg_test_path());
1106 segment
= cpml_segment_deep_dup(&original
);
1107 cpml_primitive_from_segment(&primitive
, segment
);
1109 /* Offsetting and de-offsetting can introduce rounding errors
1110 * so we use adg_assert_isapprox instead of g_assert_cmpfloat */
1113 cpml_primitive_copy(&line
, &primitive
);
1114 cpml_primitive_offset(&primitive
, 1);
1115 adg_assert_isapprox((primitive
.org
)->point
.x
, 0);
1116 adg_assert_isapprox((primitive
.org
)->point
.y
, 2);
1117 adg_assert_isapprox(primitive
.data
[1].point
.x
, 3);
1118 adg_assert_isapprox(primitive
.data
[1].point
.y
, 2);
1119 cpml_primitive_offset(&primitive
, -1);
1120 adg_assert_isapprox((primitive
.org
)->point
.x
, 0);
1121 adg_assert_isapprox((primitive
.org
)->point
.y
, 1);
1122 adg_assert_isapprox(primitive
.data
[1].point
.x
, 3);
1123 adg_assert_isapprox(primitive
.data
[1].point
.y
, 1);
1126 cpml_primitive_next(&primitive
);
1127 cpml_primitive_offset(&primitive
, 1);
1128 adg_assert_isapprox((primitive
.org
)->point
.x
, 2.003);
1129 adg_assert_isapprox((primitive
.org
)->point
.y
, 0.923);
1130 adg_assert_isapprox(primitive
.data
[1].point
.x
, 3.156);
1131 adg_assert_isapprox(primitive
.data
[1].point
.y
, 5.537);
1132 adg_assert_isapprox(primitive
.data
[2].point
.x
, 5.463);
1133 adg_assert_isapprox(primitive
.data
[2].point
.y
, 7.844);
1134 cpml_primitive_offset(&primitive
, -1);
1135 adg_assert_isapprox((primitive
.org
)->point
.x
, 3);
1136 adg_assert_isapprox((primitive
.org
)->point
.y
, 1);
1137 adg_assert_isapprox(primitive
.data
[1].point
.x
, 4);
1138 adg_assert_isapprox(primitive
.data
[1].point
.y
, 5);
1139 adg_assert_isapprox(primitive
.data
[2].point
.x
, 6);
1140 adg_assert_isapprox(primitive
.data
[2].point
.y
, 7);
1143 cpml_primitive_next(&primitive
);
1144 cpml_primitive_copy(&curve
, &primitive
);
1145 /* The offset algorithm for curves is an approximation, so
1146 * offsetting +1 and -1 does not return the original curve.
1147 * Keeping a backup around to restore the original data.
1149 backup
= cpml_primitive_deep_dup(&curve
);
1150 /* Testing different algorithms */
1151 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL
);
1152 cpml_primitive_offset(&primitive
, 1);
1153 adg_assert_isapprox((primitive
.org
)->point
.x
, 5.293);
1154 adg_assert_isapprox((primitive
.org
)->point
.y
, 7.707);
1155 adg_assert_isapprox(primitive
.data
[1].point
.x
, 7.889);
1156 adg_assert_isapprox(primitive
.data
[1].point
.y
, 8.515);
1157 adg_assert_isapprox(primitive
.data
[2].point
.x
, 11.196);
1158 adg_assert_isapprox(primitive
.data
[2].point
.y
, 9.007);
1159 adg_assert_isapprox(primitive
.data
[3].point
.x
, -1.4);
1160 adg_assert_isapprox(primitive
.data
[3].point
.y
, 1.2);
1161 cpml_primitive_copy_data(&primitive
, backup
);
1162 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_BAIOCA
);
1163 cpml_primitive_offset(&primitive
, 1);
1164 adg_assert_isapprox((primitive
.org
)->point
.x
, 5.293);
1165 adg_assert_isapprox((primitive
.org
)->point
.y
, 7.707);
1166 adg_assert_isapprox(primitive
.data
[1].point
.x
, 6.901);
1167 adg_assert_isapprox(primitive
.data
[1].point
.y
, 9.315);
1168 adg_assert_isapprox(primitive
.data
[2].point
.x
, 10.806);
1169 adg_assert_isapprox(primitive
.data
[2].point
.y
, 10.355);
1170 adg_assert_isapprox(primitive
.data
[3].point
.x
, -1.4);
1171 adg_assert_isapprox(primitive
.data
[3].point
.y
, 1.2);
1172 cpml_primitive_copy_data(&primitive
, backup
);
1173 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT
);
1174 cpml_primitive_offset(&primitive
, 1);
1175 adg_assert_isapprox((primitive
.org
)->point
.x
, 5.293);
1176 adg_assert_isapprox((primitive
.org
)->point
.y
, 7.707);
1177 adg_assert_isapprox(primitive
.data
[1].point
.x
, -5.758);
1178 adg_assert_isapprox(primitive
.data
[1].point
.y
, -3.344);
1179 adg_assert_isapprox(primitive
.data
[2].point
.x
, 24.987);
1180 adg_assert_isapprox(primitive
.data
[2].point
.y
, 20.99);
1181 adg_assert_isapprox(primitive
.data
[3].point
.x
, -1.4);
1182 adg_assert_isapprox(primitive
.data
[3].point
.y
, 1.2);
1183 cpml_primitive_copy_data(&primitive
, backup
);
1186 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_DEFAULT
);
1188 /* Close: this primitive does not own data points but should
1189 * modify the points of the previous and next primitives */
1190 cpml_primitive_next(&primitive
);
1191 cpml_primitive_offset(&primitive
, 1);
1192 adg_assert_isapprox((curve
.org
)->point
.x
, 6);
1193 adg_assert_isapprox((curve
.org
)->point
.y
, 7);
1194 adg_assert_isapprox(curve
.data
[3].point
.x
, -1.553);
1195 adg_assert_isapprox(curve
.data
[3].point
.y
, 2.894);
1196 adg_assert_isapprox((line
.org
)->point
.x
, 0.447);
1197 adg_assert_isapprox((line
.org
)->point
.y
, 1.894);
1198 adg_assert_isapprox(line
.data
[1].point
.x
, 3);
1199 adg_assert_isapprox(line
.data
[1].point
.y
, 1);
1200 cpml_primitive_offset(&primitive
, -1);
1201 adg_assert_isapprox((curve
.org
)->point
.x
, 6);
1202 adg_assert_isapprox((curve
.org
)->point
.y
, 7);
1203 adg_assert_isapprox(curve
.data
[3].point
.x
, -2);
1204 adg_assert_isapprox(curve
.data
[3].point
.y
, 2);
1205 adg_assert_isapprox((line
.org
)->point
.x
, 0);
1206 adg_assert_isapprox((line
.org
)->point
.y
, 1);
1207 adg_assert_isapprox(line
.data
[1].point
.x
, 3);
1208 adg_assert_isapprox(line
.data
[1].point
.y
, 1);
1214 _cpml_method_join(void)
1216 cairo_path_data_t path_data
[] = {
1217 { .header
= { CPML_MOVE
, 2 }},
1218 { .point
= { 0, 0 }},
1220 { .header
= { CPML_LINE
, 2 }},
1221 { .point
= { 2, 0 }},
1223 { .header
= { CPML_LINE
, 2 }},
1224 { .point
= { 2, 2 }},
1226 { .header
= { CPML_LINE
, 2 }},
1227 { .point
= { 1, 2 }},
1229 { .header
= { CPML_LINE
, 2 }},
1230 { .point
= { 1, -2 }}
1232 cairo_path_t path
= {
1233 CAIRO_STATUS_SUCCESS
,
1235 G_N_ELEMENTS(path_data
)
1237 CpmlSegment segment
;
1238 CpmlPrimitive primitive1
, primitive2
;
1240 cpml_segment_from_cairo(&segment
, &path
);
1242 cpml_primitive_from_segment(&primitive1
, &segment
);
1243 cpml_primitive_copy(&primitive2
, &primitive1
);
1244 cpml_primitive_next(&primitive2
);
1246 /* primitive1 and primitive2 are already joint */
1247 g_assert_cmpint(cpml_primitive_join(&primitive1
, &primitive2
), ==, 1);
1248 g_assert_cmpfloat((primitive2
.org
)->point
.x
, ==, 2);
1249 g_assert_cmpfloat((primitive2
.org
)->point
.y
, ==, 0);
1251 cpml_primitive_next(&primitive2
);
1252 /* Now primitive1 and primitive2 are divergent,
1253 * hence cannot be joined */
1254 g_assert_cmpint(cpml_primitive_join(&primitive1
, &primitive2
), ==, 0);
1256 cpml_primitive_next(&primitive2
);
1257 g_assert_cmpint(cpml_primitive_join(&primitive1
, &primitive2
), ==, 1);
1258 g_assert_cmpfloat((primitive2
.org
)->point
.x
, ==, 1);
1259 g_assert_cmpfloat((primitive2
.org
)->point
.y
, ==, 0);
1263 _cpml_method_to_cairo(void)
1266 CpmlSegment segment
;
1267 CpmlPrimitive primitive
;
1268 int length
, last_length
;
1270 cr
= adg_test_cairo_context();
1271 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
1272 cpml_primitive_from_segment(&primitive
, &segment
);
1274 g_assert_cmpint(adg_test_cairo_num_data(cr
), ==, 0);
1278 last_length
= length
;
1279 cpml_primitive_to_cairo(&primitive
, cr
);
1280 length
= adg_test_cairo_num_data(cr
);
1281 g_assert_cmpint(length
, >, last_length
);
1282 } while (cpml_primitive_next(&primitive
));
1288 _cpml_method_dump(gint i
)
1290 CpmlSegment segment
;
1291 CpmlPrimitive primitive
;
1295 cpml_segment_from_cairo(&segment
, (cairo_path_t
*) adg_test_path());
1296 cpml_primitive_from_segment(&primitive
, &segment
);
1297 cpml_primitive_dump(&primitive
, 1);
1299 cpml_primitive_next(&primitive
);
1300 cpml_primitive_dump(&primitive
, 1);
1303 g_test_trap_assert_passed();
1304 g_test_trap_assert_stderr_unmatched("?");
1305 g_test_trap_assert_stdout("*NULL*");
1306 g_test_trap_assert_stdout("*move*");
1307 g_test_trap_assert_stdout("*line*");
1313 main(int argc
, char *argv
[])
1315 adg_test_init(&argc
, &argv
);
1317 g_test_add_func("/cpml/primitive/behavior/browsing", _cpml_behavior_browsing
);
1319 adg_test_add_traps("/cpml/primitive/sanity/from-segment", _cpml_sanity_from_segment
, 1);
1320 adg_test_add_traps("/cpml/primitive/sanity/copy", _cpml_sanity_copy
, 2);
1321 adg_test_add_traps("/cpml/primitive/sanity/copy-data", _cpml_sanity_copy_data
, 2);
1322 adg_test_add_traps("/cpml/primitive/sanity/get-n-points", _cpml_sanity_get_n_points
, 1);
1323 adg_test_add_traps("/cpml/primitive/sanity/get-length", _cpml_sanity_get_length
, 1);
1324 adg_test_add_traps("/cpml/primitive/sanity/put-extents", _cpml_sanity_put_extents
, 2);
1325 adg_test_add_traps("/cpml/primitive/sanity/put-pair-at", _cpml_sanity_put_pair_at
, 2);
1326 adg_test_add_traps("/cpml/primitive/sanity/put-vector-at", _cpml_sanity_put_vector_at
, 2);
1327 adg_test_add_traps("/cpml/primitive/sanity/get-closest-pos", _cpml_sanity_get_closest_pos
, 2);
1328 adg_test_add_traps("/cpml/primitive/sanity/set-point", _cpml_sanity_set_point
, 2);
1329 adg_test_add_traps("/cpml/primitive/sanity/put-point", _cpml_sanity_put_point
, 2);
1330 adg_test_add_traps("/cpml/primitive/sanity/put-intersections", _cpml_sanity_put_intersections
, 3);
1331 adg_test_add_traps("/cpml/primitive/sanity/put-intersections-with-segment", _cpml_sanity_put_intersections_with_segment
, 3);
1332 adg_test_add_traps("/cpml/primitive/sanity/offset", _cpml_sanity_offset
, 1);
1333 adg_test_add_traps("/cpml/primitive/sanity/join", _cpml_sanity_join
, 2);
1334 adg_test_add_traps("/cpml/primitive/sanity/to-cairo", _cpml_sanity_to_cairo
, 2);
1335 adg_test_add_traps("/cpml/primitive/sanity/dump", _cpml_sanity_dump
, 1);
1337 g_test_add_func("/cpml/primitive/method/from-segment", _cpml_method_from_segment
);
1338 g_test_add_func("/cpml/primitive/method/copy", _cpml_method_copy
);
1339 g_test_add_func("/cpml/primitive/method/copy-data", _cpml_method_copy_data
);
1340 g_test_add_func("/cpml/primitive/method/type-get-n-points", _cpml_method_type_get_n_points
);
1341 g_test_add_func("/cpml/primitive/method/get-n-points", _cpml_method_get_n_points
);
1342 g_test_add_func("/cpml/primitive/method/get-length", _cpml_method_get_length
);
1343 g_test_add_func("/cpml/primitive/method/put-extents", _cpml_method_put_extents
);
1344 g_test_add_func("/cpml/primitive/method/put-pair-at", _cpml_method_put_pair_at
);
1345 g_test_add_func("/cpml/primitive/method/put-vector-at", _cpml_method_put_vector_at
);
1346 g_test_add_func("/cpml/primitive/method/get-closest-pos", _cpml_method_get_closest_pos
);
1347 g_test_add_func("/cpml/primitive/method/set-point", _cpml_method_set_point
);
1348 g_test_add_func("/cpml/primitive/method/put-point", _cpml_method_put_point
);
1349 g_test_add_func("/cpml/primitive/method/put-intersections", _cpml_method_put_intersections
);
1350 g_test_add_func("/cpml/primitive/method/put-intersections-with-segment", _cpml_method_put_intersections_with_segment
);
1351 g_test_add_func("/cpml/primitive/method/offset", _cpml_method_offset
);
1352 g_test_add_func("/cpml/primitive/method/join", _cpml_method_join
);
1353 g_test_add_func("/cpml/primitive/method/to-cairo", _cpml_method_to_cairo
);
1354 adg_test_add_traps("/cpml/primitive/method/dump", _cpml_method_dump
, 1);
1356 return g_test_run();