1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2011 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
36 * Carl D. Worth <cworth@cworth.org>
37 * Chris Wilson <chris@chris-wilson.co.uk>
40 #define _BSD_SOURCE /* for hypot() */
43 #include "cairo-box-inline.h"
44 #include "cairo-boxes-private.h"
45 #include "cairo-contour-inline.h"
46 #include "cairo-contour-private.h"
47 #include "cairo-error-private.h"
48 #include "cairo-path-fixed-private.h"
49 #include "cairo-slope-private.h"
54 cairo_stroke_style_t style;
60 struct stroke_contour {
61 /* Note that these are not strictly contours as they may intersect */
62 cairo_contour_t contour;
64 cairo_uint64_t contour_tolerance;
65 cairo_polygon_t *polygon;
67 const cairo_matrix_t *ctm;
68 const cairo_matrix_t *ctm_inverse;
70 cairo_bool_t ctm_det_positive;
74 cairo_point_t first_point;
76 cairo_bool_t has_initial_sub_path;
78 cairo_bool_t has_current_face;
79 cairo_stroke_face_t current_face;
81 cairo_bool_t has_first_face;
82 cairo_stroke_face_t first_face;
86 normalize_slope (double *dx, double *dy);
89 compute_face (const cairo_point_t *point,
90 const cairo_slope_t *dev_slope,
91 struct stroker *stroker,
92 cairo_stroke_face_t *face);
95 point_distance_sq (const cairo_point_t *p1,
96 const cairo_point_t *p2)
98 int32_t dx = p1->x - p2->x;
99 int32_t dy = p1->y - p2->y;
100 return _cairo_int32x32_64_mul (dx, dx) + _cairo_int32x32_64_mul (dy, dy);
104 within_tolerance (const cairo_point_t *p1,
105 const cairo_point_t *p2,
106 cairo_uint64_t tolerance)
109 return _cairo_int64_lt (point_distance_sq (p1, p2), tolerance);
113 contour_add_point (struct stroker *stroker,
114 struct stroke_contour *c,
115 const cairo_point_t *point)
117 if (! within_tolerance (point, _cairo_contour_last_point (&c->contour),
118 stroker->contour_tolerance))
119 _cairo_contour_add_point (&c->contour, point);
120 //*_cairo_contour_last_point (&c->contour) = *point;
124 translate_point (cairo_point_t *point, const cairo_point_t *offset)
126 point->x += offset->x;
127 point->y += offset->y;
131 slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
133 double c = (dx1 * dy2 - dx2 * dy1);
136 if (c < 0) return -1;
141 range_step (int i, int step, int max)
152 * Construct a fan around the midpoint using the vertices from pen between
156 add_fan (struct stroker *stroker,
157 const cairo_slope_t *in_vector,
158 const cairo_slope_t *out_vector,
159 const cairo_point_t *midpt,
160 const cairo_point_t *inpt,
161 const cairo_point_t *outpt,
162 cairo_bool_t clockwise,
163 struct stroke_contour *c)
165 int start, stop, step, i, npoints;
167 assert (stroker->pen.num_vertices);
172 start = _cairo_pen_find_active_cw_vertex_index (&stroker->pen,
174 if (_cairo_slope_compare (&stroker->pen.vertices[start].slope_cw,
176 start = range_step (start, 1, stroker->pen.num_vertices);
178 stop = _cairo_pen_find_active_cw_vertex_index (&stroker->pen,
180 if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_ccw,
183 stop = range_step (stop, -1, stroker->pen.num_vertices);
184 if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_cw,
189 npoints = stop - start;
193 start = _cairo_pen_find_active_ccw_vertex_index (&stroker->pen,
195 if (_cairo_slope_compare (&stroker->pen.vertices[start].slope_ccw,
197 start = range_step (start, -1, stroker->pen.num_vertices);
199 stop = _cairo_pen_find_active_ccw_vertex_index (&stroker->pen,
201 if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_cw,
204 stop = range_step (stop, 1, stroker->pen.num_vertices);
205 if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_ccw,
210 npoints = start - stop;
212 stop = range_step (stop, step, stroker->pen.num_vertices);
214 npoints += stroker->pen.num_vertices;
220 i = range_step (i, step, stroker->pen.num_vertices))
222 cairo_point_t p = *midpt;
223 translate_point (&p, &stroker->pen.vertices[i].point);
224 contour_add_point (stroker, c, &p);
229 join_is_clockwise (const cairo_stroke_face_t *in,
230 const cairo_stroke_face_t *out)
232 return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
236 inner_join (struct stroker *stroker,
237 const cairo_stroke_face_t *in,
238 const cairo_stroke_face_t *out,
243 const cairo_point_t *p, *outpt;
244 struct stroke_contour *inner;
245 cairo_int64_t d_p, d_last;
246 cairo_int64_t half_line_width;
249 /* XXX line segments shorter than line-width */
252 inner = &stroker->ccw;
256 inner = &stroker->cw;
261 half_line_width = CAIRO_FIXED_ONE*CAIRO_FIXED_ONE/2 * stroker->style.line_width * out->length + .5;
263 /* On the inside, the previous end-point is always
264 * closer to the new face by definition.
266 last = *_cairo_contour_last_point (&inner->contour);
267 d_last = distance_from_face (out, &last, negate);
268 _cairo_contour_remove_last_point (&inner->contour);
271 if (inner->contour.chain.num_points == 0) {
272 contour_add_point (stroker, inner, outpt);
275 p = _cairo_contour_last_point (&inner->contour);
276 d_p = distance_from_face (out, p, negate);
277 if (_cairo_int64_lt (d_p, half_line_width) &&
278 !_cairo_int64_negative (distance_along_face (out, p)))
282 _cairo_contour_remove_last_point (&inner->contour);
286 compute_inner_joint (&last, d_last, p, d_p, half_line_width);
287 contour_add_point (stroker, inner, &last);
289 const cairo_point_t *outpt;
290 struct stroke_contour *inner;
293 inner = &stroker->ccw;
296 inner = &stroker->cw;
299 contour_add_point (stroker, inner, &in->point);
300 contour_add_point (stroker, inner, outpt);
305 inner_close (struct stroker *stroker,
306 const cairo_stroke_face_t *in,
307 cairo_stroke_face_t *out)
311 const cairo_point_t *p, *outpt, *inpt;
312 struct stroke_contour *inner;
313 struct _cairo_contour_chain *chain;
315 /* XXX line segments shorter than line-width */
317 if (join_is_clockwise (in, out)) {
318 inner = &stroker->ccw;
322 inner = &stroker->cw;
327 if (inner->contour.chain.num_points == 0) {
328 contour_add_point (stroker, inner, &in->point);
329 contour_add_point (stroker, inner, inpt);
330 *_cairo_contour_first_point (&inner->contour) =
331 *_cairo_contour_last_point (&inner->contour);
335 line_width = stroker->style.line_width/2;
336 line_width *= CAIRO_FIXED_ONE;
338 d_last = sign * distance_from_face (out, outpt);
341 for (chain = &inner->contour.chain; chain; chain = chain->next) {
342 for (i = 0; i < chain->num_points; i++) {
343 p = &chain->points[i];
344 if ((d_p = sign * distance_from_face (in, p)) >= line_width &&
345 distance_from_edge (stroker, inpt, &last, p) < line_width)
350 if (p->x != last.x || p->y != last.y) {
359 double dot = (line_width - d_last) / (d_p - d_last);
360 last.x += dot * (p->x - last.x);
361 last.y += dot * (p->y - last.y);
363 *_cairo_contour_last_point (&inner->contour) = last;
365 for (chain = &inner->contour.chain; chain; chain = chain->next) {
366 for (i = 0; i < chain->num_points; i++) {
367 cairo_point_t *pp = &chain->points[i];
374 const cairo_point_t *inpt;
375 struct stroke_contour *inner;
377 if (join_is_clockwise (in, out)) {
378 inner = &stroker->ccw;
381 inner = &stroker->cw;
385 contour_add_point (stroker, inner, &in->point);
386 contour_add_point (stroker, inner, inpt);
387 *_cairo_contour_first_point (&inner->contour) =
388 *_cairo_contour_last_point (&inner->contour);
393 outer_close (struct stroker *stroker,
394 const cairo_stroke_face_t *in,
395 const cairo_stroke_face_t *out)
397 const cairo_point_t *inpt, *outpt;
398 struct stroke_contour *outer;
401 if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
402 in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
406 clockwise = join_is_clockwise (in, out);
410 outer = &stroker->cw;
414 outer = &stroker->ccw;
417 if (within_tolerance (inpt, outpt, stroker->contour_tolerance)) {
418 *_cairo_contour_first_point (&outer->contour) =
419 *_cairo_contour_last_point (&outer->contour);
423 switch (stroker->style.line_join) {
424 case CAIRO_LINE_JOIN_ROUND:
425 /* construct a fan around the common midpoint */
429 &in->point, inpt, outpt,
433 case CAIRO_LINE_JOIN_MITER:
435 /* dot product of incoming slope vector with outgoing slope vector */
436 double in_dot_out = -in->usr_vector.x * out->usr_vector.x +
437 -in->usr_vector.y * out->usr_vector.y;
438 double ml = stroker->style.miter_limit;
440 /* Check the miter limit -- lines meeting at an acute angle
441 * can generate long miters, the limit converts them to bevel
443 * Consider the miter join formed when two line segments
444 * meet at an angle psi:
451 * We can zoom in on the right half of that to see:
469 * The right triangle in that figure, (the line-width side is
470 * shown faintly with three '.' characters), gives us the
471 * following expression relating miter length, angle and line
474 * 1 /sin (psi/2) = miter_length / line_width
476 * The right-hand side of this relationship is the same ratio
477 * in which the miter limit (ml) is expressed. We want to know
478 * when the miter length is within the miter limit. That is
479 * when the following condition holds:
483 * 1 <= ml² sin²(psi/2)
484 * 2 <= ml² 2 sin²(psi/2)
485 * 2·sin²(psi/2) = 1-cos(psi)
486 * 2 <= ml² (1-cos(psi))
488 * in · out = |in| |out| cos (psi)
490 * in and out are both unit vectors, so:
492 * in · out = cos (psi)
494 * 2 <= ml² (1 - in · out)
497 if (2 <= ml * ml * (1 - in_dot_out)) {
498 double x1, y1, x2, y2;
500 double dx1, dx2, dy1, dy2;
502 double fdx1, fdy1, fdx2, fdy2;
506 * we've got the points already transformed to device
507 * space, but need to do some computation with them and
508 * also need to transform the slope from user space to
511 /* outer point of incoming line face */
512 x1 = _cairo_fixed_to_double (inpt->x);
513 y1 = _cairo_fixed_to_double (inpt->y);
514 dx1 = in->usr_vector.x;
515 dy1 = in->usr_vector.y;
516 cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
518 /* outer point of outgoing line face */
519 x2 = _cairo_fixed_to_double (outpt->x);
520 y2 = _cairo_fixed_to_double (outpt->y);
521 dx2 = out->usr_vector.x;
522 dy2 = out->usr_vector.y;
523 cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
526 * Compute the location of the outer corner of the miter.
527 * That's pretty easy -- just the intersection of the two
528 * outer edges. We've got slopes and points on each
529 * of those edges. Compute my directly, then compute
530 * mx by using the edge with the larger dy; that avoids
531 * dividing by values close to zero.
533 my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
534 (dx1 * dy2 - dx2 * dy1));
535 if (fabs (dy1) >= fabs (dy2))
536 mx = (my - y1) * dx1 / dy1 + x1;
538 mx = (my - y2) * dx2 / dy2 + x2;
541 * When the two outer edges are nearly parallel, slight
542 * perturbations in the position of the outer points of the lines
543 * caused by representing them in fixed point form can cause the
544 * intersection point of the miter to move a large amount. If
545 * that moves the miter intersection from between the two faces,
546 * then draw a bevel instead.
549 ix = _cairo_fixed_to_double (in->point.x);
550 iy = _cairo_fixed_to_double (in->point.y);
552 /* slope of one face */
553 fdx1 = x1 - ix; fdy1 = y1 - iy;
555 /* slope of the other face */
556 fdx2 = x2 - ix; fdy2 = y2 - iy;
558 /* slope from the intersection to the miter point */
559 mdx = mx - ix; mdy = my - iy;
562 * Make sure the miter point line lies between the two
563 * faces by comparing the slopes
565 if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
566 slope_compare_sgn (fdx2, fdy2, mdx, mdy))
570 p.x = _cairo_fixed_from_double (mx);
571 p.y = _cairo_fixed_from_double (my);
573 *_cairo_contour_last_point (&outer->contour) = p;
574 *_cairo_contour_first_point (&outer->contour) = p;
581 case CAIRO_LINE_JOIN_BEVEL:
584 contour_add_point (stroker, outer, outpt);
588 outer_join (struct stroker *stroker,
589 const cairo_stroke_face_t *in,
590 const cairo_stroke_face_t *out,
593 const cairo_point_t *inpt, *outpt;
594 struct stroke_contour *outer;
596 if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
597 in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
604 outer = &stroker->cw;
608 outer = &stroker->ccw;
611 switch (stroker->style.line_join) {
612 case CAIRO_LINE_JOIN_ROUND:
613 /* construct a fan around the common midpoint */
617 &in->point, inpt, outpt,
621 case CAIRO_LINE_JOIN_MITER:
623 /* dot product of incoming slope vector with outgoing slope vector */
624 double in_dot_out = -in->usr_vector.x * out->usr_vector.x +
625 -in->usr_vector.y * out->usr_vector.y;
626 double ml = stroker->style.miter_limit;
628 /* Check the miter limit -- lines meeting at an acute angle
629 * can generate long miters, the limit converts them to bevel
631 * Consider the miter join formed when two line segments
632 * meet at an angle psi:
639 * We can zoom in on the right half of that to see:
657 * The right triangle in that figure, (the line-width side is
658 * shown faintly with three '.' characters), gives us the
659 * following expression relating miter length, angle and line
662 * 1 /sin (psi/2) = miter_length / line_width
664 * The right-hand side of this relationship is the same ratio
665 * in which the miter limit (ml) is expressed. We want to know
666 * when the miter length is within the miter limit. That is
667 * when the following condition holds:
671 * 1 <= ml² sin²(psi/2)
672 * 2 <= ml² 2 sin²(psi/2)
673 * 2·sin²(psi/2) = 1-cos(psi)
674 * 2 <= ml² (1-cos(psi))
676 * in · out = |in| |out| cos (psi)
678 * in and out are both unit vectors, so:
680 * in · out = cos (psi)
682 * 2 <= ml² (1 - in · out)
685 if (2 <= ml * ml * (1 - in_dot_out)) {
686 double x1, y1, x2, y2;
688 double dx1, dx2, dy1, dy2;
690 double fdx1, fdy1, fdx2, fdy2;
694 * we've got the points already transformed to device
695 * space, but need to do some computation with them and
696 * also need to transform the slope from user space to
699 /* outer point of incoming line face */
700 x1 = _cairo_fixed_to_double (inpt->x);
701 y1 = _cairo_fixed_to_double (inpt->y);
702 dx1 = in->usr_vector.x;
703 dy1 = in->usr_vector.y;
704 cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
706 /* outer point of outgoing line face */
707 x2 = _cairo_fixed_to_double (outpt->x);
708 y2 = _cairo_fixed_to_double (outpt->y);
709 dx2 = out->usr_vector.x;
710 dy2 = out->usr_vector.y;
711 cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
714 * Compute the location of the outer corner of the miter.
715 * That's pretty easy -- just the intersection of the two
716 * outer edges. We've got slopes and points on each
717 * of those edges. Compute my directly, then compute
718 * mx by using the edge with the larger dy; that avoids
719 * dividing by values close to zero.
721 my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
722 (dx1 * dy2 - dx2 * dy1));
723 if (fabs (dy1) >= fabs (dy2))
724 mx = (my - y1) * dx1 / dy1 + x1;
726 mx = (my - y2) * dx2 / dy2 + x2;
729 * When the two outer edges are nearly parallel, slight
730 * perturbations in the position of the outer points of the lines
731 * caused by representing them in fixed point form can cause the
732 * intersection point of the miter to move a large amount. If
733 * that moves the miter intersection from between the two faces,
734 * then draw a bevel instead.
737 ix = _cairo_fixed_to_double (in->point.x);
738 iy = _cairo_fixed_to_double (in->point.y);
740 /* slope of one face */
741 fdx1 = x1 - ix; fdy1 = y1 - iy;
743 /* slope of the other face */
744 fdx2 = x2 - ix; fdy2 = y2 - iy;
746 /* slope from the intersection to the miter point */
747 mdx = mx - ix; mdy = my - iy;
750 * Make sure the miter point line lies between the two
751 * faces by comparing the slopes
753 if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
754 slope_compare_sgn (fdx2, fdy2, mdx, mdy))
758 p.x = _cairo_fixed_from_double (mx);
759 p.y = _cairo_fixed_from_double (my);
761 *_cairo_contour_last_point (&outer->contour) = p;
768 case CAIRO_LINE_JOIN_BEVEL:
771 contour_add_point (stroker,outer, outpt);
775 add_cap (struct stroker *stroker,
776 const cairo_stroke_face_t *f,
777 struct stroke_contour *c)
779 switch (stroker->style.line_cap) {
780 case CAIRO_LINE_CAP_ROUND: {
783 slope.dx = -f->dev_vector.dx;
784 slope.dy = -f->dev_vector.dy;
786 add_fan (stroker, &f->dev_vector, &slope,
787 &f->point, &f->ccw, &f->cw,
792 case CAIRO_LINE_CAP_SQUARE: {
794 cairo_slope_t fvector;
795 cairo_point_t quad[4];
797 dx = f->usr_vector.x;
798 dy = f->usr_vector.y;
799 dx *= stroker->style.line_width / 2.0;
800 dy *= stroker->style.line_width / 2.0;
801 cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
802 fvector.dx = _cairo_fixed_from_double (dx);
803 fvector.dy = _cairo_fixed_from_double (dy);
806 quad[1].x = f->ccw.x + fvector.dx;
807 quad[1].y = f->ccw.y + fvector.dy;
808 quad[2].x = f->cw.x + fvector.dx;
809 quad[2].y = f->cw.y + fvector.dy;
812 contour_add_point (stroker, c, &quad[1]);
813 contour_add_point (stroker, c, &quad[2]);
816 case CAIRO_LINE_CAP_BUTT:
820 contour_add_point (stroker, c, &f->cw);
824 add_leading_cap (struct stroker *stroker,
825 const cairo_stroke_face_t *face,
826 struct stroke_contour *c)
828 cairo_stroke_face_t reversed;
833 /* The initial cap needs an outward facing vector. Reverse everything */
834 reversed.usr_vector.x = -reversed.usr_vector.x;
835 reversed.usr_vector.y = -reversed.usr_vector.y;
836 reversed.dev_vector.dx = -reversed.dev_vector.dx;
837 reversed.dev_vector.dy = -reversed.dev_vector.dy;
840 reversed.cw = reversed.ccw;
843 add_cap (stroker, &reversed, c);
847 add_trailing_cap (struct stroker *stroker,
848 const cairo_stroke_face_t *face,
849 struct stroke_contour *c)
851 add_cap (stroker, face, c);
855 normalize_slope (double *dx, double *dy)
857 double dx0 = *dx, dy0 = *dy;
860 assert (dx0 != 0.0 || dy0 != 0.0);
871 } else if (dy0 == 0.0) {
881 mag = hypot (dx0, dy0);
890 compute_face (const cairo_point_t *point,
891 const cairo_slope_t *dev_slope,
892 struct stroker *stroker,
893 cairo_stroke_face_t *face)
895 double face_dx, face_dy;
896 cairo_point_t offset_ccw, offset_cw;
897 double slope_dx, slope_dy;
899 slope_dx = _cairo_fixed_to_double (dev_slope->dx);
900 slope_dy = _cairo_fixed_to_double (dev_slope->dy);
901 face->length = normalize_slope (&slope_dx, &slope_dy);
902 face->dev_slope.x = slope_dx;
903 face->dev_slope.y = slope_dy;
906 * rotate to get a line_width/2 vector along the face, note that
907 * the vector must be rotated the right direction in device space,
908 * but by 90° in user space. So, the rotation depends on
909 * whether the ctm reflects or not, and that can be determined
910 * by looking at the determinant of the matrix.
912 if (! _cairo_matrix_is_identity (stroker->ctm_inverse)) {
913 /* Normalize the matrix! */
914 cairo_matrix_transform_distance (stroker->ctm_inverse,
915 &slope_dx, &slope_dy);
916 normalize_slope (&slope_dx, &slope_dy);
918 if (stroker->ctm_det_positive)
920 face_dx = - slope_dy * (stroker->style.line_width / 2.0);
921 face_dy = slope_dx * (stroker->style.line_width / 2.0);
925 face_dx = slope_dy * (stroker->style.line_width / 2.0);
926 face_dy = - slope_dx * (stroker->style.line_width / 2.0);
929 /* back to device space */
930 cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
932 face_dx = - slope_dy * (stroker->style.line_width / 2.0);
933 face_dy = slope_dx * (stroker->style.line_width / 2.0);
936 offset_ccw.x = _cairo_fixed_from_double (face_dx);
937 offset_ccw.y = _cairo_fixed_from_double (face_dy);
938 offset_cw.x = -offset_ccw.x;
939 offset_cw.y = -offset_ccw.y;
942 translate_point (&face->ccw, &offset_ccw);
944 face->point = *point;
947 translate_point (&face->cw, &offset_cw);
949 face->usr_vector.x = slope_dx;
950 face->usr_vector.y = slope_dy;
952 face->dev_vector = *dev_slope;
956 add_caps (struct stroker *stroker)
958 /* check for a degenerative sub_path */
959 if (stroker->has_initial_sub_path &&
960 ! stroker->has_first_face &&
961 ! stroker->has_current_face &&
962 stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
964 /* pick an arbitrary slope to use */
965 cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
966 cairo_stroke_face_t face;
968 /* arbitrarily choose first_point */
969 compute_face (&stroker->first_point, &slope, stroker, &face);
971 add_leading_cap (stroker, &face, &stroker->ccw);
972 add_trailing_cap (stroker, &face, &stroker->ccw);
974 /* ensure the circle is complete */
975 _cairo_contour_add_point (&stroker->ccw.contour,
976 _cairo_contour_first_point (&stroker->ccw.contour));
978 _cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
979 _cairo_contour_reset (&stroker->ccw.contour);
981 if (stroker->has_current_face)
982 add_trailing_cap (stroker, &stroker->current_face, &stroker->ccw);
986 FILE *file = fopen ("contours.txt", "a");
987 _cairo_debug_print_contour (file, &stroker->path);
988 _cairo_debug_print_contour (file, &stroker->cw.contour);
989 _cairo_debug_print_contour (file, &stroker->ccw.contour);
991 _cairo_contour_reset (&stroker->path);
995 _cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
996 _cairo_contour_reset (&stroker->ccw.contour);
998 if (stroker->has_first_face) {
999 _cairo_contour_add_point (&stroker->ccw.contour,
1000 &stroker->first_face.cw);
1001 add_leading_cap (stroker, &stroker->first_face, &stroker->ccw);
1004 FILE *file = fopen ("contours.txt", "a");
1005 _cairo_debug_print_contour (file, &stroker->ccw.contour);
1010 _cairo_polygon_add_contour (stroker->polygon,
1011 &stroker->ccw.contour);
1012 _cairo_contour_reset (&stroker->ccw.contour);
1015 _cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
1016 _cairo_contour_reset (&stroker->cw.contour);
1020 static cairo_status_t
1021 close_path (void *closure);
1023 static cairo_status_t
1024 move_to (void *closure,
1025 const cairo_point_t *point)
1027 struct stroker *stroker = closure;
1029 /* Cap the start and end of the previous sub path as needed */
1032 stroker->has_first_face = FALSE;
1033 stroker->has_current_face = FALSE;
1034 stroker->has_initial_sub_path = FALSE;
1036 stroker->first_point = *point;
1039 _cairo_contour_add_point (&stroker->path, point);
1042 stroker->current_face.point = *point;
1044 return CAIRO_STATUS_SUCCESS;
1047 static cairo_status_t
1048 line_to (void *closure,
1049 const cairo_point_t *point)
1051 struct stroker *stroker = closure;
1052 cairo_stroke_face_t start;
1053 cairo_point_t *p1 = &stroker->current_face.point;
1054 cairo_slope_t dev_slope;
1056 stroker->has_initial_sub_path = TRUE;
1058 if (p1->x == point->x && p1->y == point->y)
1059 return CAIRO_STATUS_SUCCESS;
1062 _cairo_contour_add_point (&stroker->path, point);
1065 _cairo_slope_init (&dev_slope, p1, point);
1066 compute_face (p1, &dev_slope, stroker, &start);
1068 if (stroker->has_current_face) {
1069 int clockwise = _cairo_slope_compare (&stroker->current_face.dev_vector,
1072 clockwise = clockwise < 0;
1073 /* Join with final face from previous segment */
1074 if (! within_tolerance (&stroker->current_face.ccw, &start.ccw,
1075 stroker->contour_tolerance) ||
1076 ! within_tolerance (&stroker->current_face.cw, &start.cw,
1077 stroker->contour_tolerance))
1079 outer_join (stroker, &stroker->current_face, &start, clockwise);
1080 inner_join (stroker, &stroker->current_face, &start, clockwise);
1084 if (! stroker->has_first_face) {
1085 /* Save sub path's first face in case needed for closing join */
1086 stroker->first_face = start;
1087 stroker->has_first_face = TRUE;
1089 stroker->has_current_face = TRUE;
1091 contour_add_point (stroker, &stroker->cw, &start.cw);
1092 contour_add_point (stroker, &stroker->ccw, &start.ccw);
1095 stroker->current_face = start;
1096 stroker->current_face.point = *point;
1097 stroker->current_face.ccw.x += dev_slope.dx;
1098 stroker->current_face.ccw.y += dev_slope.dy;
1099 stroker->current_face.cw.x += dev_slope.dx;
1100 stroker->current_face.cw.y += dev_slope.dy;
1102 contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
1103 contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
1105 return CAIRO_STATUS_SUCCESS;
1108 static cairo_status_t
1109 spline_to (void *closure,
1110 const cairo_point_t *point,
1111 const cairo_slope_t *tangent)
1113 struct stroker *stroker = closure;
1114 cairo_stroke_face_t face;
1117 _cairo_contour_add_point (&stroker->path, point);
1119 if (tangent->dx == 0 && tangent->dy == 0) {
1120 const cairo_point_t *inpt, *outpt;
1121 struct stroke_contour *outer;
1125 face = stroker->current_face;
1127 face.usr_vector.x = -face.usr_vector.x;
1128 face.usr_vector.y = -face.usr_vector.y;
1129 face.dev_vector.dx = -face.dev_vector.dx;
1130 face.dev_vector.dy = -face.dev_vector.dy;
1136 clockwise = join_is_clockwise (&stroker->current_face, &face);
1138 inpt = &stroker->current_face.cw;
1140 outer = &stroker->cw;
1142 inpt = &stroker->current_face.ccw;
1144 outer = &stroker->ccw;
1148 &stroker->current_face.dev_vector,
1150 &stroker->current_face.point, inpt, outpt,
1153 compute_face (point, tangent, stroker, &face);
1155 if (face.dev_slope.x * stroker->current_face.dev_slope.x +
1156 face.dev_slope.y * stroker->current_face.dev_slope.y < 0)
1158 const cairo_point_t *inpt, *outpt;
1159 struct stroke_contour *outer;
1160 int clockwise = join_is_clockwise (&stroker->current_face, &face);
1162 stroker->current_face.cw.x += face.point.x - stroker->current_face.point.x;
1163 stroker->current_face.cw.y += face.point.y - stroker->current_face.point.y;
1164 contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
1166 stroker->current_face.ccw.x += face.point.x - stroker->current_face.point.x;
1167 stroker->current_face.ccw.y += face.point.y - stroker->current_face.point.y;
1168 contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
1171 inpt = &stroker->current_face.cw;
1173 outer = &stroker->cw;
1175 inpt = &stroker->current_face.ccw;
1177 outer = &stroker->ccw;
1180 &stroker->current_face.dev_vector,
1182 &stroker->current_face.point, inpt, outpt,
1186 contour_add_point (stroker, &stroker->cw, &face.cw);
1187 contour_add_point (stroker, &stroker->ccw, &face.ccw);
1190 stroker->current_face = face;
1192 return CAIRO_STATUS_SUCCESS;
1195 static cairo_status_t
1196 curve_to (void *closure,
1197 const cairo_point_t *b,
1198 const cairo_point_t *c,
1199 const cairo_point_t *d)
1201 struct stroker *stroker = closure;
1202 cairo_spline_t spline;
1203 cairo_stroke_face_t face;
1205 if (! _cairo_spline_init (&spline, spline_to, stroker,
1206 &stroker->current_face.point, b, c, d))
1207 return line_to (closure, d);
1209 compute_face (&stroker->current_face.point, &spline.initial_slope,
1212 if (stroker->has_current_face) {
1213 int clockwise = join_is_clockwise (&stroker->current_face, &face);
1214 /* Join with final face from previous segment */
1215 outer_join (stroker, &stroker->current_face, &face, clockwise);
1216 inner_join (stroker, &stroker->current_face, &face, clockwise);
1218 if (! stroker->has_first_face) {
1219 /* Save sub path's first face in case needed for closing join */
1220 stroker->first_face = face;
1221 stroker->has_first_face = TRUE;
1223 stroker->has_current_face = TRUE;
1225 contour_add_point (stroker, &stroker->cw, &face.cw);
1226 contour_add_point (stroker, &stroker->ccw, &face.ccw);
1228 stroker->current_face = face;
1230 return _cairo_spline_decompose (&spline, stroker->tolerance);
1233 static cairo_status_t
1234 close_path (void *closure)
1236 struct stroker *stroker = closure;
1237 cairo_status_t status;
1239 status = line_to (stroker, &stroker->first_point);
1240 if (unlikely (status))
1243 if (stroker->has_first_face && stroker->has_current_face) {
1244 /* Join first and final faces of sub path */
1245 outer_close (stroker, &stroker->current_face, &stroker->first_face);
1246 inner_close (stroker, &stroker->current_face, &stroker->first_face);
1248 *_cairo_contour_first_point (&stroker->ccw.contour) =
1249 *_cairo_contour_last_point (&stroker->ccw.contour);
1251 *_cairo_contour_first_point (&stroker->cw.contour) =
1252 *_cairo_contour_last_point (&stroker->cw.contour);
1255 _cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
1256 _cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
1260 FILE *file = fopen ("contours.txt", "a");
1261 _cairo_debug_print_contour (file, &stroker->path);
1262 _cairo_debug_print_contour (file, &stroker->cw.contour);
1263 _cairo_debug_print_contour (file, &stroker->ccw.contour);
1266 _cairo_contour_reset (&stroker->path);
1269 _cairo_contour_reset (&stroker->cw.contour);
1270 _cairo_contour_reset (&stroker->ccw.contour);
1272 /* Cap the start and end of the sub path as needed */
1276 stroker->has_initial_sub_path = FALSE;
1277 stroker->has_first_face = FALSE;
1278 stroker->has_current_face = FALSE;
1280 return CAIRO_STATUS_SUCCESS;
1284 _cairo_path_fixed_stroke_to_polygon (const cairo_path_fixed_t *path,
1285 const cairo_stroke_style_t *style,
1286 const cairo_matrix_t *ctm,
1287 const cairo_matrix_t *ctm_inverse,
1289 cairo_polygon_t *polygon)
1291 struct stroker stroker;
1292 cairo_status_t status;
1294 if (style->num_dashes) {
1295 return _cairo_path_fixed_stroke_dashed_to_polygon (path,
1303 stroker.style = *style;
1305 stroker.ctm_inverse = ctm_inverse;
1306 stroker.tolerance = tolerance;
1308 stroker.ctm_det_positive =
1309 _cairo_matrix_compute_determinant (ctm) >= 0.0;
1311 stroker.pen.num_vertices = 0;
1312 if (path->has_curve_to ||
1313 style->line_join == CAIRO_LINE_JOIN_ROUND ||
1314 style->line_cap == CAIRO_LINE_CAP_ROUND) {
1315 status = _cairo_pen_init (&stroker.pen,
1316 style->line_width / 2.0,
1318 if (unlikely (status))
1321 /* If the line width is so small that the pen is reduced to a
1322 single point, then we have nothing to do. */
1323 if (stroker.pen.num_vertices <= 1)
1324 return CAIRO_STATUS_SUCCESS;
1327 stroker.has_current_face = FALSE;
1328 stroker.has_first_face = FALSE;
1329 stroker.has_initial_sub_path = FALSE;
1332 remove ("contours.txt");
1333 remove ("polygons.txt");
1334 _cairo_contour_init (&stroker.path, 0);
1336 _cairo_contour_init (&stroker.cw.contour, 1);
1337 _cairo_contour_init (&stroker.ccw.contour, -1);
1338 tolerance *= CAIRO_FIXED_ONE;
1339 tolerance *= tolerance;
1340 stroker.contour_tolerance = tolerance;
1341 stroker.polygon = polygon;
1343 status = _cairo_path_fixed_interpret (path,
1349 /* Cap the start and end of the final sub path as needed */
1350 if (likely (status == CAIRO_STATUS_SUCCESS))
1351 add_caps (&stroker);
1353 _cairo_contour_fini (&stroker.cw.contour);
1354 _cairo_contour_fini (&stroker.ccw.contour);
1355 if (stroker.pen.num_vertices)
1356 _cairo_pen_fini (&stroker.pen);
1360 FILE *file = fopen ("polygons.txt", "a");
1361 _cairo_debug_print_polygon (file, polygon);