First import
[xorg_rtime.git] / xorg-server-1.4 / mi / mizerline.c
blob073f1b20f39298dcacd75037b37046f027ed12cc
1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28 All Rights Reserved
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
46 ******************************************************************/
47 #ifdef HAVE_DIX_CONFIG_H
48 #include <dix-config.h>
49 #endif
51 #include <X11/X.h>
53 #include "misc.h"
54 #include "scrnintstr.h"
55 #include "gcstruct.h"
56 #include "windowstr.h"
57 #include "pixmap.h"
58 #include "mi.h"
59 #include "miline.h"
61 /* Draw lineSolid, fillStyle-independent zero width lines.
63 * Must keep X and Y coordinates in "ints" at least until after they're
64 * translated and clipped to accomodate CoordModePrevious lines with very
65 * large coordinates.
67 * Draws the same pixels regardless of sign(dx) or sign(dy).
69 * Ken Whaley
73 /* largest positive value that can fit into a component of a point.
74 * Assumes that the point structure is {type x, y;} where type is
75 * a signed type.
77 #define MAX_COORDINATE ((1 << (((sizeof(DDXPointRec) >> 1) << 3) - 1)) - 1)
79 #define MI_OUTPUT_POINT(xx, yy)\
81 if ( !new_span && yy == current_y)\
83 if (xx < spans->x)\
84 spans->x = xx;\
85 ++*widths;\
87 else\
89 ++Nspans;\
90 ++spans;\
91 ++widths;\
92 spans->x = xx;\
93 spans->y = yy;\
94 *widths = 1;\
95 current_y = yy;\
96 new_span = FALSE;\
100 _X_EXPORT void
101 miZeroLine(pDraw, pGC, mode, npt, pptInit)
102 DrawablePtr pDraw;
103 GCPtr pGC;
104 int mode; /* Origin or Previous */
105 int npt; /* number of points */
106 DDXPointPtr pptInit;
108 int Nspans, current_y = 0;
109 DDXPointPtr ppt;
110 DDXPointPtr pspanInit, spans;
111 int *pwidthInit, *widths, list_len;
112 int xleft, ytop, xright, ybottom;
113 int new_x1, new_y1, new_x2, new_y2;
114 int x = 0, y = 0, x1, y1, x2, y2, xstart, ystart;
115 int oc1, oc2;
116 int result;
117 int pt1_clipped, pt2_clipped = 0;
118 Bool new_span;
119 int signdx, signdy;
120 int clipdx, clipdy;
121 int width, height;
122 int adx, ady;
123 int octant;
124 unsigned int bias = miGetZeroLineBias(pDraw->pScreen);
125 int e, e1, e2, e3; /* Bresenham error terms */
126 int length; /* length of lines == # of pixels on major axis */
128 xleft = pDraw->x;
129 ytop = pDraw->y;
130 xright = pDraw->x + pDraw->width - 1;
131 ybottom = pDraw->y + pDraw->height - 1;
133 if (!pGC->miTranslate)
135 /* do everything in drawable-relative coordinates */
136 xleft = 0;
137 ytop = 0;
138 xright -= pDraw->x;
139 ybottom -= pDraw->y;
142 /* it doesn't matter whether we're in drawable or screen coordinates,
143 * FillSpans simply cannot take starting coordinates outside of the
144 * range of a DDXPointRec component.
146 if (xright > MAX_COORDINATE)
147 xright = MAX_COORDINATE;
148 if (ybottom > MAX_COORDINATE)
149 ybottom = MAX_COORDINATE;
151 /* since we're clipping to the drawable's boundaries & coordinate
152 * space boundaries, we're guaranteed that the larger of width/height
153 * is the longest span we'll need to output
155 width = xright - xleft + 1;
156 height = ybottom - ytop + 1;
157 list_len = (height >= width) ? height : width;
158 pspanInit = (DDXPointPtr)ALLOCATE_LOCAL(list_len * sizeof(DDXPointRec));
159 pwidthInit = (int *)ALLOCATE_LOCAL(list_len * sizeof(int));
160 if (!pspanInit || !pwidthInit)
161 return;
163 Nspans = 0;
164 new_span = TRUE;
165 spans = pspanInit - 1;
166 widths = pwidthInit - 1;
167 ppt = pptInit;
169 xstart = ppt->x;
170 ystart = ppt->y;
171 if (pGC->miTranslate)
173 xstart += pDraw->x;
174 ystart += pDraw->y;
177 /* x2, y2, oc2 copied to x1, y1, oc1 at top of loop to simplify
178 * iteration logic
180 x2 = xstart;
181 y2 = ystart;
182 oc2 = 0;
183 MIOUTCODES(oc2, x2, y2, xleft, ytop, xright, ybottom);
185 while (--npt > 0)
187 if (Nspans > 0)
188 (*pGC->ops->FillSpans)(pDraw, pGC, Nspans, pspanInit,
189 pwidthInit, FALSE);
190 Nspans = 0;
191 new_span = TRUE;
192 spans = pspanInit - 1;
193 widths = pwidthInit - 1;
195 x1 = x2;
196 y1 = y2;
197 oc1 = oc2;
198 ++ppt;
200 x2 = ppt->x;
201 y2 = ppt->y;
202 if (pGC->miTranslate && (mode != CoordModePrevious))
204 x2 += pDraw->x;
205 y2 += pDraw->y;
207 else if (mode == CoordModePrevious)
209 x2 += x1;
210 y2 += y1;
213 oc2 = 0;
214 MIOUTCODES(oc2, x2, y2, xleft, ytop, xright, ybottom);
216 CalcLineDeltas(x1, y1, x2, y2, adx, ady, signdx, signdy, 1, 1, octant);
218 if (adx > ady)
220 e1 = ady << 1;
221 e2 = e1 - (adx << 1);
222 e = e1 - adx;
223 length = adx; /* don't draw endpoint in main loop */
225 FIXUP_ERROR(e, octant, bias);
227 new_x1 = x1;
228 new_y1 = y1;
229 new_x2 = x2;
230 new_y2 = y2;
231 pt1_clipped = 0;
232 pt2_clipped = 0;
234 if ((oc1 | oc2) != 0)
236 result = miZeroClipLine(xleft, ytop, xright, ybottom,
237 &new_x1, &new_y1, &new_x2, &new_y2,
238 adx, ady,
239 &pt1_clipped, &pt2_clipped,
240 octant, bias, oc1, oc2);
241 if (result == -1)
242 continue;
244 length = abs(new_x2 - new_x1);
246 /* if we've clipped the endpoint, always draw the full length
247 * of the segment, because then the capstyle doesn't matter
249 if (pt2_clipped)
250 length++;
252 if (pt1_clipped)
254 /* must calculate new error terms */
255 clipdx = abs(new_x1 - x1);
256 clipdy = abs(new_y1 - y1);
257 e += (clipdy * e2) + ((clipdx - clipdy) * e1);
261 /* draw the segment */
263 x = new_x1;
264 y = new_y1;
266 e3 = e2 - e1;
267 e = e - e1;
269 while (length--)
271 MI_OUTPUT_POINT(x, y);
272 e += e1;
273 if (e >= 0)
275 y += signdy;
276 e += e3;
278 x += signdx;
281 else /* Y major line */
283 e1 = adx << 1;
284 e2 = e1 - (ady << 1);
285 e = e1 - ady;
286 length = ady; /* don't draw endpoint in main loop */
288 SetYMajorOctant(octant);
289 FIXUP_ERROR(e, octant, bias);
291 new_x1 = x1;
292 new_y1 = y1;
293 new_x2 = x2;
294 new_y2 = y2;
295 pt1_clipped = 0;
296 pt2_clipped = 0;
298 if ((oc1 | oc2) != 0)
300 result = miZeroClipLine(xleft, ytop, xright, ybottom,
301 &new_x1, &new_y1, &new_x2, &new_y2,
302 adx, ady,
303 &pt1_clipped, &pt2_clipped,
304 octant, bias, oc1, oc2);
305 if (result == -1)
306 continue;
308 length = abs(new_y2 - new_y1);
310 /* if we've clipped the endpoint, always draw the full length
311 * of the segment, because then the capstyle doesn't matter
313 if (pt2_clipped)
314 length++;
316 if (pt1_clipped)
318 /* must calculate new error terms */
319 clipdx = abs(new_x1 - x1);
320 clipdy = abs(new_y1 - y1);
321 e += (clipdx * e2) + ((clipdy - clipdx) * e1);
325 /* draw the segment */
327 x = new_x1;
328 y = new_y1;
330 e3 = e2 - e1;
331 e = e - e1;
333 while (length--)
335 MI_OUTPUT_POINT(x, y);
336 e += e1;
337 if (e >= 0)
339 x += signdx;
340 e += e3;
342 y += signdy;
347 /* only do the capnotlast check on the last segment
348 * and only if the endpoint wasn't clipped. And then, if the last
349 * point is the same as the first point, do not draw it, unless the
350 * line is degenerate
352 if ( (! pt2_clipped) && (pGC->capStyle != CapNotLast) &&
353 (((xstart != x2) || (ystart != y2)) || (ppt == pptInit + 1)))
355 MI_OUTPUT_POINT(x, y);
358 if (Nspans > 0)
359 (*pGC->ops->FillSpans)(pDraw, pGC, Nspans, pspanInit,
360 pwidthInit, FALSE);
362 DEALLOCATE_LOCAL(pwidthInit);
363 DEALLOCATE_LOCAL(pspanInit);
366 _X_EXPORT void
367 miZeroDashLine(dst, pgc, mode, nptInit, pptInit)
368 DrawablePtr dst;
369 GCPtr pgc;
370 int mode;
371 int nptInit; /* number of points in polyline */
372 DDXPointRec *pptInit; /* points in the polyline */
374 /* XXX kludge until real zero-width dash code is written */
375 pgc->lineWidth = 1;
376 miWideDash (dst, pgc, mode, nptInit, pptInit);
377 pgc->lineWidth = 0;