Back to index

lightning-sunbird  0.9+nobinonly
cairo-polygon.c
Go to the documentation of this file.
00001 /* cairo - a vector graphics library with display and print output
00002  *
00003  * Copyright © 2002 University of Southern California
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it either under the terms of the GNU Lesser General Public
00007  * License version 2.1 as published by the Free Software Foundation
00008  * (the "LGPL") or, at your option, under the terms of the Mozilla
00009  * Public License Version 1.1 (the "MPL"). If you do not alter this
00010  * notice, a recipient may use your version of this file under either
00011  * the MPL or the LGPL.
00012  *
00013  * You should have received a copy of the LGPL along with this library
00014  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00016  * You should have received a copy of the MPL along with this library
00017  * in the file COPYING-MPL-1.1
00018  *
00019  * The contents of this file are subject to the Mozilla Public License
00020  * Version 1.1 (the "License"); you may not use this file except in
00021  * compliance with the License. You may obtain a copy of the License at
00022  * http://www.mozilla.org/MPL/
00023  *
00024  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
00025  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
00026  * the specific language governing rights and limitations.
00027  *
00028  * The Original Code is the cairo graphics library.
00029  *
00030  * The Initial Developer of the Original Code is University of Southern
00031  * California.
00032  *
00033  * Contributor(s):
00034  *     Carl D. Worth <cworth@cworth.org>
00035  */
00036 
00037 #include <stdlib.h>
00038 #include "cairoint.h"
00039 
00040 /* private functions */
00041 
00042 static cairo_status_t
00043 _cairo_polygon_grow_by (cairo_polygon_t *polygon, int additional);
00044 
00045 void
00046 _cairo_polygon_init (cairo_polygon_t *polygon)
00047 {
00048     polygon->num_edges = 0;
00049 
00050     polygon->edges_size = 0;
00051     polygon->edges = NULL;
00052 
00053     polygon->has_current_point = 0;
00054 }
00055 
00056 void
00057 _cairo_polygon_fini (cairo_polygon_t *polygon)
00058 {
00059     if (polygon->edges_size) {
00060        free (polygon->edges);
00061        polygon->edges = NULL;
00062        polygon->edges_size = 0;
00063        polygon->num_edges = 0;
00064     }
00065 
00066     polygon->has_current_point = 0;
00067 }
00068 
00069 static cairo_status_t
00070 _cairo_polygon_grow_by (cairo_polygon_t *polygon, int additional)
00071 {
00072     cairo_edge_t *new_edges;
00073     int old_size = polygon->edges_size;
00074     int new_size = polygon->num_edges + additional;
00075 
00076     if (new_size <= polygon->edges_size) {
00077        return CAIRO_STATUS_SUCCESS;
00078     }
00079 
00080     polygon->edges_size = new_size;
00081     new_edges = realloc (polygon->edges, polygon->edges_size * sizeof (cairo_edge_t));
00082 
00083     if (new_edges == NULL) {
00084        polygon->edges_size = old_size;
00085        return CAIRO_STATUS_NO_MEMORY;
00086     }
00087 
00088     polygon->edges = new_edges;
00089 
00090     return CAIRO_STATUS_SUCCESS;
00091 }
00092 
00093 cairo_status_t
00094 _cairo_polygon_add_edge (cairo_polygon_t *polygon, cairo_point_t *p1, cairo_point_t *p2)
00095 {
00096     cairo_status_t status;
00097     cairo_edge_t *edge;
00098 
00099     /* drop horizontal edges */
00100     if (p1->y == p2->y) {
00101        goto DONE;
00102     }
00103 
00104     if (polygon->num_edges >= polygon->edges_size) {
00105        int additional = polygon->edges_size ? polygon->edges_size : 16;
00106        status = _cairo_polygon_grow_by (polygon, additional);
00107        if (status) {
00108            return status;
00109        }
00110     }
00111 
00112     edge = &polygon->edges[polygon->num_edges];
00113     if (p1->y < p2->y) {
00114        edge->edge.p1 = *p1;
00115        edge->edge.p2 = *p2;
00116        edge->clockWise = 1;
00117     } else {
00118        edge->edge.p1 = *p2;
00119        edge->edge.p2 = *p1;
00120        edge->clockWise = 0;
00121     }
00122 
00123     polygon->num_edges++;
00124 
00125   DONE:
00126     _cairo_polygon_move_to (polygon, p2);
00127 
00128     return CAIRO_STATUS_SUCCESS;
00129 }
00130 
00131 cairo_status_t 
00132 _cairo_polygon_move_to (cairo_polygon_t *polygon, cairo_point_t *point)
00133 {
00134     if (! polygon->has_current_point)
00135        polygon->first_point = *point;
00136     polygon->current_point = *point;
00137     polygon->has_current_point = 1;
00138 
00139     return CAIRO_STATUS_SUCCESS;
00140 }
00141 
00142 cairo_status_t
00143 _cairo_polygon_line_to (cairo_polygon_t *polygon, cairo_point_t *point)
00144 {
00145     cairo_status_t status = CAIRO_STATUS_SUCCESS;
00146 
00147     if (polygon->has_current_point) {
00148        status = _cairo_polygon_add_edge (polygon, &polygon->current_point, point);
00149     } else {
00150        _cairo_polygon_move_to (polygon, point);
00151     }
00152 
00153     return status;
00154 }
00155 
00156 cairo_status_t
00157 _cairo_polygon_close (cairo_polygon_t *polygon)
00158 {
00159     cairo_status_t status;
00160 
00161     if (polygon->has_current_point) {
00162        status = _cairo_polygon_add_edge (polygon,
00163                                      &polygon->current_point,
00164                                      &polygon->first_point);
00165        if (status)
00166            return status;
00167 
00168        polygon->has_current_point = 0;
00169     }
00170 
00171     return CAIRO_STATUS_SUCCESS;
00172 }