x_attribs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 internal method is invoked by an XDrawablePlotter (or XPlotter)
  16. before drawing any polyline. It sets the relevant attributes in our X11
  17. graphics contexts (the one used for drawing, and the one used for
  18. filling) to what they should be. This includes line type, cap type,
  19. join type, line width, and fill rule.
  20. The GC's are elements of the X11-specific part of any libplot drawing
  21. state. The X11-specific part also includes `non-opaque' representations
  22. of the attributes, which can be easily queried. They are updated too.
  23. Any invoker should pass an argument to this method, indicating which of
  24. the two graphics contexts (the one used for drawing, and the one used
  25. for filling) should be updated. The only attribute we set in the latter
  26. is the fill rule; all the other attributes listed above are set in the
  27. former. */
  28. #include "sys-defines.h"
  29. #include "extern.h"
  30. /* The length of each dash must fit in an unsigned char (X11 convention) */
  31. #define MAX_DASH_LENGTH 255
  32. /* ARGS: x_gc_type specifies which of our X GC's to modify */
  33. void
  34. _pl_x_set_attributes (R___(Plotter *_plotter) int x_gc_type)
  35. {
  36. int i;
  37. if (_plotter->x_drawable1 == (Drawable)NULL
  38. && _plotter->x_drawable2 == (Drawable)NULL)
  39. /* no drawables, so GC's must not have been created (see
  40. x_savestate.c); do nothing */
  41. return;
  42. if (x_gc_type == X_GC_FOR_DRAWING)
  43. /* update attributes in GC used for drawing */
  44. {
  45. XGCValues gcv;
  46. bool have_dash_list = false;
  47. unsigned char *dash_list = (unsigned char *)NULL;
  48. int dash_list_len = 0, dash_offset = 0;
  49. if (_plotter->drawstate->dash_array_in_effect)
  50. /* have user-specified dash array */
  51. {
  52. dash_list_len = _plotter->drawstate->dash_array_len;
  53. if (dash_list_len > 0)
  54. {
  55. bool odd_length;
  56. double min_sing_val, max_sing_val;
  57. int i, dash_cycle_length;
  58. /* compute minimum singular value of user->device coordinate
  59. map, which we use as a multiplicative factor to convert line
  60. widths (cf. g_linewidth.c), dash lengths, etc. */
  61. _matrix_sing_vals (_plotter->drawstate->transform.m,
  62. &min_sing_val, &max_sing_val);
  63. odd_length = (dash_list_len & 1 ? true : false);
  64. dash_list = (unsigned char *)_pl_xmalloc ((odd_length ? 2 : 1) * dash_list_len * sizeof(unsigned char));
  65. have_dash_list = true; /* will free.. */
  66. dash_cycle_length = 0;
  67. for (i = 0; i < dash_list_len; i++)
  68. {
  69. double unrounded_dashlen;
  70. int dashlen;
  71. unrounded_dashlen =
  72. min_sing_val * _plotter->drawstate->dash_array[i];
  73. dashlen = IROUND(unrounded_dashlen);
  74. dashlen = IMAX(dashlen, 1);
  75. dashlen = IMIN(dashlen, MAX_DASH_LENGTH);
  76. /* convert dash length, int -> unsigned char */
  77. dash_list[i] = (unsigned int)dashlen;
  78. dash_cycle_length += dashlen;
  79. if (odd_length)
  80. {
  81. dash_list[dash_list_len + i] = (unsigned int)dashlen;
  82. dash_cycle_length += dashlen;
  83. }
  84. }
  85. if (odd_length)
  86. dash_list_len *= 2;
  87. dash_offset = IROUND(min_sing_val * _plotter->drawstate->dash_offset);
  88. if (dash_cycle_length > 0)
  89. /* choose an offset in range 0..dash_cycle_length-1 */
  90. {
  91. while (dash_offset < 0)
  92. dash_offset += dash_cycle_length;
  93. dash_offset %= dash_cycle_length;
  94. }
  95. gcv.line_style = LineOnOffDash;
  96. }
  97. else /* no dashes, will draw as solid line */
  98. gcv.line_style = LineSolid;
  99. }
  100. else
  101. /* have one of the canonical line types */
  102. {
  103. if (_plotter->drawstate->line_type != PL_L_SOLID)
  104. {
  105. const int *dash_array;
  106. int i, scale;
  107. dash_list_len = _pl_g_line_styles[_plotter->drawstate->line_type].dash_array_len;
  108. dash_array = _pl_g_line_styles[_plotter->drawstate->line_type].dash_array;
  109. /* scale by line width in terms of pixels, if nonzero */
  110. scale = _plotter->drawstate->quantized_device_line_width;
  111. if (scale <= 0)
  112. scale = 1;
  113. dash_list = (unsigned char *)_pl_xmalloc (PL_MAX_DASH_ARRAY_LEN * sizeof(unsigned char));
  114. have_dash_list = true; /* will free.. */
  115. for (i = 0; i < dash_list_len; i++)
  116. {
  117. int dashlen;
  118. dashlen = scale * dash_array[i];
  119. dashlen = IMAX(dashlen, 1);
  120. dashlen = IMIN(dashlen, MAX_DASH_LENGTH);
  121. dash_list[i] = (unsigned int)dashlen; /* int->unsigned char*/
  122. }
  123. /* use a non-solid line style */
  124. gcv.line_style = LineOnOffDash;
  125. dash_offset = 0;
  126. }
  127. else /* no dash list */
  128. {
  129. /* use a solid line style */
  130. gcv.line_style = LineSolid;
  131. }
  132. }
  133. /* update dash style attributes (dash offset and dash list) */
  134. if (have_dash_list)
  135. {
  136. bool do_it = false;
  137. if (_plotter->drawstate->x_gc_dash_offset != dash_offset
  138. || _plotter->drawstate->x_gc_dash_list_len != dash_list_len)
  139. do_it = true;
  140. if (do_it == false)
  141. {
  142. for (i = 0; i < dash_list_len; i++)
  143. {
  144. if ((unsigned char)_plotter->drawstate->x_gc_dash_list[i] != dash_list[i])
  145. {
  146. do_it = true;
  147. break;
  148. }
  149. }
  150. }
  151. if (do_it)
  152. {
  153. /* change the GC used for drawing */
  154. XSetDashes (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
  155. dash_offset, (char *)dash_list, dash_list_len);
  156. /* update non-opaque information on dash style, by installing
  157. dash_list as our `non-opaque dash list' */
  158. /* free former non-opaque dash list if any */
  159. if (_plotter->drawstate->x_gc_dash_list_len > 0)
  160. free ((char *)_plotter->drawstate->x_gc_dash_list);
  161. _plotter->drawstate->x_gc_dash_list = (char *)dash_list;
  162. _plotter->drawstate->x_gc_dash_list_len = dash_list_len;
  163. _plotter->drawstate->x_gc_dash_offset = dash_offset;
  164. }
  165. else
  166. free (dash_list);
  167. }
  168. /* update line style attribute */
  169. if (_plotter->drawstate->x_gc_line_style != gcv.line_style)
  170. {
  171. /* change the GC used for drawing */
  172. XChangeGC (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
  173. GCLineStyle, &gcv);
  174. /* update non-opaque line-style element */
  175. _plotter->drawstate->x_gc_line_style = gcv.line_style;
  176. }
  177. /* update cap style attribute */
  178. switch (_plotter->drawstate->cap_type)
  179. {
  180. case PL_CAP_BUTT:
  181. default:
  182. gcv.cap_style = CapButt;
  183. break;
  184. case PL_CAP_ROUND:
  185. gcv.cap_style = CapRound;
  186. break;
  187. case PL_CAP_PROJECT:
  188. gcv.cap_style = CapProjecting;
  189. break;
  190. case PL_CAP_TRIANGULAR: /* not supported by X11 */
  191. gcv.cap_style = CapRound;
  192. break;
  193. }
  194. if (_plotter->drawstate->x_gc_cap_style != gcv.cap_style)
  195. {
  196. /* change the GC used for drawing */
  197. XChangeGC (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
  198. GCCapStyle, &gcv);
  199. /* update non-opaque cap style element */
  200. _plotter->drawstate->x_gc_cap_style = gcv.cap_style;
  201. }
  202. /* update join style attribute */
  203. switch (_plotter->drawstate->join_type)
  204. {
  205. case PL_JOIN_MITER:
  206. default:
  207. gcv.join_style = JoinMiter;
  208. break;
  209. case PL_JOIN_ROUND:
  210. gcv.join_style = JoinRound;
  211. break;
  212. case PL_JOIN_BEVEL:
  213. gcv.join_style = JoinBevel;
  214. break;
  215. case PL_JOIN_TRIANGULAR: /* not supported by X11 */
  216. gcv.join_style = JoinRound;
  217. break;
  218. }
  219. if (_plotter->drawstate->x_gc_join_style != gcv.join_style)
  220. {
  221. /* change the GC used for drawing */
  222. XChangeGC (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
  223. GCJoinStyle, &gcv);
  224. /* update non-opaque join style element */
  225. _plotter->drawstate->x_gc_join_style = gcv.join_style;
  226. }
  227. /* update line width attribute */
  228. gcv.line_width = _plotter->drawstate->quantized_device_line_width;
  229. if (_plotter->drawstate->x_gc_line_width != gcv.line_width)
  230. {
  231. /* change the GC used for drawing */
  232. XChangeGC (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
  233. GCLineWidth, &gcv);
  234. /* update non-opaque line-width element */
  235. _plotter->drawstate->x_gc_line_width = gcv.line_width;
  236. }
  237. }
  238. else if (x_gc_type == X_GC_FOR_FILLING)
  239. /* update attributes in GC used for filling */
  240. {
  241. XGCValues gcv;
  242. /* update fill rule attribute */
  243. switch (_plotter->drawstate->fill_rule_type)
  244. {
  245. case PL_FILL_ODD_WINDING:
  246. default:
  247. gcv.fill_rule = EvenOddRule;
  248. break;
  249. case PL_FILL_NONZERO_WINDING:
  250. gcv.fill_rule = WindingRule;
  251. break;
  252. }
  253. if (_plotter->drawstate->x_gc_fill_rule != gcv.fill_rule)
  254. {
  255. /* change the GC used for filling */
  256. XChangeGC (_plotter->x_dpy, _plotter->drawstate->x_gc_fill,
  257. GCFillRule, &gcv);
  258. /* update non-opaque fill-rule element */
  259. _plotter->drawstate->x_gc_fill_rule = gcv.fill_rule;
  260. }
  261. }
  262. return;
  263. }