[AdgDim] Using a smart set_angle()
[adg.git] / cpml / cpml-segment.c
blob9b808f6236e6eb5703719a16533f1c0a4cc6c607
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.
21 /**
22 * SECTION:segment
23 * @Section_Id:CpmlSegment
24 * @title: CpmlSegment
25 * @short_description: Contiguous segment that can be a fragment
26 * or a whole cairo path
28 * A segment is a single contiguous line got from a cairo path. The
29 * CPML library relies on one assumption to let the data be independent
30 * from the current point (and thus from the cairo context): any segment
31 * MUST be preceded by at least one %CAIRO_PATH_MOVE_TO primitive.
32 * This means a valid segment in cairo could be rejected by CPML.
34 * #CpmlSegment provides an unobtrusive way to access a cairo path.
35 * This means #CpmlSegment itsself does not hold any coordinates but
36 * instead a bunch of pointers to the original #cairo_path_t struct:
37 * modifying data throught this struct also changes the original path.
39 * Every #CpmlPath struct can contain more than one segment: the CPML
40 * library provides iteration APIs to browse the segments of a path.
41 * Use cpml_segment_reset() to reset the iterator at the start of the
42 * cairo path (will point the first segment) and cpml_segment_next()
43 * to get the next segment. Getting the previous segment is not provided
44 * as the underlying cairo struct is not accessible in reverse order.
46 * When initialized, #CpmlSegment yet refers to the first segment so
47 * the initial reset is not required.
48 **/
50 /**
51 * CpmlPath:
53 * This is another name for the #cairo_path_t type. Although phisically
54 * they are the same struct, #CpmlPath conceptually embodies an important
55 * difference: it is a cairo path that can embed %CAIRO_PATH_ARC_TO
56 * primitives. This is not a native cairo primitive and having two
57 * different data types is a good way to make clear when a function
58 * expect or not embedded arc-to primitives.
59 **/
61 /**
62 * CpmlSegment:
63 * @path: the source #CpmlPath struct
64 * @data: the data points of the segment; the first primitive
65 * will always be a %CAIRO_PATH_MOVE_TO
66 * @num_data: size of @data
68 * This is an unobtrusive struct to identify a segment inside a
69 * cairo path. Unobtrusive means that the real coordinates are
70 * still stored in @path: CpmlSegment only provides a way to
71 * access them.
72 **/
75 #include "cpml-segment.h"
76 #include "cpml-primitive.h"
77 #include "cpml-line.h"
78 #include "cpml-curve.h"
79 #include "cpml-pair.h"
80 #include "cpml-alloca.h"
82 #include <stdio.h>
83 #include <string.h>
85 static cairo_bool_t normalize (CpmlSegment *segment);
86 static cairo_bool_t ensure_one_move_to (CpmlSegment *segment);
87 static void reshape (CpmlSegment *segment);
90 /**
91 * cpml_segment_from_cairo:
92 * @segment: a #CpmlSegment
93 * @path: the source #CpmlPath
95 * Builds a CpmlSegment from a #CpmlPath structure. This operation
96 * involves stripping the leading %CAIRO_PATH_MOVE_TO primitives and
97 * setting the internal segment structure accordling. A pointer to the
98 * source cairo path is kept.
100 * This function will fail if @path is null, empty or if its
101 * <structfield>status</structfield> member is not %CAIRO_STATUS_SUCCESS.
102 * Also, the first primitive must be a %CAIRO_PATH_MOVE_TO, so no
103 * dependency on the cairo context is needed.
105 * Return value: 1 on success, 0 on errors
107 cairo_bool_t
108 cpml_segment_from_cairo(CpmlSegment *segment, CpmlPath *path)
110 /* The cairo path should be defined and in a perfect state */
111 if (path == NULL || path->num_data == 0 ||
112 path->status != CAIRO_STATUS_SUCCESS)
113 return 0;
115 segment->path = path;
116 segment->data = path->data;
117 segment->num_data = path->num_data;
119 return normalize(segment);
123 * cpml_segment_copy:
124 * @segment: a #CpmlSegment
125 * @src: the source segment to copy
127 * Makes a shallow copy of @src into @segment.
129 * Return value: @segment or %NULL on errors
131 CpmlSegment *
132 cpml_segment_copy(CpmlSegment *segment, const CpmlSegment *src)
134 if (segment == NULL || src == NULL)
135 return NULL;
137 return memcpy(segment, src, sizeof(CpmlSegment));
142 * cpml_segment_reset:
143 * @segment: a #CpmlSegment
145 * Modifies @segment to point to the first segment of the source cairo path.
147 void
148 cpml_segment_reset(CpmlSegment *segment)
150 segment->data = segment->path->data;
151 segment->num_data = segment->path->num_data;
152 normalize(segment);
156 * cpml_segment_next:
157 * @segment: a #CpmlSegment
159 * Modifies @segment to point to the next segment of the source cairo path.
161 * Return value: 1 on success, 0 if no next segment found or errors
163 cairo_bool_t
164 cpml_segment_next(CpmlSegment *segment)
166 int rest = segment->path->num_data - segment->num_data +
167 segment->path->data - segment->data;
169 if (rest <= 0)
170 return 0;
172 segment->data += segment->num_data;
173 segment->num_data = rest;
175 return normalize(segment);
180 * cpml_segment_to_cairo:
181 * @segment: a #CpmlSegment
182 * @cr: the destination cairo context
184 * Appends the path of @segment to @cr. The segment is "flattened",
185 * that is %CAIRO_PATH_ARC_TO primitives are approximated by one
186 * or more %CAIRO_PATH_CURVE_TO using cpml_arc_to_cairo(). Check
187 * its documentation for further details.
189 void
190 cpml_segment_to_cairo(const CpmlSegment *segment, cairo_t *cr)
192 CpmlPrimitive primitive;
194 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
196 do {
197 cpml_primitive_to_cairo(&primitive, cr);
198 } while (cpml_primitive_next(&primitive));
202 * cpml_segment_dump:
203 * @segment: a #CpmlSegment
205 * Dumps the specified @segment to stdout. Useful for debugging purposes.
207 void
208 cpml_segment_dump(const CpmlSegment *segment)
210 CpmlPrimitive primitive;
211 cairo_bool_t first_call = 1;
213 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
215 do {
216 cpml_primitive_dump(&primitive, first_call);
217 first_call = 0;
218 } while (cpml_primitive_next(&primitive));
223 * cpml_segment_reverse:
224 * @segment: a #CpmlSegment
226 * Reverses @segment in-place. The resulting rendering will be the same,
227 * but with the primitives generated in reverse order.
229 void
230 cpml_segment_reverse(CpmlSegment *segment)
232 cairo_path_data_t *data, *dst_data;
233 size_t data_size;
234 double end_x, end_y;
235 int n, num_points, n_point;
236 const cairo_path_data_t *src_data;
238 data_size = sizeof(cairo_path_data_t) * segment->num_data;
239 data = cpml_alloca(data_size);
240 end_x = segment->data[1].point.x;
241 end_y = segment->data[1].point.y;
243 for (n = 2; n < segment->num_data; ++n) {
244 src_data = segment->data + n;
245 num_points = src_data->header.length;
247 dst_data = data + segment->num_data - n - num_points + 2;
248 dst_data->header.type = src_data->header.type;
249 dst_data->header.length = num_points;
251 for (n_point = 1; n_point < num_points; ++n_point) {
252 dst_data[num_points - n_point].point.x = end_x;
253 dst_data[num_points - n_point].point.y = end_y;
254 end_x = src_data[n_point].point.x;
255 end_y = src_data[n_point].point.y;
258 n += n_point - 1;
261 data[0].header.type = CAIRO_PATH_MOVE_TO;
262 data[0].header.length = 2;
263 data[1].point.x = end_x;
264 data[1].point.y = end_y;
265 memcpy(segment->data, data, data_size);
269 * cpml_segment_transform:
270 * @segment: a #CpmlSegment
271 * @matrix: the matrix to be applied
273 * Applies @matrix on all the points of @segment.
275 void
276 cpml_segment_transform(CpmlSegment *segment, const cairo_matrix_t *matrix)
278 cairo_path_data_t *data;
279 int n, n_point, num_points;
281 data = segment->data;
283 for (n = 0; n < segment->num_data; n += num_points) {
284 num_points = data->header.length;
285 ++data;
286 for (n_point = 1; n_point < num_points; ++n_point) {
287 cairo_matrix_transform_point(matrix, &data->point.x, &data->point.y);
288 ++data;
294 * cpml_segment_intersection:
295 * @segment: the first #CpmlSegment
296 * @segment2: the second #CpmlSegment
297 * @dest: the destination vector of #CpmlPair
298 * @max: maximum number of intersections to return
300 * Computes the intersections between @segment and @segment2 and
301 * returns the found points in @dest. If the intersections are more
302 * than @max, only the first @max pairs are stored in @dest.
304 * To get the job done, the primitives of @segment are sequentially
305 * scanned for intersections with any primitive in @segment2. This
306 * means @segment has a higher precedence over @segment2.
308 * Return value: the number of intersections found
311 cpml_segment_intersection(const CpmlSegment *segment,
312 const CpmlSegment *segment2,
313 CpmlPair *dest, int max)
315 CpmlPrimitive portion;
316 int partial, total;
318 cpml_primitive_from_segment(&portion, (CpmlSegment *) segment);
319 total = 0;
321 do {
322 partial = cpml_primitive_intersection_with_segment(&portion,
323 segment2,
324 dest + total,
325 max - total);
326 total += partial;
327 } while (total < max && cpml_primitive_next(&portion));
329 return total;
333 * cpml_segment_offset:
334 * @segment: a #CpmlSegment
335 * @offset: the offset distance
337 * Offsets a segment of the specified amount, that is builds a "parallel"
338 * segment at the @offset distance from the original one and returns the
339 * result by replacing the original @segment.
341 * <important>
342 * <title>TODO</title>
343 * <itemizedlist>
344 * <listitem>Closed path are not yet managed: an elegant solution is not
345 * so obvious: use cpml_close_offset() when available.</listitem>
346 * <listitem>Degenerated primitives, such as lines of length 0, are not
347 * managed properly.</listitem>
348 * </itemizedlist>
349 * </important>
351 void
352 cpml_segment_offset(CpmlSegment *segment, double offset)
354 CpmlPrimitive primitive;
355 CpmlPrimitive last_primitive;
356 cairo_path_data_t org, old_end;
357 cairo_bool_t first_cycle;
359 cpml_primitive_from_segment(&primitive, segment);
360 first_cycle = 1;
362 do {
363 if (!first_cycle) {
364 org = old_end;
365 primitive.org = &org;
368 old_end = *cpml_primitive_get_point(&primitive, -1);
369 cpml_primitive_offset(&primitive, offset);
371 if (!first_cycle) {
372 cpml_primitive_join(&last_primitive, &primitive);
373 primitive.org = cpml_primitive_get_point(&last_primitive, -1);
376 cpml_primitive_copy(&last_primitive, &primitive);
377 first_cycle = 0;
378 } while (cpml_primitive_next(&primitive));
382 * normalize:
383 * @segment: a #CpmlSegment
385 * Strips the leading %CAIRO_PATH_MOVE_TO primitives, updating
386 * the CpmlSegment structure accordling. One, and only one,
387 * %CAIRO_PATH_MOVE_TO primitive is left.
389 * Return value: 1 on success, 0 on no leading MOVE_TOs or on errors
391 static cairo_bool_t
392 normalize(CpmlSegment *segment)
394 if (!ensure_one_move_to(segment))
395 return 0;
397 reshape(segment);
398 return 1;
402 * ensure_one_move_to:
403 * @segment: a #CpmlSegment
405 * Strips the leading %CAIRO_PATH_MOVE_TO primitives, updating
406 * the <structname>CpmlSegment</structname> structure accordling.
407 * One, and only one, %CAIRO_PATH_MOVE_TO primitive is left.
409 * Return value: 1 on success, 0 on no leading MOVE_TOs or on empty path
411 static cairo_bool_t
412 ensure_one_move_to(CpmlSegment *segment)
414 cairo_path_data_t *new_data;
415 int new_num_data, length;
417 new_data = segment->data;
419 /* Check for at least one move to */
420 if (new_data->header.type != CAIRO_PATH_MOVE_TO)
421 return 0;
423 new_num_data = segment->num_data;
424 length = 0;
426 /* Strip the leading CAIRO_PATH_MOVE_TO, leaving only the last one */
427 do {
428 new_data += length;
429 new_num_data -= length;
430 length = new_data->header.length;
432 /* Check for end of cairo path data */
433 if (length >= new_num_data)
434 return 0;
435 } while (new_data[length].header.type == CAIRO_PATH_MOVE_TO);
437 segment->data = new_data;
438 segment->num_data = new_num_data;
440 return 1;
444 * reshape:
445 * @segment: a #CpmlSegment
447 * Looks for the segment termination and modify the
448 * <structfield>num_data</structfield> field of @segment accordling.
449 * @segment must have only one leading %CAIRO_PATH_MOVE_TO and
450 * it is supposed to be non-empty, conditions yet imposed by the
451 * ensure_one_move_to() function.
453 static void
454 reshape(CpmlSegment *segment)
456 cairo_path_data_t *data;
457 int num_data, new_num_data, length;
459 /* Skip the leading move to */
460 new_num_data = 2;
461 data = segment->data + new_num_data;
463 /* Calculate the remaining data in the cairo path */
464 num_data = segment->path->num_data -
465 (segment->data - segment->path->data);
467 while (new_num_data < num_data) {
468 /* A primitive is considered valid if it has implemented
469 * its own type_get_npoints() */
470 if (cpml_primitive_type_get_npoints(data->header.type) < 0)
471 break;
473 length = data->header.length;
474 data += length;
475 new_num_data += length;
478 segment->num_data = new_num_data;