1 #define __NR_SVP_C__
3 /*
4 * Pixel buffer rendering library
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * This code is in public domain
10 */
12 #define noNR_VERBOSE
14 #define NR_SVP_LENGTH_MAX 128
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #ifdef HAVE_IEEEFP_H
21 # include <ieeefp.h>
22 #endif
24 #include <glib/gmem.h>
25 #include "nr-rect.h"
26 #include "nr-svp-private.h"
28 /* Sorted vector paths */
30 void
31 nr_svp_free (NRSVP *svp)
32 {
33 if (svp->points) g_free (svp->points);
34 free (svp);
35 }
37 void
38 nr_svp_bbox (NRSVP *svp, NRRect *bbox, unsigned int clear)
39 {
40 unsigned int sidx;
41 float x0, y0, x1, y1;
43 x0 = y0 = NR_HUGE;
44 x1 = y1 = -NR_HUGE;
46 for (sidx = 0; sidx < svp->length; sidx++) {
47 NRSVPSegment *seg;
48 seg = svp->segments + sidx;
49 if (seg->length) {
50 x0 = MIN (x0, seg->x0);
51 y0 = MIN (y0, svp->points[seg->start].y);
52 x1 = MAX (x1, seg->x1);
53 y1 = MAX (y1, svp->points[seg->start + seg->length - 1].y);
54 }
55 }
57 if ((x1 > x0) && (y1 > y0)) {
58 if (clear || (bbox->x1 <= bbox->x0) || (bbox->y1 <= bbox->y0)) {
59 bbox->x0 = x0;
60 bbox->y0 = y0;
61 bbox->x1 = x1;
62 bbox->y1 = y1;
63 } else {
64 bbox->x0 = MIN (bbox->x0, x0);
65 bbox->y0 = MIN (bbox->y0, y0);
66 bbox->x1 = MAX (bbox->x1, x1);
67 bbox->y1 = MAX (bbox->y1, y1);
68 }
69 }
70 }
72 /* NRVertex */
74 #define NR_VERTEX_ALLOC_SIZE 4096
75 static NRVertex *ffvertex = NULL;
77 NRVertex *
78 nr_vertex_new (void)
79 {
80 NRVertex * v;
81 #ifndef NR_VERTEX_ALLOC
83 v = ffvertex;
85 if (v == NULL) {
86 int i;
87 v = g_new (NRVertex, NR_VERTEX_ALLOC_SIZE);
88 for (i = 1; i < (NR_VERTEX_ALLOC_SIZE - 1); i++) v[i].next = &v[i + 1];
89 v[NR_VERTEX_ALLOC_SIZE - 1].next = NULL;
90 ffvertex = v + 1;
91 } else {
92 ffvertex = v->next;
93 }
94 #else
95 v = g_new (NRVertex, 1);
96 #endif
98 v->next = NULL;
100 return v;
101 }
103 NRVertex *
104 nr_vertex_new_xy (NR::Coord x, NR::Coord y)
105 {
106 NRVertex * v;
108 if (!finite(x) || !finite(y)) {
109 g_critical("nr_vertex_new_xy: BUG: Coordinates are not finite");
110 x = y = 0;
111 } else if (!( fabs(x) < 1e17 && fabs(y) < 1e17 )) {
112 g_critical("nr_vertex_new_xy: Coordinates out of range");
113 x = y = 0;
114 }
116 v = nr_vertex_new ();
118 v->x = x;
119 v->y = y;
121 return v;
122 }
124 void
125 nr_vertex_free_one (NRVertex * v)
126 {
127 #ifndef NR_VERTEX_ALLOC
128 v->next = ffvertex;
129 ffvertex = v;
130 #else
131 g_free (v);
132 #endif
133 }
135 void
136 nr_vertex_free_list (NRVertex * v)
137 {
138 #ifndef NR_VERTEX_ALLOC
139 NRVertex * l;
140 for (l = v; l->next != NULL; l = l->next);
141 l->next = ffvertex;
142 ffvertex = v;
143 #else
144 NRVertex *l, *n;
145 l = v;
146 while (l) {
147 n = l->next;
148 g_free (l);
149 l = n;
150 }
151 #endif
152 }
154 NRVertex *
155 nr_vertex_reverse_list (NRVertex * v)
156 {
157 NRVertex * p;
159 p = NULL;
161 while (v) {
162 NRVertex * n;
163 n = v->next;
164 v->next = p;
165 p = v;
166 v = n;
167 }
169 return p;
170 }
172 /*
173 Local Variables:
174 mode:c++
175 c-file-style:"stroustrup"
176 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
177 indent-tabs-mode:nil
178 fill-column:99
179 End:
180 */
181 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :