gxpdash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gxpdash.c,v 1.2 2000/09/19 19:00:40 lpd Exp $ */
  16. /* Dash expansion for paths */
  17. #include "math_.h"
  18. #include "gx.h"
  19. #include "gsmatrix.h" /* for gscoord.h */
  20. #include "gscoord.h"
  21. #include "gxfixed.h"
  22. #include "gsline.h"
  23. #include "gzline.h"
  24. #include "gzpath.h"
  25. /* Expand a dashed path into explicit segments. */
  26. /* The path contains no curves. */
  27. private int subpath_expand_dashes(P4(const subpath *, gx_path *,
  28. const gs_imager_state *,
  29. const gx_dash_params *));
  30. int
  31. gx_path_add_dash_expansion(const gx_path * ppath_old, gx_path * ppath,
  32. const gs_imager_state * pis)
  33. {
  34. const subpath *psub;
  35. const gx_dash_params *dash = &gs_currentlineparams(pis)->dash;
  36. int code = 0;
  37. if (dash->pattern_size == 0)
  38. return gx_path_copy(ppath_old, ppath);
  39. for (psub = ppath_old->first_subpath; psub != 0 && code >= 0;
  40. psub = (const subpath *)psub->last->next
  41. )
  42. code = subpath_expand_dashes(psub, ppath, pis, dash);
  43. return code;
  44. }
  45. private int
  46. subpath_expand_dashes(const subpath * psub, gx_path * ppath,
  47. const gs_imager_state * pis, const gx_dash_params * dash)
  48. {
  49. const float *pattern = dash->pattern;
  50. int count, index;
  51. bool ink_on;
  52. double elt_length;
  53. fixed x0 = psub->pt.x, y0 = psub->pt.y;
  54. fixed x, y;
  55. const segment *pseg;
  56. int wrap = (dash->init_ink_on && psub->is_closed ? -1 : 0);
  57. int drawing = wrap;
  58. segment_notes notes = ~sn_not_first;
  59. int code;
  60. if ((code = gx_path_add_point(ppath, x0, y0)) < 0)
  61. return code;
  62. /*
  63. * To do the right thing at the beginning of a closed path, we have
  64. * to skip any initial line, and then redo it at the end of the
  65. * path. Drawing = -1 while skipping, 0 while drawing normally, and
  66. * 1 on the second round. Note that drawing != 0 implies ink_on.
  67. */
  68. top:count = dash->pattern_size;
  69. ink_on = dash->init_ink_on;
  70. index = dash->init_index;
  71. elt_length = dash->init_dist_left;
  72. x = x0, y = y0;
  73. pseg = (const segment *)psub;
  74. while ((pseg = pseg->next) != 0 && pseg->type != s_start) {
  75. fixed sx = pseg->pt.x, sy = pseg->pt.y;
  76. fixed udx = sx - x, udy = sy - y;
  77. double length, dx, dy;
  78. double scale = 1;
  79. double left;
  80. if (!(udx | udy)) /* degenerate */
  81. dx = 0, dy = 0, length = 0;
  82. else {
  83. gs_point d;
  84. dx = udx, dy = udy; /* scaled as fixed */
  85. gs_imager_idtransform(pis, dx, dy, &d);
  86. length = hypot(d.x, d.y) * (1.0 / fixed_1);
  87. if (gs_imager_currentdashadapt(pis)) {
  88. double reps = length / dash->pattern_length;
  89. scale = reps / ceil(reps);
  90. /* Ensure we're starting at the start of a */
  91. /* repetition. (This shouldn't be necessary, */
  92. /* but it is.) */
  93. count = dash->pattern_size;
  94. ink_on = dash->init_ink_on;
  95. index = dash->init_index;
  96. elt_length = dash->init_dist_left * scale;
  97. }
  98. }
  99. left = length;
  100. while (left > elt_length) { /* We are using up the line segment. */
  101. double fraction = elt_length / length;
  102. fixed nx = x + (fixed) (dx * fraction);
  103. fixed ny = y + (fixed) (dy * fraction);
  104. if (ink_on) {
  105. if (drawing >= 0)
  106. code = gx_path_add_line_notes(ppath, nx, ny,
  107. notes & pseg->notes);
  108. notes |= sn_not_first;
  109. } else {
  110. if (drawing > 0) /* done */
  111. return 0;
  112. code = gx_path_add_point(ppath, nx, ny);
  113. notes &= ~sn_not_first;
  114. drawing = 0;
  115. }
  116. if (code < 0)
  117. return code;
  118. left -= elt_length;
  119. ink_on = !ink_on;
  120. if (++index == count)
  121. index = 0;
  122. elt_length = pattern[index] * scale;
  123. x = nx, y = ny;
  124. }
  125. elt_length -= left;
  126. /* Handle the last dash of a segment. */
  127. on:if (ink_on) {
  128. if (drawing >= 0) {
  129. code =
  130. (pseg->type == s_line_close && drawing > 0 ?
  131. gx_path_close_subpath_notes(ppath,
  132. notes & pseg->notes) :
  133. gx_path_add_line_notes(ppath, sx, sy,
  134. notes & pseg->notes));
  135. notes |= sn_not_first;
  136. }
  137. } else {
  138. code = gx_path_add_point(ppath, sx, sy);
  139. notes &= ~sn_not_first;
  140. if (elt_length < fixed2float(fixed_epsilon) &&
  141. (pseg->next == 0 || pseg->next->type == s_start)
  142. ) { /*
  143. * Ink is off, but we're within epsilon of the end
  144. * of the dash element, and at the end of the
  145. * subpath. "Stretch" a little so we get a dot.
  146. */
  147. if (code < 0)
  148. return code;
  149. elt_length = 0;
  150. ink_on = true;
  151. if (++index == count)
  152. index = 0;
  153. elt_length = pattern[index] * scale;
  154. goto on;
  155. }
  156. if (drawing > 0) /* done */
  157. return code;
  158. drawing = 0;
  159. }
  160. if (code < 0)
  161. return code;
  162. x = sx, y = sy;
  163. }
  164. /* Check for wraparound. */
  165. if (wrap && drawing <= 0) { /* We skipped some initial lines. */
  166. /* Go back and do them now. */
  167. drawing = 1;
  168. goto top;
  169. }
  170. return 0;
  171. }