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