midash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "regionstr.h"
  40. #include "mistruct.h"
  41. #include "mifpoly.h"
  42. static miDashPtr CheckDashStorage(miDashPtr *ppseg, int nseg, int *pnsegMax);
  43. /* return a list of DashRec. there will be an extra
  44. entry at the end holding the last point of the polyline.
  45. this means that the code that actually draws dashes can
  46. get a pair of points for every dash. only the point in the last
  47. dash record is useful; the other fields are not used.
  48. nseg is the number of segments, not the number of points.
  49. example:
  50. dash1.start
  51. dash2.start
  52. dash3.start
  53. last-point
  54. defines a list of segments
  55. (dash1.pt, dash2.pt)
  56. (dash2.pt, dash3.pt)
  57. (dash3.pt, last-point)
  58. and nseg == 3.
  59. NOTE:
  60. EVEN_DASH == ~ODD_DASH
  61. NOTE ALSO:
  62. miDashLines may return 0 segments, going from pt[0] to pt[0] with one dash.
  63. */
  64. miDashPtr
  65. miDashLine(npt, ppt, nDash, pDash, offset, pnseg)
  66. int npt;
  67. DDXPointPtr ppt;
  68. unsigned int nDash;
  69. unsigned char *pDash;
  70. unsigned int offset;
  71. int *pnseg;
  72. {
  73. DDXPointRec pt1, pt2;
  74. int lenCur; /* npt used from this dash */
  75. int lenMax; /* npt in this dash */
  76. int iDash = 0; /* index of current dash */
  77. int which; /* EVEN_DASH or ODD_DASH */
  78. miDashPtr pseg; /* list of dash segments */
  79. miDashPtr psegBase; /* start of list */
  80. int nseg = 0; /* number of dashes so far */
  81. int nsegMax = 0; /* num segs we can fit in this list */
  82. int x, y, len;
  83. int adx, ady, signdx, signdy;
  84. int du, dv, e1, e2, e, base_e = 0;
  85. lenCur = offset;
  86. which = EVEN_DASH;
  87. while(lenCur >= pDash[iDash])
  88. {
  89. lenCur -= pDash[iDash];
  90. iDash++;
  91. if (iDash >= nDash)
  92. iDash = 0;
  93. which = ~which;
  94. }
  95. lenMax = pDash[iDash];
  96. psegBase = (miDashPtr)NULL;
  97. pt2 = ppt[0]; /* just in case there is only one point */
  98. while(--npt)
  99. {
  100. if (PtEqual(ppt[0], ppt[1]))
  101. {
  102. ppt++;
  103. continue; /* no duplicated points in polyline */
  104. }
  105. pt1 = *ppt++;
  106. pt2 = *ppt;
  107. adx = pt2.x - pt1.x;
  108. ady = pt2.y - pt1.y;
  109. signdx = sign(adx);
  110. signdy = sign(ady);
  111. adx = abs(adx);
  112. ady = abs(ady);
  113. if (adx > ady)
  114. {
  115. du = adx;
  116. dv = ady;
  117. len = adx;
  118. }
  119. else
  120. {
  121. du = ady;
  122. dv = adx;
  123. len = ady;
  124. }
  125. e1 = dv * 2;
  126. e2 = e1 - 2*du;
  127. e = e1 - du;
  128. x = pt1.x;
  129. y = pt1.y;
  130. nseg++;
  131. pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
  132. if (!pseg)
  133. return (miDashPtr)NULL;
  134. pseg->pt = pt1;
  135. pseg->e1 = e1;
  136. pseg->e2 = e2;
  137. base_e = pseg->e = e;
  138. pseg->which = which;
  139. pseg->newLine = 1;
  140. while (len--)
  141. {
  142. if (adx > ady)
  143. {
  144. /* X_AXIS */
  145. if (((signdx > 0) && (e < 0)) ||
  146. ((signdx <=0) && (e <=0))
  147. )
  148. {
  149. e += e1;
  150. }
  151. else
  152. {
  153. y += signdy;
  154. e += e2;
  155. }
  156. x += signdx;
  157. }
  158. else
  159. {
  160. /* Y_AXIS */
  161. if (((signdx > 0) && (e < 0)) ||
  162. ((signdx <=0) && (e <=0))
  163. )
  164. {
  165. e +=e1;
  166. }
  167. else
  168. {
  169. x += signdx;
  170. e += e2;
  171. }
  172. y += signdy;
  173. }
  174. lenCur++;
  175. if (lenCur >= lenMax && (len || npt <= 1))
  176. {
  177. nseg++;
  178. pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
  179. if (!pseg)
  180. return (miDashPtr)NULL;
  181. pseg->pt.x = x;
  182. pseg->pt.y = y;
  183. pseg->e1 = e1;
  184. pseg->e2 = e2;
  185. pseg->e = e;
  186. which = ~which;
  187. pseg->which = which;
  188. pseg->newLine = 0;
  189. /* move on to next dash */
  190. iDash++;
  191. if (iDash >= nDash)
  192. iDash = 0;
  193. lenMax = pDash[iDash];
  194. lenCur = 0;
  195. }
  196. } /* while len-- */
  197. } /* while --npt */
  198. if (lenCur == 0 && nseg != 0)
  199. {
  200. nseg--;
  201. which = ~which;
  202. }
  203. *pnseg = nseg;
  204. pseg = CheckDashStorage(&psegBase, nseg+1, &nsegMax);
  205. if (!pseg)
  206. return (miDashPtr)NULL;
  207. pseg->pt = pt2;
  208. pseg->e = base_e;
  209. pseg->which = which;
  210. pseg->newLine = 0;
  211. return psegBase;
  212. }
  213. #define NSEGDELTA 16
  214. /* returns a pointer to the pseg[nseg-1], growing the storage as
  215. necessary. this interface seems unnecessarily cumbersome.
  216. */
  217. static
  218. miDashPtr
  219. CheckDashStorage(
  220. miDashPtr *ppseg, /* base pointer */
  221. int nseg, /* number of segment we want to write to */
  222. int *pnsegMax) /* size (in segments) of list so far */
  223. {
  224. if (nseg > *pnsegMax)
  225. {
  226. miDashPtr newppseg;
  227. *pnsegMax += NSEGDELTA;
  228. newppseg = (miDashPtr)realloc(*ppseg,
  229. (*pnsegMax)*sizeof(miDashRec));
  230. if (!newppseg)
  231. {
  232. free(*ppseg);
  233. return (miDashPtr)NULL;
  234. }
  235. *ppseg = newppseg;
  236. }
  237. return(*ppseg+(nseg-1));
  238. }
  239. _X_EXPORT void
  240. miStepDash (dist, pDashIndex, pDash, numInDashList, pDashOffset)
  241. int dist; /* distance to step */
  242. int *pDashIndex; /* current dash */
  243. unsigned char *pDash; /* dash list */
  244. int numInDashList; /* total length of dash list */
  245. int *pDashOffset; /* offset into current dash */
  246. {
  247. int dashIndex, dashOffset;
  248. int totallen;
  249. int i;
  250. dashIndex = *pDashIndex;
  251. dashOffset = *pDashOffset;
  252. if (dist < pDash[dashIndex] - dashOffset)
  253. {
  254. *pDashOffset = dashOffset + dist;
  255. return;
  256. }
  257. dist -= pDash[dashIndex] - dashOffset;
  258. if (++dashIndex == numInDashList)
  259. dashIndex = 0;
  260. totallen = 0;
  261. for (i = 0; i < numInDashList; i++)
  262. totallen += pDash[i];
  263. if (totallen && totallen <= dist)
  264. dist = dist % totallen;
  265. while (dist >= pDash[dashIndex])
  266. {
  267. dist -= pDash[dashIndex];
  268. if (++dashIndex == numInDashList)
  269. dashIndex = 0;
  270. }
  271. *pDashIndex = dashIndex;
  272. *pDashOffset = dist;
  273. }