g_endpath.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 endpath() method, which is a GNU extension to
  16. libplot. A path object may be constructed incrementally, by repeated
  17. invocation of such operations as cont(), arc(), etc. The construction
  18. may be terminated, and the path object finalized, by an explict
  19. invocation of endpath(). If endpath() is invoked when no path is under
  20. construction, it has no effect. */
  21. /* endpath() is a wrapper around the internal paint_path() method, which
  22. any Plotter can define as it chooses. Any path is stored as a plPath
  23. structure in the drawing state. This may contain a list of segments
  24. (line segments, curve segments etc.; which are allowed is
  25. Plotter-dependent) or simply a Plotter-specific drawing primitive such
  26. as a circle, ellipse, or rectangle. paint_path() should be able to
  27. handle anything that is appropriate for the given type of Plotter. */
  28. /* This file also contains the endsubpath() and closepath() methods. */
  29. #include "sys-defines.h"
  30. #include "extern.h"
  31. int
  32. _API_endpath (S___(Plotter *_plotter))
  33. {
  34. int i;
  35. if (!_plotter->data->open)
  36. {
  37. _plotter->error (R___(_plotter)
  38. "endpath: invalid operation");
  39. return -1;
  40. }
  41. /* end simple path under construction (if any), and move it to the array
  42. of stored simple paths */
  43. _API_endsubpath (S___(_plotter));
  44. if (_plotter->drawstate->num_paths == 0)
  45. /* no stored simple paths; so nothing to do, and we're out of here */
  46. return 0;
  47. /* at this point, compound path is available as an array of simple paths,
  48. of length at least 1, in _plotter->drawstate->paths[] */
  49. /* Two cases: either the line mode is `disconnected', or it isn't (the
  50. normal case). */
  51. if (!_plotter->drawstate->points_are_connected)
  52. /* Special case: "disconnected" linemode. If we have a pen, path will
  53. be drawn as a sequence of filled circles, one per juncture point.
  54. Path will not be filled (our convention). */
  55. {
  56. if (_plotter->drawstate->pen_type != 0)
  57. /* have a pen, so we can draw something */
  58. {
  59. plPath **saved_paths;
  60. int saved_num_paths;
  61. double radius = 0.5 * _plotter->drawstate->line_width;
  62. int i;
  63. /* Switch to a temporary paths buffer. Needed because the
  64. fcircle() method calls endpath(), which would otherwise mess
  65. up the real paths buffer. */
  66. saved_paths = _plotter->drawstate->paths;
  67. saved_num_paths = _plotter->drawstate->num_paths;
  68. _plotter->drawstate->paths = (plPath **)NULL;
  69. _plotter->drawstate->num_paths = 0;
  70. /* save graphics state */
  71. _API_savestate (S___(_plotter));
  72. /* set attributes appropriate for drawing filled edgeless
  73. circles, in the current pen (rather than filling) color */
  74. _API_filltype (R___(_plotter) 1);
  75. _API_fillcolor (R___(_plotter)
  76. _plotter->drawstate->fgcolor.red,
  77. _plotter->drawstate->fgcolor.green,
  78. _plotter->drawstate->fgcolor.blue);
  79. _API_pentype (R___(_plotter) 0); /* edgeless */
  80. _API_linemod (R___(_plotter) "solid"); /* necessary; see below*/
  81. /* loop over saved simple paths */
  82. for (i = 0; i < saved_num_paths; i++)
  83. {
  84. plPath *path;
  85. bool closed;
  86. int j;
  87. path = saved_paths[i];
  88. /* sanity check: if linemode is disconnected, we should never
  89. have created any simple path other than a segment list;
  90. also, should have at least two juncture points */
  91. if (path->type != PATH_SEGMENT_LIST || path->num_segments < 2)
  92. continue;
  93. /* check for closure */
  94. if ((path->num_segments >= 3)
  95. && (path->segments[path->num_segments - 1].p.x ==
  96. path->segments[0].p.x)
  97. && (path->segments[path->num_segments - 1].p.y ==
  98. path->segments[0].p.y))
  99. closed = true;
  100. else
  101. closed = false; /* 2-point ones should be open */
  102. /* draw each point as a filled circle, diameter = line width */
  103. for (j = 0; j < path->num_segments - (closed ? 1 : 0); j++)
  104. _API_fcircle (R___(_plotter)
  105. path->segments[j].p.x,
  106. path->segments[j].p.y,
  107. radius);
  108. if (closed)
  109. /* restore graphics cursor */
  110. _plotter->drawstate->pos = path->segments[0].p;
  111. }
  112. /* Restore graphics state. This will first do a recursive
  113. endpath() and hence reset the newly populated paths buffer.
  114. That won't result in infinite recursion: since the line type
  115. was set to "solid" above, the `points_are_connected' element
  116. is now `false', and this code won't be invoked again. */
  117. _API_restorestate (S___(_plotter));
  118. /* switch back to original paths buffer */
  119. _plotter->drawstate->paths = saved_paths;
  120. _plotter->drawstate->num_paths = saved_num_paths;
  121. }
  122. }
  123. else
  124. /* normal case: line mode isn't disconnected, so no contortions needed */
  125. {
  126. if (_plotter->drawstate->num_paths == 1)
  127. /* compound path is just a single simple path, so paint it by
  128. calling the Plotter-specific paint_path() method (the painting
  129. may involve both filling and/or edging) */
  130. {
  131. _plotter->drawstate->path = _plotter->drawstate->paths[0];
  132. _plotter->paint_path (S___(_plotter));
  133. _plotter->drawstate->path = (plPath *)NULL;
  134. }
  135. else
  136. /* compound path comprises more than one simple path */
  137. {
  138. /* first, attempt to use Plotter-specific support for painting
  139. compound paths (not many Plotters have this) */
  140. if (_plotter->paint_paths (S___(_plotter)) == false)
  141. /* Plotter either has no such support, or was unable to paint
  142. this particular compound path; so we paint it in a clever,
  143. device-independent way. For filling, we merge the simple
  144. paths into a single path, and invoke paint_path() on the
  145. result. For edging, we stroke each of the simple paths
  146. individually. */
  147. {
  148. int fill_type, pen_type;
  149. fill_type = _plotter->drawstate->fill_type;
  150. pen_type = _plotter->drawstate->pen_type;
  151. if (fill_type && _plotter->data->have_solid_fill)
  152. /* fill the compound path, by merging its simple paths into
  153. a single simple path, and then invoking paint_path() on
  154. the result */
  155. {
  156. plPath **merged_paths;
  157. _plotter->drawstate->fill_type = fill_type;
  158. _plotter->drawstate->pen_type = 0; /* unedged */
  159. merged_paths = _merge_paths ((const plPath **)_plotter->drawstate->paths,
  160. _plotter->drawstate->num_paths);
  161. for (i = 0; i < _plotter->drawstate->num_paths; i++)
  162. {
  163. if (merged_paths[i] == (plPath *)NULL)
  164. continue;
  165. _plotter->drawstate->path = merged_paths[i];
  166. _plotter->paint_path (S___(_plotter));
  167. if (merged_paths[i] != _plotter->drawstate->paths[i])
  168. _delete_plPath (merged_paths[i]);
  169. }
  170. _plotter->drawstate->path = (plPath *)NULL;
  171. }
  172. if (pen_type)
  173. /* edge the compound path, i.e., edge each of its simple
  174. paths */
  175. {
  176. _plotter->drawstate->pen_type = pen_type;
  177. _plotter->drawstate->fill_type = 0; /* unfilled */
  178. for (i = 0; i < _plotter->drawstate->num_paths; i++)
  179. {
  180. _plotter->drawstate->path = _plotter->drawstate->paths[i];
  181. _plotter->paint_path (S___(_plotter));
  182. }
  183. _plotter->drawstate->path = (plPath *)NULL;
  184. }
  185. /* restore filling/edging attributes */
  186. _plotter->drawstate->fill_type = fill_type;
  187. _plotter->drawstate->pen_type = pen_type;
  188. }
  189. }
  190. }
  191. /* compound path is now painted, so remove it from paths buffer */
  192. for (i = 0; i < _plotter->drawstate->num_paths; i++)
  193. _delete_plPath (_plotter->drawstate->paths[i]);
  194. free (_plotter->drawstate->paths);
  195. _plotter->drawstate->paths = (plPath **)NULL;
  196. _plotter->drawstate->num_paths = 0;
  197. return 0;
  198. }
  199. int
  200. _API_endsubpath (S___(Plotter *_plotter))
  201. {
  202. if (!_plotter->data->open)
  203. {
  204. _plotter->error (R___(_plotter)
  205. "endsubpath: invalid operation");
  206. return -1;
  207. }
  208. if (_plotter->drawstate->path)
  209. /* have a simple path under construction, so move it to list of stored
  210. simple paths */
  211. {
  212. if (_plotter->drawstate->num_paths == 0)
  213. _plotter->drawstate->paths =
  214. (plPath **)_pl_xmalloc(sizeof (plPath *));
  215. else
  216. _plotter->drawstate->paths =
  217. (plPath **)_pl_xrealloc(_plotter->drawstate->paths,
  218. (_plotter->drawstate->num_paths + 1)
  219. * sizeof (plPath *));
  220. _plotter->drawstate->paths[_plotter->drawstate->num_paths++] =
  221. _plotter->drawstate->path;
  222. _plotter->drawstate->path = (plPath *)NULL;
  223. }
  224. return 0;
  225. }
  226. int
  227. _API_closepath (S___(Plotter *_plotter))
  228. {
  229. if (!_plotter->data->open)
  230. {
  231. _plotter->error (R___(_plotter)
  232. "closepath: invalid operation");
  233. return -1;
  234. }
  235. /* NOT YET IMPLEMENTED */
  236. return 0;
  237. }