Changes.
[cairo/gpu.git] / src / cairo-polygon.c
blob202cb4d731fa256b6766e83e1e2cfb1255caaa5c
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2002 University of Southern California
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
31 * California.
33 * Contributor(s):
34 * Carl D. Worth <cworth@cworth.org>
37 #include "cairoint.h"
39 void
40 _cairo_polygon_init (cairo_polygon_t *polygon)
42 VG (VALGRIND_MAKE_MEM_UNDEFINED (polygon, sizeof (cairo_polygon_t)));
44 polygon->status = CAIRO_STATUS_SUCCESS;
46 polygon->num_edges = 0;
48 polygon->edges = polygon->edges_embedded;
49 polygon->edges_size = ARRAY_LENGTH (polygon->edges_embedded);
51 polygon->has_current_point = FALSE;
54 void
55 _cairo_polygon_fini (cairo_polygon_t *polygon)
57 if (polygon->edges != polygon->edges_embedded)
58 free (polygon->edges);
60 VG (VALGRIND_MAKE_MEM_NOACCESS (polygon, sizeof (cairo_polygon_t)));
63 /* make room for at least one more edge */
64 static cairo_bool_t
65 _cairo_polygon_grow (cairo_polygon_t *polygon)
67 cairo_edge_t *new_edges;
68 int old_size = polygon->edges_size;
69 int new_size = 4 * old_size;
71 if (CAIRO_INJECT_FAULT ()) {
72 polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
73 return FALSE;
76 if (polygon->edges == polygon->edges_embedded) {
77 new_edges = _cairo_malloc_ab (new_size, sizeof (cairo_edge_t));
78 if (new_edges != NULL)
79 memcpy (new_edges, polygon->edges, old_size * sizeof (cairo_edge_t));
80 } else {
81 new_edges = _cairo_realloc_ab (polygon->edges,
82 new_size, sizeof (cairo_edge_t));
85 if (unlikely (new_edges == NULL)) {
86 polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
87 return FALSE;
90 polygon->edges = new_edges;
91 polygon->edges_size = new_size;
93 return TRUE;
96 void
97 _cairo_polygon_add_edge (cairo_polygon_t *polygon,
98 const cairo_point_t *p1,
99 const cairo_point_t *p2,
100 int dir)
102 cairo_edge_t *edge;
104 /* drop horizontal edges */
105 if (p1->y == p2->y)
106 return;
108 if (polygon->num_edges == polygon->edges_size) {
109 if (! _cairo_polygon_grow (polygon))
110 return;
113 edge = &polygon->edges[polygon->num_edges++];
114 if (p1->y < p2->y) {
115 edge->edge.p1 = *p1;
116 edge->edge.p2 = *p2;
117 edge->dir = dir;
118 } else {
119 edge->edge.p1 = *p2;
120 edge->edge.p2 = *p1;
121 edge->dir = -dir;
125 void
126 _cairo_polygon_move_to (cairo_polygon_t *polygon,
127 const cairo_point_t *point)
129 if (! polygon->has_current_point)
130 polygon->first_point = *point;
132 polygon->current_point = *point;
133 polygon->has_current_point = TRUE;
136 void
137 _cairo_polygon_line_to (cairo_polygon_t *polygon,
138 const cairo_point_t *point)
140 if (polygon->has_current_point)
141 _cairo_polygon_add_edge (polygon, &polygon->current_point, point, 1);
143 _cairo_polygon_move_to (polygon, point);
146 void
147 _cairo_polygon_close (cairo_polygon_t *polygon)
149 if (polygon->has_current_point) {
150 _cairo_polygon_add_edge (polygon,
151 &polygon->current_point,
152 &polygon->first_point,
155 polygon->has_current_point = FALSE;