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
34 * Carl D. Worth <cworth@cworth.org>
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
;
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 */
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
));
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
);
81 polygon
->edges
= new_edges
;
82 polygon
->edges_size
= new_size
;
88 _cairo_polygon_add_edge (cairo_polygon_t
*polygon
,
89 const cairo_point_t
*p1
,
90 const cairo_point_t
*p2
)
94 /* drop horizontal edges */
98 if (polygon
->num_edges
== polygon
->edges_size
) {
99 if (! _cairo_polygon_grow (polygon
))
103 edge
= &polygon
->edges
[polygon
->num_edges
++];
115 _cairo_polygon_move_to (polygon
, p2
);
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
;
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
);
136 _cairo_polygon_move_to (polygon
, point
);
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
;