mi_zerarc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /* This file is part of the GNU libxmi package.
  2. Copyright (C) 1985, 1986, 1987, 1988, 1989, X Consortium. For an
  3. associated permission notice, see the accompanying file README-X.
  4. GNU enhancements Copyright (C) 1998, 1999, 2000, 2005, Free Software
  5. Foundation, Inc.
  6. The GNU libxmi package is free software. You may redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software foundation; either version 2, or (at your
  9. option) any later version.
  10. The GNU libxmi package is distributed in the hope that it will be
  11. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with the GNU plotutils package; see the file COPYING. If not, write to
  16. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. /* Original author: Bob Scheifler, MIT X Consortium, mid 1980s.
  19. Hacked by Robert S. Maier <rsm@math.arizona.edu>, 1998-99 */
  20. /* Derived from:
  21. * "Algorithm for drawing ellipses or hyperbolae with a digital plotter"
  22. * by M. L. V. Pitteway
  23. * The Computer Journal, November 1967, Volume 10, Number 3, pp. 282-289
  24. */
  25. /* This module contains the miZeroPolyArc() function and its reentrant
  26. counterpart miZeroPolyArc_r. They draw single-pixel (Bresenham)
  27. polyarcs, either solid or dashed. A fast integer algorithm is used.
  28. A polyarc is a list of arcs, which may or may not be contiguous. An
  29. `arc' is an elliptic arc, i.e., a segment of an ellipse. The principal
  30. axes of the ellipse must be aligned with the coordinate axes.
  31. The cap mode and join mode in the graphics context are ignored.
  32. All painting goes through the low-level MI_COPY_AND_PAINT_SPANS() and
  33. MI_PAINT_SPANS() macros. */
  34. #include "sys-defines.h"
  35. #include "extern.h"
  36. #include "xmi.h"
  37. #include "mi_spans.h"
  38. #include "mi_gc.h"
  39. #include "mi_api.h"
  40. #include "mi_arc.h"
  41. #include "mi_zerarc.h"
  42. #define FULLCIRCLE (360 * 64)
  43. #define OCTANT (45 * 64)
  44. #define QUADRANT (90 * 64)
  45. #define HALFCIRCLE (180 * 64)
  46. #define QUADRANT3 (270 * 64)
  47. /* trig functions, angle specified in 1/64 degrees */
  48. #define Dsin(d) ((d) == 0 ? 0.0 : ((d) == QUADRANT ? 1.0 : \
  49. ((d) == HALFCIRCLE ? 0.0 : \
  50. ((d) == QUADRANT3 ? -1.0 : sin((double)d*(M_PI/11520.0))))))
  51. #define Dcos(d) ((d) == 0 ? 1.0 : ((d) == QUADRANT ? 0.0 : \
  52. ((d) == HALFCIRCLE ? -1.0 : \
  53. ((d) == QUADRANT3 ? 0.0 : cos((double)d*(M_PI/11520.0))))))
  54. #define EPSILON45 64
  55. typedef struct
  56. {
  57. bool skipStart;
  58. bool haveStart;
  59. miPoint startPt;
  60. bool haveLast;
  61. bool skipLast;
  62. miPoint endPt;
  63. int dashNum;
  64. int dashIndex;
  65. int dashOffset;
  66. int dashNumInit;
  67. int dashIndexInit;
  68. int dashOffsetInit;
  69. } miDashInfo;
  70. static const miZeroArcPt _oob_arc_pt = {INT_MAX, INT_MAX, 0};
  71. /* forward references */
  72. static bool miZeroArcSetup (const miArc *arc, miZeroArc *info, bool ok360);
  73. static miPoint * miZeroArcPts (const miArc *arc, miPoint *pts);
  74. static void miZeroArcDashPts (const miGC *pGC, const miArc *arc, miDashInfo *dinfo, int maxPts, miPoint **pts);
  75. /*
  76. * This is the reentrant version, miZeroPolyArc_r. The non-reentrant
  77. * version, miZeroPolyArc, simply calls this version, using an in-library
  78. * `rasterized ellipse' cache. */
  79. void
  80. miZeroPolyArc_r (miPaintedSet *paintedSet, const miGC *pGC, int narcs, const miArc *parcs, miEllipseCache *ellipseCache)
  81. {
  82. const miArc *arc;
  83. miDashInfo dinfo;
  84. int j;
  85. if (pGC->lineStyle != (int)MI_LINE_SOLID)
  86. /* initialize structure used in dashing */
  87. {
  88. dinfo.haveStart = false;
  89. dinfo.skipStart = false;
  90. dinfo.haveLast = false;
  91. dinfo.dashIndexInit = 0;
  92. dinfo.dashNumInit = 0;
  93. dinfo.dashOffsetInit = 0;
  94. /* perform initial offsetting into the dash array */
  95. miStepDash (pGC->dashOffset, &dinfo.dashNumInit, &dinfo.dashIndexInit,
  96. pGC->dash, pGC->numInDashList, &dinfo.dashOffsetInit);
  97. }
  98. for (arc = parcs, j = narcs; --j >= 0; arc++)
  99. {
  100. if (!MI_CAN_ZERO_ARC(arc))
  101. /* Too large an arc for integer algorithm to perform properly, so
  102. hand it off to floating-point wide polyarc algorithm, which can
  103. do zero-width polyarcs too. */
  104. /* N.B. This handoff is lame. If dashing, dash pattern won't be
  105. carried over from arc to contiguous arc. */
  106. miPolyArc_r (paintedSet, pGC, 1, arc, ellipseCache);
  107. else
  108. /* not unusually large, use integer Bresenham algorithm */
  109. {
  110. miPoint **ptsInit, **pts;
  111. int maxPts = 0, numPts, i, n;
  112. int numPixels = pGC->numPixels;
  113. if (arc->width > arc->height)
  114. maxPts = arc->width + (arc->height >> 1);
  115. else
  116. maxPts = arc->height + (arc->width >> 1);
  117. if (maxPts == 0)
  118. continue;
  119. /* max points produced by Bresenham algorithm (overestimate?) */
  120. numPts = 4 * maxPts;
  121. /* generate points (note that if dashing, dash pattern will carry
  122. over from arc to contiguous arc) */
  123. ptsInit =
  124. (miPoint **)mi_xmalloc(numPixels * sizeof(miPoint *));
  125. pts =
  126. (miPoint **)mi_xmalloc(numPixels * sizeof(miPoint *));
  127. if (pGC->lineStyle == (int)MI_LINE_SOLID)
  128. {
  129. for (i = 0; i < numPixels; i++)
  130. {
  131. if (i == 1)
  132. ptsInit[i] = (miPoint *)mi_xmalloc(numPts * sizeof(miPoint));
  133. else /* `solid' uses paint type #1 only */
  134. ptsInit[i] = (miPoint *)NULL;
  135. pts[i] = ptsInit[i];
  136. }
  137. /* compute points, return pointer to slot after
  138. last-generated point */
  139. pts[1] = miZeroArcPts (arc, ptsInit[1]);
  140. }
  141. else /* on/off dashed or double-dashed */
  142. {
  143. for (i = 0; i < numPixels; i++)
  144. {
  145. ptsInit[i] = (miPoint *)mi_xmalloc(numPts * sizeof(miPoint));
  146. pts[i] = ptsInit[i];
  147. }
  148. /* compute points, return ptrs to ones after last-generated */
  149. dinfo.skipLast = (i == 0 ? false : true);
  150. miZeroArcDashPts (pGC, arc, &dinfo, maxPts, pts);
  151. dinfo.skipStart = true;
  152. }
  153. /* paint all generated points (except if not double-dashing,
  154. don't paint points in paint type #0) */
  155. for (i = 0; i < numPixels; i++)
  156. {
  157. if (ptsInit[i] == (miPoint *)NULL)
  158. continue;
  159. if (i == 0 && pGC->lineStyle != (int)MI_LINE_DOUBLE_DASH)
  160. {
  161. free (ptsInit[i]);
  162. continue;
  163. }
  164. n = pts[i] - ptsInit[i];
  165. if (n > 0)
  166. {
  167. unsigned int *widths;
  168. int k;
  169. widths = (unsigned int *)mi_xmalloc(n * sizeof(unsigned int));
  170. for (k = 0; k < n; k++)
  171. widths[k] = 1;
  172. miQuickSortSpansY (ptsInit[i], widths, n);
  173. MI_PAINT_SPANS(paintedSet, pGC->pixels[i], n, ptsInit[i], widths)
  174. }
  175. } /* end of drawing loop over paint types */
  176. /* free arrays of pointers to storage */
  177. free (pts);
  178. free (ptsInit);
  179. } /* end of integer Bresenham algorithm applied to a single arc */
  180. } /* end of loop over arcs */
  181. }
  182. #ifndef NO_NONREENTRANT_POLYARC_SUPPORT
  183. void
  184. miZeroPolyArc (miPaintedSet *paintedSet, const miGC *pGC, int narcs, const miArc *parcs)
  185. {
  186. if (_mi_ellipseCache == (miEllipseCache *)NULL)
  187. _mi_ellipseCache = miNewEllipseCache ();
  188. miZeroPolyArc_r (paintedSet, pGC, narcs, parcs, _mi_ellipseCache);
  189. }
  190. #endif /* not NO_NONREENTRANT_POLYARC_SUPPORT */
  191. #define Pixelate(pts, xval, yval) \
  192. { \
  193. pts->x = xval; \
  194. pts->y = yval; \
  195. pts++; \
  196. }
  197. #define DoPix(pts, mask, idx, xval, yval) \
  198. if (mask & (1 << idx)) Pixelate(pts, xval, yval);
  199. /* Generate points that make up a solid zero-width arc, and write them to
  200. pre-allocated storage; return pointer to miPoint after last-generated
  201. point. Storage supplied by caller, should be sufficiently large. */
  202. static miPoint *
  203. miZeroArcPts (const miArc *arc, miPoint *pts)
  204. {
  205. miZeroArc info;
  206. int x, y, a, b, d;
  207. unsigned int mask;
  208. int k1, k3, dx, dy;
  209. bool do360;
  210. do360 = miZeroArcSetup(arc, &info, true);
  211. MIARCSETUP(info, x, y, k1, k3, a, b, d, dx, dy);
  212. mask = info.initialMask;
  213. if (!(arc->width & 1)) /* even width */
  214. {
  215. DoPix (pts, mask, 1, info.xorgo, info.yorg);
  216. DoPix (pts, mask, 3, info.xorgo, info.yorgo);
  217. }
  218. if (!info.end.x || !info.end.y)
  219. {
  220. mask = info.end.mask;
  221. info.end = info.altend;
  222. }
  223. if (do360 && (arc->width == arc->height) && !(arc->width & 1))
  224. /* full circle, even diameter */
  225. {
  226. int yorgh = info.yorg + info.h;
  227. int xorghp = info.xorg + info.h;
  228. int xorghn = info.xorg - info.h;
  229. for ( ; ; )
  230. {
  231. Pixelate(pts, info.xorg + x, info.yorg + y);
  232. Pixelate(pts, info.xorg - x, info.yorg + y);
  233. Pixelate(pts, info.xorg - x, info.yorgo - y);
  234. Pixelate(pts, info.xorg + x, info.yorgo - y);
  235. if (a < 0)
  236. break;
  237. Pixelate(pts, xorghp - y, yorgh - x);
  238. Pixelate(pts, xorghn + y, yorgh - x);
  239. Pixelate(pts, xorghn + y, yorgh + x);
  240. Pixelate(pts, xorghp - y, yorgh + x);
  241. MIARCCIRCLESTEP(x, y, a, b, d, k1, k3, ;);
  242. }
  243. if (x > 1 && pts[-1].x == pts[-5].x && pts[-1].y == pts[-5].y)
  244. pts -= 4;
  245. x = info.w;
  246. y = info.h;
  247. }
  248. else if (do360)
  249. /* full ellipse */
  250. {
  251. while (y < (int)info.h || x < (int)info.w)
  252. {
  253. MIARCOCTANTSHIFT(info, x, y, dx, dy, a, b, d, k1, k3, ;);
  254. Pixelate(pts, info.xorg + x, info.yorg + y);
  255. Pixelate(pts, info.xorgo - x, info.yorg + y);
  256. Pixelate(pts, info.xorgo - x, info.yorgo - y);
  257. Pixelate(pts, info.xorg + x, info.yorgo - y);
  258. MIARCSTEP(x, y, dx, dy, a, b, d, k1, k3, ;, ;);
  259. }
  260. }
  261. else
  262. /* hard case */
  263. {
  264. while (y < (int)info.h || x < (int)info.w)
  265. {
  266. MIARCOCTANTSHIFT(info, x, y, dx, dy, a, b, d, k1, k3, ;);
  267. if ((x == info.start.x) || (y == info.start.y))
  268. {
  269. mask = info.start.mask;
  270. info.start = info.altstart;
  271. }
  272. DoPix (pts, mask, 0, info.xorg + x, info.yorg + y);
  273. DoPix (pts, mask, 1, info.xorgo - x, info.yorg + y);
  274. DoPix (pts, mask, 2, info.xorgo - x, info.yorgo - y);
  275. DoPix (pts, mask, 3, info.xorg + x, info.yorgo - y);
  276. if ((x == info.end.x) || (y == info.end.y))
  277. {
  278. mask = info.end.mask;
  279. info.end = info.altend;
  280. }
  281. MIARCSTEP(x, y, dx, dy, a, b, d, k1, k3, ;, ;);
  282. }
  283. }
  284. if ((x == info.start.x) || (y == info.start.y))
  285. mask = info.start.mask;
  286. DoPix (pts, mask, 0, info.xorg + x, info.yorg + y);
  287. DoPix (pts, mask, 2, info.xorgo - x, info.yorgo - y);
  288. if (arc->height & 1) /* odd height */
  289. {
  290. DoPix (pts, mask, 1, info.xorgo - x, info.yorg + y);
  291. DoPix (pts, mask, 3, info.xorg + x, info.yorgo - y);
  292. }
  293. return pts;
  294. }
  295. #undef DoPix
  296. #define DoPix(arcPts, mask, idx, xval, yval) \
  297. if (mask & (1 << idx)) \
  298. { \
  299. arcPts[idx]->x = xval; \
  300. arcPts[idx]->y = yval; \
  301. arcPts[idx]++; \
  302. }
  303. /* Generate the points that make up a dashed zero-width arc, possibly
  304. multicolored; write them to pre-allocated storage, indexed by paint
  305. type. */
  306. /* ARGS: dinfo = updated vars (e.g. dashNum, dashIndex),
  307. pts[i] = storage for paint type #i */
  308. static void
  309. miZeroArcDashPts (const miGC *pGC, const miArc *arc, miDashInfo *dinfo, int maxPts, miPoint **pts)
  310. {
  311. miZeroArc info;
  312. int x, y, a, b, d;
  313. unsigned int mask;
  314. int k1, k3, dx, dy;
  315. int dashRemaining, numPixels;
  316. miPoint *points, *arcPts[4];
  317. miPoint *startPts[5], *endPts[5];
  318. int deltas[5];
  319. miPoint *pt, *startPt, *lastPt;
  320. int i, j, seg, startseg;
  321. /* allocate temp storage, split into four pieces */
  322. points = (miPoint *)mi_xmalloc(sizeof(miPoint) * 4 * maxPts);
  323. for (i = 0; i < 4; i++)
  324. arcPts[i] = points + (i * maxPts);
  325. miZeroArcSetup (arc, &info, false);
  326. MIARCSETUP(info, x, y, k1, k3, a, b, d, dx, dy);
  327. mask = info.initialMask;
  328. startseg = info.startAngle / QUADRANT;
  329. startPt = arcPts[startseg];
  330. if (!(arc->width & 1))
  331. {
  332. DoPix (arcPts, mask, 1, info.xorgo, info.yorg);
  333. DoPix (arcPts, mask, 3, info.xorgo, info.yorgo);
  334. }
  335. if (!info.end.x || !info.end.y)
  336. {
  337. mask = info.end.mask;
  338. info.end = info.altend;
  339. }
  340. while (y < (int)info.h || x < (int)info.w)
  341. {
  342. MIARCOCTANTSHIFT(info, x, y, dx, dy, a, b, d, k1, k3, ;);
  343. if ((x == info.firstx) || (y == info.firsty))
  344. startPt = arcPts[startseg];
  345. if ((x == info.start.x) || (y == info.start.y))
  346. {
  347. mask = info.start.mask;
  348. info.start = info.altstart;
  349. }
  350. DoPix (arcPts, mask, 0, info.xorg + x, info.yorg + y);
  351. DoPix (arcPts, mask, 1, info.xorgo - x, info.yorg + y);
  352. DoPix (arcPts, mask, 2, info.xorgo - x, info.yorgo - y);
  353. DoPix (arcPts, mask, 3, info.xorg + x, info.yorgo - y);
  354. if ((x == info.end.x) || (y == info.end.y))
  355. {
  356. mask = info.end.mask;
  357. info.end = info.altend;
  358. }
  359. MIARCSTEP(x, y, dx, dy, a, b, d, k1, k3, ;, ;);
  360. }
  361. if ((x == info.firstx) || (y == info.firsty))
  362. startPt = arcPts[startseg];
  363. if ((x == info.start.x) || (y == info.start.y))
  364. mask = info.start.mask;
  365. DoPix (arcPts, mask, 0, info.xorg + x, info.yorg + y);
  366. DoPix (arcPts, mask, 2, info.xorgo - x, info.yorgo - y);
  367. if (arc->height & 1)
  368. {
  369. DoPix (arcPts, mask, 1, info.xorgo - x, info.yorg + y);
  370. DoPix (arcPts, mask, 3, info.xorg + x, info.yorgo - y);
  371. }
  372. for (i = 0; i < 4; i++)
  373. {
  374. seg = (startseg + i) & 3;
  375. pt = points + (seg * maxPts);
  376. if (seg & 1)
  377. {
  378. startPts[i] = pt;
  379. endPts[i] = arcPts[seg];
  380. deltas[i] = 1;
  381. }
  382. else
  383. {
  384. startPts[i] = arcPts[seg] - 1;
  385. endPts[i] = pt - 1;
  386. deltas[i] = -1;
  387. }
  388. }
  389. startPts[4] = startPts[0];
  390. endPts[4] = startPt;
  391. startPts[0] = startPt;
  392. if (startseg & 1)
  393. {
  394. if (startPts[4] != endPts[4])
  395. endPts[4]--;
  396. deltas[4] = 1;
  397. }
  398. else
  399. {
  400. if (startPts[0] > startPts[4])
  401. startPts[0]--;
  402. if (startPts[4] < endPts[4])
  403. endPts[4]--;
  404. deltas[4] = -1;
  405. }
  406. if (arc->angle2 < 0)
  407. {
  408. miPoint *tmps, *tmpe;
  409. int tmpd;
  410. tmpd = deltas[0];
  411. tmps = startPts[0] - tmpd;
  412. tmpe = endPts[0] - tmpd;
  413. startPts[0] = endPts[4] - deltas[4];
  414. endPts[0] = startPts[4] - deltas[4];
  415. deltas[0] = -deltas[4];
  416. startPts[4] = tmpe;
  417. endPts[4] = tmps;
  418. deltas[4] = -tmpd;
  419. tmpd = deltas[1];
  420. tmps = startPts[1] - tmpd;
  421. tmpe = endPts[1] - tmpd;
  422. startPts[1] = endPts[3] - deltas[3];
  423. endPts[1] = startPts[3] - deltas[3];
  424. deltas[1] = -deltas[3];
  425. startPts[3] = tmpe;
  426. endPts[3] = tmps;
  427. deltas[3] = -tmpd;
  428. tmps = startPts[2] - deltas[2];
  429. startPts[2] = endPts[2] - deltas[2];
  430. endPts[2] = tmps;
  431. deltas[2] = -deltas[2];
  432. }
  433. for (i = 0; i < 5 && startPts[i] == endPts[i]; i++)
  434. ;
  435. if (i == 5)
  436. return;
  437. pt = startPts[i];
  438. for (j = 4; startPts[j] == endPts[j]; j--)
  439. ;
  440. lastPt = endPts[j] - deltas[j];
  441. if (dinfo->haveLast &&
  442. (pt->x == dinfo->endPt.x) && (pt->y == dinfo->endPt.y))
  443. startPts[i] += deltas[i];
  444. else /* not contiguous; restart dash pattern */
  445. {
  446. dinfo->dashNum = dinfo->dashNumInit;
  447. dinfo->dashIndex = dinfo->dashIndexInit;
  448. dinfo->dashOffset = dinfo->dashOffsetInit;
  449. }
  450. if (!dinfo->skipStart && (info.startAngle != info.endAngle))
  451. {
  452. dinfo->startPt = *pt;
  453. dinfo->haveStart = true;
  454. }
  455. else if (!dinfo->skipLast && dinfo->haveStart &&
  456. (lastPt->x == dinfo->startPt.x) &&
  457. (lastPt->y == dinfo->startPt.y) &&
  458. (lastPt != startPts[i]))
  459. endPts[j] = lastPt;
  460. if (info.startAngle != info.endAngle)
  461. {
  462. dinfo->haveLast = true;
  463. dinfo->endPt = *lastPt;
  464. }
  465. /* iterate through generated points, updating dash information (e.g.,
  466. dashNum and paint type), writing points in paint type `i' into
  467. pre-allocated array pts[i] */
  468. dashRemaining = (int)pGC->dash[dinfo->dashIndex] - dinfo->dashOffset;
  469. numPixels = pGC->numPixels;
  470. for (i = 0; i < 5; i++)
  471. {
  472. int delta;
  473. pt = startPts[i];
  474. lastPt = endPts[i];
  475. delta = deltas[i];
  476. while (pt != lastPt)
  477. {
  478. int dashNum, paintType;
  479. /* use a paint type that cycles through 1..(numPixels-1) for
  480. even-numbered dashes, and is 0 for odd-numbered ones */
  481. dashNum = dinfo->dashNum;
  482. paintType = (dashNum & 1) ? 0 : 1 + ((dashNum / 2) % (numPixels-1));
  483. while ((pt != lastPt) && --dashRemaining >= 0)
  484. {
  485. *(pts[paintType]++) = *pt;
  486. pt += delta;
  487. }
  488. if (dashRemaining <= 0)
  489. /* on to next dash */
  490. {
  491. ++(dinfo->dashNum);
  492. if (++(dinfo->dashIndex) == pGC->numInDashList)
  493. /* loop to beginning of dash array */
  494. dinfo->dashIndex = 0;
  495. dashRemaining = (int)pGC->dash[dinfo->dashIndex];
  496. }
  497. }
  498. }
  499. /* pass back amount left in now-current dash, so that dash pattern will
  500. continue from arc to contiguous arc */
  501. dinfo->dashOffset = (int)pGC->dash[dinfo->dashIndex] - dashRemaining;
  502. /* free temp storage */
  503. free (points);
  504. }
  505. /*
  506. * (x - l)^2 / (W/2)^2 + (y + H/2)^2 / (H/2)^2 = 1
  507. *
  508. * where l is either 0 or .5
  509. *
  510. * alpha = 4(W^2)
  511. * beta = 4(H^2)
  512. * gamma = 0
  513. * u = 2(W^2)H
  514. * v = 4(H^2)l
  515. * k = -4(H^2)(l^2)
  516. *
  517. */
  518. /* Helper function called by ZeroArcPts() and ZeroArcDashPts() above.
  519. Generates a miZeroArc struct for any specified arc. */
  520. static bool
  521. miZeroArcSetup (const miArc *arc, miZeroArc *info, bool ok360)
  522. {
  523. int l, i;
  524. int angle1, angle2;
  525. int startseg, endseg;
  526. int startAngle, endAngle;
  527. miZeroArcPt start, end;
  528. bool overlap;
  529. l = arc->width & 1;
  530. if (arc->width == arc->height) /* circular arc */
  531. {
  532. info->alpha = 4;
  533. info->beta = 4;
  534. info->k1 = -8;
  535. info->k3 = -16;
  536. info->b = 12;
  537. info->a = (arc->width << 2) - 12;
  538. info->d = 17 - (arc->width << 1);
  539. if (l)
  540. {
  541. info->b -= 4;
  542. info->a += 4;
  543. info->d -= 7;
  544. }
  545. }
  546. else if (arc->width == 0 || arc->height == 0) /* degenerate arc */
  547. {
  548. info->alpha = 0;
  549. info->beta = 0;
  550. info->k1 = 0;
  551. info->k3 = 0;
  552. info->a = -(int)arc->height;
  553. info->b = 0;
  554. info->d = -1;
  555. }
  556. else /* non-degenerate non-circular arc */
  557. {
  558. /* initial conditions */
  559. info->alpha = (arc->width * arc->width) << 2;
  560. info->beta = (arc->height * arc->height) << 2;
  561. info->k1 = info->beta << 1;
  562. info->k3 = info->k1 + (info->alpha << 1);
  563. info->b = l ? 0 : -info->beta;
  564. info->a = info->alpha * arc->height;
  565. info->d = info->b - (info->a >> 1) - (info->alpha >> 2);
  566. if (l)
  567. info->d -= info->beta >> 2;
  568. info->a -= info->b;
  569. /* take first step, d < 0 always */
  570. info->b -= info->k1;
  571. info->a += info->k1;
  572. info->d += info->b;
  573. /* octant change, b < 0 always */
  574. info->k1 = -info->k1;
  575. info->k3 = -info->k3;
  576. info->b = -info->b;
  577. info->d = info->b - info->a - info->d;
  578. info->a = info->a - (info->b << 1);
  579. }
  580. info->dx = 1;
  581. info->dy = 0;
  582. info->w = (arc->width + 1) >> 1;
  583. info->h = arc->height >> 1;
  584. info->xorg = arc->x + (arc->width >> 1);
  585. info->yorg = arc->y;
  586. info->xorgo = info->xorg + l;
  587. info->yorgo = info->yorg + arc->height;
  588. if (arc->width == 0)
  589. {
  590. if (arc->height == 0)
  591. {
  592. info->x = 0;
  593. info->y = 0;
  594. info->initialMask = 0;
  595. info->startAngle = 0;
  596. info->endAngle = 0;
  597. info->start = _oob_arc_pt;
  598. info->end = _oob_arc_pt;
  599. return false;
  600. }
  601. info->x = 0;
  602. info->y = 1;
  603. }
  604. else
  605. {
  606. info->x = 1;
  607. info->y = 0;
  608. }
  609. angle1 = arc->angle1;
  610. angle2 = arc->angle2;
  611. if ((angle1 == 0) && (angle2 >= FULLCIRCLE))
  612. {
  613. startAngle = 0;
  614. endAngle = 0;
  615. }
  616. else
  617. {
  618. if (angle2 > FULLCIRCLE)
  619. angle2 = FULLCIRCLE;
  620. else if (angle2 < -FULLCIRCLE)
  621. angle2 = -FULLCIRCLE;
  622. if (angle2 < 0)
  623. {
  624. startAngle = angle1 + angle2;
  625. endAngle = angle1;
  626. }
  627. else
  628. {
  629. startAngle = angle1;
  630. endAngle = angle1 + angle2;
  631. }
  632. if (startAngle < 0)
  633. startAngle = FULLCIRCLE - (-startAngle) % FULLCIRCLE;
  634. if (startAngle >= FULLCIRCLE)
  635. startAngle = startAngle % FULLCIRCLE;
  636. if (endAngle < 0)
  637. endAngle = FULLCIRCLE - (-endAngle) % FULLCIRCLE;
  638. if (endAngle >= FULLCIRCLE)
  639. endAngle = endAngle % FULLCIRCLE;
  640. }
  641. info->startAngle = startAngle;
  642. info->endAngle = endAngle;
  643. if (ok360 && (startAngle == endAngle) && arc->angle2
  644. && arc->width && arc->height)
  645. {
  646. info->initialMask = 0xf;
  647. info->start = _oob_arc_pt;
  648. info->end = _oob_arc_pt;
  649. return true;
  650. }
  651. startseg = startAngle / OCTANT;
  652. if (!arc->height || (((startseg + 1) & 2) && arc->width))
  653. {
  654. start.x = (int)(Dcos(startAngle) * ((arc->width + 1) / 2.0));
  655. if (start.x < 0)
  656. start.x = -start.x;
  657. start.y = -1;
  658. }
  659. else
  660. {
  661. start.y = (int)(Dsin(startAngle) * (arc->height / 2.0));
  662. if (start.y < 0)
  663. start.y = -start.y;
  664. start.y = info->h - start.y;
  665. start.x = INT_MAX;
  666. }
  667. endseg = endAngle / OCTANT;
  668. if (!arc->height || (((endseg + 1) & 2) && arc->width))
  669. {
  670. end.x = (int)(Dcos(endAngle) * ((arc->width + 1) / 2.0));
  671. if (end.x < 0)
  672. end.x = -end.x;
  673. end.y = -1;
  674. }
  675. else
  676. {
  677. end.y = (int)(Dsin(endAngle) * (arc->height / 2.0));
  678. if (end.y < 0)
  679. end.y = -end.y;
  680. end.y = info->h - end.y;
  681. end.x = INT_MAX;
  682. }
  683. info->firstx = start.x;
  684. info->firsty = start.y;
  685. info->initialMask = 0;
  686. overlap = ((arc->angle2 != 0) && (endAngle <= startAngle)) ? true : false;
  687. for (i = 0; i < 4; i++)
  688. {
  689. if (overlap ?
  690. ((i * QUADRANT <= endAngle) || ((i + 1) * QUADRANT > startAngle)) :
  691. ((i * QUADRANT <= endAngle) && ((i + 1) * QUADRANT > startAngle)))
  692. info->initialMask |= (1 << i);
  693. }
  694. start.mask = info->initialMask;
  695. end.mask = info->initialMask;
  696. startseg >>= 1;
  697. endseg >>= 1;
  698. overlap = (overlap && (endseg == startseg)) ? true : false;
  699. if (start.x != end.x || start.y != end.y || !overlap)
  700. {
  701. if (startseg & 1)
  702. {
  703. if (!overlap)
  704. info->initialMask &= ~(1 << startseg);
  705. if (start.x > end.x || start.y > end.y)
  706. end.mask &= ~(1 << startseg);
  707. }
  708. else
  709. {
  710. start.mask &= ~(1 << startseg);
  711. if (((start.x < end.x || start.y < end.y) ||
  712. (start.x == end.x && start.y == end.y && (endseg & 1))) &&
  713. !overlap)
  714. end.mask &= ~(1 << startseg);
  715. }
  716. if (endseg & 1)
  717. {
  718. end.mask &= ~(1 << endseg);
  719. if (((start.x > end.x || start.y > end.y) ||
  720. (start.x == end.x && start.y == end.y && !(startseg & 1))) &&
  721. !overlap)
  722. start.mask &= ~(1 << endseg);
  723. }
  724. else
  725. {
  726. if (!overlap)
  727. info->initialMask &= ~(1 << endseg);
  728. if (start.x < end.x || start.y < end.y)
  729. start.mask &= ~(1 << endseg);
  730. }
  731. }
  732. /* take care of case when start and stop are both near 45 */
  733. /* handle here rather than adding extra code to pixelization loops */
  734. if (startAngle &&
  735. ((start.y < 0 && end.y >= 0) || (start.y >= 0 && end.y < 0)))
  736. {
  737. i = (startAngle + OCTANT) % OCTANT;
  738. if (i < EPSILON45 || i > OCTANT - EPSILON45)
  739. {
  740. i = (endAngle + OCTANT) % OCTANT;
  741. if (i < EPSILON45 || i > OCTANT - EPSILON45)
  742. {
  743. if (start.y < 0)
  744. {
  745. i = (int)(Dsin(startAngle) * (arc->height / 2.0));
  746. if (i < 0)
  747. i = -i;
  748. if ((int)info->h - i == end.y)
  749. start.mask = end.mask;
  750. }
  751. else
  752. {
  753. i = (int)(Dsin(endAngle) * (arc->height / 2.0));
  754. if (i < 0)
  755. i = -i;
  756. if ((int)info->h - i == start.y)
  757. end.mask = start.mask;
  758. }
  759. }
  760. }
  761. }
  762. if (startseg & 1)
  763. {
  764. info->start = start;
  765. info->end = _oob_arc_pt;
  766. }
  767. else
  768. {
  769. info->end = start;
  770. info->start = _oob_arc_pt;
  771. }
  772. if (endseg & 1)
  773. {
  774. info->altend = end;
  775. if (info->altend.x < info->end.x || info->altend.y < info->end.y)
  776. {
  777. miZeroArcPt tmp;
  778. tmp = info->altend;
  779. info->altend = info->end;
  780. info->end = tmp;
  781. }
  782. info->altstart = _oob_arc_pt;
  783. }
  784. else
  785. {
  786. info->altstart = end;
  787. if (info->altstart.x < info->start.x ||
  788. info->altstart.y < info->start.y)
  789. {
  790. miZeroArcPt tmp;
  791. tmp = info->altstart;
  792. info->altstart = info->start;
  793. info->start = tmp;
  794. }
  795. info->altend = _oob_arc_pt;
  796. }
  797. if (!info->start.x || !info->start.y)
  798. {
  799. info->initialMask = info->start.mask;
  800. info->start = info->altstart;
  801. }
  802. if (!arc->width && (arc->height == 1))
  803. {
  804. /* kludge! */
  805. info->initialMask |= info->end.mask;
  806. info->initialMask |= info->initialMask << 1;
  807. info->end.x = 0;
  808. info->end.mask = 0;
  809. }
  810. return false;
  811. }