1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008, 2009 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.
22 * @title: CpmlPrimitive
23 * @short_description: Basic component of segments
25 * A primitive is an atomic geometric element found inside #CpmlSegment.
26 * The available primitives are defined in the #cairo_path_data_type_t
27 * enum, excluding %CAIRO_PATH_MOVE_TO, as it is not considered a valid
28 * primitive and it is managed in different way (the moveto primitives
29 * are used to set the origin of the first primitive in a segment).
34 * @segment: the source #CpmlSegment
35 * @org: a pointer to the first point of the primitive
36 * @data: the array of the path data, prepended by the header
38 * As for #CpmlSegment, also the primitive is unobtrusive. This
39 * means CpmlPrimitive does not include any coordinates but instead
40 * keeps pointers to the original segment (and, by transition, to
41 * the underlying #cairo_path_t struct).
44 #include "cpml-primitive.h"
45 #include "cpml-line.h"
47 #include "cpml-curve.h"
48 #include "cpml-close.h"
54 static void dump_cairo_point (const cairo_path_data_t
*path_data
);
58 * cpml_primitive_copy:
59 * @primitive: the destination #CpmlPrimitive
60 * @src: the source #CpmlPrimitive
62 * Copies @src in @primitive.
64 * Return value: @primitive
67 cpml_primitive_copy(CpmlPrimitive
*primitive
, const CpmlPrimitive
*src
)
69 return memcpy(primitive
, src
, sizeof(CpmlPrimitive
));
73 * cpml_primitive_from_segment:
74 * @primitive: the destination #CpmlPrimitive struct
75 * @segment: the source segment
77 * Initializes @primitive to the first primitive of @segment.
79 * Return value: @primitive
82 cpml_primitive_from_segment(CpmlPrimitive
*primitive
, CpmlSegment
*segment
)
84 primitive
->segment
= segment
;
86 /* The first element of a CpmlSegment is always a CAIRO_PATH_MOVE_TO,
87 * as ensured by cpml_segment_from_cairo() and by the browsing APIs,
88 * so the origin is in the second data item */
89 primitive
->org
= &segment
->data
[1];
91 /* Also, the segment APIs ensure that @segment is prepended by
92 * only one CAIRO_PATH_MOVE_TO */
93 primitive
->data
= segment
->data
+ 2;
99 * cpml_primitive_reset:
100 * @primitive: a #CpmlPrimitive
102 * Resets @primitive so it refers to the first primitive of the
106 cpml_primitive_reset(CpmlPrimitive
*primitive
)
108 primitive
->org
= &primitive
->segment
->data
[1];
109 primitive
->data
= primitive
->segment
->data
+ 2;
113 * cpml_primitive_next:
114 * @primitive: a #CpmlPrimitive
117 * Changes @primitive so it refers to the next primitive on the
118 * source segment. If there are no more primitives, @primitive is
119 * not changed and 0 is returned.
121 * Return value: 1 on success, 0 if no next primitive found or errors
124 cpml_primitive_next(CpmlPrimitive
*primitive
)
126 cairo_path_data_t
*new_data
;
128 new_data
= primitive
->data
+ primitive
->data
->header
.length
;
129 if (new_data
- primitive
->segment
->data
>= primitive
->segment
->num_data
)
132 primitive
->org
= cpml_primitive_get_point(primitive
, -1);
133 primitive
->data
= new_data
;
139 * cpml_primitive_get_npoints:
140 * @primitive: a #CpmlPrimitive
142 * Gets the number of points required to identify @primitive.
143 * It is similar to cpml_primitive_type_get_npoints() but using
144 * a @primitive instance instead of a type.
146 * Return value: the number of points or -1 on errors
149 cpml_primitive_get_npoints(const CpmlPrimitive
*primitive
)
151 return cpml_primitive_type_get_npoints(primitive
->data
->header
.type
);
155 * cpml_primitive_get_point:
156 * @primitive: a #CpmlPrimitive
157 * @npoint: the index of the point to retrieve
159 * Gets the specified @npoint from @primitive. The index starts
160 * at 0: if @npoint is 0, the start point (the origin) is
161 * returned, 1 for the second point and so on. If @npoint is
162 * negative, it is considered as a negative index from the end,
163 * so that -1 is the end point, -2 the point before the end point
166 * %CAIRO_PATH_CLOSE_PATH is managed in a special way: if @npoint
167 * is -1 or 1 and @primitive is a close-path, this function cycles
168 * the source #CpmlSegment and returns the first point. This is
169 * needed because requesting the end point (or the second point)
170 * of a close path is a valid operation and must returns the start
173 * Return value: a pointer to the requested point (in cairo format)
174 * or %NULL if the point is outside the valid range
177 cpml_primitive_get_point(const CpmlPrimitive
*primitive
, int npoint
)
181 /* For a start point request, simply return the origin
182 * without further checking */
184 return primitive
->org
;
186 /* The CAIRO_PATH_CLOSE_PATH special case */
187 if (primitive
->data
->header
.type
== CAIRO_PATH_CLOSE_PATH
&&
188 (npoint
== 1 || npoint
== -1))
189 return &primitive
->segment
->data
[1];
191 npoints
= cpml_primitive_get_npoints(primitive
);
195 /* If npoint is negative, consider it as a negative index from the end */
197 npoint
= npoints
+ npoint
;
199 /* Out of range condition */
200 if (npoint
< 0 || npoint
>= npoints
)
203 return npoint
== 0 ? primitive
->org
: &primitive
->data
[npoint
];
207 * cpml_primitive_to_cairo:
208 * @primitive: a #CpmlPrimitive
209 * @cr: the destination cairo context
211 * Renders a single @primitive to the @cr cairo context.
212 * As a special case, if the primitive is a #CAIRO_PATH_CLOSE_PATH,
213 * an equivalent line is rendered, because a close path left alone
216 * Also a #CAIRO_PATH_ARC_TO primitive is treated specially, as it
217 * is not natively supported by cairo and has its own rendering API.
220 cpml_primitive_to_cairo(const CpmlPrimitive
*primitive
, cairo_t
*cr
)
223 cairo_path_data_t
*path_data
;
225 cairo_move_to(cr
, primitive
->org
->point
.x
, primitive
->org
->point
.y
);
227 if (primitive
->data
->header
.type
== CAIRO_PATH_CLOSE_PATH
) {
228 path_data
= cpml_primitive_get_point(primitive
, -1);
229 cairo_line_to(cr
, path_data
->point
.x
, path_data
->point
.y
);
230 } else if (primitive
->data
->header
.type
== CAIRO_PATH_ARC_TO
) {
231 cpml_arc_to_cairo(primitive
, cr
);
233 path
.status
= CAIRO_STATUS_SUCCESS
;
234 path
.data
= primitive
->data
;
235 path
.num_data
= primitive
->data
->header
.length
;
236 cairo_append_path(cr
, &path
);
241 * cpml_primitive_dump:
242 * @primitive: a #CpmlPrimitive
243 * @org_also: whether to output also the origin coordinates
245 * Dumps info on the specified @primitive to stdout: useful for
246 * debugging purposes. If @org_also is 1, a %CAIRO_PATH_MOVE_TO
247 * to the origin is prepended to the data otherwise the
248 * <structfield>org</structfield> field is not used.
251 cpml_primitive_dump(const CpmlPrimitive
*primitive
, cairo_bool_t org_also
)
253 const cairo_path_data_t
*data
;
254 int type
, n
, npoints
;
256 data
= primitive
->data
;
257 type
= data
->header
.type
;
258 npoints
= cpml_primitive_get_npoints(primitive
);
260 printf("Unhandled primitive type (%d)\n", type
);
264 /* Dump the origin movement, if requested */
267 dump_cairo_point(primitive
->org
);
273 case CAIRO_PATH_LINE_TO
:
277 case CAIRO_PATH_ARC_TO
:
281 case CAIRO_PATH_CURVE_TO
:
285 case CAIRO_PATH_CLOSE_PATH
:
286 printf("Path close");
290 printf("Unknown primitive (type = %d)", type
);
294 for (n
= 1; n
< npoints
; ++n
)
295 dump_cairo_point(cpml_primitive_get_point(primitive
, n
));
301 * cpml_primitive_intersection_with_segment:
302 * @primitive: a #CpmlPrimitive
303 * @segment: a #CpmlSegment
304 * @dest: the destination vector of #CpmlPair
305 * @max: maximum number of intersections to return
307 * Computes the intersections between @segment and @primitive by
308 * sequentially scanning the primitives in @segment and looking
309 * for intersections with @primitive.
310 * If the intersections are more than @max, only the first @max pairs
311 * are stored in @dest.
313 * Return value: the number of intersections found
316 cpml_primitive_intersection_with_segment(const CpmlPrimitive
*primitive
,
317 const CpmlSegment
*segment
,
318 CpmlPair
*dest
, int max
)
320 CpmlPrimitive portion
;
322 CpmlPair tmp_pairs
[4];
324 cpml_primitive_from_segment(&portion
, (CpmlSegment
*) segment
);
328 partial
= cpml_primitive_intersection(&portion
, primitive
, tmp_pairs
);
329 if (total
+ partial
> max
)
330 partial
= max
- total
;
333 memcpy(dest
+ total
, tmp_pairs
, partial
* sizeof(CpmlPair
));
336 } while (total
< max
&& cpml_primitive_next(&portion
));
343 * cpml_primitive_type_get_npoints:
344 * @type: a primitive type
346 * Gets the number of points required to identify the @type primitive.
349 * This function is primitive dependent, that is every primitive has
350 * its own implementation.
353 * Return value: the number of points or -1 on errors
356 cpml_primitive_type_get_npoints(cairo_path_data_type_t type
)
360 case CAIRO_PATH_LINE_TO
:
361 return cpml_line_type_get_npoints();
363 case CAIRO_PATH_ARC_TO
:
364 return cpml_arc_type_get_npoints();
366 case CAIRO_PATH_CURVE_TO
:
367 return cpml_curve_type_get_npoints();
369 case CAIRO_PATH_CLOSE_PATH
:
370 return cpml_close_type_get_npoints();
380 * cpml_primitive_pair_at:
381 * @primitive: a #CpmlPrimitive
382 * @pair: the destination #CpmlPair
383 * @pos: the position value
385 * Abstracts the pair_at() family functions by providing a common
386 * way to access the underlying primitive-specific implementation.
388 * It gets the coordinates of the point lying on @primitive
389 * at position @pos. @pos is an homogeneous factor where 0 is the
390 * start point, 1 the end point, 0.5 the mid point and so on.
391 * The relation 0 < @pos < 1 should be satisfied, although some
392 * primitives accept value outside this range.
395 * This function is primitive dependent, that is every primitive has
396 * its own implementation.
400 cpml_primitive_pair_at(const CpmlPrimitive
*primitive
,
401 CpmlPair
*pair
, double pos
)
403 switch (primitive
->data
->header
.type
) {
405 case CAIRO_PATH_LINE_TO
:
406 cpml_line_pair_at(primitive
, pair
, pos
);
409 case CAIRO_PATH_ARC_TO
:
410 cpml_arc_pair_at(primitive
, pair
, pos
);
413 case CAIRO_PATH_CURVE_TO
:
414 cpml_curve_pair_at(primitive
, pair
, pos
);
417 case CAIRO_PATH_CLOSE_PATH
:
418 cpml_close_pair_at(primitive
, pair
, pos
);
427 * cpml_primitive_vector_at:
428 * @primitive: a #CpmlPrimitive
429 * @vector: the destination #CpmlVector
430 * @pos: the position value
432 * Abstracts the vector_at() family functions by providing a common
433 * way to access the underlying primitive-specific implementation.
435 * It gets the steepness of the point at position @pos on @primitive.
436 * @pos is an homogeneous factor where 0 is the start point, 1 the
437 * end point, 0.5 the mid point and so on.
438 * The relation 0 < @pos < 1 should be satisfied, although some
439 * primitives accept value outside this range.
442 * This function is primitive dependent, that is every primitive has
443 * its own implementation.
447 cpml_primitive_vector_at(const CpmlPrimitive
*primitive
,
448 CpmlVector
*vector
, double pos
)
450 switch (primitive
->data
->header
.type
) {
452 case CAIRO_PATH_LINE_TO
:
453 cpml_line_vector_at(primitive
, vector
, pos
);
456 case CAIRO_PATH_ARC_TO
:
457 cpml_arc_vector_at(primitive
, vector
, pos
);
460 case CAIRO_PATH_CURVE_TO
:
461 cpml_curve_vector_at(primitive
, vector
, pos
);
464 case CAIRO_PATH_CLOSE_PATH
:
465 cpml_close_vector_at(primitive
, vector
, pos
);
474 * cpml_primitive_join:
475 * @primitive: the first #CpmlPrimitive
476 * @primitive2: the second #CpmlPrimitive
478 * Joins two primitive modifying the end point of @primitive and the
479 * start point of @primitive2 so that the resulting points will overlap.
482 * <title>TODO</title>
484 * <listitem>Actually, the join is done by extending the end vector
485 * of @primitive and the start vector of @primitive2 and
486 * interpolating the intersection: this means no primitive
487 * dependent code is needed. Anyway, it is likely to change
488 * in the future because this approach is quite naive when
489 * curves are involved.</listitem>
493 * Return value: 1 on success, 0 if the end vector of @primitive
494 * and the start vector of @primitive2 are parallel
497 cpml_primitive_join(CpmlPrimitive
*primitive
, CpmlPrimitive
*primitive2
)
499 cairo_path_data_t
*end1
, *start2
;
500 CpmlPrimitive line1
, line2
;
501 cairo_path_data_t data1
[2], data2
[2];
504 end1
= cpml_primitive_get_point(primitive
, -1);
505 start2
= cpml_primitive_get_point(primitive2
, 0);
507 /* Check if the primitives are yet connected */
508 if (end1
->point
.x
== start2
->point
.x
&& end1
->point
.y
== start2
->point
.y
)
511 line1
.org
= cpml_primitive_get_point(primitive
, -2);
513 data1
[0].header
.type
= CAIRO_PATH_LINE_TO
;
518 data2
[0].header
.type
= CAIRO_PATH_LINE_TO
;
519 data2
[1] = *cpml_primitive_get_point(primitive2
, 1);
521 if (!cpml_line_intersection(&line1
, &line2
, &joint
))
524 cpml_pair_to_cairo(&joint
, end1
);
525 cpml_pair_to_cairo(&joint
, start2
);
531 * cpml_primitive_intersection:
532 * @primitive: the first #CpmlPrimitive
533 * @primitive2: the second #CpmlPrimitive
534 * @dest: the destination #CpmlPair (or a vector of #CpmlPair)
536 * Finds the intersection points between the given primitives and
537 * returns the result in @dest. The size of @dest is dependent
538 * from the type of the most complex primitive involved in the
539 * operation. If there are curves involved, @dest MUST be an array
540 * of at least 4 #CpmlPair. If an arc is the most complex primitive
541 * involved, @dest MUST be sized to 2 #CpmlPair. In the simplest
542 * case, that is intersection between two lines, only 1 #CpmlPair
545 * If the primitive types are not known in advance, simply suppose
546 * the worst case and leave room for 4 #CpmlPair in @dest.
549 * This function is primitive dependent: every new primitive must
550 * expose API to get intersections with any other primitive type
551 * (excluding %CAIRO_PATH_CLOSE_PATH, as it is converted to a line
553 * <para>The convention used by CPML is that a primitive should
554 * expose only APIs dealing with lower complexity primitives.
555 * This is required to avoid double functions: you will have
556 * only a cpml_curve_intersection_with_line() function not a
557 * cpml_line_intersection_with_curve(), as the latter is
558 * easily reproduced by calling the former with @primitive2
559 * and @primitive switched.
562 * Return value: the number of intersection points found
565 cpml_primitive_intersection(const CpmlPrimitive
*primitive
,
566 const CpmlPrimitive
*primitive2
,
569 cairo_path_data_type_t type1
, type2
;
571 type1
= primitive
->data
->header
.type
;
572 type2
= primitive
->data
->header
.type
;
574 /* Close path primitives are treated as line-to */
575 if (type1
== CAIRO_PATH_CLOSE_PATH
)
576 type1
= CAIRO_PATH_LINE_TO
;
577 if (type2
== CAIRO_PATH_CLOSE_PATH
)
578 type2
= CAIRO_PATH_LINE_TO
;
580 /* Order the two primitives in ascending complexity, to facilitate
581 * the dispatcher logic */
582 if (cpml_primitive_type_get_npoints(type1
) > cpml_primitive_type_get_npoints(type2
)) {
583 const CpmlPrimitive
*tmp_primitive
;
584 cairo_path_data_type_t tmp_type
;
587 tmp_primitive
= primitive
;
590 primitive
= primitive2
;
593 primitive2
= tmp_primitive
;
596 /* Dispatcher: probably there's a smarter way to do this */
599 case CAIRO_PATH_LINE_TO
:
600 if (type2
== CAIRO_PATH_LINE_TO
)
601 return cpml_line_intersection(primitive2
, primitive
, dest
);
602 else if (type2
== CAIRO_PATH_ARC_TO
)
603 return cpml_arc_intersection_with_line(primitive2
, primitive
, dest
);
604 else if (type2
== CAIRO_PATH_CURVE_TO
)
605 return cpml_curve_intersection_with_line(primitive2
, primitive
, dest
);
608 case CAIRO_PATH_ARC_TO
:
609 if (type2
== CAIRO_PATH_ARC_TO
)
610 return cpml_arc_intersection(primitive2
, primitive
, dest
);
611 else if (type2
== CAIRO_PATH_CURVE_TO
)
612 return cpml_curve_intersection_with_arc(primitive2
, primitive
, dest
);
615 case CAIRO_PATH_CURVE_TO
:
616 if (type2
== CAIRO_PATH_CURVE_TO
)
617 return cpml_curve_intersection(primitive2
, primitive
, dest
);
624 /* Primitive combination not found */
629 * cpml_primitive_offset:
630 * @primitive: a #CpmlPrimitive
631 * @offset: distance for the computed offset primitive
633 * Given a primitive, computes the same (or approximated) parallel
634 * primitive distant @offset from the original one and returns
635 * the result by changing @primitive.
638 * This function is primitive dependent, that is every primitive has
639 * its own implementation.
643 cpml_primitive_offset(CpmlPrimitive
*primitive
, double offset
)
645 switch (primitive
->data
->header
.type
) {
647 case CAIRO_PATH_LINE_TO
:
648 cpml_line_offset(primitive
, offset
);
651 case CAIRO_PATH_ARC_TO
:
652 cpml_arc_offset(primitive
, offset
);
655 case CAIRO_PATH_CURVE_TO
:
656 cpml_curve_offset(primitive
, offset
);
659 case CAIRO_PATH_CLOSE_PATH
:
660 cpml_close_offset(primitive
, offset
);
669 dump_cairo_point(const cairo_path_data_t
*path_data
)
671 printf("(%g %g) ", path_data
->point
.x
, path_data
->point
.y
);