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 © 2005 Red Hat, Inc.
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>
41 #include "cairo-box-inline.h"
42 #include "cairo-error-private.h"
43 #include "cairo-list-inline.h"
44 #include "cairo-path-fixed-private.h"
45 #include "cairo-slope-private.h"
48 _cairo_path_fixed_add (cairo_path_fixed_t *path,
50 const cairo_point_t *points,
54 _cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
55 cairo_path_buf_t *buf);
57 static cairo_path_buf_t *
58 _cairo_path_buf_create (int size_ops, int size_points);
61 _cairo_path_buf_destroy (cairo_path_buf_t *buf);
64 _cairo_path_buf_add_op (cairo_path_buf_t *buf,
68 _cairo_path_buf_add_points (cairo_path_buf_t *buf,
69 const cairo_point_t *points,
73 _cairo_path_fixed_init (cairo_path_fixed_t *path)
75 VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
77 cairo_list_init (&path->buf.base.link);
79 path->buf.base.num_ops = 0;
80 path->buf.base.num_points = 0;
81 path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
82 path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
83 path->buf.base.op = path->buf.op;
84 path->buf.base.points = path->buf.points;
86 path->current_point.x = 0;
87 path->current_point.y = 0;
88 path->last_move_point = path->current_point;
89 path->start_point = path->current_point;
91 path->has_current_point = FALSE;
92 path->needs_move_to = TRUE;
93 path->has_extents = FALSE;
94 path->has_curve_to = FALSE;
95 path->stroke_is_rectilinear = TRUE;
96 path->fill_is_rectilinear = TRUE;
97 path->fill_maybe_region = TRUE;
98 path->fill_is_empty = TRUE;
99 path->is_convex = TRUE;
101 path->extents.p1.x = path->extents.p1.y = 0;
102 path->extents.p2.x = path->extents.p2.y = 0;
106 _cairo_path_fixed_init_copy (cairo_path_fixed_t *path,
107 const cairo_path_fixed_t *other)
109 cairo_path_buf_t *buf, *other_buf;
110 unsigned int num_points, num_ops;
112 VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
114 cairo_list_init (&path->buf.base.link);
116 path->buf.base.op = path->buf.op;
117 path->buf.base.points = path->buf.points;
118 path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
119 path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
121 path->current_point = other->current_point;
122 path->last_move_point = other->last_move_point;
124 path->has_current_point = other->has_current_point;
125 path->needs_move_to = other->needs_move_to;
126 path->has_extents = other->has_extents;
127 path->has_curve_to = other->has_curve_to;
128 path->stroke_is_rectilinear = other->stroke_is_rectilinear;
129 path->fill_is_rectilinear = other->fill_is_rectilinear;
130 path->fill_maybe_region = other->fill_maybe_region;
131 path->fill_is_empty = other->fill_is_empty;
133 path->extents = other->extents;
135 path->buf.base.num_ops = other->buf.base.num_ops;
136 path->buf.base.num_points = other->buf.base.num_points;
138 path->is_convex = other->is_convex;
140 memcpy (path->buf.op, other->buf.base.op,
141 other->buf.base.num_ops * sizeof (other->buf.op[0]));
142 memcpy (path->buf.points, other->buf.points,
143 other->buf.base.num_points * sizeof (other->buf.points[0]));
145 num_points = num_ops = 0;
146 for (other_buf = cairo_path_buf_next (cairo_path_head (other));
147 other_buf != cairo_path_head (other);
148 other_buf = cairo_path_buf_next (other_buf))
150 num_ops += other_buf->num_ops;
151 num_points += other_buf->num_points;
155 buf = _cairo_path_buf_create (num_ops, num_points);
156 if (unlikely (buf == NULL)) {
157 _cairo_path_fixed_fini (path);
158 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
161 for (other_buf = cairo_path_buf_next (cairo_path_head (other));
162 other_buf != cairo_path_head (other);
163 other_buf = cairo_path_buf_next (other_buf))
165 memcpy (buf->op + buf->num_ops, other_buf->op,
166 other_buf->num_ops * sizeof (buf->op[0]));
167 buf->num_ops += other_buf->num_ops;
169 memcpy (buf->points + buf->num_points, other_buf->points,
170 other_buf->num_points * sizeof (buf->points[0]));
171 buf->num_points += other_buf->num_points;
174 _cairo_path_fixed_add_buf (path, buf);
177 return CAIRO_STATUS_SUCCESS;
181 _cairo_path_fixed_hash (const cairo_path_fixed_t *path)
183 unsigned long hash = _CAIRO_HASH_INIT_VALUE;
184 const cairo_path_buf_t *buf;
188 cairo_path_foreach_buf_start (buf, path) {
189 hash = _cairo_hash_bytes (hash, buf->op,
190 buf->num_ops * sizeof (buf->op[0]));
191 count += buf->num_ops;
192 } cairo_path_foreach_buf_end (buf, path);
193 hash = _cairo_hash_bytes (hash, &count, sizeof (count));
196 cairo_path_foreach_buf_start (buf, path) {
197 hash = _cairo_hash_bytes (hash, buf->points,
198 buf->num_points * sizeof (buf->points[0]));
199 count += buf->num_points;
200 } cairo_path_foreach_buf_end (buf, path);
201 hash = _cairo_hash_bytes (hash, &count, sizeof (count));
207 _cairo_path_fixed_size (const cairo_path_fixed_t *path)
209 const cairo_path_buf_t *buf;
210 int num_points, num_ops;
212 num_ops = num_points = 0;
213 cairo_path_foreach_buf_start (buf, path) {
214 num_ops += buf->num_ops;
215 num_points += buf->num_points;
216 } cairo_path_foreach_buf_end (buf, path);
218 return num_ops * sizeof (buf->op[0]) +
219 num_points * sizeof (buf->points[0]);
223 _cairo_path_fixed_equal (const cairo_path_fixed_t *a,
224 const cairo_path_fixed_t *b)
226 const cairo_path_buf_t *buf_a, *buf_b;
227 const cairo_path_op_t *ops_a, *ops_b;
228 const cairo_point_t *points_a, *points_b;
229 int num_points_a, num_ops_a;
230 int num_points_b, num_ops_b;
235 /* use the flags to quickly differentiate based on contents */
236 if (a->has_curve_to != b->has_curve_to)
241 if (a->extents.p1.x != b->extents.p1.x ||
242 a->extents.p1.y != b->extents.p1.y ||
243 a->extents.p2.x != b->extents.p2.x ||
244 a->extents.p2.y != b->extents.p2.y)
249 num_ops_a = num_points_a = 0;
250 cairo_path_foreach_buf_start (buf_a, a) {
251 num_ops_a += buf_a->num_ops;
252 num_points_a += buf_a->num_points;
253 } cairo_path_foreach_buf_end (buf_a, a);
255 num_ops_b = num_points_b = 0;
256 cairo_path_foreach_buf_start (buf_b, b) {
257 num_ops_b += buf_b->num_ops;
258 num_points_b += buf_b->num_points;
259 } cairo_path_foreach_buf_end (buf_b, b);
261 if (num_ops_a == 0 && num_ops_b == 0)
264 if (num_ops_a != num_ops_b || num_points_a != num_points_b)
267 buf_a = cairo_path_head (a);
268 num_points_a = buf_a->num_points;
269 num_ops_a = buf_a->num_ops;
271 points_a = buf_a->points;
273 buf_b = cairo_path_head (b);
274 num_points_b = buf_b->num_points;
275 num_ops_b = buf_b->num_ops;
277 points_b = buf_b->points;
280 int num_ops = MIN (num_ops_a, num_ops_b);
281 int num_points = MIN (num_points_a, num_points_b);
283 if (memcmp (ops_a, ops_b, num_ops * sizeof (cairo_path_op_t)))
285 if (memcmp (points_a, points_b, num_points * sizeof (cairo_point_t)))
288 num_ops_a -= num_ops;
290 num_points_a -= num_points;
291 points_a += num_points;
292 if (num_ops_a == 0 || num_points_a == 0) {
293 if (num_ops_a || num_points_a)
296 buf_a = cairo_path_buf_next (buf_a);
297 if (buf_a == cairo_path_head (a))
300 num_points_a = buf_a->num_points;
301 num_ops_a = buf_a->num_ops;
303 points_a = buf_a->points;
306 num_ops_b -= num_ops;
308 num_points_b -= num_points;
309 points_b += num_points;
310 if (num_ops_b == 0 || num_points_b == 0) {
311 if (num_ops_b || num_points_b)
314 buf_b = cairo_path_buf_next (buf_b);
315 if (buf_b == cairo_path_head (b))
318 num_points_b = buf_b->num_points;
319 num_ops_b = buf_b->num_ops;
321 points_b = buf_b->points;
329 _cairo_path_fixed_create (void)
331 cairo_path_fixed_t *path;
333 path = malloc (sizeof (cairo_path_fixed_t));
335 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
339 _cairo_path_fixed_init (path);
344 _cairo_path_fixed_fini (cairo_path_fixed_t *path)
346 cairo_path_buf_t *buf;
348 buf = cairo_path_buf_next (cairo_path_head (path));
349 while (buf != cairo_path_head (path)) {
350 cairo_path_buf_t *this = buf;
351 buf = cairo_path_buf_next (buf);
352 _cairo_path_buf_destroy (this);
355 VG (VALGRIND_MAKE_MEM_NOACCESS (path, sizeof (cairo_path_fixed_t)));
359 _cairo_path_fixed_destroy (cairo_path_fixed_t *path)
361 _cairo_path_fixed_fini (path);
365 static cairo_path_op_t
366 _cairo_path_fixed_last_op (cairo_path_fixed_t *path)
368 cairo_path_buf_t *buf;
370 buf = cairo_path_tail (path);
371 assert (buf->num_ops != 0);
373 return buf->op[buf->num_ops - 1];
376 static inline const cairo_point_t *
377 _cairo_path_fixed_penultimate_point (cairo_path_fixed_t *path)
379 cairo_path_buf_t *buf;
381 buf = cairo_path_tail (path);
382 if (likely (buf->num_points >= 2)) {
383 return &buf->points[buf->num_points - 2];
385 cairo_path_buf_t *prev_buf = cairo_path_buf_prev (buf);
387 assert (prev_buf->num_points >= 2 - buf->num_points);
388 return &prev_buf->points[prev_buf->num_points - (2 - buf->num_points)];
393 _cairo_path_fixed_drop_line_to (cairo_path_fixed_t *path)
395 cairo_path_buf_t *buf;
397 assert (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO);
399 buf = cairo_path_tail (path);
405 _cairo_path_fixed_move_to (cairo_path_fixed_t *path,
409 _cairo_path_fixed_new_sub_path (path);
411 path->has_current_point = TRUE;
412 path->current_point.x = x;
413 path->current_point.y = y;
414 path->last_move_point = path->current_point;
416 if (_cairo_path_fixed_is_empty (path))
417 path->is_convex = TRUE;
419 path->is_convex = FALSE;
421 return CAIRO_STATUS_SUCCESS;
424 static cairo_status_t
425 _cairo_path_fixed_move_to_apply (cairo_path_fixed_t *path)
427 if (likely (! path->needs_move_to))
428 return CAIRO_STATUS_SUCCESS;
430 path->needs_move_to = FALSE;
432 if (path->has_extents) {
433 _cairo_box_add_point (&path->extents, &path->current_point);
435 _cairo_box_set (&path->extents, &path->current_point, &path->current_point);
436 path->has_extents = TRUE;
439 if (path->fill_maybe_region) {
440 path->fill_maybe_region = _cairo_fixed_is_integer (path->current_point.x) &&
441 _cairo_fixed_is_integer (path->current_point.y);
444 path->last_move_point = path->current_point;
446 return _cairo_path_fixed_add (path, CAIRO_PATH_OP_MOVE_TO, &path->current_point, 1);
450 _cairo_path_fixed_new_sub_path (cairo_path_fixed_t *path)
452 if (! path->needs_move_to) {
453 /* If the current subpath doesn't need_move_to, it contains at least one command */
454 if (path->fill_is_rectilinear) {
455 /* Implicitly close for fill */
456 path->fill_is_rectilinear = path->current_point.x == path->last_move_point.x ||
457 path->current_point.y == path->last_move_point.y;
458 path->fill_maybe_region &= path->fill_is_rectilinear;
460 path->needs_move_to = TRUE;
463 if (_cairo_path_fixed_is_empty (path))
464 path->is_convex = TRUE;
466 path->is_convex = FALSE;
468 path->has_current_point = FALSE;
472 _cairo_path_fixed_rel_move_to (cairo_path_fixed_t *path,
476 if (unlikely (! path->has_current_point))
477 return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
479 return _cairo_path_fixed_move_to (path,
480 path->current_point.x + dx,
481 path->current_point.y + dy);
486 _cairo_path_fixed_line_to (cairo_path_fixed_t *path,
490 cairo_status_t status;
496 /* When there is not yet a current point, the line_to operation
497 * becomes a move_to instead. Note: We have to do this by
498 * explicitly calling into _cairo_path_fixed_move_to to ensure
499 * that the last_move_point state is updated properly.
501 if (! path->has_current_point)
502 return _cairo_path_fixed_move_to (path, point.x, point.y);
504 status = _cairo_path_fixed_move_to_apply (path);
505 if (unlikely (status))
508 /* If the previous op was but the initial MOVE_TO and this segment
509 * is degenerate, then we can simply skip this point. Note that
510 * a move-to followed by a degenerate line-to is a valid path for
511 * stroking, but at all other times is simply a degenerate segment.
513 if (_cairo_path_fixed_last_op (path) != CAIRO_PATH_OP_MOVE_TO) {
514 if (x == path->current_point.x && y == path->current_point.y)
515 return CAIRO_STATUS_SUCCESS;
518 /* If the previous op was also a LINE_TO with the same gradient,
519 * then just change its end-point rather than adding a new op.
521 if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
522 const cairo_point_t *p;
524 p = _cairo_path_fixed_penultimate_point (path);
525 if (p->x == path->current_point.x && p->y == path->current_point.y) {
526 /* previous line element was degenerate, replace */
527 _cairo_path_fixed_drop_line_to (path);
529 cairo_slope_t prev, self;
531 _cairo_slope_init (&prev, p, &path->current_point);
532 _cairo_slope_init (&self, &path->current_point, &point);
533 if (_cairo_slope_equal (&prev, &self) &&
534 /* cannot trim anti-parallel segments whilst stroking */
535 ! _cairo_slope_backwards (&prev, &self))
537 _cairo_path_fixed_drop_line_to (path);
538 /* In this case the flags might be more restrictive than
539 * what we actually need.
540 * When changing the flags definition we should check if
541 * changing the line_to point can affect them.
547 if (path->stroke_is_rectilinear) {
548 path->stroke_is_rectilinear = path->current_point.x == x ||
549 path->current_point.y == y;
550 path->fill_is_rectilinear &= path->stroke_is_rectilinear;
551 path->fill_maybe_region &= path->fill_is_rectilinear;
552 if (path->fill_maybe_region) {
553 path->fill_maybe_region = _cairo_fixed_is_integer (x) &&
554 _cairo_fixed_is_integer (y);
556 if (path->fill_is_empty) {
557 path->fill_is_empty = path->current_point.x == x &&
558 path->current_point.y == y;
562 path->current_point = point;
564 _cairo_box_add_point (&path->extents, &point);
566 /* if line to point does not match start point, is_convex false */
567 if (path->is_convex) {
568 if (path->start_point.x != x &&
569 path->start_point.y != y)
570 path->is_convex = FALSE;
573 return _cairo_path_fixed_add (path, CAIRO_PATH_OP_LINE_TO, &point, 1);
577 _cairo_path_fixed_rel_line_to (cairo_path_fixed_t *path,
581 if (unlikely (! path->has_current_point))
582 return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
584 return _cairo_path_fixed_line_to (path,
585 path->current_point.x + dx,
586 path->current_point.y + dy);
590 _cairo_path_fixed_curve_to (cairo_path_fixed_t *path,
591 cairo_fixed_t x0, cairo_fixed_t y0,
592 cairo_fixed_t x1, cairo_fixed_t y1,
593 cairo_fixed_t x2, cairo_fixed_t y2)
595 cairo_status_t status;
596 cairo_point_t point[3];
598 /* If this curves does not move, replace it with a line-to.
599 * This frequently happens with rounded-rectangles and r==0.
601 if (path->current_point.x == x2 && path->current_point.y == y2) {
602 if (x1 == x2 && x0 == x2 && y1 == y2 && y0 == y2)
603 return _cairo_path_fixed_line_to (path, x2, y2);
605 /* We may want to check for the absence of a cusp, in which case
606 * we can also replace the curve-to with a line-to.
610 /* make sure subpaths are started properly */
611 if (! path->has_current_point) {
612 status = _cairo_path_fixed_move_to (path, x0, y0);
613 assert (status == CAIRO_STATUS_SUCCESS);
616 status = _cairo_path_fixed_move_to_apply (path);
617 if (unlikely (status))
620 /* If the previous op was a degenerate LINE_TO, drop it. */
621 if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
622 const cairo_point_t *p;
624 p = _cairo_path_fixed_penultimate_point (path);
625 if (p->x == path->current_point.x && p->y == path->current_point.y) {
626 /* previous line element was degenerate, replace */
627 _cairo_path_fixed_drop_line_to (path);
631 point[0].x = x0; point[0].y = y0;
632 point[1].x = x1; point[1].y = y1;
633 point[2].x = x2; point[2].y = y2;
635 _cairo_box_add_curve_to (&path->extents, &path->current_point,
636 &point[0], &point[1], &point[2]);
638 path->current_point = point[2];
639 path->has_curve_to = TRUE;
640 path->stroke_is_rectilinear = FALSE;
641 path->fill_is_rectilinear = FALSE;
642 path->fill_maybe_region = FALSE;
643 path->fill_is_empty = FALSE;
645 path->is_convex = FALSE;
647 return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CURVE_TO, point, 3);
651 _cairo_path_fixed_rel_curve_to (cairo_path_fixed_t *path,
652 cairo_fixed_t dx0, cairo_fixed_t dy0,
653 cairo_fixed_t dx1, cairo_fixed_t dy1,
654 cairo_fixed_t dx2, cairo_fixed_t dy2)
656 if (unlikely (! path->has_current_point))
657 return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
659 return _cairo_path_fixed_curve_to (path,
660 path->current_point.x + dx0,
661 path->current_point.y + dy0,
663 path->current_point.x + dx1,
664 path->current_point.y + dy1,
666 path->current_point.x + dx2,
667 path->current_point.y + dy2);
671 _cairo_path_fixed_close_path (cairo_path_fixed_t *path)
673 cairo_status_t status;
675 if (! path->has_current_point)
676 return CAIRO_STATUS_SUCCESS;
679 * Add a line_to, to compute flags and solve any degeneracy.
680 * It will be removed later (if it was actually added).
682 status = _cairo_path_fixed_line_to (path,
683 path->last_move_point.x,
684 path->last_move_point.y);
685 if (unlikely (status))
689 * If the command used to close the path is a line_to, drop it.
690 * We must check that last command is actually a line_to,
691 * because the path could have been closed with a curve_to (and
692 * the previous line_to not added as it would be degenerate).
694 if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO)
695 _cairo_path_fixed_drop_line_to (path);
697 path->needs_move_to = TRUE; /* After close_path, add an implicit move_to */
699 return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CLOSE_PATH, NULL, 0);
703 _cairo_path_fixed_get_current_point (cairo_path_fixed_t *path,
707 if (! path->has_current_point)
710 *x = path->current_point.x;
711 *y = path->current_point.y;
716 static cairo_status_t
717 _cairo_path_fixed_add (cairo_path_fixed_t *path,
719 const cairo_point_t *points,
722 cairo_path_buf_t *buf = cairo_path_tail (path);
724 if (buf->num_ops + 1 > buf->size_ops ||
725 buf->num_points + num_points > buf->size_points)
727 buf = _cairo_path_buf_create (buf->num_ops * 2, buf->num_points * 2);
728 if (unlikely (buf == NULL))
729 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
731 _cairo_path_fixed_add_buf (path, buf);
735 const char *op_str[] = {
745 len += snprintf (buf + len, sizeof (buf), "[");
746 for (i = 0; i < num_points; i++) {
748 len += snprintf (buf + len, sizeof (buf), " ");
749 len += snprintf (buf + len, sizeof (buf), "(%f, %f)",
750 _cairo_fixed_to_double (points[i].x),
751 _cairo_fixed_to_double (points[i].y));
753 len += snprintf (buf + len, sizeof (buf), "]");
755 #define STRINGIFYFLAG(x) (path->x ? #x " " : "")
757 "_cairo_path_fixed_add (%s, %s) [%s%s%s%s%s%s%s%s]\n",
758 op_str[(int) op], buf,
759 STRINGIFYFLAG(has_current_point),
760 STRINGIFYFLAG(needs_move_to),
761 STRINGIFYFLAG(has_extents),
762 STRINGIFYFLAG(has_curve_to),
763 STRINGIFYFLAG(stroke_is_rectilinear),
764 STRINGIFYFLAG(fill_is_rectilinear),
765 STRINGIFYFLAG(fill_is_empty),
766 STRINGIFYFLAG(fill_maybe_region)
771 _cairo_path_buf_add_op (buf, op);
772 _cairo_path_buf_add_points (buf, points, num_points);
774 return CAIRO_STATUS_SUCCESS;
778 _cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
779 cairo_path_buf_t *buf)
781 cairo_list_add_tail (&buf->link, &cairo_path_head (path)->link);
784 COMPILE_TIME_ASSERT (sizeof (cairo_path_op_t) == 1);
785 static cairo_path_buf_t *
786 _cairo_path_buf_create (int size_ops, int size_points)
788 cairo_path_buf_t *buf;
790 /* adjust size_ops to ensure that buf->points is naturally aligned */
791 size_ops += sizeof (double) - ((sizeof (cairo_path_buf_t) + size_ops) % sizeof (double));
792 buf = _cairo_malloc_ab_plus_c (size_points, sizeof (cairo_point_t), size_ops + sizeof (cairo_path_buf_t));
796 buf->size_ops = size_ops;
797 buf->size_points = size_points;
799 buf->op = (cairo_path_op_t *) (buf + 1);
800 buf->points = (cairo_point_t *) (buf->op + size_ops);
807 _cairo_path_buf_destroy (cairo_path_buf_t *buf)
813 _cairo_path_buf_add_op (cairo_path_buf_t *buf,
816 buf->op[buf->num_ops++] = op;
820 _cairo_path_buf_add_points (cairo_path_buf_t *buf,
821 const cairo_point_t *points,
827 memcpy (buf->points + buf->num_points,
829 sizeof (points[0]) * num_points);
830 buf->num_points += num_points;
834 _cairo_path_fixed_interpret (const cairo_path_fixed_t *path,
835 cairo_path_fixed_move_to_func_t *move_to,
836 cairo_path_fixed_line_to_func_t *line_to,
837 cairo_path_fixed_curve_to_func_t *curve_to,
838 cairo_path_fixed_close_path_func_t *close_path,
841 const cairo_path_buf_t *buf;
842 cairo_status_t status;
844 cairo_path_foreach_buf_start (buf, path) {
845 const cairo_point_t *points = buf->points;
848 for (i = 0; i < buf->num_ops; i++) {
849 switch (buf->op[i]) {
850 case CAIRO_PATH_OP_MOVE_TO:
851 status = (*move_to) (closure, &points[0]);
854 case CAIRO_PATH_OP_LINE_TO:
855 status = (*line_to) (closure, &points[0]);
858 case CAIRO_PATH_OP_CURVE_TO:
859 status = (*curve_to) (closure, &points[0], &points[1], &points[2]);
864 case CAIRO_PATH_OP_CLOSE_PATH:
865 status = (*close_path) (closure);
869 if (unlikely (status))
872 } cairo_path_foreach_buf_end (buf, path);
874 if (path->needs_move_to && path->has_current_point)
875 return (*move_to) (closure, &path->current_point);
877 return CAIRO_STATUS_SUCCESS;
880 typedef struct _cairo_path_fixed_append_closure {
881 cairo_point_t offset;
882 cairo_path_fixed_t *path;
883 } cairo_path_fixed_append_closure_t;
885 static cairo_status_t
886 _append_move_to (void *abstract_closure,
887 const cairo_point_t *point)
889 cairo_path_fixed_append_closure_t *closure = abstract_closure;
891 return _cairo_path_fixed_move_to (closure->path,
892 point->x + closure->offset.x,
893 point->y + closure->offset.y);
896 static cairo_status_t
897 _append_line_to (void *abstract_closure,
898 const cairo_point_t *point)
900 cairo_path_fixed_append_closure_t *closure = abstract_closure;
902 return _cairo_path_fixed_line_to (closure->path,
903 point->x + closure->offset.x,
904 point->y + closure->offset.y);
907 static cairo_status_t
908 _append_curve_to (void *abstract_closure,
909 const cairo_point_t *p0,
910 const cairo_point_t *p1,
911 const cairo_point_t *p2)
913 cairo_path_fixed_append_closure_t *closure = abstract_closure;
915 return _cairo_path_fixed_curve_to (closure->path,
916 p0->x + closure->offset.x,
917 p0->y + closure->offset.y,
918 p1->x + closure->offset.x,
919 p1->y + closure->offset.y,
920 p2->x + closure->offset.x,
921 p2->y + closure->offset.y);
924 static cairo_status_t
925 _append_close_path (void *abstract_closure)
927 cairo_path_fixed_append_closure_t *closure = abstract_closure;
929 return _cairo_path_fixed_close_path (closure->path);
933 _cairo_path_fixed_append (cairo_path_fixed_t *path,
934 const cairo_path_fixed_t *other,
938 cairo_path_fixed_append_closure_t closure;
941 closure.offset.x = tx;
942 closure.offset.y = ty;
944 return _cairo_path_fixed_interpret (other,
953 _cairo_path_fixed_offset_and_scale (cairo_path_fixed_t *path,
956 cairo_fixed_t scalex,
957 cairo_fixed_t scaley)
959 cairo_path_buf_t *buf;
962 if (scalex == CAIRO_FIXED_ONE && scaley == CAIRO_FIXED_ONE) {
963 _cairo_path_fixed_translate (path, offx, offy);
967 path->last_move_point.x = _cairo_fixed_mul (scalex, path->last_move_point.x) + offx;
968 path->last_move_point.y = _cairo_fixed_mul (scaley, path->last_move_point.y) + offy;
969 path->current_point.x = _cairo_fixed_mul (scalex, path->current_point.x) + offx;
970 path->current_point.y = _cairo_fixed_mul (scaley, path->current_point.y) + offy;
972 path->fill_maybe_region = TRUE;
974 cairo_path_foreach_buf_start (buf, path) {
975 for (i = 0; i < buf->num_points; i++) {
976 if (scalex != CAIRO_FIXED_ONE)
977 buf->points[i].x = _cairo_fixed_mul (buf->points[i].x, scalex);
978 buf->points[i].x += offx;
980 if (scaley != CAIRO_FIXED_ONE)
981 buf->points[i].y = _cairo_fixed_mul (buf->points[i].y, scaley);
982 buf->points[i].y += offy;
984 if (path->fill_maybe_region) {
985 path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
986 _cairo_fixed_is_integer (buf->points[i].y);
989 } cairo_path_foreach_buf_end (buf, path);
991 path->fill_maybe_region &= path->fill_is_rectilinear;
993 path->extents.p1.x = _cairo_fixed_mul (scalex, path->extents.p1.x) + offx;
994 path->extents.p2.x = _cairo_fixed_mul (scalex, path->extents.p2.x) + offx;
996 cairo_fixed_t t = path->extents.p1.x;
997 path->extents.p1.x = path->extents.p2.x;
998 path->extents.p2.x = t;
1001 path->extents.p1.y = _cairo_fixed_mul (scaley, path->extents.p1.y) + offy;
1002 path->extents.p2.y = _cairo_fixed_mul (scaley, path->extents.p2.y) + offy;
1004 cairo_fixed_t t = path->extents.p1.y;
1005 path->extents.p1.y = path->extents.p2.y;
1006 path->extents.p2.y = t;
1011 _cairo_path_fixed_translate (cairo_path_fixed_t *path,
1015 cairo_path_buf_t *buf;
1018 if (offx == 0 && offy == 0)
1021 path->last_move_point.x += offx;
1022 path->last_move_point.y += offy;
1023 path->current_point.x += offx;
1024 path->current_point.y += offy;
1026 path->fill_maybe_region = TRUE;
1028 cairo_path_foreach_buf_start (buf, path) {
1029 for (i = 0; i < buf->num_points; i++) {
1030 buf->points[i].x += offx;
1031 buf->points[i].y += offy;
1033 if (path->fill_maybe_region) {
1034 path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
1035 _cairo_fixed_is_integer (buf->points[i].y);
1038 } cairo_path_foreach_buf_end (buf, path);
1040 path->fill_maybe_region &= path->fill_is_rectilinear;
1042 path->extents.p1.x += offx;
1043 path->extents.p1.y += offy;
1044 path->extents.p2.x += offx;
1045 path->extents.p2.y += offy;
1050 _cairo_path_fixed_transform_point (cairo_point_t *p,
1051 const cairo_matrix_t *matrix)
1055 dx = _cairo_fixed_to_double (p->x);
1056 dy = _cairo_fixed_to_double (p->y);
1057 cairo_matrix_transform_point (matrix, &dx, &dy);
1058 p->x = _cairo_fixed_from_double (dx);
1059 p->y = _cairo_fixed_from_double (dy);
1063 * _cairo_path_fixed_transform:
1064 * @path: a #cairo_path_fixed_t to be transformed
1065 * @matrix: a #cairo_matrix_t
1067 * Transform the fixed-point path according to the given matrix.
1068 * There is a fast path for the case where @matrix has no rotation
1072 _cairo_path_fixed_transform (cairo_path_fixed_t *path,
1073 const cairo_matrix_t *matrix)
1075 cairo_box_t extents;
1076 cairo_point_t point;
1077 cairo_path_buf_t *buf;
1080 if (matrix->yx == 0.0 && matrix->xy == 0.0) {
1081 /* Fast path for the common case of scale+transform */
1082 _cairo_path_fixed_offset_and_scale (path,
1083 _cairo_fixed_from_double (matrix->x0),
1084 _cairo_fixed_from_double (matrix->y0),
1085 _cairo_fixed_from_double (matrix->xx),
1086 _cairo_fixed_from_double (matrix->yy));
1090 _cairo_path_fixed_transform_point (&path->last_move_point, matrix);
1091 _cairo_path_fixed_transform_point (&path->current_point, matrix);
1093 buf = cairo_path_head (path);
1094 if (buf->num_points == 0)
1097 extents = path->extents;
1098 point = buf->points[0];
1099 _cairo_path_fixed_transform_point (&point, matrix);
1100 _cairo_box_set (&path->extents, &point, &point);
1102 cairo_path_foreach_buf_start (buf, path) {
1103 for (i = 0; i < buf->num_points; i++) {
1104 _cairo_path_fixed_transform_point (&buf->points[i], matrix);
1105 _cairo_box_add_point (&path->extents, &buf->points[i]);
1107 } cairo_path_foreach_buf_end (buf, path);
1109 if (path->has_curve_to) {
1110 cairo_bool_t is_tight;
1112 _cairo_matrix_transform_bounding_box_fixed (matrix, &extents, &is_tight);
1114 cairo_bool_t has_extents;
1116 has_extents = _cairo_path_bounder_extents (path, &extents);
1117 assert (has_extents);
1119 path->extents = extents;
1122 /* flags might become more strict than needed */
1123 path->stroke_is_rectilinear = FALSE;
1124 path->fill_is_rectilinear = FALSE;
1125 path->fill_is_empty = FALSE;
1126 path->fill_maybe_region = FALSE;
1129 /* Closure for path flattening */
1130 typedef struct cairo_path_flattener {
1132 cairo_point_t current_point;
1133 cairo_path_fixed_move_to_func_t *move_to;
1134 cairo_path_fixed_line_to_func_t *line_to;
1135 cairo_path_fixed_close_path_func_t *close_path;
1139 static cairo_status_t
1140 _cpf_move_to (void *closure,
1141 const cairo_point_t *point)
1143 cpf_t *cpf = closure;
1145 cpf->current_point = *point;
1147 return cpf->move_to (cpf->closure, point);
1150 static cairo_status_t
1151 _cpf_line_to (void *closure,
1152 const cairo_point_t *point)
1154 cpf_t *cpf = closure;
1156 cpf->current_point = *point;
1158 return cpf->line_to (cpf->closure, point);
1161 static cairo_status_t
1162 _cpf_curve_to (void *closure,
1163 const cairo_point_t *p1,
1164 const cairo_point_t *p2,
1165 const cairo_point_t *p3)
1167 cpf_t *cpf = closure;
1168 cairo_spline_t spline;
1170 cairo_point_t *p0 = &cpf->current_point;
1172 if (! _cairo_spline_init (&spline,
1173 (cairo_spline_add_point_func_t)cpf->line_to,
1177 return _cpf_line_to (closure, p3);
1180 cpf->current_point = *p3;
1182 return _cairo_spline_decompose (&spline, cpf->tolerance);
1185 static cairo_status_t
1186 _cpf_close_path (void *closure)
1188 cpf_t *cpf = closure;
1190 return cpf->close_path (cpf->closure);
1194 _cairo_path_fixed_interpret_flat (const cairo_path_fixed_t *path,
1195 cairo_path_fixed_move_to_func_t *move_to,
1196 cairo_path_fixed_line_to_func_t *line_to,
1197 cairo_path_fixed_close_path_func_t *close_path,
1203 if (! path->has_curve_to) {
1204 return _cairo_path_fixed_interpret (path,
1212 flattener.tolerance = tolerance;
1213 flattener.move_to = move_to;
1214 flattener.line_to = line_to;
1215 flattener.close_path = close_path;
1216 flattener.closure = closure;
1217 return _cairo_path_fixed_interpret (path,
1226 _canonical_box (cairo_box_t *box,
1227 const cairo_point_t *p1,
1228 const cairo_point_t *p2)
1230 if (p1->x <= p2->x) {
1238 if (p1->y <= p2->y) {
1247 static inline cairo_bool_t
1248 _path_is_quad (const cairo_path_fixed_t *path)
1250 const cairo_path_buf_t *buf = cairo_path_head (path);
1252 /* Do we have the right number of ops? */
1253 if (buf->num_ops < 4 || buf->num_ops > 6)
1256 /* Check whether the ops are those that would be used for a rectangle */
1257 if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
1258 buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
1259 buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
1260 buf->op[3] != CAIRO_PATH_OP_LINE_TO)
1265 /* we accept an implicit close for filled paths */
1266 if (buf->num_ops > 4) {
1267 /* Now, there are choices. The rectangle might end with a LINE_TO
1268 * (to the original point), but this isn't required. If it
1269 * doesn't, then it must end with a CLOSE_PATH. */
1270 if (buf->op[4] == CAIRO_PATH_OP_LINE_TO) {
1271 if (buf->points[4].x != buf->points[0].x ||
1272 buf->points[4].y != buf->points[0].y)
1274 } else if (buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH) {
1278 if (buf->num_ops == 6) {
1279 /* A trailing CLOSE_PATH or MOVE_TO is ok */
1280 if (buf->op[5] != CAIRO_PATH_OP_MOVE_TO &&
1281 buf->op[5] != CAIRO_PATH_OP_CLOSE_PATH)
1289 static inline cairo_bool_t
1290 _points_form_rect (const cairo_point_t *points)
1292 if (points[0].y == points[1].y &&
1293 points[1].x == points[2].x &&
1294 points[2].y == points[3].y &&
1295 points[3].x == points[0].x)
1297 if (points[0].x == points[1].x &&
1298 points[1].y == points[2].y &&
1299 points[2].x == points[3].x &&
1300 points[3].y == points[0].y)
1306 * Check whether the given path contains a single rectangle.
1309 _cairo_path_fixed_is_box (const cairo_path_fixed_t *path,
1312 const cairo_path_buf_t *buf;
1314 if (! path->fill_is_rectilinear)
1317 if (! _path_is_quad (path))
1320 buf = cairo_path_head (path);
1321 if (_points_form_rect (buf->points)) {
1322 _canonical_box (box, &buf->points[0], &buf->points[2]);
1329 /* Determine whether two lines A->B and C->D intersect based on the
1330 * algorithm described here: http://paulbourke.net/geometry/pointlineplane/ */
1331 static inline cairo_bool_t
1332 _lines_intersect_or_are_coincident (cairo_point_t a,
1337 cairo_int64_t numerator_a, numerator_b, denominator;
1338 cairo_bool_t denominator_negative;
1340 denominator = _cairo_int64_sub (_cairo_int32x32_64_mul (d.y - c.y, b.x - a.x),
1341 _cairo_int32x32_64_mul (d.x - c.x, b.y - a.y));
1342 numerator_a = _cairo_int64_sub (_cairo_int32x32_64_mul (d.x - c.x, a.y - c.y),
1343 _cairo_int32x32_64_mul (d.y - c.y, a.x - c.x));
1344 numerator_b = _cairo_int64_sub (_cairo_int32x32_64_mul (b.x - a.x, a.y - c.y),
1345 _cairo_int32x32_64_mul (b.y - a.y, a.x - c.x));
1347 if (_cairo_int64_is_zero (denominator)) {
1348 /* If the denominator and numerators are both zero,
1349 * the lines are coincident. */
1350 if (_cairo_int64_is_zero (numerator_a) && _cairo_int64_is_zero (numerator_b))
1353 /* Otherwise, a zero denominator indicates the lines are
1354 * parallel and never intersect. */
1358 /* The lines intersect if both quotients are between 0 and 1 (exclusive). */
1360 /* We first test whether either quotient is a negative number. */
1361 denominator_negative = _cairo_int64_negative (denominator);
1362 if (_cairo_int64_negative (numerator_a) ^ denominator_negative)
1364 if (_cairo_int64_negative (numerator_b) ^ denominator_negative)
1367 /* A zero quotient indicates an "intersection" at an endpoint, which
1368 * we aren't considering a true intersection. */
1369 if (_cairo_int64_is_zero (numerator_a) || _cairo_int64_is_zero (numerator_b))
1372 /* If the absolute value of the numerator is larger than or equal to the
1373 * denominator the result of the division would be greater than or equal
1375 if (! denominator_negative) {
1376 if (! _cairo_int64_lt (numerator_a, denominator) ||
1377 ! _cairo_int64_lt (numerator_b, denominator))
1380 if (! _cairo_int64_lt (denominator, numerator_a) ||
1381 ! _cairo_int64_lt (denominator, numerator_b))
1389 _cairo_path_fixed_is_simple_quad (const cairo_path_fixed_t *path)
1391 const cairo_point_t *points;
1393 if (! _path_is_quad (path))
1396 points = cairo_path_head (path)->points;
1397 if (_points_form_rect (points))
1400 if (_lines_intersect_or_are_coincident (points[0], points[1],
1401 points[3], points[2]))
1404 if (_lines_intersect_or_are_coincident (points[0], points[3],
1405 points[1], points[2]))
1412 _cairo_path_fixed_is_stroke_box (const cairo_path_fixed_t *path,
1415 const cairo_path_buf_t *buf = cairo_path_head (path);
1417 if (! path->fill_is_rectilinear)
1420 /* Do we have the right number of ops? */
1421 if (buf->num_ops != 5)
1424 /* Check whether the ops are those that would be used for a rectangle */
1425 if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
1426 buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
1427 buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
1428 buf->op[3] != CAIRO_PATH_OP_LINE_TO ||
1429 buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH)
1434 /* Ok, we may have a box, if the points line up */
1435 if (buf->points[0].y == buf->points[1].y &&
1436 buf->points[1].x == buf->points[2].x &&
1437 buf->points[2].y == buf->points[3].y &&
1438 buf->points[3].x == buf->points[0].x)
1440 _canonical_box (box, &buf->points[0], &buf->points[2]);
1444 if (buf->points[0].x == buf->points[1].x &&
1445 buf->points[1].y == buf->points[2].y &&
1446 buf->points[2].x == buf->points[3].x &&
1447 buf->points[3].y == buf->points[0].y)
1449 _canonical_box (box, &buf->points[0], &buf->points[2]);
1457 * Check whether the given path contains a single rectangle
1458 * that is logically equivalent to:
1459 * <informalexample><programlisting>
1460 * cairo_move_to (cr, x, y);
1461 * cairo_rel_line_to (cr, width, 0);
1462 * cairo_rel_line_to (cr, 0, height);
1463 * cairo_rel_line_to (cr, -width, 0);
1464 * cairo_close_path (cr);
1465 * </programlisting></informalexample>
1468 _cairo_path_fixed_is_rectangle (const cairo_path_fixed_t *path,
1471 const cairo_path_buf_t *buf;
1473 if (! _cairo_path_fixed_is_box (path, box))
1476 /* This check is valid because the current implementation of
1477 * _cairo_path_fixed_is_box () only accepts rectangles like:
1478 * move,line,line,line[,line|close[,close|move]]. */
1479 buf = cairo_path_head (path);
1480 if (buf->num_ops > 4)
1487 _cairo_path_fixed_iter_init (cairo_path_fixed_iter_t *iter,
1488 const cairo_path_fixed_t *path)
1490 iter->first = iter->buf = cairo_path_head (path);
1496 _cairo_path_fixed_is_single_line (const cairo_path_fixed_t *path)
1498 const cairo_path_buf_t *buf = cairo_path_head (path);
1500 if (buf->num_ops > 2)
1502 if (buf->num_ops <= 1)
1505 return buf->op[0] == CAIRO_PATH_OP_MOVE_TO &&
1506 buf->op[1] == CAIRO_PATH_OP_LINE_TO;
1510 _cairo_path_fixed_is_empty (const cairo_path_fixed_t *path)
1513 const cairo_path_buf_t *buf = cairo_path_head (path);
1514 for (i = 0; i < buf->num_ops; i++) {
1515 if (buf->op[i] != CAIRO_PATH_OP_MOVE_TO)
1523 _cairo_path_fixed_is_single_arc (const cairo_path_fixed_t *path)
1525 const cairo_path_buf_t *buf = cairo_path_head (path);
1526 if (buf->num_ops > 2)
1529 if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
1530 buf->op[1] != CAIRO_PATH_OP_CURVE_TO)
1537 _cairo_path_fixed_iter_next_op (cairo_path_fixed_iter_t *iter)
1539 if (++iter->n_op >= iter->buf->num_ops) {
1540 iter->buf = cairo_path_buf_next (iter->buf);
1541 if (iter->buf == iter->first) {
1554 _cairo_path_fixed_iter_is_fill_box (cairo_path_fixed_iter_t *_iter,
1557 cairo_point_t points[5];
1558 cairo_path_fixed_iter_t iter;
1560 if (_iter->buf == NULL)
1565 if (iter.n_op == iter.buf->num_ops && ! _cairo_path_fixed_iter_next_op (&iter))
1568 /* Check whether the ops are those that would be used for a rectangle */
1569 if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_MOVE_TO)
1571 points[0] = iter.buf->points[iter.n_point++];
1572 if (! _cairo_path_fixed_iter_next_op (&iter))
1575 if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
1577 points[1] = iter.buf->points[iter.n_point++];
1578 if (! _cairo_path_fixed_iter_next_op (&iter))
1581 /* a horizontal/vertical closed line is also a degenerate rectangle */
1582 switch (iter.buf->op[iter.n_op]) {
1583 case CAIRO_PATH_OP_CLOSE_PATH:
1584 _cairo_path_fixed_iter_next_op (&iter);
1585 case CAIRO_PATH_OP_MOVE_TO: /* implicit close */
1586 box->p1 = box->p2 = points[0];
1591 case CAIRO_PATH_OP_LINE_TO:
1595 points[2] = iter.buf->points[iter.n_point++];
1596 if (! _cairo_path_fixed_iter_next_op (&iter))
1599 if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
1601 points[3] = iter.buf->points[iter.n_point++];
1603 /* Now, there are choices. The rectangle might end with a LINE_TO
1604 * (to the original point), but this isn't required. If it
1605 * doesn't, then it must end with a CLOSE_PATH (which may be implicit). */
1606 if (! _cairo_path_fixed_iter_next_op (&iter)) {
1607 /* implicit close due to fill */
1608 } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_LINE_TO) {
1609 points[4] = iter.buf->points[iter.n_point++];
1610 if (points[4].x != points[0].x || points[4].y != points[0].y)
1612 _cairo_path_fixed_iter_next_op (&iter);
1613 } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_CLOSE_PATH) {
1614 _cairo_path_fixed_iter_next_op (&iter);
1615 } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_MOVE_TO) {
1616 /* implicit close-path due to new-sub-path */
1621 /* Ok, we may have a box, if the points line up */
1622 if (points[0].y == points[1].y &&
1623 points[1].x == points[2].x &&
1624 points[2].y == points[3].y &&
1625 points[3].x == points[0].x)
1627 box->p1 = points[0];
1628 box->p2 = points[2];
1633 if (points[0].x == points[1].x &&
1634 points[1].y == points[2].y &&
1635 points[2].x == points[3].x &&
1636 points[3].y == points[0].y)
1638 box->p1 = points[1];
1639 box->p2 = points[3];
1648 _cairo_path_fixed_iter_at_end (const cairo_path_fixed_iter_t *iter)
1650 if (iter->buf == NULL)
1653 return iter->n_op == iter->buf->num_ops;