[AdgTableStyle] Using different line style for frame and grid
[adg.git] / src / cpml / cpml-primitive.c
blob1710818d3bd4ab4f74889327905936d41ffd02e2
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008,2009,2010 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.
21 /**
22 * SECTION:cpml-primitive
23 * @Section_Id:CpmlPrimitive
24 * @title: CpmlPrimitive
25 * @short_description: Basic component of segments
27 * A primitive is an atomic geometric element found inside #CpmlSegment.
28 * The available primitives are the same defined by #cairo_path_data_type_t
29 * with the additional #CPML_ARC type (check #CpmlPrimitiveType
30 * for further information) and without #CPML_MOVE as it is not
31 * considered a primitive and it is managed in different way: the move-to
32 * primitives are only used to define the origin of a segment.
33 **/
35 /**
36 * CpmlPrimitiveType:
38 * This is another name for #cairo_path_data_type_t type. Although
39 * phisically they are the same struct, #CpmlPrimitiveType conceptually
40 * embodies an important difference: it can be used to specify the
41 * special #CPML_ARC primitive. This is not a native cairo
42 * primitive and having two different types is a good way to make clear
43 * when a function expect or not embedded arc-to primitives.
44 **/
46 /**
47 * CpmlPrimitive:
48 * @segment: the source #CpmlSegment
49 * @org: a pointer to the first point of the primitive
50 * @data: the array of the path data, prepended by the header
52 * As for #CpmlSegment, also the primitive is unobtrusive. This
53 * means CpmlPrimitive does not include any coordinates but instead
54 * keeps pointers to the original segment (and, by transition, to
55 * the underlying #CpmlPath struct).
56 **/
58 /**
59 * CPML_MOVE:
61 * An operation that denotes a current point movement internally used to
62 * keep track of the starting point of a primitive.
63 * It is equivalent to the %CAIRO_PATH_MOVE_TO cairo constant.
64 **/
67 #include "cpml-internal.h"
68 #include "cpml-extents.h"
69 #include "cpml-segment.h"
70 #include "cpml-primitive.h"
71 #include "cpml-primitive-private.h"
72 #include "cpml-line.h"
73 #include "cpml-arc.h"
74 #include "cpml-curve.h"
75 #include "cpml-close.h"
76 #include <string.h>
77 #include <stdio.h>
80 static const _CpmlPrimitiveClass *
81 get_class_from_type (CpmlPrimitiveType type);
82 static const _CpmlPrimitiveClass *
83 get_class (const CpmlPrimitive *primitive);
84 static void dump_cairo_point (const cairo_path_data_t *path_data);
87 /**
88 * cpml_primitive_type_get_n_points:
89 * @type: a primitive type
91 * Gets the number of points required to identify the @type primitive.
93 * Returns: the number of points or %0 on errors
94 **/
95 size_t
96 cpml_primitive_type_get_n_points(CpmlPrimitiveType type)
98 const _CpmlPrimitiveClass *class_data = get_class_from_type(type);
100 if (class_data == NULL)
101 return 0;
103 return class_data->n_points;
107 * cpml_primitive_from_segment:
108 * @primitive: the destination #CpmlPrimitive struct
109 * @segment: the source segment
111 * Initializes @primitive to the first primitive of @segment.
113 void
114 cpml_primitive_from_segment(CpmlPrimitive *primitive, CpmlSegment *segment)
116 primitive->segment = segment;
118 /* The first element of a CpmlSegment is always a CPML_MOVE,
119 * as ensured by cpml_segment_from_cairo() and by the browsing APIs,
120 * so the origin is in the second data item */
121 primitive->org = segment->data + 1;
123 /* Also, the segment APIs ensure that @segment is prepended by
124 * only one CPML_MOVE */
125 primitive->data = segment->data + segment->data->header.length;
129 * cpml_primitive_copy:
130 * @primitive: the destination #CpmlPrimitive
131 * @src: the source #CpmlPrimitive
133 * Copies @src in @primitive. This is a shallow copy: the internal fields
134 * of @primitive refer to the same memory as the original @src primitive.
136 void
137 cpml_primitive_copy(CpmlPrimitive *primitive, const CpmlPrimitive *src)
139 memcpy(primitive, src, sizeof(CpmlPrimitive));
143 * cpml_primitive_reset:
144 * @primitive: a #CpmlPrimitive
146 * Resets @primitive so it refers to the first primitive of the
147 * source segment.
149 void
150 cpml_primitive_reset(CpmlPrimitive *primitive)
152 cpml_primitive_from_segment(primitive, primitive->segment);
156 * cpml_primitive_next:
157 * @primitive: a #CpmlPrimitive
160 * Changes @primitive so it refers to the next primitive on the
161 * source segment. If there are no more primitives, @primitive is
162 * not changed and 0 is returned.
164 * Returns: 1 on success, 0 if no next primitive found or errors
166 cairo_bool_t
167 cpml_primitive_next(CpmlPrimitive *primitive)
169 cairo_path_data_t *new_data;
170 const cairo_path_data_t *end_data;
172 new_data = primitive->data + primitive->data->header.length;
173 end_data = primitive->segment->data + primitive->segment->num_data;
175 if (new_data >= end_data)
176 return 0;
178 primitive->org = cpml_primitive_get_point(primitive, -1);
179 primitive->data = new_data;
181 return 1;
185 * cpml_primitive_get_n_points:
186 * @primitive: a #CpmlPrimitive
188 * Gets the number of points required to identify @primitive.
189 * It is similar to cpml_primitive_type_get_n_points() but using
190 * a @primitive instance instead of a type.
192 * Returns: the number of points or %0 on errors
194 size_t
195 cpml_primitive_get_n_points(const CpmlPrimitive *primitive)
197 return cpml_primitive_type_get_n_points(primitive->data->header.type);
201 * cpml_primitive_get_point:
202 * @primitive: a #CpmlPrimitive
203 * @n_point: the index of the point to retrieve
205 * Gets the specified @n_point from @primitive. The index starts
206 * at 0: if @n_point is 0, the start point (the origin) is
207 * returned, 1 for the second point and so on. If @n_point is
208 * negative, it is considered as a negative index from the end,
209 * so that -1 is the end point, -2 the point before the end point
210 * and so on.
212 * #CPML_CLOSE is managed in a special way: if @n_point
213 * is -1 or 1 and @primitive is a close-path, this function cycles
214 * the source #CpmlSegment and returns the first point. This is
215 * needed because requesting the end point (or the second point)
216 * of a close path is a valid operation and must returns the start
217 * of the segment.
219 * Returns: a pointer to the requested point (in cairo format)
220 * or %NULL if the point is outside the valid range
222 cairo_path_data_t *
223 cpml_primitive_get_point(const CpmlPrimitive *primitive, int n_point)
225 size_t n_points;
227 /* For a start point request, simply return the origin
228 * without further checking */
229 if (n_point == 0)
230 return primitive->org;
232 /* The CPML_CLOSE special case */
233 if (primitive->data->header.type == CPML_CLOSE &&
234 (n_point == 1 || n_point == -1))
235 return &primitive->segment->data[1];
237 n_points = cpml_primitive_get_n_points(primitive);
238 if (n_points == 0)
239 return NULL;
241 /* If n_point is negative, consider it as a negative index from the end */
242 if (n_point < 0)
243 n_point = n_points + n_point;
245 /* Out of range condition */
246 if (n_point < 0 || n_point >= n_points)
247 return NULL;
249 return n_point == 0 ? primitive->org : &primitive->data[n_point];
253 * cpml_primitive_get_length:
254 * @primitive: a #CpmlPrimitive
256 * Abstracts the length() family functions by providing a common
257 * way to access the underlying primitive-specific implementation.
258 * The function returns the length of @primitive.
260 * Returns: the requested length or 0 on errors
262 double
263 cpml_primitive_get_length(const CpmlPrimitive *primitive)
265 const _CpmlPrimitiveClass *class_data = get_class(primitive);
267 if (class_data == NULL || class_data->get_length == NULL)
268 return 0;
270 return class_data->get_length(primitive);
274 * cpml_primitive_put_extents:
275 * @primitive: a #CpmlPrimitive
276 * @extents: where to store the extents
278 * Abstracts the extents() family functions by providing a common
279 * way to access the underlying primitive-specific implementation.
281 * This function stores in @extents the bounding box of @primitive.
283 * On errors, that is if the extents cannot be calculated for some
284 * reason, this function does nothing.
286 void
287 cpml_primitive_put_extents(const CpmlPrimitive *primitive,
288 CpmlExtents *extents)
290 const _CpmlPrimitiveClass *class_data = get_class(primitive);
292 if (class_data == NULL || class_data->put_extents == NULL)
293 return;
295 class_data->put_extents(primitive, extents);
299 * cpml_primitive_put_pair_at:
300 * @primitive: a #CpmlPrimitive
301 * @pos: the position value
302 * @pair: the destination #CpmlPair
304 * Abstracts the put_pair_at() family functions by providing a common
305 * way to access the underlying primitive-specific implementation.
307 * It gets the coordinates of the point lying on @primitive
308 * at position @pos. @pos is an homogeneous factor where 0 is the
309 * start point, 1 the end point, 0.5 the mid point and so on.
310 * @pos can be less than 0 or greater than %1, in which case the
311 * coordinates of @pair are interpolated.
313 * On errors, that is if the coordinates cannot be calculated for
314 * some reason, this function does nothing.
316 void
317 cpml_primitive_put_pair_at(const CpmlPrimitive *primitive, double pos,
318 CpmlPair *pair)
320 const _CpmlPrimitiveClass *class_data = get_class(primitive);
322 if (class_data == NULL || class_data->put_pair_at == NULL)
323 return;
325 class_data->put_pair_at(primitive, pos, pair);
329 * cpml_primitive_put_vector_at:
330 * @primitive: a #CpmlPrimitive
331 * @pos: the position value
332 * @vector: the destination #CpmlVector
334 * Abstracts the put_vector_at() family functions by providing a common
335 * way to access the underlying primitive-specific implementation.
337 * It gets the steepness of the point at position @pos on @primitive.
338 * @pos is an homogeneous factor where 0 is the start point, 1 the
339 * end point, 0.5 the mid point and so on.
340 * @pos can be less than 0 or greater than %1, in which case the
341 * coordinates of @pair are interpolated.
343 * On errors, that is if the steepness cannot be calculated for
344 * some reason, this function does nothing.
346 void
347 cpml_primitive_put_vector_at(const CpmlPrimitive *primitive, double pos,
348 CpmlVector *vector)
350 const _CpmlPrimitiveClass *class_data = get_class(primitive);
352 if (class_data == NULL || class_data->put_vector_at == NULL)
353 return;
355 class_data->put_vector_at(primitive, pos, vector);
359 * cpml_primitive_get_closest_pos:
360 * @primitive: a #CpmlPrimitive
361 * @pair: the coordinates of the subject point
363 * Returns the pos value of the point on @primitive nearest to @pair.
364 * The returned value is always clamped between %0 and %1.
366 * Returns: the requested pos value between %0 and %1, or %-1 on errors
368 double
369 cpml_primitive_get_closest_pos(const CpmlPrimitive *primitive,
370 const CpmlPair *pair)
372 const _CpmlPrimitiveClass *class_data = get_class(primitive);
374 if (class_data == NULL || class_data->get_closest_pos == NULL)
375 return -1;
377 return class_data->get_closest_pos(primitive, pair);
381 * cpml_primitive_put_intersections:
382 * @primitive: the first #CpmlPrimitive
383 * @primitive2: the second #CpmlPrimitive
384 * @n_dest: maximum number of intersections to return
385 * @dest: the destination buffer that can contain @n_dest #CpmlPair
387 * Finds the intersection points between the given primitives and
388 * returns the result in @dest. The size of @dest should be enough
389 * to store @n_dest #CpmlPair. The maximum number of intersections
390 * is dependent on the type of the primitive involved in the
391 * operation. If there are at least one Bézier curve involved, up to
392 * %4 intersections could be returned. Otherwise, if there is an arc
393 * the intersections will be %2 at maximum. For line primitives, there
394 * is only %1 point (or %0 if the lines are parallel).
396 * <note>
397 * <para>
398 * The convention used by the CPML library is that a primitive should
399 * implement only the intersection algorithms with lower degree
400 * primitives. This is required to avoid code duplication: intersection
401 * between arc and Bézier curves must be implemented by #CPML_CURVE and
402 * intersection between lines and arcs must be implemented by #CPML_ARC.
403 * cpml_primitive_put_intersections() will take care of swapping the
404 * arguments if they are not properly ordered.
405 * </para>
406 * </note>
408 * Returns: the number of intersection points found or 0 if the
409 * primitives do not intersect or on errors
411 size_t
412 cpml_primitive_put_intersections(const CpmlPrimitive *primitive,
413 const CpmlPrimitive *primitive2,
414 size_t n_dest, CpmlPair *dest)
416 const _CpmlPrimitiveClass *class_data;
417 size_t n_points, n_points2;
419 class_data = get_class(primitive);
421 if (class_data == NULL || class_data->put_intersections == NULL)
422 return 0;
424 n_points = cpml_primitive_get_n_points(primitive);
425 n_points2 = cpml_primitive_get_n_points(primitive2);
427 if (n_points == 0 || n_points2 == 0)
428 return 0;
430 /* Primitives reordering: the first must be the more complex one */
431 if (n_points < n_points2) {
432 const CpmlPrimitive *old_primitive2 = primitive2;
433 primitive2 = primitive;
434 primitive = old_primitive2;
437 return class_data->put_intersections(primitive, primitive2, n_dest, dest);
441 * cpml_primitive_put_intersections_with_segment:
442 * @primitive: a #CpmlPrimitive
443 * @segment: a #CpmlSegment
444 * @n_dest: maximum number of intersection pairs to return
445 * @dest: the destination buffer of #CpmlPair
447 * Computes the intersections between @segment and @primitive by
448 * sequentially scanning the primitives in @segment and looking
449 * for their intersections with @primitive.
451 * If the intersections are more than @n_dest, only the first
452 * @n_dest pairs are stored.
454 * Returns: the number of intersections found
456 size_t
457 cpml_primitive_put_intersections_with_segment(const CpmlPrimitive *primitive,
458 const CpmlSegment *segment,
459 size_t n_dest, CpmlPair *dest)
461 CpmlPrimitive portion;
462 size_t found;
464 cpml_primitive_from_segment(&portion, (CpmlSegment *) segment);
465 found = 0;
467 while (found < n_dest) {
468 found += cpml_primitive_put_intersections(&portion, primitive,
469 n_dest-found, dest+found);
470 if (!cpml_primitive_next(&portion))
471 break;
474 return found;
478 * cpml_primitive_offset:
479 * @primitive: a #CpmlPrimitive
480 * @offset: distance for the computed offset primitive
482 * Given a primitive, computes the same (or approximated) parallel
483 * primitive distant @offset from the original one and returns
484 * the result by changing @primitive.
486 * On errors, that is if the offset primitive cannot be calculated
487 * for some reason, this function does nothing.
489 void
490 cpml_primitive_offset(CpmlPrimitive *primitive, double offset)
492 const _CpmlPrimitiveClass *class_data = get_class(primitive);
494 if (class_data == NULL || class_data->offset == NULL)
495 return;
497 return class_data->offset(primitive, offset);
501 * cpml_primitive_join:
502 * @primitive: the first #CpmlPrimitive
503 * @primitive2: the second #CpmlPrimitive
505 * Joins two primitive modifying the end point of @primitive and the
506 * start point of @primitive2 so that the resulting points will overlap.
508 * <important>
509 * <title>TODO</title>
510 * <itemizedlist>
511 * <listitem>Actually, the join is done by extending the end vector
512 * of @primitive and the start vector of @primitive2 and
513 * interpolating the intersection: this means no primitive
514 * dependent code is needed. Anyway, it is likely to change
515 * in the future because this approach is quite naive when
516 * curves are involved.</listitem>
517 * </itemizedlist>
518 * </important>
520 * Returns: 1 on success, 0 if the end vector of @primitive
521 * and the start vector of @primitive2 are parallel
523 cairo_bool_t
524 cpml_primitive_join(CpmlPrimitive *primitive, CpmlPrimitive *primitive2)
526 cairo_path_data_t *end1, *start2;
527 CpmlPrimitive line1, line2;
528 cairo_path_data_t data1[2], data2[2];
529 CpmlPair joint;
531 end1 = cpml_primitive_get_point(primitive, -1);
532 start2 = cpml_primitive_get_point(primitive2, 0);
534 /* Check if the primitives are yet connected */
535 if (end1->point.x == start2->point.x && end1->point.y == start2->point.y)
536 return 1;
538 line1.org = cpml_primitive_get_point(primitive, -2);
539 line1.data = data1;
540 data1[0].header.type = CPML_LINE;
541 data1[1] = *end1;
543 line2.org = start2;
544 line2.data = data2;
545 data2[0].header.type = CPML_LINE;
546 data2[1] = *cpml_primitive_get_point(primitive2, 1);
548 if (!cpml_primitive_put_intersections(&line1, &line2, 1, &joint))
549 return 0;
551 cpml_pair_to_cairo(&joint, end1);
552 cpml_pair_to_cairo(&joint, start2);
554 return 1;
558 * cpml_primitive_to_cairo:
559 * @primitive: a #CpmlPrimitive
560 * @cr: the destination cairo context
562 * Renders a single @primitive to the @cr cairo context.
563 * As a special case, if the primitive is a #CPML_CLOSE, an
564 * equivalent line is rendered, because a close path left alone
565 * is not renderable.
567 * Also a #CPML_ARC primitive is treated specially, as it is not
568 * natively supported by cairo and has its own rendering API.
570 void
571 cpml_primitive_to_cairo(const CpmlPrimitive *primitive, cairo_t *cr)
573 cairo_path_t path;
574 cairo_path_data_t *path_data;
576 cairo_move_to(cr, primitive->org->point.x, primitive->org->point.y);
578 switch (primitive->data->header.type) {
580 case CPML_CLOSE:
581 path_data = cpml_primitive_get_point(primitive, -1);
582 cairo_line_to(cr, path_data->point.x, path_data->point.y);
583 break;
585 case CPML_ARC:
586 cpml_arc_to_cairo(primitive, cr);
587 break;
589 default:
590 path.status = CAIRO_STATUS_SUCCESS;
591 path.data = primitive->data;
592 path.num_data = primitive->data->header.length;
593 cairo_append_path(cr, &path);
594 break;
599 * cpml_primitive_dump:
600 * @primitive: a #CpmlPrimitive
601 * @org_also: whether to output also the origin coordinates
603 * Dumps info on the specified @primitive to stdout: useful for
604 * debugging purposes. If @org_also is 1, a #CPML_MOVE to the
605 * origin is prepended to the data otherwise the
606 * <structfield>org</structfield> field is not used.
608 void
609 cpml_primitive_dump(const CpmlPrimitive *primitive, cairo_bool_t org_also)
611 const cairo_path_data_t *data;
612 int type;
613 const _CpmlPrimitiveClass *class_data;
614 size_t n, n_points;
616 data = primitive->data;
617 type = data->header.type;
618 class_data = get_class_from_type(type);
620 if (class_data == NULL) {
621 printf("Unknown primitive type (%d)\n", type);
622 return;
625 /* Dump the origin, if requested */
626 if (org_also) {
627 printf("move to ");
628 dump_cairo_point(primitive->org);
629 printf("\n");
632 printf("%s ", class_data->name);
634 n_points = cpml_primitive_get_n_points(primitive);
635 for (n = 1; n < n_points; ++n)
636 dump_cairo_point(cpml_primitive_get_point(primitive, n));
638 printf("\n");
642 static const _CpmlPrimitiveClass *
643 get_class_from_type(CpmlPrimitiveType type)
645 switch (type) {
646 case CPML_LINE:
647 return _cpml_line_get_class();
648 case CPML_ARC:
649 return _cpml_arc_get_class();
650 case CPML_CURVE:
651 return _cpml_curve_get_class();
652 case CPML_CLOSE:
653 return _cpml_close_get_class();
654 default:
655 break;
658 return NULL;
661 static const _CpmlPrimitiveClass *
662 get_class(const CpmlPrimitive *primitive)
664 return get_class_from_type(primitive->data->header.type);
667 static void
668 dump_cairo_point(const cairo_path_data_t *path_data)
670 printf("(%g %g) ", path_data->point.x, path_data->point.y);