r_path.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains the internal paint_path() and paint_paths() methods,
  16. which the public method endpath() is a wrapper around. */
  17. /* This file contains the internal path_is_flushable() method, which is
  18. invoked after any path segment is added to the segment list, provided
  19. (0) the segment list has become greater than or equal to the
  20. `max_unfilled_path_length' Plotter parameter, (1) the path isn't to be
  21. filled. In most Plotters, this operation simply returns true. */
  22. /* This file also contains the internal maybe_prepaint_segments() method.
  23. It is called immediately after any segment is added to a path. Some
  24. Plotters, at least under some circumstances, treat endpath() as a no-op.
  25. Instead, they plot the segments of a path in real time. */
  26. #include "sys-defines.h"
  27. #include "extern.h"
  28. /* for Cohen-Sutherland clipper, see g_clipper.c */
  29. enum { ACCEPTED = 0x1, CLIPPED_FIRST = 0x2, CLIPPED_SECOND = 0x4 };
  30. /* forward references */
  31. static void _emit_regis_vector (plIntPoint istart, plIntPoint iend, bool skip_null, char *tmpbuf);
  32. void
  33. _pl_r_paint_path (S___(Plotter *_plotter))
  34. {
  35. char tmpbuf[32];
  36. if (_plotter->drawstate->pen_type == 0
  37. && _plotter->drawstate->fill_type == 0)
  38. /* nothing to draw */
  39. return;
  40. switch ((int)_plotter->drawstate->path->type)
  41. {
  42. case (int)PATH_SEGMENT_LIST:
  43. {
  44. int i;
  45. /* sanity checks */
  46. if (_plotter->drawstate->path->num_segments == 0)/* nothing to do */
  47. break;
  48. if (_plotter->drawstate->path->num_segments == 1) /*shouldn't happen */
  49. break;
  50. if (_plotter->drawstate->fill_type)
  51. /* fill the path */
  52. {
  53. bool within_display = true;
  54. /* are all juncture points contained within the ReGIS display? */
  55. for (i = 0; i < _plotter->drawstate->path->num_segments; i++)
  56. {
  57. double x, y;
  58. int i_x, i_y;
  59. x = XD(_plotter->drawstate->path->segments[i].p.x,
  60. _plotter->drawstate->path->segments[i].p.y);
  61. y = YD(_plotter->drawstate->path->segments[i].p.x,
  62. _plotter->drawstate->path->segments[i].p.y);
  63. i_x = IROUND(x);
  64. i_y = IROUND(y);
  65. if (i_x < REGIS_DEVICE_X_MIN
  66. || i_x > REGIS_DEVICE_X_MAX
  67. || i_y < REGIS_DEVICE_Y_MIN
  68. || i_y > REGIS_DEVICE_Y_MAX)
  69. {
  70. within_display = false;
  71. break;
  72. }
  73. }
  74. if (within_display)
  75. /* can fill path using ReGIS primitives */
  76. {
  77. double x, y;
  78. plIntPoint first, oldpoint, newpoint;
  79. _pl_r_set_fill_color (S___(_plotter));
  80. x = XD(_plotter->drawstate->path->segments[0].p.x,
  81. _plotter->drawstate->path->segments[0].p.y);
  82. y = YD(_plotter->drawstate->path->segments[0].p.x,
  83. _plotter->drawstate->path->segments[0].p.y);
  84. first.x = IROUND(x);
  85. first.y = IROUND(y);
  86. _pl_r_regis_move (R___(_plotter) first.x, first.y); /* use P[..] */
  87. _write_string (_plotter->data, "F(");
  88. _write_string (_plotter->data, "V");
  89. oldpoint = first;
  90. for (i = 1; i < _plotter->drawstate->path->num_segments; i++)
  91. {
  92. x = XD(_plotter->drawstate->path->segments[i].p.x,
  93. _plotter->drawstate->path->segments[i].p.y);
  94. y = YD(_plotter->drawstate->path->segments[i].p.x,
  95. _plotter->drawstate->path->segments[i].p.y);
  96. newpoint.x = IROUND(x);
  97. newpoint.y = IROUND(y);
  98. /* emit vector; omit it if it has zero-length in the
  99. integer device frame (unless it's the 1st vector) */
  100. _emit_regis_vector (oldpoint, newpoint,
  101. i > 1 ? true : false, tmpbuf);
  102. _write_string (_plotter->data, tmpbuf);
  103. oldpoint = newpoint;
  104. }
  105. /* if path isn't closed, add a vector to close it (ReGIS
  106. behaves unreliably if this isn't done) */
  107. _emit_regis_vector (newpoint, first, true, tmpbuf);
  108. _write_string (_plotter->data, tmpbuf);
  109. /* terminate F(V..) command */
  110. _write_string (_plotter->data, ")\n");
  111. _plotter->regis_position_is_unknown = true; /* to us */
  112. }
  113. else
  114. /* path extends beyond ReGIS display, so must clip before
  115. filling */
  116. {
  117. /* NOT IMPLEMENTED YET */
  118. }
  119. }
  120. if (_plotter->drawstate->pen_type)
  121. /* edge the path */
  122. {
  123. bool attributes_set = false;
  124. bool path_in_progress = false;
  125. for (i = 1; i < _plotter->drawstate->path->num_segments; i++)
  126. {
  127. plPoint start, end; /* endpoints of seg. (in device coors) */
  128. plIntPoint istart, iend; /* same, quantized to integer */
  129. int clipval;
  130. /* nominal starting point and ending point for new line
  131. segment, in floating point device coordinates */
  132. start.x = XD(_plotter->drawstate->path->segments[i-1].p.x,
  133. _plotter->drawstate->path->segments[i-1].p.y);
  134. start.y = YD(_plotter->drawstate->path->segments[i-1].p.x,
  135. _plotter->drawstate->path->segments[i-1].p.y);
  136. end.x = XD(_plotter->drawstate->path->segments[i].p.x,
  137. _plotter->drawstate->path->segments[i].p.y);
  138. end.y = YD(_plotter->drawstate->path->segments[i].p.x,
  139. _plotter->drawstate->path->segments[i].p.y);
  140. /* clip line segment to rectangular clipping region in
  141. device frame */
  142. clipval = _clip_line (&start.x, &start.y, &end.x, &end.y,
  143. REGIS_DEVICE_X_MIN_CLIP,
  144. REGIS_DEVICE_X_MAX_CLIP,
  145. REGIS_DEVICE_Y_MIN_CLIP,
  146. REGIS_DEVICE_Y_MAX_CLIP);
  147. if (!(clipval & ACCEPTED)) /* line segment is OOB */
  148. {
  149. if (path_in_progress) /* terminate it */
  150. _write_string (_plotter->data, "\n");
  151. path_in_progress = false;
  152. continue; /* drop this line segment */
  153. }
  154. if (clipval & CLIPPED_FIRST) /* must move */
  155. {
  156. if (path_in_progress) /* terminate it */
  157. _write_string (_plotter->data, "\n");
  158. path_in_progress = false;
  159. }
  160. /* convert clipped starting point, ending point to integer
  161. ReGIS coors */
  162. istart.x = IROUND(start.x);
  163. istart.y = IROUND(start.y);
  164. iend.x = IROUND(end.x);
  165. iend.y = IROUND(end.y);
  166. if (path_in_progress
  167. && istart.x == iend.x && istart.y == iend.y)
  168. /* redundant, so drop this line segment */
  169. continue;
  170. if (attributes_set == false)
  171. /* will be drawing something, so sync ReGIS line type and
  172. set the ReGIS foreground color to be our pen color;
  173. this code gets executed the first time we get here */
  174. {
  175. _pl_r_set_attributes (S___(_plotter));
  176. _pl_r_set_pen_color (S___(_plotter));
  177. attributes_set = true;
  178. }
  179. if (path_in_progress == false)
  180. {
  181. /* if necessary, move graphics cursor to first point of
  182. line segment, using P command */
  183. _pl_r_regis_move (R___(_plotter) istart.x, istart.y);
  184. /* emit op code for V command, to begin polyline */
  185. _write_string (_plotter->data, "V");
  186. if (iend.x != istart.x || iend.y != istart.y)
  187. /* emit V[] command: ensure initial pixel is painted */
  188. _write_string (_plotter->data, "[]");
  189. path_in_progress = true;
  190. }
  191. _emit_regis_vector (istart, iend, true, tmpbuf);
  192. _write_string (_plotter->data, tmpbuf);
  193. /* update our notion of ReGIS's notion of position */
  194. _plotter->regis_pos.x = iend.x;
  195. _plotter->regis_pos.y = iend.y;
  196. }
  197. /* entire path has been drawn */
  198. if (path_in_progress == true)
  199. _write_string (_plotter->data, "\n");
  200. }
  201. }
  202. break;
  203. case (int)PATH_CIRCLE:
  204. {
  205. double xd, yd, radius_d;
  206. int i_x, i_y, i_radius; /* center and radius, quantized */
  207. plPoint pc;
  208. double radius;
  209. pc = _plotter->drawstate->path->pc;
  210. radius = _plotter->drawstate->path->radius;
  211. /* known to be a circle in device frame, so compute center and
  212. radius in that frame */
  213. xd = XD(pc.x, pc.y);
  214. yd = YD(pc.x, pc.y);
  215. radius_d = sqrt (XDV(radius,0) * XDV(radius,0)
  216. + YDV(radius,0) * YDV(radius,0));
  217. i_x = IROUND(xd);
  218. i_y = IROUND(yd);
  219. i_radius = IROUND(radius_d);
  220. if (i_x - i_radius < REGIS_DEVICE_X_MIN
  221. || i_x + i_radius > REGIS_DEVICE_X_MAX
  222. || i_y - i_radius < REGIS_DEVICE_Y_MIN
  223. || i_y + i_radius > REGIS_DEVICE_Y_MAX)
  224. /* circle extends beyond edge of display, so polygonalize and
  225. recurse */
  226. {
  227. plPath *oldpath = _plotter->drawstate->path;
  228. _plotter->drawstate->path = _flatten_path (oldpath);
  229. _plotter->paint_path (S___(_plotter)); /* recursive invocation */
  230. _delete_plPath (_plotter->drawstate->path);
  231. _plotter->drawstate->path = oldpath;
  232. }
  233. else
  234. /* circle contained within display, can draw using ReGIS circle
  235. primitive */
  236. {
  237. if (_plotter->drawstate->fill_type)
  238. /* fill the circle */
  239. {
  240. _pl_r_set_fill_color (S___(_plotter));
  241. _pl_r_regis_move (R___(_plotter) i_x, i_y); /* use P command */
  242. if (i_radius > 0)
  243. {
  244. sprintf (tmpbuf, "F(C[+%d])\n", i_radius);
  245. _plotter->regis_position_is_unknown = true; /* to us */
  246. }
  247. else
  248. sprintf (tmpbuf, "V[]\n");
  249. _write_string (_plotter->data, tmpbuf);
  250. }
  251. if (_plotter->drawstate->pen_type)
  252. /* edge the circle */
  253. {
  254. _pl_r_set_attributes (S___(_plotter));
  255. _pl_r_set_pen_color (S___(_plotter));
  256. _pl_r_regis_move (R___(_plotter) i_x, i_y); /* use P command */
  257. if (i_radius > 0)
  258. {
  259. sprintf (tmpbuf, "C[+%d]\n", i_radius);
  260. _plotter->regis_position_is_unknown = true; /* to us */
  261. }
  262. else
  263. sprintf (tmpbuf, "V[]\n");
  264. _write_string (_plotter->data, tmpbuf);
  265. }
  266. }
  267. }
  268. break;
  269. default: /* shouldn't happen */
  270. break;
  271. }
  272. }
  273. /* A low-level method for moving the graphics cursor of a ReGIS device to
  274. agree with the Plotter's notion of what the graphics cursor should be. */
  275. void
  276. _pl_r_regis_move (R___(Plotter *_plotter) int xx, int yy)
  277. {
  278. char tmpbuf[32];
  279. plIntPoint newpoint;
  280. /* sanity check */
  281. if (xx < REGIS_DEVICE_X_MIN || xx > REGIS_DEVICE_X_MAX
  282. || yy < REGIS_DEVICE_Y_MIN || yy > REGIS_DEVICE_Y_MAX)
  283. return;
  284. newpoint.x = xx;
  285. newpoint.y = yy;
  286. if (_plotter->regis_position_is_unknown)
  287. {
  288. sprintf (tmpbuf, "P[%d,%d]\n", xx, yy);
  289. _write_string (_plotter->data, tmpbuf);
  290. }
  291. else if (xx != _plotter->regis_pos.x || yy != _plotter->regis_pos.y)
  292. {
  293. _write_string (_plotter->data, "P");
  294. _emit_regis_vector (_plotter->regis_pos, newpoint, false, tmpbuf);
  295. _write_string (_plotter->data, tmpbuf);
  296. _write_string (_plotter->data, "\n");
  297. }
  298. /* update our knowledge of cursor position */
  299. _plotter->regis_position_is_unknown = false;
  300. _plotter->regis_pos = newpoint;
  301. }
  302. static void
  303. _emit_regis_vector (plIntPoint istart, plIntPoint iend, bool skip_null, char *tmpbuf)
  304. {
  305. plIntVector v;
  306. bool xneg = false, yneg = false;
  307. char xrelbuf[32], yrelbuf[32], xbuf[32], ybuf[32];
  308. int xrellen, yrellen, xlen, ylen;
  309. char *x, *y;
  310. v.x = iend.x - istart.x;
  311. v.y = iend.y - istart.y;
  312. /* trivial case */
  313. if (v.x == 0 && v.y == 0)
  314. {
  315. if (skip_null == false)
  316. sprintf (tmpbuf, "[]");
  317. else
  318. *tmpbuf = '\0'; /* empty string */
  319. return;
  320. }
  321. /* compute length of endpoint in terms of characters, when printed in
  322. relative and absolute coordinates */
  323. if (v.x < 0)
  324. {
  325. xneg = true;
  326. v.x = -v.x;
  327. }
  328. if (v.y < 0)
  329. {
  330. yneg = true;
  331. v.y = -v.y;
  332. }
  333. sprintf (xrelbuf, "%s%d", (xneg ? "-" : "+"), v.x);
  334. xrellen = strlen (xrelbuf);
  335. sprintf (yrelbuf, "%s%d", (yneg ? "-" : "+"), v.y);
  336. yrellen = strlen (yrelbuf);
  337. sprintf (xbuf, "%d", iend.x);
  338. xlen = strlen (xbuf);
  339. sprintf (ybuf, "%d", iend.y);
  340. ylen = strlen (ybuf);
  341. /* use whichever (relative/absolute) is shorter; prefer relative */
  342. x = (xrellen <= xlen ? xrelbuf : xbuf);
  343. y = (yrellen <= ylen ? yrelbuf : ybuf);
  344. /* draw vector: emit point coordinates */
  345. if (v.x == 0)
  346. sprintf (tmpbuf, "[,%s]", y);
  347. else if (v.y == 0)
  348. sprintf (tmpbuf, "[%s]", x);
  349. else
  350. sprintf (tmpbuf, "[%s,%s]", x, y);
  351. }
  352. bool
  353. _pl_r_paint_paths (S___(Plotter *_plotter))
  354. {
  355. return false;
  356. }
  357. bool
  358. _pl_r_path_is_flushable (S___(Plotter *_plotter))
  359. {
  360. return true;
  361. }
  362. void
  363. _pl_r_maybe_prepaint_segments (R___(Plotter *_plotter) int prev_num_segments)
  364. {
  365. }