gxpcopy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* Copyright (C) 1992, 2000 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: gxpcopy.c,v 1.26 2005/08/30 06:38:44 igor Exp $ */
  14. /* Path copying and flattening */
  15. #include "math_.h"
  16. #include "gx.h"
  17. #include "gserrors.h"
  18. #include "gconfigv.h" /* for USE_FPU */
  19. #include "gxfixed.h"
  20. #include "gxfarith.h"
  21. #include "gxistate.h" /* for access to line params */
  22. #include "gzpath.h"
  23. #include "vdtrace.h"
  24. /* Forward declarations */
  25. private void adjust_point_to_tangent(segment *, const segment *,
  26. const gs_fixed_point *);
  27. /* Copy a path, optionally flattening or monotonizing it. */
  28. /* If the copy fails, free the new path. */
  29. int
  30. gx_path_copy_reducing(const gx_path *ppath_old, gx_path *ppath,
  31. fixed fixed_flatness, const gs_imager_state *pis,
  32. gx_path_copy_options options)
  33. {
  34. const segment *pseg;
  35. fixed flat = fixed_flatness;
  36. gs_fixed_point expansion;
  37. /*
  38. * Since we're going to be adding to the path, unshare it
  39. * before we start.
  40. */
  41. int code = gx_path_unshare(ppath);
  42. if (code < 0)
  43. return code;
  44. #ifdef DEBUG
  45. if (gs_debug_c('P'))
  46. gx_dump_path(ppath_old, "before reducing");
  47. #endif
  48. if (options & pco_for_stroke) {
  49. /* Precompute the maximum expansion of the bounding box. */
  50. double width = pis->line_params.half_width;
  51. expansion.x =
  52. float2fixed((fabs(pis->ctm.xx) + fabs(pis->ctm.yx)) * width) * 2;
  53. expansion.y =
  54. float2fixed((fabs(pis->ctm.xy) + fabs(pis->ctm.yy)) * width) * 2;
  55. }
  56. vd_setcolor(RGB(255,255,0));
  57. pseg = (const segment *)(ppath_old->first_subpath);
  58. while (pseg) {
  59. switch (pseg->type) {
  60. case s_start:
  61. code = gx_path_add_point(ppath,
  62. pseg->pt.x, pseg->pt.y);
  63. vd_moveto(pseg->pt.x, pseg->pt.y);
  64. break;
  65. case s_curve:
  66. {
  67. const curve_segment *pc = (const curve_segment *)pseg;
  68. if (fixed_flatness == max_fixed) { /* don't flatten */
  69. if (options & pco_monotonize)
  70. code = gx_curve_monotonize(ppath, pc);
  71. else
  72. code = gx_path_add_curve_notes(ppath,
  73. pc->p1.x, pc->p1.y, pc->p2.x, pc->p2.y,
  74. pc->pt.x, pc->pt.y, pseg->notes);
  75. } else {
  76. fixed x0 = ppath->position.x;
  77. fixed y0 = ppath->position.y;
  78. segment_notes notes = pseg->notes;
  79. curve_segment cseg;
  80. int k;
  81. if (options & pco_for_stroke) {
  82. /*
  83. * When flattening for stroking, the flatness
  84. * must apply to the outside of the resulting
  85. * stroked region. We approximate this by
  86. * dividing the flatness by the ratio of the
  87. * expanded bounding box to the original
  88. * bounding box. This is crude, but pretty
  89. * simple to calculate, and produces reasonably
  90. * good results.
  91. */
  92. fixed min01, max01, min23, max23;
  93. fixed ex, ey, flat_x, flat_y;
  94. #define SET_EXTENT(r, c0, c1, c2, c3)\
  95. BEGIN\
  96. if (c0 < c1) min01 = c0, max01 = c1;\
  97. else min01 = c1, max01 = c0;\
  98. if (c2 < c3) min23 = c2, max23 = c3;\
  99. else min23 = c3, max23 = c2;\
  100. r = max(max01, max23) - min(min01, min23);\
  101. END
  102. SET_EXTENT(ex, x0, pc->p1.x, pc->p2.x, pc->pt.x);
  103. SET_EXTENT(ey, y0, pc->p1.y, pc->p2.y, pc->pt.y);
  104. #undef SET_EXTENT
  105. /*
  106. * We check for the degenerate case specially
  107. * to avoid a division by zero.
  108. */
  109. if (ex == 0 || ey == 0)
  110. k = 0;
  111. else {
  112. flat_x =
  113. fixed_mult_quo(fixed_flatness, ex,
  114. ex + expansion.x);
  115. flat_y =
  116. fixed_mult_quo(fixed_flatness, ey,
  117. ey + expansion.y);
  118. flat = min(flat_x, flat_y);
  119. k = gx_curve_log2_samples(x0, y0, pc, flat);
  120. }
  121. } else
  122. k = gx_curve_log2_samples(x0, y0, pc, flat);
  123. if (options & pco_accurate) {
  124. segment *start;
  125. segment *end;
  126. /*
  127. * Add an extra line, which will become
  128. * the tangent segment.
  129. */
  130. code = gx_path_add_line_notes(ppath, x0, y0,
  131. notes);
  132. if (code < 0)
  133. break;
  134. vd_lineto(x0, y0);
  135. start = ppath->current_subpath->last;
  136. notes |= sn_not_first;
  137. cseg = *pc;
  138. code = gx_subdivide_curve(ppath, k, &cseg, notes);
  139. if (code < 0)
  140. break;
  141. /*
  142. * Adjust the first and last segments so that
  143. * they line up with the tangents.
  144. */
  145. end = ppath->current_subpath->last;
  146. vd_lineto(ppath->position.x, ppath->position.y);
  147. if ((code = gx_path_add_line_notes(ppath,
  148. ppath->position.x,
  149. ppath->position.y,
  150. pseg->notes | sn_not_first)) < 0)
  151. break;
  152. if (start->next->pt.x != pc->p1.x || start->next->pt.y != pc->p1.y)
  153. adjust_point_to_tangent(start, start->next, &pc->p1);
  154. else if (start->next->pt.x != pc->p2.x || start->next->pt.y != pc->p2.y)
  155. adjust_point_to_tangent(start, start->next, &pc->p2);
  156. else
  157. adjust_point_to_tangent(start, start->next, &end->prev->pt);
  158. if (end->prev->pt.x != pc->p2.x || end->prev->pt.y != pc->p2.y)
  159. adjust_point_to_tangent(end, end->prev, &pc->p2);
  160. else if (end->prev->pt.x != pc->p1.x || end->prev->pt.y != pc->p1.y)
  161. adjust_point_to_tangent(end, end->prev, &pc->p1);
  162. else
  163. adjust_point_to_tangent(end, end->prev, &start->pt);
  164. } else {
  165. cseg = *pc;
  166. code = gx_subdivide_curve(ppath, k, &cseg, notes);
  167. }
  168. }
  169. break;
  170. }
  171. case s_line:
  172. code = gx_path_add_line_notes(ppath,
  173. pseg->pt.x, pseg->pt.y, pseg->notes);
  174. vd_lineto(pseg->pt.x, pseg->pt.y);
  175. break;
  176. case s_line_close:
  177. code = gx_path_close_subpath(ppath);
  178. vd_closepath;
  179. break;
  180. default: /* can't happen */
  181. code = gs_note_error(gs_error_unregistered);
  182. }
  183. if (code < 0) {
  184. gx_path_new(ppath);
  185. return code;
  186. }
  187. pseg = pseg->next;
  188. }
  189. if (path_last_is_moveto(ppath_old))
  190. gx_path_add_point(ppath, ppath_old->position.x,
  191. ppath_old->position.y);
  192. if (ppath_old->bbox_set) {
  193. if (ppath->bbox_set) {
  194. ppath->bbox.p.x = min(ppath_old->bbox.p.x, ppath->bbox.p.x);
  195. ppath->bbox.p.y = min(ppath_old->bbox.p.y, ppath->bbox.p.y);
  196. ppath->bbox.q.x = max(ppath_old->bbox.q.x, ppath->bbox.q.x);
  197. ppath->bbox.q.y = max(ppath_old->bbox.q.y, ppath->bbox.q.y);
  198. } else {
  199. ppath->bbox_set = true;
  200. ppath->bbox = ppath_old->bbox;
  201. }
  202. }
  203. #ifdef DEBUG
  204. if (gs_debug_c('P'))
  205. gx_dump_path(ppath, "after reducing");
  206. #endif
  207. return 0;
  208. }
  209. /*
  210. * Adjust one end of a line (the first or last line of a flattened curve)
  211. * so it falls on the curve tangent. The closest point on the line from
  212. * (0,0) to (C,D) to a point (U,V) -- i.e., the point on the line at which
  213. * a perpendicular line from the point intersects it -- is given by
  214. * T = (C*U + D*V) / (C^2 + D^2)
  215. * (X,Y) = (C*T,D*T)
  216. * However, any smaller value of T will also work: the one we actually
  217. * use is 0.25 * the value we just derived. We must check that
  218. * numerical instabilities don't lead to a negative value of T.
  219. */
  220. private void
  221. adjust_point_to_tangent(segment * pseg, const segment * next,
  222. const gs_fixed_point * p1)
  223. {
  224. const fixed x0 = pseg->pt.x, y0 = pseg->pt.y;
  225. const fixed fC = p1->x - x0, fD = p1->y - y0;
  226. /*
  227. * By far the commonest case is that the end of the curve is
  228. * horizontal or vertical. Check for this specially, because
  229. * we can handle it with far less work (and no floating point).
  230. */
  231. if (fC == 0) {
  232. /* Vertical tangent. */
  233. const fixed DT = arith_rshift(next->pt.y - y0, 2);
  234. if (fD == 0)
  235. return; /* anomalous case */
  236. if_debug1('2', "[2]adjusting vertical: DT = %g\n",
  237. fixed2float(DT));
  238. if ((DT ^ fD) > 0)
  239. pseg->pt.y = DT + y0;
  240. } else if (fD == 0) {
  241. /* Horizontal tangent. */
  242. const fixed CT = arith_rshift(next->pt.x - x0, 2);
  243. if_debug1('2', "[2]adjusting horizontal: CT = %g\n",
  244. fixed2float(CT));
  245. if ((CT ^ fC) > 0)
  246. pseg->pt.x = CT + x0;
  247. } else {
  248. /* General case. */
  249. const double C = fC, D = fD;
  250. double T = (C * (next->pt.x - x0) + D * (next->pt.y - y0)) /
  251. (C * C + D * D);
  252. if_debug3('2', "[2]adjusting: C = %g, D = %g, T = %g\n",
  253. C, D, T);
  254. if (T > 0) {
  255. if (T > 1) {
  256. /* Don't go outside the curve bounding box. */
  257. T = 1;
  258. }
  259. pseg->pt.x = arith_rshift((fixed) (C * T), 2) + x0;
  260. pseg->pt.y = arith_rshift((fixed) (D * T), 2) + y0;
  261. }
  262. }
  263. }
  264. /* ---------------- Monotonic curves ---------------- */
  265. /* Test whether a path is free of non-monotonic curves. */
  266. bool
  267. gx_path__check_curves(const gx_path * ppath, gx_path_copy_options options, fixed fixed_flat)
  268. {
  269. const segment *pseg = (const segment *)(ppath->first_subpath);
  270. gs_fixed_point pt0;
  271. while (pseg) {
  272. switch (pseg->type) {
  273. case s_start:
  274. {
  275. const subpath *psub = (const subpath *)pseg;
  276. /* Skip subpaths without curves. */
  277. if (!psub->curve_count)
  278. pseg = psub->last;
  279. }
  280. break;
  281. case s_curve:
  282. {
  283. const curve_segment *pc = (const curve_segment *)pseg;
  284. if (options & pco_monotonize) {
  285. double t[2];
  286. int nz = gx_curve_monotonic_points(pt0.y,
  287. pc->p1.y, pc->p2.y, pc->pt.y, t);
  288. if (nz != 0)
  289. return false;
  290. nz = gx_curve_monotonic_points(pt0.x,
  291. pc->p1.x, pc->p2.x, pc->pt.x, t);
  292. if (nz != 0)
  293. return false;
  294. }
  295. if (options & pco_small_curves) {
  296. fixed ax, bx, cx, ay, by, cy;
  297. int k = gx_curve_log2_samples(pt0.x, pt0.y, pc, fixed_flat);
  298. if(!curve_coeffs_ranged(pt0.x, pc->p1.x, pc->p2.x, pc->pt.x,
  299. pt0.y, pc->p1.y, pc->p2.y, pc->pt.y,
  300. &ax, &bx, &cx, &ay, &by, &cy, k))
  301. return false;
  302. }
  303. }
  304. break;
  305. default:
  306. ;
  307. }
  308. pt0 = pseg->pt;
  309. pseg = pseg->next;
  310. }
  311. return true;
  312. }
  313. /* Monotonize a curve, by splitting it if necessary. */
  314. /* In the worst case, this could split the curve into 9 pieces. */
  315. int
  316. gx_curve_monotonize(gx_path * ppath, const curve_segment * pc)
  317. {
  318. fixed x0 = ppath->position.x, y0 = ppath->position.y;
  319. segment_notes notes = pc->notes;
  320. double t[4], tt = 1, tp;
  321. int c[4];
  322. int n0, n1, n, i, j, k = 0;
  323. fixed ax, bx, cx, ay, by, cy, v01, v12;
  324. fixed px, py, qx, qy, rx, ry, sx, sy;
  325. const double delta = 0.0000001;
  326. /* Roots of the derivative : */
  327. n0 = gx_curve_monotonic_points(x0, pc->p1.x, pc->p2.x, pc->pt.x, t);
  328. n1 = gx_curve_monotonic_points(y0, pc->p1.y, pc->p2.y, pc->pt.y, t + n0);
  329. n = n0 + n1;
  330. if (n == 0)
  331. return gx_path_add_curve_notes(ppath, pc->p1.x, pc->p1.y,
  332. pc->p2.x, pc->p2.y, pc->pt.x, pc->pt.y, notes);
  333. if (n0 > 0)
  334. c[0] = 1;
  335. if (n0 > 1)
  336. c[1] = 1;
  337. if (n1 > 0)
  338. c[n0] = 2;
  339. if (n1 > 1)
  340. c[n0 + 1] = 2;
  341. /* Order roots : */
  342. for (i = 0; i < n; i++)
  343. for (j = i + 1; j < n; j++)
  344. if (t[i] > t[j]) {
  345. int w;
  346. double v = t[i]; t[i] = t[j]; t[j] = v;
  347. w = c[i]; c[i] = c[j]; c[j] = w;
  348. }
  349. /* Drop roots near zero : */
  350. for (k = 0; k < n; k++)
  351. if (t[k] >= delta)
  352. break;
  353. /* Merge close roots, and drop roots at 1 : */
  354. if (t[n - 1] > 1 - delta)
  355. n--;
  356. for (i = k + 1, j = k; i < n && t[k] < 1 - delta; i++)
  357. if (any_abs(t[i] - t[j]) < delta) {
  358. t[j] = (t[j] + t[i]) / 2; /* Unlikely 3 roots are close. */
  359. c[j] |= c[i];
  360. } else {
  361. j++;
  362. t[j] = t[i];
  363. c[j] = c[i];
  364. }
  365. n = j + 1;
  366. /* Do split : */
  367. curve_points_to_coefficients(x0, pc->p1.x, pc->p2.x, pc->pt.x, ax, bx, cx, v01, v12);
  368. curve_points_to_coefficients(y0, pc->p1.y, pc->p2.y, pc->pt.y, ay, by, cy, v01, v12);
  369. ax *= 3, bx *= 2; /* Coefficients of the derivative. */
  370. ay *= 3, by *= 2;
  371. px = x0;
  372. py = y0;
  373. qx = (fixed)((pc->p1.x - px) * t[0] + 0.5);
  374. qy = (fixed)((pc->p1.y - py) * t[0] + 0.5);
  375. tp = 0;
  376. for (i = k; i < n; i++) {
  377. double ti = t[i];
  378. double t2 = ti * ti, t3 = t2 * ti;
  379. double omt = 1 - ti, omt2 = omt * omt, omt3 = omt2 * omt;
  380. double x = x0 * omt3 + 3 * pc->p1.x * omt2 * ti + 3 * pc->p2.x * omt * t2 + pc->pt.x * t3;
  381. double y = y0 * omt3 + 3 * pc->p1.y * omt2 * ti + 3 * pc->p2.y * omt * t2 + pc->pt.y * t3;
  382. double ddx = (c[i] & 1 ? 0 : ax * t2 + bx * ti + cx); /* Suppress noize. */
  383. double ddy = (c[i] & 2 ? 0 : ay * t2 + by * ti + cy);
  384. fixed dx = (fixed)(ddx + 0.5);
  385. fixed dy = (fixed)(ddy + 0.5);
  386. int code;
  387. tt = (i + 1 < n ? t[i + 1] : 1) - ti;
  388. rx = (fixed)(dx * (t[i] - tp) / 3 + 0.5);
  389. ry = (fixed)(dy * (t[i] - tp) / 3 + 0.5);
  390. sx = (fixed)(x + 0.5);
  391. sy = (fixed)(y + 0.5);
  392. /* Suppress the derivative sign noize near a beak : */
  393. if ((double)(sx - px) * qx + (double)(sy - py) * qy < 0)
  394. qx = -qx, qy = -qy;
  395. if ((double)(sx - px) * rx + (double)(sy - py) * ry < 0)
  396. rx = -rx, ry = -qy;
  397. /* Do add : */
  398. code = gx_path_add_curve_notes(ppath, px + qx, py + qy, sx - rx, sy - ry, sx, sy, notes);
  399. if (code < 0)
  400. return code;
  401. notes |= sn_not_first;
  402. px = sx;
  403. py = sy;
  404. qx = (fixed)(dx * tt / 3 + 0.5);
  405. qy = (fixed)(dy * tt / 3 + 0.5);
  406. tp = t[i];
  407. }
  408. sx = pc->pt.x;
  409. sy = pc->pt.y;
  410. rx = (fixed)((pc->pt.x - pc->p2.x) * tt + 0.5);
  411. ry = (fixed)((pc->pt.y - pc->p2.y) * tt + 0.5);
  412. /* Suppress the derivative sign noize near peaks : */
  413. if ((double)(sx - px) * qx + (double)(sy - py) * qy < 0)
  414. qx = -qx, qy = -qy;
  415. if ((double)(sx - px) * rx + (double)(sy - py) * ry < 0)
  416. rx = -rx, ry = -qy;
  417. return gx_path_add_curve_notes(ppath, px + qx, py + qy, sx - rx, sy - ry, sx, sy, notes);
  418. }
  419. /*
  420. * Split a curve if necessary into pieces that are monotonic in X or Y as a
  421. * function of the curve parameter t. This allows us to rasterize curves
  422. * directly without pre-flattening. This takes a fair amount of analysis....
  423. * Store the values of t of the split points in pst[0] and pst[1]. Return
  424. * the number of split points (0, 1, or 2).
  425. */
  426. int
  427. gx_curve_monotonic_points(fixed v0, fixed v1, fixed v2, fixed v3,
  428. double pst[2])
  429. {
  430. /*
  431. Let
  432. v(t) = a*t^3 + b*t^2 + c*t + d, 0 <= t <= 1.
  433. Then
  434. dv(t) = 3*a*t^2 + 2*b*t + c.
  435. v(t) has a local minimum or maximum (or inflection point)
  436. precisely where dv(t) = 0. Now the roots of dv(t) = 0 (i.e.,
  437. the zeros of dv(t)) are at
  438. t = ( -2*b +/- sqrt(4*b^2 - 12*a*c) ) / 6*a
  439. = ( -b +/- sqrt(b^2 - 3*a*c) ) / 3*a
  440. (Note that real roots exist iff b^2 >= 3*a*c.)
  441. We want to know if these lie in the range (0..1).
  442. (The endpoints don't count.) Call such a root a "valid zero."
  443. Since computing the roots is expensive, we would like to have
  444. some cheap tests to filter out cases where they don't exist
  445. (i.e., where the curve is already monotonic).
  446. */
  447. fixed v01, v12, a, b, c, b2, a3;
  448. fixed dv_end, b2abs, a3abs;
  449. curve_points_to_coefficients(v0, v1, v2, v3, a, b, c, v01, v12);
  450. b2 = b << 1;
  451. a3 = (a << 1) + a;
  452. /*
  453. If a = 0, the only possible zero is t = -c / 2*b.
  454. This zero is valid iff sign(c) != sign(b) and 0 < |c| < 2*|b|.
  455. */
  456. if (a == 0) {
  457. if ((b ^ c) < 0 && any_abs(c) < any_abs(b2) && c != 0) {
  458. *pst = (double)(-c) / b2;
  459. return 1;
  460. } else
  461. return 0;
  462. }
  463. /*
  464. Iff a curve is horizontal at t = 0, c = 0. In this case,
  465. there can be at most one other zero, at -2*b / 3*a.
  466. This zero is valid iff sign(a) != sign(b) and 0 < 2*|b| < 3*|a|.
  467. */
  468. if (c == 0) {
  469. if ((a ^ b) < 0 && any_abs(b2) < any_abs(a3) && b != 0) {
  470. *pst = (double)(-b2) / a3;
  471. return 1;
  472. } else
  473. return 0;
  474. }
  475. /*
  476. Similarly, iff a curve is horizontal at t = 1, 3*a + 2*b + c = 0.
  477. In this case, there can be at most one other zero,
  478. at -1 - 2*b / 3*a, iff sign(a) != sign(b) and 1 < -2*b / 3*a < 2,
  479. i.e., 3*|a| < 2*|b| < 6*|a|.
  480. */
  481. else if ((dv_end = a3 + b2 + c) == 0) {
  482. if ((a ^ b) < 0 &&
  483. (b2abs = any_abs(b2)) > (a3abs = any_abs(a3)) &&
  484. b2abs < a3abs << 1
  485. ) {
  486. *pst = (double)(-b2 - a3) / a3;
  487. return 1;
  488. } else
  489. return 0;
  490. }
  491. /*
  492. If sign(dv_end) != sign(c), at least one valid zero exists,
  493. since dv(0) and dv(1) have opposite signs and hence
  494. dv(t) must be zero somewhere in the interval [0..1].
  495. */
  496. else if ((dv_end ^ c) < 0);
  497. /*
  498. If sign(a) = sign(b), no valid zero exists,
  499. since dv is monotonic on [0..1] and has the same sign
  500. at both endpoints.
  501. */
  502. else if ((a ^ b) >= 0)
  503. return 0;
  504. /*
  505. Otherwise, dv(t) may be non-monotonic on [0..1]; it has valid zeros
  506. iff its sign anywhere in this interval is different from its sign
  507. at the endpoints, which occurs iff it has an extremum in this
  508. interval and the extremum is of the opposite sign from c.
  509. To find this out, we look for the local extremum of dv(t)
  510. by observing
  511. d2v(t) = 6*a*t + 2*b
  512. which has a zero only at
  513. t1 = -b / 3*a
  514. Now if t1 <= 0 or t1 >= 1, no valid zero exists.
  515. Note that we just determined that sign(a) != sign(b), so we know t1 > 0.
  516. */
  517. else if (any_abs(b) >= any_abs(a3))
  518. return 0;
  519. /*
  520. Otherwise, we just go ahead with the computation of the roots,
  521. and test them for being in the correct range. Note that a valid
  522. zero is an inflection point of v(t) iff d2v(t) = 0; we don't
  523. bother to check for this case, since it's rare.
  524. */
  525. {
  526. double nbf = (double)(-b);
  527. double a3f = (double)a3;
  528. double radicand = nbf * nbf - a3f * c;
  529. if (radicand < 0) {
  530. if_debug1('2', "[2]negative radicand = %g\n", radicand);
  531. return 0;
  532. } {
  533. double root = sqrt(radicand);
  534. int nzeros = 0;
  535. double z = (nbf - root) / a3f;
  536. /*
  537. * We need to return the zeros in the correct order.
  538. * We know that root is non-negative, but a3f may be either
  539. * positive or negative, so we need to check the ordering
  540. * explicitly.
  541. */
  542. if_debug2('2', "[2]zeros at %g, %g\n", z, (nbf + root) / a3f);
  543. if (z > 0 && z < 1)
  544. *pst = z, nzeros = 1;
  545. if (root != 0) {
  546. z = (nbf + root) / a3f;
  547. if (z > 0 && z < 1) {
  548. if (nzeros && a3f < 0) /* order is reversed */
  549. pst[1] = *pst, *pst = z;
  550. else
  551. pst[nzeros] = z;
  552. nzeros++;
  553. }
  554. }
  555. return nzeros;
  556. }
  557. }
  558. }
  559. /* ---------------- Path optimization for the filling algorithm. ---------------- */
  560. private bool
  561. find_contacting_segments(const subpath *sp0, segment *sp0last,
  562. const subpath *sp1, segment *sp1last,
  563. segment **sc0, segment **sc1)
  564. {
  565. segment *s0, *s1;
  566. const segment *s0s, *s1s;
  567. int count0, count1, search_limit = 50;
  568. int min_length = fixed_1 * 1;
  569. /* This is a simplified algorithm, which only checks for quazi-colinear vertical lines.
  570. "Quazi-vertical" means dx <= 1 && dy >= min_length . */
  571. /* To avoid a big unuseful expence of the processor time,
  572. we search the first subpath from the end
  573. (assuming that it was recently merged near the end),
  574. and restrict the search with search_limit segments
  575. against a quadratic scanning of two long subpaths.
  576. Thus algorithm is not necessary finds anything contacting.
  577. Instead it either quickly finds something, or maybe not. */
  578. for (s0 = sp0last, count0 = 0; count0 < search_limit && s0 != (segment *)sp0; s0 = s0->prev, count0++) {
  579. s0s = s0->prev;
  580. if (s0->type == s_line && (s0s->pt.x == s0->pt.x ||
  581. (any_abs(s0s->pt.x - s0->pt.x) == 1 && any_abs(s0s->pt.y - s0->pt.y) > min_length))) {
  582. for (s1 = sp1last, count1 = 0; count1 < search_limit && s1 != (segment *)sp1; s1 = s1->prev, count1++) {
  583. s1s = s1->prev;
  584. if (s1->type == s_line && (s1s->pt.x == s1->pt.x ||
  585. (any_abs(s1s->pt.x - s1->pt.x) == 1 && any_abs(s1s->pt.y - s1->pt.y) > min_length))) {
  586. if (s0s->pt.x == s1s->pt.x || s0->pt.x == s1->pt.x || s0->pt.x == s1s->pt.x || s0s->pt.x == s1->pt.x) {
  587. if (s0s->pt.y < s0->pt.y && s1s->pt.y > s1->pt.y) {
  588. fixed y0 = max(s0s->pt.y, s1->pt.y);
  589. fixed y1 = min(s0->pt.y, s1s->pt.y);
  590. if (y0 <= y1) {
  591. *sc0 = s0;
  592. *sc1 = s1;
  593. return true;
  594. }
  595. }
  596. if (s0s->pt.y > s0->pt.y && s1s->pt.y < s1->pt.y) {
  597. fixed y0 = max(s0->pt.y, s1s->pt.y);
  598. fixed y1 = min(s0s->pt.y, s1->pt.y);
  599. if (y0 <= y1) {
  600. *sc0 = s0;
  601. *sc1 = s1;
  602. return true;
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. }
  610. return false;
  611. }
  612. int
  613. gx_path_merge_contacting_contours(gx_path *ppath)
  614. {
  615. /* Now this is a simplified algorithm,
  616. which merge only contours by a common quazi-vertical line. */
  617. int window = 5/* max spot holes */ * 6/* segments per subpath */;
  618. subpath *sp0 = ppath->segments->contents.subpath_first;
  619. for (; sp0 != NULL; sp0 = (subpath *)sp0->last->next) {
  620. segment *sp0last = sp0->last;
  621. subpath *sp1 = (subpath *)sp0last->next, *spnext;
  622. subpath *sp1p = sp0;
  623. int count;
  624. for (count = 0; sp1 != NULL && count < window; sp1 = spnext, count++) {
  625. segment *sp1last = sp1->last;
  626. segment *sc0, *sc1;
  627. spnext = (subpath *)sp1last->next;
  628. if (find_contacting_segments(sp0, sp0last, sp1, sp1last, &sc0, &sc1)) {
  629. /* Detach the subpath 1 from the path: */
  630. sp1->prev->next = sp1last->next;
  631. if (sp1last->next != NULL)
  632. sp1last->next->prev = sp1->prev;
  633. sp1->prev = 0;
  634. sp1last->next = 0;
  635. /* Change 'closepath' of the subpath 1 to a line (maybe degenerate) : */
  636. if (sp1last->type == s_line_close)
  637. sp1last->type = s_line;
  638. /* Rotate the subpath 1 to sc1 : */
  639. { segment *old_first = sp1->next;
  640. /* Detach s_start and make a loop : */
  641. sp1last->next = old_first;
  642. old_first->prev = sp1last;
  643. /* Unlink before sc1 : */
  644. sp1last = sc1->prev;
  645. sc1->prev->next = 0;
  646. sc1->prev = 0; /* Safety. */
  647. /* sp1 is not longer in use. Free it : */
  648. if (ppath->segments->contents.subpath_current == sp1) {
  649. ppath->segments->contents.subpath_current = sp1p;
  650. }
  651. gs_free_object(ppath->memory, sp1, "gx_path_merge_contacting_contours");
  652. sp1 = 0; /* Safety. */
  653. }
  654. /* Insert the subpath 1 into the subpath 0 before sc0 :*/
  655. sc0->prev->next = sc1;
  656. sc1->prev = sc0->prev;
  657. sp1last->next = sc0;
  658. sc0->prev = sp1last;
  659. /* Remove degenearte "bridge" segments : (fixme: Not done due to low importance). */
  660. /* Edit the subpath count : */
  661. ppath->subpath_count--;
  662. } else
  663. sp1p = sp1;
  664. }
  665. }
  666. return 0;
  667. }