gxpflat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* Copyright (C) 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxpflat.c,v 1.45 2005/08/10 19:36:11 igor Exp $ */
  14. /* Path flattening algorithms */
  15. #include "string_.h"
  16. #include "gx.h"
  17. #include "gxarith.h"
  18. #include "gxfixed.h"
  19. #include "gzpath.h"
  20. #include "memory_.h"
  21. #include "vdtrace.h"
  22. #include <assert.h>
  23. /* ---------------- Curve flattening ---------------- */
  24. /*
  25. * To calculate how many points to sample along a path in order to
  26. * approximate it to the desired degree of flatness, we define
  27. * dist((x,y)) = abs(x) + abs(y);
  28. * then the number of points we need is
  29. * N = 1 + sqrt(3/4 * D / flatness),
  30. * where
  31. * D = max(dist(p0 - 2*p1 + p2), dist(p1 - 2*p2 + p3)).
  32. * Since we are going to use a power of 2 for the number of intervals,
  33. * we can avoid the square root by letting
  34. * N = 1 + 2^(ceiling(log2(3/4 * D / flatness) / 2)).
  35. * (Reference: DEC Paris Research Laboratory report #1, May 1989.)
  36. *
  37. * We treat two cases specially. First, if the curve is very
  38. * short, we halve the flatness, to avoid turning short shallow curves
  39. * into short straight lines. Second, if the curve forms part of a
  40. * character (indicated by flatness = 0), we let
  41. * N = 1 + 2 * max(abs(x3-x0), abs(y3-y0)).
  42. * This is probably too conservative, but it produces good results.
  43. */
  44. int
  45. gx_curve_log2_samples(fixed x0, fixed y0, const curve_segment * pc,
  46. fixed fixed_flat)
  47. {
  48. fixed
  49. x03 = pc->pt.x - x0,
  50. y03 = pc->pt.y - y0;
  51. int k;
  52. if (x03 < 0)
  53. x03 = -x03;
  54. if (y03 < 0)
  55. y03 = -y03;
  56. if ((x03 | y03) < int2fixed(16))
  57. fixed_flat >>= 1;
  58. if (fixed_flat == 0) { /* Use the conservative method. */
  59. fixed m = max(x03, y03);
  60. for (k = 1; m > fixed_1;)
  61. k++, m >>= 1;
  62. } else {
  63. const fixed
  64. x12 = pc->p1.x - pc->p2.x, y12 = pc->p1.y - pc->p2.y,
  65. dx0 = x0 - pc->p1.x - x12, dy0 = y0 - pc->p1.y - y12,
  66. dx1 = x12 - pc->p2.x + pc->pt.x, dy1 = y12 - pc->p2.y + pc->pt.y,
  67. adx0 = any_abs(dx0), ady0 = any_abs(dy0),
  68. adx1 = any_abs(dx1), ady1 = any_abs(dy1);
  69. fixed
  70. d = max(adx0, adx1) + max(ady0, ady1);
  71. /*
  72. * The following statement is split up to work around a
  73. * bug in the gcc 2.7.2 optimizer on H-P RISC systems.
  74. */
  75. uint qtmp = d - (d >> 2) /* 3/4 * D */ +fixed_flat - 1;
  76. uint q = qtmp / fixed_flat;
  77. if_debug6('2', "[2]d01=%g,%g d12=%g,%g d23=%g,%g\n",
  78. fixed2float(pc->p1.x - x0), fixed2float(pc->p1.y - y0),
  79. fixed2float(-x12), fixed2float(-y12),
  80. fixed2float(pc->pt.x - pc->p2.x), fixed2float(pc->pt.y - pc->p2.y));
  81. if_debug2('2', " D=%f, flat=%f,",
  82. fixed2float(d), fixed2float(fixed_flat));
  83. /* Now we want to set k = ceiling(log2(q) / 2). */
  84. for (k = 0; q > 1;)
  85. k++, q = (q + 3) >> 2;
  86. if_debug1('2', " k=%d\n", k);
  87. }
  88. return k;
  89. }
  90. /*
  91. * Split a curve segment into two pieces at the (parametric) midpoint.
  92. * Algorithm is from "The Beta2-split: A special case of the Beta-spline
  93. * Curve and Surface Representation," B. A. Barsky and A. D. DeRose, IEEE,
  94. * 1985, courtesy of Crispin Goswell.
  95. */
  96. private void
  97. split_curve_midpoint(fixed x0, fixed y0, const curve_segment * pc,
  98. curve_segment * pc1, curve_segment * pc2)
  99. { /*
  100. * We have to define midpoint carefully to avoid overflow.
  101. * (If it overflows, something really pathological is going
  102. * on, but we could get infinite recursion that way....)
  103. */
  104. #define midpoint(a,b)\
  105. (arith_rshift_1(a) + arith_rshift_1(b) + (((a) | (b)) & 1))
  106. fixed x12 = midpoint(pc->p1.x, pc->p2.x);
  107. fixed y12 = midpoint(pc->p1.y, pc->p2.y);
  108. /*
  109. * pc1 or pc2 may be the same as pc, so we must be a little careful
  110. * about the order in which we store the results.
  111. */
  112. pc1->p1.x = midpoint(x0, pc->p1.x);
  113. pc1->p1.y = midpoint(y0, pc->p1.y);
  114. pc2->p2.x = midpoint(pc->p2.x, pc->pt.x);
  115. pc2->p2.y = midpoint(pc->p2.y, pc->pt.y);
  116. pc1->p2.x = midpoint(pc1->p1.x, x12);
  117. pc1->p2.y = midpoint(pc1->p1.y, y12);
  118. pc2->p1.x = midpoint(x12, pc2->p2.x);
  119. pc2->p1.y = midpoint(y12, pc2->p2.y);
  120. if (pc2 != pc)
  121. pc2->pt.x = pc->pt.x,
  122. pc2->pt.y = pc->pt.y;
  123. pc1->pt.x = midpoint(pc1->p2.x, pc2->p1.x);
  124. pc1->pt.y = midpoint(pc1->p2.y, pc2->p1.y);
  125. #undef midpoint
  126. }
  127. private inline void
  128. print_points(const gs_fixed_point *points, int count)
  129. {
  130. #ifdef DEBUG
  131. int i;
  132. if (!gs_debug_c('3'))
  133. return;
  134. for (i = 0; i < count; i++)
  135. if_debug2('3', "[3]out x=%d y=%d\n", points[i].x, points[i].y);
  136. #endif
  137. }
  138. bool
  139. curve_coeffs_ranged(fixed x0, fixed x1, fixed x2, fixed x3,
  140. fixed y0, fixed y1, fixed y2, fixed y3,
  141. fixed *ax, fixed *bx, fixed *cx,
  142. fixed *ay, fixed *by, fixed *cy,
  143. int k)
  144. {
  145. fixed x01, x12, y01, y12;
  146. curve_points_to_coefficients(x0, x1, x2, x3,
  147. *ax, *bx, *cx, x01, x12);
  148. curve_points_to_coefficients(y0, y1, y2, y3,
  149. *ay, *by, *cy, y01, y12);
  150. # define max_fast (max_fixed / 6)
  151. # define min_fast (-max_fast)
  152. # define in_range(v) (v < max_fast && v > min_fast)
  153. if (k > k_sample_max ||
  154. !in_range(*ax) || !in_range(*ay) ||
  155. !in_range(*bx) || !in_range(*by) ||
  156. !in_range(*cx) || !in_range(*cy)
  157. )
  158. return false;
  159. #undef max_fast
  160. #undef min_fast
  161. #undef in_range
  162. return true;
  163. }
  164. /* Initialize the iterator.
  165. Momotonic curves with non-zero length are only allowed.
  166. */
  167. bool
  168. gx_flattened_iterator__init(gx_flattened_iterator *this,
  169. fixed x0, fixed y0, const curve_segment *pc, int k)
  170. {
  171. /* Note : Immediately after the ininialization it keeps an invalid (zero length) segment. */
  172. fixed x1, y1, x2, y2;
  173. const int k2 = k << 1, k3 = k2 + k;
  174. fixed bx2, by2, ax6, ay6;
  175. x1 = pc->p1.x;
  176. y1 = pc->p1.y;
  177. x2 = pc->p2.x;
  178. y2 = pc->p2.y;
  179. this->x0 = this->lx0 = this->lx1 = x0;
  180. this->y0 = this->ly0 = this->ly1 = y0;
  181. this->x3 = pc->pt.x;
  182. this->y3 = pc->pt.y;
  183. if (!curve_coeffs_ranged(this->x0, x1, x2, this->x3,
  184. this->y0, y1, y2, this->y3,
  185. &this->ax, &this->bx, &this->cx,
  186. &this->ay, &this->by, &this->cy, k))
  187. return false;
  188. this->curve = true;
  189. vd_curve(this->x0, this->y0, x1, y1, x2, y2, this->x3, this->y3, 0, RGB(255, 255, 255));
  190. this->k = k;
  191. # ifdef DEBUG
  192. if (gs_debug_c('3')) {
  193. dlprintf4("[3]x0=%f y0=%f x1=%f y1=%f\n",
  194. fixed2float(this->x0), fixed2float(this->y0),
  195. fixed2float(x1), fixed2float(y1));
  196. dlprintf5(" x2=%f y2=%f x3=%f y3=%f k=%d\n",
  197. fixed2float(x2), fixed2float(y2),
  198. fixed2float(this->x3), fixed2float(this->y3), this->k);
  199. }
  200. # endif
  201. if (k == -1) {
  202. /* A special hook for gx_subdivide_curve_rec.
  203. Only checked the range.
  204. Returning with no initialization. */
  205. return true;
  206. }
  207. this->rmask = (1 << k3) - 1;
  208. this->i = (1 << k);
  209. this->rx = this->ry = 0;
  210. if_debug6('3', "[3]ax=%f bx=%f cx=%f\n ay=%f by=%f cy=%f\n",
  211. fixed2float(this->ax), fixed2float(this->bx), fixed2float(this->cx),
  212. fixed2float(this->ay), fixed2float(this->by), fixed2float(this->cy));
  213. bx2 = this->bx << 1;
  214. by2 = this->by << 1;
  215. ax6 = ((this->ax << 1) + this->ax) << 1;
  216. ay6 = ((this->ay << 1) + this->ay) << 1;
  217. this->idx = arith_rshift(this->cx, this->k);
  218. this->idy = arith_rshift(this->cy, this->k);
  219. this->rdx = ((uint)this->cx << k2) & this->rmask;
  220. this->rdy = ((uint)this->cy << k2) & this->rmask;
  221. /* bx/y terms */
  222. this->id2x = arith_rshift(bx2, k2);
  223. this->id2y = arith_rshift(by2, k2);
  224. this->rd2x = ((uint)bx2 << this->k) & this->rmask;
  225. this->rd2y = ((uint)by2 << this->k) & this->rmask;
  226. # define adjust_rem(r, q, rmask) if ( r > rmask ) q ++, r &= rmask
  227. /* We can compute all the remainders as ints, */
  228. /* because we know they don't exceed M. */
  229. /* cx/y terms */
  230. this->idx += arith_rshift_1(this->id2x);
  231. this->idy += arith_rshift_1(this->id2y);
  232. this->rdx += ((uint)this->bx << this->k) & this->rmask,
  233. this->rdy += ((uint)this->by << this->k) & this->rmask;
  234. adjust_rem(this->rdx, this->idx, this->rmask);
  235. adjust_rem(this->rdy, this->idy, this->rmask);
  236. /* ax/y terms */
  237. this->idx += arith_rshift(this->ax, k3);
  238. this->idy += arith_rshift(this->ay, k3);
  239. this->rdx += (uint)this->ax & this->rmask;
  240. this->rdy += (uint)this->ay & this->rmask;
  241. adjust_rem(this->rdx, this->idx, this->rmask);
  242. adjust_rem(this->rdy, this->idy, this->rmask);
  243. this->id2x += this->id3x = arith_rshift(ax6, k3);
  244. this->id2y += this->id3y = arith_rshift(ay6, k3);
  245. this->rd2x += this->rd3x = (uint)ax6 & this->rmask,
  246. this->rd2y += this->rd3y = (uint)ay6 & this->rmask;
  247. adjust_rem(this->rd2x, this->id2x, this->rmask);
  248. adjust_rem(this->rd2y, this->id2y, this->rmask);
  249. # undef adjust_rem
  250. return true;
  251. }
  252. private inline bool
  253. check_diff_overflow(fixed v0, fixed v1)
  254. {
  255. if (v0 < v1) {
  256. if (v1 - v0 < 0)
  257. return true;
  258. } else {
  259. if (v0 - v1 < 0)
  260. return true;
  261. }
  262. return false;
  263. }
  264. /* Initialize the iterator with a line. */
  265. bool
  266. gx_flattened_iterator__init_line(gx_flattened_iterator *this,
  267. fixed x0, fixed y0, fixed x1, fixed y1)
  268. {
  269. bool ox = check_diff_overflow(x0, x1);
  270. bool oy = check_diff_overflow(y0, y1);
  271. this->x0 = this->lx0 = this->lx1 = x0;
  272. this->y0 = this->ly0 = this->ly1 = y0;
  273. this->x3 = x1;
  274. this->y3 = y1;
  275. if (ox || oy) {
  276. /* Subdivide a long line into 2 segments, because the filling algorithm
  277. and the stroking algorithm need to compute differences
  278. of coordinates of end points. */
  279. /* Note : the result of subdivision may be not strongly colinear. */
  280. this->ax = this->bx = 0;
  281. this->ay = this->by = 0;
  282. this->cx = (ox ? (x1 >> 1) - (x0 >> 1) : (x1 - x0) / 2);
  283. this->cy = (oy ? (y1 >> 1) - (y0 >> 1) : (y1 - y0) / 2);
  284. this->k = 1;
  285. this->i = 2;
  286. } else {
  287. this->k = 0;
  288. this->i = 1;
  289. }
  290. this->curve = false;
  291. return true;
  292. }
  293. #ifdef DEBUG
  294. private inline void
  295. gx_flattened_iterator__print_state(gx_flattened_iterator *this)
  296. {
  297. if (!gs_debug_c('3'))
  298. return;
  299. dlprintf4("[3]dx=%f+%d, dy=%f+%d\n",
  300. fixed2float(this->idx), this->rdx,
  301. fixed2float(this->idy), this->rdy);
  302. dlprintf4(" d2x=%f+%d, d2y=%f+%d\n",
  303. fixed2float(this->id2x), this->rd2x,
  304. fixed2float(this->id2y), this->rd2y);
  305. dlprintf4(" d3x=%f+%d, d3y=%f+%d\n",
  306. fixed2float(this->id3x), this->rd3x,
  307. fixed2float(this->id3y), this->rd3y);
  308. }
  309. #endif
  310. /* Move to the next segment and store it to this->lx0, this->ly0, this->lx1, this->ly1 .
  311. * Return true iff there exist more segments.
  312. */
  313. bool
  314. gx_flattened_iterator__next(gx_flattened_iterator *this)
  315. {
  316. /*
  317. * We can compute successive values by finite differences,
  318. * using the formulas:
  319. x(t) =
  320. a*t^3 + b*t^2 + c*t + d =>
  321. dx(t) = x(t+e)-x(t) =
  322. a*(3*t^2*e + 3*t*e^2 + e^3) + b*(2*t*e + e^2) + c*e =
  323. (3*a*e)*t^2 + (3*a*e^2 + 2*b*e)*t + (a*e^3 + b*e^2 + c*e) =>
  324. d2x(t) = dx(t+e)-dx(t) =
  325. (3*a*e)*(2*t*e + e^2) + (3*a*e^2 + 2*b*e)*e =
  326. (6*a*e^2)*t + (6*a*e^3 + 2*b*e^2) =>
  327. d3x(t) = d2x(t+e)-d2x(t) =
  328. 6*a*e^3;
  329. x(0) = d, dx(0) = (a*e^3 + b*e^2 + c*e),
  330. d2x(0) = 6*a*e^3 + 2*b*e^2;
  331. * In these formulas, e = 1/2^k; of course, there are separate
  332. * computations for the x and y values.
  333. *
  334. * There is a tradeoff in doing the above computation in fixed
  335. * point. If we separate out the constant term (d) and require that
  336. * all the other values fit in a long, then on a 32-bit machine with
  337. * 12 bits of fraction in a fixed, k = 4 implies a maximum curve
  338. * size of 128 pixels; anything larger requires subdividing the
  339. * curve. On the other hand, doing the computations in explicit
  340. * double precision slows down the loop by a factor of 3 or so. We
  341. * found to our surprise that the latter is actually faster, because
  342. * the additional subdivisions cost more than the slower loop.
  343. *
  344. * We represent each quantity as I+R/M, where I is an "integer" and
  345. * the "remainder" R lies in the range 0 <= R < M=2^(3*k). Note
  346. * that R may temporarily exceed M; for this reason, we require that
  347. * M have at least one free high-order bit. To reduce the number of
  348. * variables, we don't actually compute M, only M-1 (rmask). */
  349. fixed x = this->lx1, y = this->ly1;
  350. this->lx0 = this->lx1;
  351. this->ly0 = this->ly1;
  352. /* Fast check for N == 3, a common special case for small characters. */
  353. if (this->k <= 1) {
  354. --this->i;
  355. if (this->i == 0)
  356. goto last;
  357. # define poly2(a,b,c) arith_rshift_1(arith_rshift_1(arith_rshift_1(a) + b) + c)
  358. x += poly2(this->ax, this->bx, this->cx);
  359. y += poly2(this->ay, this->by, this->cy);
  360. # undef poly2
  361. if_debug2('3', "[3]dx=%f, dy=%f\n",
  362. fixed2float(x - this->x0), fixed2float(y - this->y0));
  363. if_debug5('3', "[3]%s x=%g, y=%g x=%d y=%d\n",
  364. (((x ^ this->x0) | (y ^ this->y0)) & float2fixed(-0.5) ?
  365. "add" : "skip"),
  366. fixed2float(x), fixed2float(y), x, y);
  367. this->lx1 = x, this->ly1 = y;
  368. vd_bar(this->lx0, this->ly0, this->lx1, this->ly1, 1, RGB(0, 255, 0));
  369. return true;
  370. } else {
  371. --this->i;
  372. if (this->i == 0)
  373. goto last; /* don't bother with last accum */
  374. # ifdef DEBUG
  375. gx_flattened_iterator__print_state(this);
  376. # endif
  377. # define accum(i, r, di, dr, rmask)\
  378. if ( (r += dr) > rmask ) r &= rmask, i += di + 1;\
  379. else i += di
  380. accum(x, this->rx, this->idx, this->rdx, this->rmask);
  381. accum(y, this->ry, this->idy, this->rdy, this->rmask);
  382. accum(this->idx, this->rdx, this->id2x, this->rd2x, this->rmask);
  383. accum(this->idy, this->rdy, this->id2y, this->rd2y, this->rmask);
  384. accum(this->id2x, this->rd2x, this->id3x, this->rd3x, this->rmask);
  385. accum(this->id2y, this->rd2y, this->id3y, this->rd3y, this->rmask);
  386. if_debug5('3', "[3]%s x=%g, y=%g x=%d y=%d\n",
  387. (((x ^ this->lx0) | (y ^ this->ly0)) & float2fixed(-0.5) ?
  388. "add" : "skip"),
  389. fixed2float(x), fixed2float(y), x, y);
  390. # undef accum
  391. this->lx1 = this->x = x;
  392. this->ly1 = this->y = y;
  393. vd_bar(this->lx0, this->ly0, this->lx1, this->ly1, 1, RGB(0, 255, 0));
  394. return true;
  395. }
  396. last:
  397. this->lx1 = this->x3;
  398. this->ly1 = this->y3;
  399. if_debug4('3', "[3]last x=%g, y=%g x=%d y=%d\n",
  400. fixed2float(this->lx1), fixed2float(this->ly1), this->lx1, this->ly1);
  401. vd_bar(this->lx0, this->ly0, this->lx1, this->ly1, 1, RGB(0, 255, 0));
  402. return false;
  403. }
  404. private inline void
  405. gx_flattened_iterator__unaccum(gx_flattened_iterator *this)
  406. {
  407. # define unaccum(i, r, di, dr, rmask)\
  408. if ( r < dr ) r += rmask + 1 - dr, i -= di + 1;\
  409. else r -= dr, i -= di
  410. unaccum(this->id2x, this->rd2x, this->id3x, this->rd3x, this->rmask);
  411. unaccum(this->id2y, this->rd2y, this->id3y, this->rd3y, this->rmask);
  412. unaccum(this->idx, this->rdx, this->id2x, this->rd2x, this->rmask);
  413. unaccum(this->idy, this->rdy, this->id2y, this->rd2y, this->rmask);
  414. unaccum(this->x, this->rx, this->idx, this->rdx, this->rmask);
  415. unaccum(this->y, this->ry, this->idy, this->rdy, this->rmask);
  416. # undef unaccum
  417. }
  418. /* Move back to the previous segment and store it to this->lx0, this->ly0, this->lx1, this->ly1 .
  419. * This only works for states reached with gx_flattened_iterator__next.
  420. * Return true iff there exist more segments.
  421. */
  422. bool
  423. gx_flattened_iterator__prev(gx_flattened_iterator *this)
  424. {
  425. bool last; /* i.e. the first one in the forth order. */
  426. assert(this->i < 1 << this->k);
  427. this->lx1 = this->lx0;
  428. this->ly1 = this->ly0;
  429. if (this->k <= 1) {
  430. /* If k==0, we have a single segment, return it.
  431. If k==1 && i < 2, return the last segment.
  432. Otherwise must not pass here.
  433. We caould allow to pass here with this->i == 1 << this->k,
  434. but we want to check the assertion about the last segment below.
  435. */
  436. this->i++;
  437. this->lx0 = this->x0;
  438. this->ly0 = this->y0;
  439. vd_bar(this->lx0, this->ly0, this->lx1, this->ly1, 1, RGB(0, 0, 255));
  440. return false;
  441. }
  442. gx_flattened_iterator__unaccum(this);
  443. this->i++;
  444. # ifdef DEBUG
  445. if_debug5('3', "[3]%s x=%g, y=%g x=%d y=%d\n",
  446. (((this->x ^ this->lx1) | (this->y ^ this->ly1)) & float2fixed(-0.5) ?
  447. "add" : "skip"),
  448. fixed2float(this->x), fixed2float(this->y), this->x, this->y);
  449. gx_flattened_iterator__print_state(this);
  450. # endif
  451. last = (this->i == (1 << this->k) - 1);
  452. this->lx0 = this->x;
  453. this->ly0 = this->y;
  454. vd_bar(this->lx0, this->ly0, this->lx1, this->ly1, 1, RGB(0, 0, 255));
  455. if (last)
  456. assert(this->lx0 == this->x0 && this->ly0 == this->y0);
  457. return !last;
  458. }
  459. /* Switching from the forward scanning to the backward scanning for the filtered1. */
  460. void
  461. gx_flattened_iterator__switch_to_backscan(gx_flattened_iterator *this, bool not_first)
  462. {
  463. /* When scanning forth, the accumulator stands on the end of a segment,
  464. except for the last segment.
  465. When scanning back, the accumulator should stand on the beginning of a segment.
  466. Asuuming that at least one forward step is done.
  467. */
  468. if (not_first)
  469. if (this->i > 0 && this->k != 1 /* This case doesn't use the accumulator. */)
  470. gx_flattened_iterator__unaccum(this);
  471. }
  472. #define max_points 50 /* arbitrary */
  473. private int
  474. generate_segments(gx_path * ppath, const gs_fixed_point *points,
  475. int count, segment_notes notes)
  476. {
  477. /* vd_moveto(ppath->position.x, ppath->position.y); */
  478. if (notes & sn_not_first) {
  479. /* vd_lineto_multi(points, count); */
  480. print_points(points, count);
  481. return gx_path_add_lines_notes(ppath, points, count, notes);
  482. } else {
  483. int code;
  484. /* vd_lineto(points[0].x, points[0].y); */
  485. print_points(points, 1);
  486. code = gx_path_add_line_notes(ppath, points[0].x, points[0].y, notes);
  487. if (code < 0)
  488. return code;
  489. /* vd_lineto_multi(points + 1, count - 1); */
  490. print_points(points + 1, count - 1);
  491. return gx_path_add_lines_notes(ppath, points + 1, count - 1, notes | sn_not_first);
  492. }
  493. }
  494. private int
  495. gx_subdivide_curve_rec(gx_flattened_iterator *this,
  496. gx_path * ppath, int k, curve_segment * pc,
  497. segment_notes notes, gs_fixed_point *points)
  498. {
  499. int code;
  500. top :
  501. if (!gx_flattened_iterator__init(this,
  502. ppath->position.x, ppath->position.y, pc, k)) {
  503. /* Curve is too long. Break into two pieces and recur. */
  504. curve_segment cseg;
  505. k--;
  506. split_curve_midpoint(ppath->position.x, ppath->position.y, pc, &cseg, pc);
  507. code = gx_subdivide_curve_rec(this, ppath, k, &cseg, notes, points);
  508. if (code < 0)
  509. return code;
  510. notes |= sn_not_first;
  511. goto top;
  512. } else if (k == -1) {
  513. /* fixme : Don't need to init the iterator. Just wanted to check in_range. */
  514. return gx_path_add_curve_notes(ppath, pc->p1.x, pc->p1.y, pc->p2.x, pc->p2.y,
  515. pc->pt.x, pc->pt.y, notes);
  516. } else {
  517. gs_fixed_point *ppt = points;
  518. bool more;
  519. for(;;) {
  520. more = gx_flattened_iterator__next(this);
  521. ppt->x = this->lx1;
  522. ppt->y = this->ly1;
  523. ppt++;
  524. if (ppt == &points[max_points] || !more) {
  525. gs_fixed_point *pe = (more ? ppt - 2 : ppt);
  526. code = generate_segments(ppath, points, pe - points, notes);
  527. if (code < 0)
  528. return code;
  529. if (!more)
  530. return 0;
  531. notes |= sn_not_first;
  532. memcpy(points, pe, (char *)ppt - (char *)pe);
  533. ppt = points + (ppt - pe);
  534. }
  535. }
  536. }
  537. }
  538. #undef coord_near
  539. /*
  540. * Flatten a segment of the path by repeated sampling.
  541. * 2^k is the number of lines to produce (i.e., the number of points - 1,
  542. * including the endpoints); we require k >= 1.
  543. * If k or any of the coefficient values are too large,
  544. * use recursive subdivision to whittle them down.
  545. */
  546. int
  547. gx_subdivide_curve(gx_path * ppath, int k, curve_segment * pc, segment_notes notes)
  548. {
  549. gs_fixed_point points[max_points + 1];
  550. gx_flattened_iterator iter;
  551. return gx_subdivide_curve_rec(&iter, ppath, k, pc, notes, points);
  552. }
  553. #undef max_points