Debian Codegraph @

texk/ps2pk/curves.c

raw
1 /* $XConsortium: curves.c,v 1.3 91/10/10 11:17:56 rws Exp $ */
2 /* Copyright International Business Machines,Corp. 1991 */
3 /* All Rights Reserved */
4
5 /* License to use, copy, modify, and distribute this software */
6 /* and its documentation for any purpose and without fee is */
7 /* hereby granted, provided that licensee provides a license to */
8 /* IBM, Corp. to use, copy, modify, and distribute derivative */
9 /* works and their documentation for any purpose and without */
10 /* fee, that the above copyright notice appear in all copies */
11 /* and that both that copyright notice and this permission */
12 /* notice appear in supporting documentation, and that the name */
13 /* of IBM not be used in advertising or publicity pertaining to */
14 /* distribution of the software without specific, written prior */
15 /* permission. */
16
17 /* IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES */
18 /* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT */
19 /* LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, */
20 /* FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF */
21 /* THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND */
22 /* PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT */
23 /* OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF */
24 /* THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES */
25 /* THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN */
26 /* NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR */
27 /* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING */
28 /* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF */
29 /* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT */
30 /* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS */
31 /* SOFTWARE. */
32 /*
33 :h1.CURVES Module - Stepping Beziers
34
35 This module is responsible for "rasterizing"
36 third order curves. That is, it changes the high level curve
37 specification into a list of pels that that curve travels
38 through.
39
40 :h3.Include Files
41
42 Include files needed:
43 */
44
45 #include "types.h"
46 #include "objects.h"
47 #include "spaces.h"
48 #include "paths.h"
49 #include "regions.h"
50 #include "curves.h"
51 #include "lines.h"
52 #include "arith.h"
53
54
55 /*
56 :h3.Functions Provided to Other Modules
57
58 External entry points:
59 */
60 /*SHARED LINE(S) ORIGINATED HERE*/
61
62 /*
63 Note that "stepping" and "flattening" are so similiar that they use the
64 same routine. When the "region" parameter is NULL, that is a flag that
65 we are flattening instead of stepping.
66 */
67 /*
68 :h2.Bezier Third Order Curves
69 */
70 /*
71 :h3.The "bezierinfo" Structure
72
73 This structure is used to store information used when we subdivide
74 Bezier curves.
75 */
76
77 struct bezierinfo {
78 struct region *region; /* the region being built or NULL */
79 struct fractpoint last; /* not used yet; maybe could save some work */
80 struct fractpoint origin; /* the origin of the bezier */
81 } ;
82
83 /*
84 Checking for termination of the subdivision process:
85 This is the stupidest test in the world, just check if the coordinatewise
86 distance from an end control point to the next control point is less than
87 one half pel. If so, we must be done.
88 This returns 1 if the subdivision is terminated and 0 if you still need
89 to subdivide.
90 */
91
92 static int BezierTerminationTest(fractpel xa, fractpel ya, fractpel xb, fractpel yb,
93 fractpel xc, fractpel yc, fractpel xd, fractpel yd)
94 {
95 fractpel dmax;
96 dmax = ABS(xa - xb);
97 dmax = MAX(dmax,ABS(ya - yb));
98 dmax = MAX(dmax,ABS(xd - xc));
99 dmax = MAX(dmax,ABS(yd - yc));
100 if(dmax > FPHALF)
101 return(0); /* not done yet */
102 else
103 return(1); /* done */
104 }
105
106 /*
107 :h3.StepBezierRecurse() - The Recursive Logic in StepBezier()
108
109 The recursion involves dividing the control polygon into two smaller
110 control polygons by finding the midpoints of the lines. This idea is
111 described in any graphics text book and its simplicity is what caused
112 Bezier to define his curves as he did. If the input region 'R' is NULL,
113 the result is a path that is the 'flattened' curve; otherwise StepBezier
114 returns nothing special.
115 */
116 static struct segment *StepBezierRecurse(
117 struct bezierinfo *I, /* Region under construction or NULL */
118 fractpel xA, fractpel yA, /* A control point */
119 fractpel xB, fractpel yB, /* B control point */
120 fractpel xC, fractpel yC, /* C control point */
121 fractpel xD, fractpel yD) /* D control point */
122
123 {
124 if (BezierTerminationTest(xA,yA,xB,yB,xC,yC,xD,yD))
125 {
126 if (I->region == NULL)
127 return(PathSegment(LINETYPE, xD - xA, yD - yA));
128 else
129 StepLine(I->region, I->origin.x + xA, I->origin.y + yA,
130 I->origin.x + xD, I->origin.y + yD);
131 }
132 else
133 {
134 fractpel xAB,yAB;
135 fractpel xBC,yBC;
136 fractpel xCD,yCD;
137 fractpel xABC,yABC;
138 fractpel xBCD,yBCD;
139 fractpel xABCD,yABCD;
140
141 xAB = xA + xB; yAB = yA + yB;
142 xBC = xB + xC; yBC = yB + yC;
143 xCD = xC + xD; yCD = yC + yD;
144
145 xABC = xAB + xBC; yABC = yAB + yBC;
146 xBCD = xBC + xCD; yBCD = yBC + yCD;
147
148 xABCD = xABC + xBCD; yABCD = yABC + yBCD;
149
150 xAB >>= 1; yAB >>= 1;
151 xBC >>= 1; yBC >>= 1;
152 xCD >>= 1; yCD >>= 1;
153 xABC >>= 2; yABC >>= 2;
154 xBCD >>= 2; yBCD >>= 2;
155 xABCD >>= 3; yABCD >>= 3;
156
157 if (I->region == NULL)
158 {
159 return( Join(
160 StepBezierRecurse(I, xA, yA, xAB, yAB, xABC, yABC, xABCD, yABCD),
161 StepBezierRecurse(I, xABCD, yABCD, xBCD, yBCD, xCD, yCD, xD, yD)
162 )
163 );
164 }
165 else
166 {
167 StepBezierRecurse(I, xA, yA, xAB, yAB, xABC, yABC, xABCD, yABCD);
168 StepBezierRecurse(I, xABCD, yABCD, xBCD, yBCD, xCD, yCD, xD, yD);
169 }
170 }
171 /*NOTREACHED*/
172 return NULL;
173 }
174
175 /*
176 :h3.TOOBIG() - Macro to Test if a Coordinate is Too Big to Bezier SubDivide Normally
177
178 Intermediate values in the Bezier subdivision are 8 times bigger than
179 the starting values. If this overflows, a 'long', we are in trouble:
180 */
181
182 #if defined(BITS)
183 #undef BITS
184 #endif
185 #define BITS (sizeof(int32_t)*8)
186 #define HIGHTEST(p) (((p)>>(BITS-4)) != 0) /* includes sign bit */
187 #define TOOBIG(xy) ((xy < 0) ? HIGHTEST(-xy) : HIGHTEST(xy))
188
189 /*
190 :h3.StepBezier() - Produce Run Ends for a Bezier Curve
191
192 This is the entry point called from outside the module.
193 */
194
195 struct segment *StepBezier(
196 struct region *R, /* Region under construction or NULL */
197 fractpel xA, fractpel yA, /* A control point */
198 fractpel xB, fractpel yB, /* B control point */
199 fractpel xC, fractpel yC, /* C control point */
200 fractpel xD, fractpel yD) /* D control point */
201 {
202 struct bezierinfo Info;
203
204 Info.region = R;
205 Info.origin.x = xA;
206 Info.origin.y = yA;
207
208 xB -= xA;
209 xC -= xA;
210 xD -= xA;
211 yB -= yA;
212 yC -= yA;
213 yD -= yA;
214
215 if ( TOOBIG(xB) || TOOBIG(yB) || TOOBIG(xC) || TOOBIG(yC)
216 || TOOBIG(xD) || TOOBIG(yD) )
217 t1_abort("Beziers this big not yet supported");
218
219 return(StepBezierRecurse(&Info,
220 (fractpel) 0, (fractpel) 0, xB, yB, xC, yC, xD, yD));
221 }
222