added MouseWheel event support for Silverlight 3.0
[moon.git] / cairo / src / cairo-polygon.c
blob1392bfa1847d5fbf110aa3b5cdcbfca65f376de5
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 polygon->status = CAIRO_STATUS_SUCCESS;
44 polygon->num_edges = 0;
46 polygon->edges = polygon->edges_embedded;
47 polygon->edges_size = ARRAY_LENGTH (polygon->edges_embedded);
49 polygon->has_current_point = FALSE;
52 void
53 _cairo_polygon_fini (cairo_polygon_t *polygon)
55 if (polygon->edges != polygon->edges_embedded)
56 free (polygon->edges);
59 /* make room for at least one more edge */
60 static cairo_bool_t
61 _cairo_polygon_grow (cairo_polygon_t *polygon)
63 cairo_edge_t *new_edges;
64 int old_size = polygon->edges_size;
65 int new_size = 4 * old_size;
67 if (polygon->edges == polygon->edges_embedded) {
68 new_edges = _cairo_malloc_ab (new_size, sizeof (cairo_edge_t));
69 if (new_edges != NULL)
70 memcpy (new_edges, polygon->edges, old_size * sizeof (cairo_edge_t));
71 } else {
72 new_edges = _cairo_realloc_ab (polygon->edges,
73 new_size, sizeof (cairo_edge_t));
76 if (new_edges == NULL) {
77 polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
78 return FALSE;
81 polygon->edges = new_edges;
82 polygon->edges_size = new_size;
84 return TRUE;
87 static void
88 _cairo_polygon_add_edge (cairo_polygon_t *polygon,
89 const cairo_point_t *p1,
90 const cairo_point_t *p2)
92 cairo_edge_t *edge;
94 /* drop horizontal edges */
95 if (p1->y == p2->y)
96 goto DONE;
98 if (polygon->num_edges == polygon->edges_size) {
99 if (! _cairo_polygon_grow (polygon))
100 return;
103 edge = &polygon->edges[polygon->num_edges++];
104 if (p1->y < p2->y) {
105 edge->edge.p1 = *p1;
106 edge->edge.p2 = *p2;
107 edge->clockWise = 1;
108 } else {
109 edge->edge.p1 = *p2;
110 edge->edge.p2 = *p1;
111 edge->clockWise = 0;
114 DONE:
115 _cairo_polygon_move_to (polygon, p2);
118 void
119 _cairo_polygon_move_to (cairo_polygon_t *polygon,
120 const cairo_point_t *point)
122 if (! polygon->has_current_point)
123 polygon->first_point = *point;
125 polygon->current_point = *point;
126 polygon->has_current_point = TRUE;
129 void
130 _cairo_polygon_line_to (cairo_polygon_t *polygon,
131 const cairo_point_t *point)
133 if (polygon->has_current_point)
134 _cairo_polygon_add_edge (polygon, &polygon->current_point, point);
135 else
136 _cairo_polygon_move_to (polygon, point);
139 void
140 _cairo_polygon_close (cairo_polygon_t *polygon)
142 if (polygon->has_current_point) {
143 _cairo_polygon_add_edge (polygon,
144 &polygon->current_point,
145 &polygon->first_point);
147 polygon->has_current_point = FALSE;