mizerline.c 9.5 KB

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