m_path.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include "sys-defines.h"
  18. #include "extern.h"
  19. void
  20. _pl_m_paint_path (S___(Plotter *_plotter))
  21. {
  22. const plPath *path;
  23. bool explicit_endpath, sync_miter_limit = false;
  24. /* sync basic path attributes */
  25. _pl_m_set_attributes (R___(_plotter)
  26. PL_ATTR_TRANSFORMATION_MATRIX
  27. | PL_ATTR_PEN_COLOR | PL_ATTR_PEN_TYPE
  28. | PL_ATTR_LINE_STYLE | PL_ATTR_LINE_WIDTH
  29. | PL_ATTR_JOIN_STYLE | PL_ATTR_CAP_STYLE
  30. | PL_ATTR_FILL_COLOR | PL_ATTR_FILL_TYPE
  31. | PL_ATTR_FILL_RULE);
  32. /* our one and only simple path to paint */
  33. path = _plotter->drawstate->path;
  34. if (_plotter->drawstate->join_type == PL_JOIN_MITER
  35. && (path->type == PATH_SEGMENT_LIST || path->type == PATH_BOX))
  36. /* path may have mitered juncture points */
  37. sync_miter_limit = true;
  38. if (sync_miter_limit)
  39. _pl_m_set_attributes (R___(_plotter) PL_ATTR_MITER_LIMIT);
  40. if (path->type == PATH_SEGMENT_LIST)
  41. explicit_endpath = true;
  42. else
  43. explicit_endpath = false;
  44. /* emit metafile object-drawing instructions to draw the path; include a
  45. preliminary syncing of the `orientation' attribute if relevant */
  46. _pl_m_paint_path_internal (R___(_plotter) path);
  47. if (explicit_endpath)
  48. {
  49. _pl_m_emit_op_code (R___(_plotter) O_ENDPATH);
  50. _pl_m_emit_terminator (S___(_plotter));
  51. }
  52. }
  53. bool
  54. _pl_m_paint_paths (S___(Plotter *_plotter))
  55. {
  56. const plPath *path;
  57. bool sync_miter_limit = false;
  58. int i;
  59. /* sanity check */
  60. if (_plotter->drawstate->num_paths == 0)
  61. return true;
  62. /* sync basic path attributes */
  63. _pl_m_set_attributes (R___(_plotter)
  64. PL_ATTR_TRANSFORMATION_MATRIX
  65. | PL_ATTR_PEN_COLOR | PL_ATTR_PEN_TYPE
  66. | PL_ATTR_LINE_STYLE | PL_ATTR_LINE_WIDTH
  67. | PL_ATTR_JOIN_STYLE | PL_ATTR_CAP_STYLE
  68. | PL_ATTR_FILL_COLOR | PL_ATTR_FILL_TYPE
  69. | PL_ATTR_FILL_RULE);
  70. if (_plotter->drawstate->join_type == PL_JOIN_MITER)
  71. {
  72. for (i = 0; i < _plotter->drawstate->num_paths; i++)
  73. {
  74. path = _plotter->drawstate->paths[i];
  75. if (path->type == PATH_SEGMENT_LIST || path->type == PATH_BOX)
  76. /* compound path may have mitered juncture points */
  77. {
  78. sync_miter_limit = true;
  79. break;
  80. }
  81. }
  82. }
  83. if (sync_miter_limit)
  84. _pl_m_set_attributes (R___(_plotter) PL_ATTR_MITER_LIMIT);
  85. /* loop over simple paths in compound path */
  86. for (i = 0; i < _plotter->drawstate->num_paths; i++)
  87. {
  88. path = _plotter->drawstate->paths[i];
  89. /* emit metafile object-drawing instructions to draw the path; first
  90. sync `orientation' attribute, if relevant */
  91. _pl_m_paint_path_internal (R___(_plotter) path);
  92. if (i < _plotter->drawstate->num_paths - 1)
  93. {
  94. _pl_m_emit_op_code (R___(_plotter) O_ENDSUBPATH);
  95. _pl_m_emit_terminator (S___(_plotter));
  96. }
  97. }
  98. if (_plotter->drawstate->paths[_plotter->drawstate->num_paths - 1]->type == PATH_SEGMENT_LIST)
  99. /* append explicit (as opposed to implicit) endpath; if we didn't wish
  100. to be clever, we'd append one even if the final simple path isn't a
  101. segment list */
  102. {
  103. _pl_m_emit_op_code (R___(_plotter) O_ENDPATH);
  104. _pl_m_emit_terminator (S___(_plotter));
  105. }
  106. /* succesfully painted compound path */
  107. return true;
  108. }
  109. /* Internal routine, called by the MetaPlotter-specific versions of
  110. paint_path() and paint_paths(). Besides emitting metafile instructions
  111. to draw a path, it may emit an instruction to update the `orientation'
  112. attribute, relevant to paths that are boxes/circles/ellipses. */
  113. void
  114. _pl_m_paint_path_internal (R___(Plotter *_plotter) const plPath *path)
  115. {
  116. if (path->type == PATH_BOX
  117. || path->type == PATH_CIRCLE || path->type == PATH_ELLIPSE)
  118. /* sync orientation; orientation is stored in the path itself, not in
  119. the drawing state */
  120. {
  121. int orientation = (path->clockwise ? -1 : 1);
  122. if (_plotter->meta_orientation != orientation)
  123. {
  124. _pl_m_emit_op_code (R___(_plotter) O_ORIENTATION);
  125. _pl_m_emit_integer (R___(_plotter) orientation);
  126. _pl_m_emit_terminator (S___(_plotter));
  127. _plotter->meta_orientation = orientation;
  128. }
  129. }
  130. switch ((int)path->type)
  131. {
  132. case (int)PATH_SEGMENT_LIST:
  133. {
  134. plPathSegment segment;
  135. int i;
  136. /* last-minute sanity check */
  137. if (path->num_segments == 0)/* nothing to do */
  138. break;
  139. if (path->num_segments == 1) /* shouldn't happen */
  140. break;
  141. segment = path->segments[0]; /* initial moveto */
  142. if (_plotter->meta_pos.x != segment.p.x
  143. || _plotter->meta_pos.y != segment.p.y)
  144. {
  145. _pl_m_emit_op_code (R___(_plotter) O_FMOVE);
  146. _pl_m_emit_float (R___(_plotter) segment.p.x);
  147. _pl_m_emit_float (R___(_plotter) segment.p.y);
  148. _pl_m_emit_terminator (S___(_plotter));
  149. _plotter->meta_pos = segment.p;
  150. }
  151. for (i = 1; i < path->num_segments; i++)
  152. {
  153. plPathSegment prev_segment;
  154. prev_segment = segment;
  155. segment = path->segments[i];
  156. switch ((int)segment.type)
  157. {
  158. case (int)S_LINE:
  159. _pl_m_emit_op_code (R___(_plotter) O_FCONT);
  160. _pl_m_emit_float (R___(_plotter) segment.p.x);
  161. _pl_m_emit_float (R___(_plotter) segment.p.y);
  162. _pl_m_emit_terminator (S___(_plotter));
  163. _plotter->meta_pos = segment.p;
  164. break;
  165. case (int)S_ARC:
  166. _pl_m_emit_op_code (R___(_plotter) O_FARC);
  167. _pl_m_emit_float (R___(_plotter) segment.pc.x);
  168. _pl_m_emit_float (R___(_plotter) segment.pc.y);
  169. _pl_m_emit_float (R___(_plotter) prev_segment.p.x);
  170. _pl_m_emit_float (R___(_plotter) prev_segment.p.y);
  171. _pl_m_emit_float (R___(_plotter) segment.p.x);
  172. _pl_m_emit_float (R___(_plotter) segment.p.y);
  173. _pl_m_emit_terminator (S___(_plotter));
  174. _plotter->meta_pos = segment.p;
  175. break;
  176. case (int)S_ELLARC:
  177. _pl_m_emit_op_code (R___(_plotter) O_FELLARC);
  178. _pl_m_emit_float (R___(_plotter) segment.pc.x);
  179. _pl_m_emit_float (R___(_plotter) segment.pc.y);
  180. _pl_m_emit_float (R___(_plotter) prev_segment.p.x);
  181. _pl_m_emit_float (R___(_plotter) prev_segment.p.y);
  182. _pl_m_emit_float (R___(_plotter) segment.p.x);
  183. _pl_m_emit_float (R___(_plotter) segment.p.y);
  184. _pl_m_emit_terminator (S___(_plotter));
  185. _plotter->meta_pos = segment.p;
  186. break;
  187. case (int)S_QUAD:
  188. _pl_m_emit_op_code (R___(_plotter) O_FBEZIER2);
  189. _pl_m_emit_float (R___(_plotter) prev_segment.p.x);
  190. _pl_m_emit_float (R___(_plotter) prev_segment.p.y);
  191. _pl_m_emit_float (R___(_plotter) segment.pc.x);
  192. _pl_m_emit_float (R___(_plotter) segment.pc.y);
  193. _pl_m_emit_float (R___(_plotter) segment.p.x);
  194. _pl_m_emit_float (R___(_plotter) segment.p.y);
  195. _pl_m_emit_terminator (S___(_plotter));
  196. _plotter->meta_pos = segment.p;
  197. break;
  198. case (int)S_CUBIC:
  199. _pl_m_emit_op_code (R___(_plotter) O_FBEZIER3);
  200. _pl_m_emit_float (R___(_plotter) prev_segment.p.x);
  201. _pl_m_emit_float (R___(_plotter) prev_segment.p.y);
  202. _pl_m_emit_float (R___(_plotter) segment.pc.x);
  203. _pl_m_emit_float (R___(_plotter) segment.pc.y);
  204. _pl_m_emit_float (R___(_plotter) segment.pd.x);
  205. _pl_m_emit_float (R___(_plotter) segment.pd.y);
  206. _pl_m_emit_float (R___(_plotter) segment.p.x);
  207. _pl_m_emit_float (R___(_plotter) segment.p.y);
  208. _pl_m_emit_terminator (S___(_plotter));
  209. _plotter->meta_pos = segment.p;
  210. break;
  211. default: /* shouldn't happen */
  212. break;
  213. }
  214. }
  215. }
  216. break;
  217. case (int)PATH_BOX:
  218. {
  219. _pl_m_emit_op_code (R___(_plotter) O_FBOX);
  220. _pl_m_emit_float (R___(_plotter) path->p0.x);
  221. _pl_m_emit_float (R___(_plotter) path->p0.y);
  222. _pl_m_emit_float (R___(_plotter) path->p1.x);
  223. _pl_m_emit_float (R___(_plotter) path->p1.y);
  224. _pl_m_emit_terminator (S___(_plotter));
  225. _plotter->meta_pos.x = 0.5 * (path->p0.x + path->p1.x);
  226. _plotter->meta_pos.y = 0.5 * (path->p0.y + path->p1.y);
  227. }
  228. break;
  229. case (int)PATH_CIRCLE:
  230. {
  231. _pl_m_emit_op_code (R___(_plotter) O_FCIRCLE);
  232. _pl_m_emit_float (R___(_plotter) path->pc.x);
  233. _pl_m_emit_float (R___(_plotter) path->pc.y);
  234. _pl_m_emit_float (R___(_plotter) path->radius);
  235. _pl_m_emit_terminator (S___(_plotter));
  236. _plotter->meta_pos = path->pc;
  237. }
  238. break;
  239. case (int)PATH_ELLIPSE:
  240. {
  241. _pl_m_emit_op_code (R___(_plotter) O_FELLIPSE);
  242. _pl_m_emit_float (R___(_plotter) path->pc.x);
  243. _pl_m_emit_float (R___(_plotter) path->pc.y);
  244. _pl_m_emit_float (R___(_plotter) path->rx);
  245. _pl_m_emit_float (R___(_plotter) path->ry);
  246. _pl_m_emit_float (R___(_plotter) path->angle);
  247. _pl_m_emit_terminator (S___(_plotter));
  248. _plotter->meta_pos = path->pc;
  249. }
  250. break;
  251. default: /* shouldn't happen */
  252. break;
  253. }
  254. }
  255. bool
  256. _pl_m_path_is_flushable (S___(Plotter *_plotter))
  257. {
  258. return true;
  259. }
  260. void
  261. _pl_m_maybe_prepaint_segments (R___(Plotter *_plotter) int prev_num_segments)
  262. {
  263. return;
  264. }