[CpmlSegment] Added CpmlPath definition
[adg.git] / cpml / cpml-segment.c
blobf685ef65bd57acbdc5b576e25ad7a2a362e81475
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.
20 /**
21 * SECTION:segment
22 * @title: CpmlSegment
23 * @short_description: Contiguous lines
25 * A segment is a single contiguous line got from a cairo path.
27 * Every #CpmlPath struct can contain more than one segment:
28 * the CPML library provides iteration APIs to browse these segments.
29 * The <structname>CpmlSegment</structname> struct internally keeps
30 * a reference to the source #CpmlPath so it can be used both
31 * for getting data from the current segment, browsing the next and
32 * reset to the first (it is used also as a forward iterator).
34 * Use cpml_segment_reset() to reset the iterator at the start of the
35 * cairo path (will point the first segment) and cpml_segment_next()
36 * to get the next segment. When initialized,
37 * <structname>CpmlSegment</structname> yet refers to the first
38 * segment, so the initial reset is useless.
40 * Getting the previous segment is not provided, as the underlying
41 * cairo struct is not accessible in reverse order.
42 **/
44 /**
45 * CpmlPath:
47 * This is another name for the #cairo_path_t type. Although phisically
48 * they are the same struct, #CpmlPath conceptually embodies an important
49 * difference: it is a cairo path that can embed %CAIRO_PATH_ARC_TO
50 * primitives. This is not a native cairo primitive and having two
51 * different data types is a good way to make clear when a function
52 * expect or not embedded arc-to primitives.
53 **/
55 /**
56 * CpmlSegment:
57 * @path: the source #CpmlPath struct
58 * @data: the segment data
59 * @num_data: size of @data
61 * This is an unobtrusive struct to identify a segment inside a
62 * cairo path. Unobtrusive means that the real coordinates are
63 * still stored in @source: CpmlSegment only provides a way to
64 * access the underlying cairo path.
65 **/
67 #include "cpml-segment.h"
68 #include "cpml-primitive.h"
69 #include "cpml-line.h"
70 #include "cpml-curve.h"
71 #include "cpml-pair.h"
72 #include "cpml-alloca.h"
74 #include <stdio.h>
75 #include <string.h>
77 static cairo_bool_t normalize (CpmlSegment *segment);
78 static cairo_bool_t ensure_one_move_to (CpmlSegment *segment);
79 static void reshape (CpmlSegment *segment);
82 /**
83 * cpml_segment_from_cairo:
84 * @segment: a #CpmlSegment
85 * @path: the source #CpmlPath
87 * Builds a CpmlSegment from a #CpmlPath structure. This operation
88 * involves stripping the leading %CAIRO_PATH_MOVE_TO primitives and
89 * setting the internal segment structure accordling. A pointer to the
90 * source cairo path is kept.
92 * This function will fail if @path is null, empty or if its
93 * <structfield>status</structfield> member is not %CAIRO_STATUS_SUCCESS.
94 * Also, the first primitive must be a %CAIRO_PATH_MOVE_TO, so no
95 * dependency on the cairo context is needed.
97 * Return value: 1 on success, 0 on errors
98 **/
99 cairo_bool_t
100 cpml_segment_from_cairo(CpmlSegment *segment, CpmlPath *path)
102 /* The cairo path should be defined and in a perfect state */
103 if (path == NULL || path->num_data == 0 ||
104 path->status != CAIRO_STATUS_SUCCESS)
105 return 0;
107 segment->path = path;
108 segment->data = path->data;
109 segment->num_data = path->num_data;
111 return normalize(segment);
115 * cpml_segment_copy:
116 * @segment: a #CpmlSegment
117 * @src: the source segment to copy
119 * Makes a shallow copy of @src into @segment.
121 * Return value: @segment or %NULL on errors
123 CpmlSegment *
124 cpml_segment_copy(CpmlSegment *segment, const CpmlSegment *src)
126 if (segment == NULL || src == NULL)
127 return NULL;
129 return memcpy(segment, src, sizeof(CpmlSegment));
134 * cpml_segment_reset:
135 * @segment: a #CpmlSegment
137 * Modifies @segment to point to the first segment of the source cairo path.
139 void
140 cpml_segment_reset(CpmlSegment *segment)
142 segment->data = segment->path->data;
143 segment->num_data = segment->path->num_data;
144 normalize(segment);
148 * cpml_segment_next:
149 * @segment: a #CpmlSegment
151 * Modifies @segment to point to the next segment of the source cairo path.
153 * Return value: 1 on success, 0 if no next segment found or errors
155 cairo_bool_t
156 cpml_segment_next(CpmlSegment *segment)
158 int rest = segment->path->num_data - segment->num_data +
159 segment->path->data - segment->data;
161 if (rest <= 0)
162 return 0;
164 segment->data += segment->num_data;
165 segment->num_data = rest;
167 return normalize(segment);
172 * cpml_segment_to_cairo:
173 * @segment: a #CpmlSegment
174 * @cr: the destination cairo context
176 * Appends the path of @segment to @cr using cairo_append_path().
178 void
179 cpml_segment_to_cairo(const CpmlSegment *segment, cairo_t *cr)
181 CpmlPrimitive primitive;
183 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
185 do {
186 cpml_primitive_to_cairo(&primitive, cr);
187 } while (cpml_primitive_next(&primitive));
191 * cpml_segment_dump:
192 * @segment: a #CpmlSegment
194 * Dumps the specified @segment to stdout. Useful for debugging purposes.
196 void
197 cpml_segment_dump(const CpmlSegment *segment)
199 CpmlPrimitive primitive;
200 cairo_bool_t first_call = 1;
202 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
204 do {
205 cpml_primitive_dump(&primitive, first_call);
206 first_call = 0;
207 } while (cpml_primitive_next(&primitive));
212 * cpml_segment_reverse:
213 * @segment: a #CpmlSegment
215 * Reverses @segment in-place. The resulting rendering will be the same,
216 * but with the primitives generated in reverse order.
218 void
219 cpml_segment_reverse(CpmlSegment *segment)
221 cairo_path_data_t *data, *dst_data;
222 size_t data_size;
223 double end_x, end_y;
224 int n, num_points, n_point;
225 const cairo_path_data_t *src_data;
227 data_size = sizeof(cairo_path_data_t) * segment->num_data;
228 data = cpml_alloca(data_size);
229 end_x = segment->data[1].point.x;
230 end_y = segment->data[1].point.y;
232 for (n = 2; n < segment->num_data; ++n) {
233 src_data = segment->data + n;
234 num_points = src_data->header.length;
236 dst_data = data + segment->num_data - n - num_points + 2;
237 dst_data->header.type = src_data->header.type;
238 dst_data->header.length = num_points;
240 for (n_point = 1; n_point < num_points; ++n_point) {
241 dst_data[num_points - n_point].point.x = end_x;
242 dst_data[num_points - n_point].point.y = end_y;
243 end_x = src_data[n_point].point.x;
244 end_y = src_data[n_point].point.y;
247 n += n_point - 1;
250 data[0].header.type = CAIRO_PATH_MOVE_TO;
251 data[0].header.length = 2;
252 data[1].point.x = end_x;
253 data[1].point.y = end_y;
254 memcpy(segment->data, data, data_size);
258 * cpml_segment_transform:
259 * @segment: a #CpmlSegment
260 * @matrix: the matrix to be applied
262 * Applies @matrix on all the points of @segment.
264 void
265 cpml_segment_transform(CpmlSegment *segment, const cairo_matrix_t *matrix)
267 cairo_path_data_t *data;
268 int n, n_point, num_points;
270 data = segment->data;
272 for (n = 0; n < segment->num_data; n += num_points) {
273 num_points = data->header.length;
274 ++data;
275 for (n_point = 1; n_point < num_points; ++n_point) {
276 cairo_matrix_transform_point(matrix, &data->point.x, &data->point.y);
277 ++data;
283 * cpml_segment_intersection:
284 * @segment: the first #CpmlSegment
285 * @segment2: the second #CpmlSegment
286 * @dest: the destination vector of #CpmlPair
287 * @max: maximum number of intersections to return
289 * Computes the intersections between @segment and @segment2 and
290 * returns the found points in @dest. If the intersections are more
291 * than @max, only the first @max pairs are stored in @dest.
293 * To get the job done, the primitives of @segment are sequentially
294 * scanned for intersections with any primitive in @segment2. This
295 * means @segment has a higher precedence over @segment2.
297 * Return value: the number of intersections found
300 cpml_segment_intersection(const CpmlSegment *segment,
301 const CpmlSegment *segment2,
302 CpmlPair *dest, int max)
304 CpmlPrimitive portion;
305 int partial, total;
307 cpml_primitive_from_segment(&portion, (CpmlSegment *) segment);
308 total = 0;
310 do {
311 partial = cpml_primitive_intersection_with_segment(&portion,
312 segment2,
313 dest + total,
314 max - total);
315 total += partial;
316 } while (total < max && cpml_primitive_next(&portion));
318 return total;
322 * cpml_segment_offset:
323 * @segment: a #CpmlSegment
324 * @offset: the offset distance
326 * Offsets a segment of the specified amount, that is builds a "parallel"
327 * segment at the @offset distance from the original one and returns the
328 * result by replacing the original @segment.
330 * <important>
331 * <title>TODO</title>
332 * <itemizedlist>
333 * <listitem>Closed path are not yet managed: an elegant solution is not
334 * so obvious: use cpml_close_offset() when available.</listitem>
335 * <listitem>Degenerated primitives, such as lines of length 0, are not
336 * managed properly.</listitem>
337 * </itemizedlist>
338 * </important>
340 void
341 cpml_segment_offset(CpmlSegment *segment, double offset)
343 CpmlPrimitive primitive;
344 CpmlPrimitive last_primitive;
345 cairo_path_data_t org, old_end;
346 cairo_bool_t first_cycle;
348 cpml_primitive_from_segment(&primitive, segment);
349 first_cycle = 1;
351 do {
352 if (!first_cycle) {
353 org = old_end;
354 primitive.org = &org;
357 old_end = *cpml_primitive_get_point(&primitive, -1);
358 cpml_primitive_offset(&primitive, offset);
360 if (!first_cycle) {
361 cpml_primitive_join(&last_primitive, &primitive);
362 primitive.org = cpml_primitive_get_point(&last_primitive, -1);
365 cpml_primitive_copy(&last_primitive, &primitive);
366 first_cycle = 0;
367 } while (cpml_primitive_next(&primitive));
371 * normalize:
372 * @segment: a #CpmlSegment
374 * Strips the leading %CAIRO_PATH_MOVE_TO primitives, updating
375 * the CpmlSegment structure accordling. One, and only one,
376 * %CAIRO_PATH_MOVE_TO primitive is left.
378 * Return value: 1 on success, 0 on no leading MOVE_TOs or on errors
380 static cairo_bool_t
381 normalize(CpmlSegment *segment)
383 if (!ensure_one_move_to(segment))
384 return 0;
386 reshape(segment);
387 return 1;
391 * ensure_one_move_to:
392 * @segment: a #CpmlSegment
394 * Strips the leading %CAIRO_PATH_MOVE_TO primitives, updating
395 * the <structname>CpmlSegment</structname> structure accordling.
396 * One, and only one, %CAIRO_PATH_MOVE_TO primitive is left.
398 * Return value: 1 on success, 0 on no leading MOVE_TOs or on empty path
400 static cairo_bool_t
401 ensure_one_move_to(CpmlSegment *segment)
403 cairo_path_data_t *new_data;
404 int new_num_data, length;
406 new_data = segment->data;
408 /* Check for at least one move to */
409 if (new_data->header.type != CAIRO_PATH_MOVE_TO)
410 return 0;
412 new_num_data = segment->num_data;
413 length = 0;
415 /* Strip the leading CAIRO_PATH_MOVE_TO, leaving only the last one */
416 do {
417 new_data += length;
418 new_num_data -= length;
419 length = new_data->header.length;
421 /* Check for end of cairo path data */
422 if (length >= new_num_data)
423 return 0;
424 } while (new_data[length].header.type == CAIRO_PATH_MOVE_TO);
426 segment->data = new_data;
427 segment->num_data = new_num_data;
429 return 1;
433 * reshape:
434 * @segment: a #CpmlSegment
436 * Looks for the segment termination and modify the
437 * <structfield>num_data</structfield> field of @segment accordling.
438 * @segment must have only one leading %CAIRO_PATH_MOVE_TO and
439 * it is supposed to be non-empty, conditions yet imposed by the
440 * ensure_one_move_to() function.
442 static void
443 reshape(CpmlSegment *segment)
445 cairo_path_data_t *data;
446 int num_data, new_num_data, length;
448 /* Skip the leading move to */
449 new_num_data = 2;
450 data = segment->data + new_num_data;
452 /* Calculate the remaining data in the cairo path */
453 num_data = segment->path->num_data -
454 (segment->data - segment->path->data);
456 while (new_num_data < num_data) {
457 /* A primitive is considered valid if it has implemented
458 * its own type_get_npoints() */
459 if (cpml_primitive_type_get_npoints(data->header.type) < 0)
460 break;
462 length = data->header.length;
463 data += length;
464 new_num_data += length;
467 segment->num_data = new_num_data;