g_attrib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 linemod method, which is a standard part of
  16. libplot. It sets a drawing attribute: the line style used in subsequent
  17. drawing operations.
  18. This version searches for the specified line style in a table of known
  19. line styles (see g_dash2.c). */
  20. /* This file also contains the capmod method, which is a GNU extension to
  21. libplot. It sets a drawing attribute: the cap mode used when
  22. subsequently drawing open paths. */
  23. /* This file also contains the joinmod method, which is a GNU extension to
  24. libplot. It sets a drawing attribute: the join mode used when
  25. subsequently drawing paths consisting of more than a single segment. */
  26. /* This file also contains the miterlimit method, which is a GNU extension
  27. to libplot. It sets a drawing attribute: the miter limit of polylines
  28. subsequently drawn on the display device. This attribute controls the
  29. treatment of corners when the join mode is set to "miter". */
  30. /* This file contains the orientation method, which is a GNU extension to
  31. libplot. It sets a drawing attribute: whether or not closed paths of
  32. the three built-in types (rectangles, circles, ellipses) should be drawn
  33. counterclockwise or clockwise. The former is the default. */
  34. /* This file contains the fillmod method, which is a GNU extension to
  35. libplot. It sets a drawing attribute: the fill rule used when
  36. subsequently drawing filled objects, i.e., the rule used to determine
  37. which points are `inside'.
  38. In principle, both the `odd winding number' rule and the `nonzero
  39. winding number' rule are supported. The former is the default. */
  40. #include "sys-defines.h"
  41. #include "extern.h"
  42. int
  43. _API_linemod (R___(Plotter *_plotter) const char *s)
  44. {
  45. bool matched = false;
  46. char *line_mode;
  47. int i;
  48. if (!_plotter->data->open)
  49. {
  50. _plotter->error (R___(_plotter)
  51. "linemod: invalid operation");
  52. return -1;
  53. }
  54. _API_endpath (S___(_plotter)); /* flush path if any */
  55. /* null pointer resets to default */
  56. if ((!s) || !strcmp(s, "(null)"))
  57. s = _default_drawstate.line_mode;
  58. free ((char *)_plotter->drawstate->line_mode);
  59. line_mode = (char *)_pl_xmalloc (strlen (s) + 1);
  60. strcpy (line_mode, s);
  61. _plotter->drawstate->line_mode = line_mode;
  62. if (strcmp (s, "disconnected") == 0)
  63. /* we'll implement disconnected lines by drawing a filled circle at
  64. each path join point; see g_endpath.c */
  65. {
  66. _plotter->drawstate->line_type = PL_L_SOLID;
  67. _plotter->drawstate->points_are_connected = false;
  68. matched = true;
  69. }
  70. else /* search table of libplot's builtin line types */
  71. for (i = 0; i < PL_NUM_LINE_TYPES; i++)
  72. {
  73. if (strcmp (s, _pl_g_line_styles[i].name) == 0)
  74. {
  75. _plotter->drawstate->line_type =
  76. _pl_g_line_styles[i].type;
  77. _plotter->drawstate->points_are_connected = true;
  78. matched = true;
  79. break;
  80. }
  81. }
  82. if (matched == false)
  83. /* don't recognize, silently switch to default mode */
  84. _API_linemod (R___(_plotter) _default_drawstate.line_mode);
  85. /* for future paths, use builtin line style rather than user-specified
  86. dash array */
  87. _plotter->drawstate->dash_array_in_effect = false;
  88. return 0;
  89. }
  90. int
  91. _API_capmod (R___(Plotter *_plotter) const char *s)
  92. {
  93. char *cap_mode;
  94. if (!_plotter->data->open)
  95. {
  96. _plotter->error (R___(_plotter)
  97. "capmod: invalid operation");
  98. return -1;
  99. }
  100. _API_endpath (S___(_plotter)); /* flush path if any */
  101. /* null pointer resets to default */
  102. if ((!s) || !strcmp(s, "(null)"))
  103. s = _default_drawstate.cap_mode;
  104. free ((char *)_plotter->drawstate->cap_mode);
  105. cap_mode = (char *)_pl_xmalloc (strlen (s) + 1);
  106. strcpy (cap_mode, s);
  107. _plotter->drawstate->cap_mode = cap_mode;
  108. /* The following four cap types are now standard. */
  109. if (strcmp( s, "butt") == 0)
  110. _plotter->drawstate->cap_type = PL_CAP_BUTT;
  111. else if (strcmp( s, "round") == 0)
  112. _plotter->drawstate->cap_type = PL_CAP_ROUND;
  113. else if (strcmp( s, "projecting") == 0)
  114. _plotter->drawstate->cap_type = PL_CAP_PROJECT;
  115. else if (strcmp( s, "triangular") == 0)
  116. _plotter->drawstate->cap_type = PL_CAP_TRIANGULAR;
  117. else
  118. /* don't recognize, silently switch to default mode */
  119. return _API_capmod (R___(_plotter) _default_drawstate.cap_mode);
  120. return 0;
  121. }
  122. int
  123. _API_joinmod (R___(Plotter *_plotter) const char *s)
  124. {
  125. char *join_mode;
  126. if (!_plotter->data->open)
  127. {
  128. _plotter->error (R___(_plotter)
  129. "joinmod: invalid operation");
  130. return -1;
  131. }
  132. _API_endpath (S___(_plotter)); /* flush path if any */
  133. /* null pointer resets to default */
  134. if ((!s) || !strcmp(s, "(null)"))
  135. s = _default_drawstate.join_mode;
  136. free ((char *)_plotter->drawstate->join_mode);
  137. join_mode = (char *)_pl_xmalloc (strlen (s) + 1);
  138. strcpy (join_mode, s);
  139. _plotter->drawstate->join_mode = join_mode;
  140. /* The following four join types are now standard. */
  141. if (strcmp( s, "miter") == 0)
  142. _plotter->drawstate->join_type = PL_JOIN_MITER;
  143. else if (strcmp( s, "mitre") == 0)
  144. _plotter->drawstate->join_type = PL_JOIN_MITER;
  145. else if (strcmp( s, "round") == 0)
  146. _plotter->drawstate->join_type = PL_JOIN_ROUND;
  147. else if (strcmp( s, "bevel") == 0)
  148. _plotter->drawstate->join_type = PL_JOIN_BEVEL;
  149. else if (strcmp( s, "triangular") == 0)
  150. _plotter->drawstate->join_type = PL_JOIN_TRIANGULAR;
  151. else
  152. /* unknown, so silently switch to default mode (via recursive call) */
  153. return _API_joinmod (R___(_plotter) _default_drawstate.join_mode);
  154. return 0;
  155. }
  156. /* Below is the miterlimit method, which is a GNU extension to libplot. It
  157. sets a drawing attribute: the miter limit of polylines subsequently
  158. drawn on the display device. This attribute controls the treatment of
  159. corners when the join mode is set to "miter".
  160. At a join point of a wide polyline, the `miter length' is defined to be
  161. the distance between the inner corner and the outer corner. The miter
  162. limit is the maximum value that can be tolerated for the miter length
  163. divided by the line width. If this value is exceeded, the miter will be
  164. `cut off': the "bevel" join mode will be used instead.
  165. Examples of typical values for the miter limit are 10.43 (the
  166. unchangeable value used by the X Window System, which cuts off miters at
  167. join angles less than 11 degrees), 5.0 (the default value used by
  168. HP-GL/2 and PCL 5 devices, which cuts off miters at join angles less
  169. than 22.1 degrees), 2.0 (cuts off miters at join angles less than 60
  170. degrees), 1.414 (cuts off miters at join angles less than 90 degrees),
  171. and 1.0 (cuts off all miters).
  172. In general, the miter limit is the cosecant of one-half of the minimum
  173. join angle for mitering, and values less than 1.0 are meaningless. For
  174. example, 10.43 is csc((11 degrees)/2) = 1 / sin((11 degrees)/2).
  175. Mitering is allowed to take place if 1 / sin(theta/2) <= MITERLIMIT,
  176. i.e. sin(theta/2) >= 1/MITERLIMIT, where theta > 0 is the join angle. */
  177. int
  178. _API_fmiterlimit (R___(Plotter *_plotter) double new_miter_limit)
  179. {
  180. if (!_plotter->data->open)
  181. {
  182. _plotter->error (R___(_plotter)
  183. "flinewidth: invalid operation");
  184. return -1;
  185. }
  186. _API_endpath (S___(_plotter)); /* flush path if any */
  187. if (new_miter_limit < 1.0) /* reset to default */
  188. new_miter_limit = PL_DEFAULT_MITER_LIMIT;
  189. /* set the new miter limit in the drawing state */
  190. _plotter->drawstate->miter_limit = new_miter_limit;
  191. return 0;
  192. }
  193. /* Below is the orientation method, which is a GNU extension to libplot.
  194. It sets a drawing attribute: whether or not closed paths of the three
  195. built-in types (rectangles, circles, ellipses) should be drawn
  196. counterclockwise or clockwise. The former is the default. */
  197. /* This attribute-setting method, though path-related, doesn't invoke
  198. _API_endpath() to end the compound path under construction (if any).
  199. That's because it makes sense to invoke this method between the simple
  200. paths of a compound path. */
  201. int
  202. _API_orientation (R___(Plotter *_plotter) int direction)
  203. {
  204. if (!_plotter->data->open)
  205. {
  206. _plotter->error (R___(_plotter)
  207. "orientation: invalid operation");
  208. return -1;
  209. }
  210. if (direction != 1 && direction != -1)
  211. /* OOB switches to default */
  212. direction = _default_drawstate.orientation;
  213. _plotter->drawstate->orientation = direction;
  214. return 0;
  215. }
  216. /* Below is the fillmod method, which is a GNU extension to libplot. It
  217. sets a drawing attribute: the fill rule used when subsequently drawing
  218. filled objects, i.e., the rule used to determine which points are
  219. `inside'.
  220. In principle, both the `odd winding number' rule and the `nonzero
  221. winding number' rule are supported. The former is the default. */
  222. int
  223. _API_fillmod (R___(Plotter *_plotter) const char *s)
  224. {
  225. const char *default_s;
  226. char *fill_rule;
  227. if (!_plotter->data->open)
  228. {
  229. _plotter->error (R___(_plotter)
  230. "fillmod: invalid operation");
  231. return -1;
  232. }
  233. _API_endpath (S___(_plotter)); /* flush path if any */
  234. /* determine default fill rule (can't just read from default drawing
  235. state, because not all Plotters support both standard rules) */
  236. default_s = _default_drawstate.fill_rule;
  237. if (strcmp (default_s, "even-odd") == 0
  238. && _plotter->data->have_odd_winding_fill == 0)
  239. default_s = "nonzero-winding";
  240. else if (strcmp (default_s, "nonzero-winding") == 0
  241. && _plotter->data->have_nonzero_winding_fill == 0)
  242. default_s = "even-odd";
  243. /* null pointer resets to default */
  244. if ((!s) || !strcmp(s, "(null)"))
  245. s = default_s;
  246. free ((char *)_plotter->drawstate->fill_rule);
  247. fill_rule = (char *)_pl_xmalloc (strlen (s) + 1);
  248. strcpy (fill_rule, s);
  249. _plotter->drawstate->fill_rule = fill_rule;
  250. if ((strcmp (s, "even-odd") == 0 || strcmp (s, "alternate") == 0)
  251. && _plotter->data->have_odd_winding_fill)
  252. _plotter->drawstate->fill_rule_type = PL_FILL_ODD_WINDING;
  253. else if ((strcmp (s, "nonzero-winding") == 0 || strcmp (s, "winding") == 0)
  254. && _plotter->data->have_nonzero_winding_fill)
  255. _plotter->drawstate->fill_rule_type = PL_FILL_NONZERO_WINDING;
  256. else
  257. /* unknown, so silently switch to default fill rule (via recursive call) */
  258. _API_fillmod (R___(_plotter) default_s);
  259. return 0;
  260. }