Different default font when WIN32 is defined
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_vpath_bpath.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* Basic constructors and operations for bezier paths */
21
22 #include <math.h>
23
24 #include "art_misc.h"
25
26 #include "art_bpath.h"
27 #include "art_vpath.h"
28 #include "art_vpath_bpath.h"
29
30 /* p must be allocated 2^level points. */
31
32 /* level must be >= 1 */
33 ArtPoint *
34 art_bezier_to_vec (double x0, double y0,
35                    double x1, double y1,
36                    double x2, double y2,
37                    double x3, double y3,
38                    ArtPoint *p,
39                    int level)
40 {
41   double x_m, y_m;
42
43 #ifdef VERBOSE
44   printf ("bezier_to_vec: %g,%g %g,%g %g,%g %g,%g %d\n",
45           x0, y0, x1, y1, x2, y2, x3, y3, level);
46 #endif
47   if (level == 1) {
48     x_m = (x0 + 3 * (x1 + x2) + x3) * 0.125;
49     y_m = (y0 + 3 * (y1 + y2) + y3) * 0.125;
50     p->x = x_m;
51     p->y = y_m;
52     p++;
53     p->x = x3;
54     p->y = y3;
55     p++;
56 #ifdef VERBOSE
57     printf ("-> (%g, %g) -> (%g, %g)\n", x_m, y_m, x3, y3);
58 #endif
59   } else {
60     double xa1, ya1;
61     double xa2, ya2;
62     double xb1, yb1;
63     double xb2, yb2;
64
65     xa1 = (x0 + x1) * 0.5;
66     ya1 = (y0 + y1) * 0.5;
67     xa2 = (x0 + 2 * x1 + x2) * 0.25;
68     ya2 = (y0 + 2 * y1 + y2) * 0.25;
69     xb1 = (x1 + 2 * x2 + x3) * 0.25;
70     yb1 = (y1 + 2 * y2 + y3) * 0.25;
71     xb2 = (x2 + x3) * 0.5;
72     yb2 = (y2 + y3) * 0.5;
73     x_m = (xa2 + xb1) * 0.5;
74     y_m = (ya2 + yb1) * 0.5;
75 #ifdef VERBOSE
76     printf ("%g,%g %g,%g %g,%g %g,%g\n", xa1, ya1, xa2, ya2,
77             xb1, yb1, xb2, yb2);
78 #endif
79     p = art_bezier_to_vec (x0, y0, xa1, ya1, xa2, ya2, x_m, y_m, p, level - 1);
80     p = art_bezier_to_vec (x_m, y_m, xb1, yb1, xb2, yb2, x3, y3, p, level - 1);
81   }
82   return p;
83 }
84
85 #define RENDER_LEVEL 4
86 #define RENDER_SIZE (1 << (RENDER_LEVEL))
87
88 /**
89  * art_vpath_render_bez: Render a bezier segment into the vpath. 
90  * @p_vpath: Where the pointer to the #ArtVpath structure is stored.
91  * @pn_points: Pointer to the number of points in *@p_vpath.
92  * @pn_points_max: Pointer to the number of points allocated.
93  * @x0: X coordinate of starting bezier point.
94  * @y0: Y coordinate of starting bezier point.
95  * @x1: X coordinate of first bezier control point.
96  * @y1: Y coordinate of first bezier control point.
97  * @x2: X coordinate of second bezier control point.
98  * @y2: Y coordinate of second bezier control point.
99  * @x3: X coordinate of ending bezier point.
100  * @y3: Y coordinate of ending bezier point.
101  * @flatness: Flatness control.
102  *
103  * Renders a bezier segment into the vector path, reallocating and
104  * updating *@p_vpath and *@pn_vpath_max as necessary. *@pn_vpath is
105  * incremented by the number of vector points added.
106  *
107  * This step includes (@x0, @y0) but not (@x3, @y3).
108  *
109  * The @flatness argument guides the amount of subdivision. The Adobe
110  * PostScript reference manual defines flatness as the maximum
111  * deviation between the any point on the vpath approximation and the
112  * corresponding point on the "true" curve, and we follow this
113  * definition here. A value of 0.25 should ensure high quality for aa
114  * rendering.
115 **/
116 static void
117 art_vpath_render_bez (ArtVpath **p_vpath, int *pn, int *pn_max,
118                       double x0, double y0,
119                       double x1, double y1,
120                       double x2, double y2,
121                       double x3, double y3,
122                       double flatness)
123 {
124   double x3_0, y3_0;
125   double z3_0_dot;
126   double z1_dot, z2_dot;
127   double z1_perp, z2_perp;
128   double max_perp_sq;
129
130   double x_m, y_m;
131   double xa1, ya1;
132   double xa2, ya2;
133   double xb1, yb1;
134   double xb2, yb2;
135
136   /* It's possible to optimize this routine a fair amount.
137
138      First, once the _dot conditions are met, they will also be met in
139      all further subdivisions. So we might recurse to a different
140      routine that only checks the _perp conditions.
141
142      Second, the distance _should_ decrease according to fairly
143      predictable rules (a factor of 4 with each subdivision). So it might
144      be possible to note that the distance is within a factor of 4 of
145      acceptable, and subdivide once. But proving this might be hard.
146
147      Third, at the last subdivision, x_m and y_m can be computed more
148      expeditiously (as in the routine above).
149
150      Finally, if we were able to subdivide by, say 2 or 3, this would
151      allow considerably finer-grain control, i.e. fewer points for the
152      same flatness tolerance. This would speed things up downstream.
153
154      In any case, this routine is unlikely to be the bottleneck. It's
155      just that I have this undying quest for more speed...
156
157   */
158
159   x3_0 = x3 - x0;
160   y3_0 = y3 - y0;
161
162   /* z3_0_dot is dist z0-z3 squared */
163   z3_0_dot = x3_0 * x3_0 + y3_0 * y3_0;
164
165   /* todo: this test is far from satisfactory. */
166   if (z3_0_dot < 0.001)
167     goto nosubdivide;
168
169   /* we can avoid subdivision if:
170
171      z1 has distance no more than flatness from the z0-z3 line
172
173      z1 is no more z0'ward than flatness past z0-z3
174
175      z1 is more z0'ward than z3'ward on the line traversing z0-z3
176
177      and correspondingly for z2 */
178
179   /* perp is distance from line, multiplied by dist z0-z3 */
180   max_perp_sq = flatness * flatness * z3_0_dot;
181
182   z1_perp = (y1 - y0) * x3_0 - (x1 - x0) * y3_0;
183   if (z1_perp * z1_perp > max_perp_sq)
184     goto subdivide;
185
186   z2_perp = (y3 - y2) * x3_0 - (x3 - x2) * y3_0;
187   if (z2_perp * z2_perp > max_perp_sq)
188     goto subdivide;
189
190   z1_dot = (x1 - x0) * x3_0 + (y1 - y0) * y3_0;
191   if (z1_dot < 0 && z1_dot * z1_dot > max_perp_sq)
192     goto subdivide;
193
194   z2_dot = (x3 - x2) * x3_0 + (y3 - y2) * y3_0;
195   if (z2_dot < 0 && z2_dot * z2_dot > max_perp_sq)
196     goto subdivide;
197
198   if (z1_dot + z1_dot > z3_0_dot)
199     goto subdivide;
200
201   if (z2_dot + z2_dot > z3_0_dot)
202     goto subdivide;
203
204  nosubdivide:
205   /* don't subdivide */
206   art_vpath_add_point (p_vpath, pn, pn_max,
207                        ART_LINETO, x3, y3);
208   return;
209
210  subdivide:
211
212   xa1 = (x0 + x1) * 0.5;
213   ya1 = (y0 + y1) * 0.5;
214   xa2 = (x0 + 2 * x1 + x2) * 0.25;
215   ya2 = (y0 + 2 * y1 + y2) * 0.25;
216   xb1 = (x1 + 2 * x2 + x3) * 0.25;
217   yb1 = (y1 + 2 * y2 + y3) * 0.25;
218   xb2 = (x2 + x3) * 0.5;
219   yb2 = (y2 + y3) * 0.5;
220   x_m = (xa2 + xb1) * 0.5;
221   y_m = (ya2 + yb1) * 0.5;
222 #ifdef VERBOSE
223   printf ("%g,%g %g,%g %g,%g %g,%g\n", xa1, ya1, xa2, ya2,
224           xb1, yb1, xb2, yb2);
225 #endif
226   art_vpath_render_bez (p_vpath, pn, pn_max,
227                         x0, y0, xa1, ya1, xa2, ya2, x_m, y_m, flatness);
228   art_vpath_render_bez (p_vpath, pn, pn_max,
229                         x_m, y_m, xb1, yb1, xb2, yb2, x3, y3, flatness);
230 }
231
232 /**
233  * art_bez_path_to_vec: Create vpath from bezier path.
234  * @bez: Bezier path.
235  * @flatness: Flatness control.
236  *
237  * Creates a vector path closely approximating the bezier path defined by
238  * @bez. The @flatness argument controls the amount of subdivision. In
239  * general, the resulting vpath deviates by at most @flatness pixels
240  * from the "ideal" path described by @bez.
241  *
242  * Return value: Newly allocated vpath.
243  **/
244 ArtVpath *
245 art_bez_path_to_vec (const ArtBpath *bez, double flatness)
246 {
247   ArtVpath *vec;
248   int vec_n, vec_n_max;
249   int bez_index;
250   double x, y;
251
252   vec_n = 0;
253   vec_n_max = RENDER_SIZE;
254   vec = art_new (ArtVpath, vec_n_max);
255
256   /* Initialization is unnecessary because of the precondition that the
257      bezier path does not begin with LINETO or CURVETO, but is here
258      to make the code warning-free. */
259   x = 0;
260   y = 0;
261
262   bez_index = 0;
263   do
264     {
265 #ifdef VERBOSE
266       printf ("%s %g %g\n",
267               bez[bez_index].code == ART_CURVETO ? "curveto" :
268               bez[bez_index].code == ART_LINETO ? "lineto" :
269               bez[bez_index].code == ART_MOVETO ? "moveto" :
270               bez[bez_index].code == ART_MOVETO_OPEN ? "moveto-open" :
271               "end", bez[bez_index].x3, bez[bez_index].y3);
272 #endif
273       /* make sure space for at least one more code */
274       if (vec_n >= vec_n_max)
275         art_expand (vec, ArtVpath, vec_n_max);
276       switch (bez[bez_index].code)
277         {
278         case ART_MOVETO_OPEN:
279         case ART_MOVETO:
280         case ART_LINETO:
281           x = bez[bez_index].x3;
282           y = bez[bez_index].y3;
283           vec[vec_n].code = bez[bez_index].code;
284           vec[vec_n].x = x;
285           vec[vec_n].y = y;
286           vec_n++;
287           break;
288         case ART_END:
289           vec[vec_n].code = bez[bez_index].code;
290           vec[vec_n].x = 0;
291           vec[vec_n].y = 0;
292           vec_n++;
293           break;
294         case ART_CURVETO:
295 #ifdef VERBOSE
296           printf ("%g,%g %g,%g %g,%g %g,%g\n", x, y,
297                          bez[bez_index].x1, bez[bez_index].y1,
298                          bez[bez_index].x2, bez[bez_index].y2,
299                          bez[bez_index].x3, bez[bez_index].y3);
300 #endif
301           art_vpath_render_bez (&vec, &vec_n, &vec_n_max,
302                                 x, y,
303                                 bez[bez_index].x1, bez[bez_index].y1,
304                                 bez[bez_index].x2, bez[bez_index].y2,
305                                 bez[bez_index].x3, bez[bez_index].y3,
306                                 flatness);
307           x = bez[bez_index].x3;
308           y = bez[bez_index].y3;
309           break;
310         }
311     }
312   while (bez[bez_index++].code != ART_END);
313   return vec;
314 }
315