h_attribs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 before drawing any path. It sets the
  16. relevant attributes of an HP-GL or HP-GL/2 plotter (fill rule, line
  17. type, cap type, join type, line width) to what they should be. */
  18. #include "sys-defines.h"
  19. #include "extern.h"
  20. /* Each dash and gap in our canonical line modes ("shortdashed",
  21. "dotdashed") etc. has length that we take to be an integer multiple of
  22. the line width. (For the integers, see g_dash2.c). Actually, when
  23. performing this computation we impose a floor on the line width
  24. (measured in the device frame, in scaled HP-GL coordinates. */
  25. #define MIN_DASH_UNIT (PL_MIN_DASH_UNIT_AS_FRACTION_OF_DISPLAY_SIZE * (HPGL_SCALED_DEVICE_RIGHT - HPGL_SCALED_DEVICE_LEFT))
  26. /* HP-GL's native line types, indexed into by our internal line style
  27. number (PL_L_SOLID/PL_L_DOTTED/
  28. PL_L_DOTDASHED/PL_L_SHORTDASHED/PL_L_LONGDASHED/PL_L_DOTDOTDASHED/PL_L_DOTDOTDOTDASHED).
  29. We use HP-GL's native line types only if we aren't emitting HP-GL/2,
  30. since HP-GL/2 supports programmatic definition of line styles. */
  31. static const int _hpgl_line_type[PL_NUM_LINE_TYPES] =
  32. { HPGL_L_SOLID, HPGL_L_DOTTED, HPGL_L_DOTDASHED,
  33. HPGL_L_SHORTDASHED, HPGL_L_LONGDASHED, HPGL_L_DOTDOTDASHED,
  34. HPGL_L_DOTDOTDOTDASHED };
  35. /* In HP-GL/2, native line type (HP-GL/2 numbering) that will be redefined
  36. programmatically as the dashed line style which we'll use. Should not
  37. be any of the preceding, and should be in range 1..8. */
  38. #define SPECIAL_HPGL_LINE_TYPE 8
  39. /* HP-GL/2 joinstyles, indexed by internal number(miter/rd./bevel/triangular)*/
  40. static const int _hpgl_join_style[] =
  41. { HPGL_JOIN_MITER_BEVEL, HPGL_JOIN_ROUND, HPGL_JOIN_BEVEL, HPGL_JOIN_TRIANGULAR };
  42. /* HP-GL/2 capstyles, indexed by internal number(butt/rd./project/triangular)*/
  43. static const int _hpgl_cap_style[] =
  44. { HPGL_CAP_BUTT, HPGL_CAP_ROUND, HPGL_CAP_PROJECT, HPGL_CAP_TRIANGULAR };
  45. #define FUZZ 0.0000001
  46. void
  47. _pl_h_set_attributes (S___(Plotter *_plotter))
  48. {
  49. double desired_hpgl_pen_width;
  50. double width, height, diagonal_p1_p2_distance;
  51. /* first, compute desired linewidth in scaled HP-GL coors (i.e. as
  52. fraction of diagonal distance between P1,P2) */
  53. width = (double)(HPGL_SCALED_DEVICE_RIGHT - HPGL_SCALED_DEVICE_LEFT);
  54. height = (double)(HPGL_SCALED_DEVICE_TOP - HPGL_SCALED_DEVICE_BOTTOM);
  55. diagonal_p1_p2_distance = sqrt (width * width + height * height);
  56. desired_hpgl_pen_width
  57. = _plotter->drawstate->device_line_width / diagonal_p1_p2_distance;
  58. /* if plotter's policy on dashing lines needs to be adjusted, do so */
  59. if (_plotter->hpgl_version == 2
  60. && (_plotter->drawstate->dash_array_in_effect
  61. || (_plotter->hpgl_line_type !=
  62. _hpgl_line_type[_plotter->drawstate->line_type])
  63. || (_plotter->hpgl_pen_width != desired_hpgl_pen_width)))
  64. /* HP-GL/2 case, and we need to emit HP-GL/2 instructions that define a
  65. new line type. Why? Several possibilities: (1) user called
  66. linedash(), in which case we always define the line type here, or
  67. (2) user called linemod() to change the canonical line style, in
  68. which case we need to define a line type here containing the
  69. corresponding dash array, or (3) user called linewidth(), in which
  70. case we need to define the new line type here because (in the
  71. canonical line style case) the dash lengths we'll use depend on the
  72. line width. */
  73. {
  74. double min_sing_val, max_sing_val;
  75. double *dashbuf, dash_cycle_length;
  76. int i, num_dashes;
  77. /* compute minimum singular value of user->device coordinate map,
  78. which we use as a multiplicative factor to convert line widths
  79. (cf. g_linewidth.c), dash lengths, etc. */
  80. _matrix_sing_vals (_plotter->drawstate->transform.m,
  81. &min_sing_val, &max_sing_val);
  82. if (_plotter->drawstate->dash_array_in_effect)
  83. /* user invoked linedash() */
  84. {
  85. num_dashes = _plotter->drawstate->dash_array_len;
  86. if (num_dashes > 0)
  87. dashbuf = (double *)_pl_xmalloc (num_dashes * sizeof(double));
  88. else
  89. dashbuf = NULL; /* solid line */
  90. dash_cycle_length = 0.0;
  91. for (i = 0; i < num_dashes; i++)
  92. {
  93. /* convert dash length to device coordinates */
  94. dashbuf[i] = min_sing_val * _plotter->drawstate->dash_array[i];
  95. dash_cycle_length += dashbuf[i];
  96. }
  97. }
  98. else
  99. /* have a canonical line type, but since this is HP-GL/2, rather
  100. than pre-HP-GL/2 or generic HP-GL, we'll implement it as a
  101. user-defined line type for accuracy */
  102. {
  103. if (_plotter->drawstate->line_type == PL_L_SOLID)
  104. {
  105. num_dashes = 0;
  106. dash_cycle_length = 0.0;
  107. dashbuf = NULL;
  108. }
  109. else
  110. {
  111. const int *dash_array;
  112. double scale;
  113. num_dashes =
  114. _pl_g_line_styles[_plotter->drawstate->line_type].dash_array_len;
  115. dashbuf = (double *)_pl_xmalloc (num_dashes * sizeof(double));
  116. /* scale the array of integers by line width (actually by
  117. floored line width; see comments at head of file) */
  118. dash_array = _pl_g_line_styles[_plotter->drawstate->line_type].dash_array;
  119. scale = DMAX(MIN_DASH_UNIT,_plotter->drawstate->device_line_width);
  120. dash_cycle_length = 0.0;
  121. for (i = 0; i < num_dashes; i++)
  122. {
  123. dashbuf[i] = scale * dash_array[i];
  124. dash_cycle_length += dashbuf[i];
  125. }
  126. }
  127. }
  128. if (num_dashes == 0 || dash_cycle_length == 0.0)
  129. /* just switch to solid line type */
  130. {
  131. strcpy (_plotter->data->page->point, "LT;");
  132. _update_buffer (_plotter->data->page);
  133. _plotter->hpgl_line_type = HPGL_L_SOLID;
  134. }
  135. else
  136. /* create user-defined line-type, and switch to it */
  137. {
  138. bool odd_length = (num_dashes & 1 ? true : false);
  139. /* create user-defined line type */
  140. sprintf (_plotter->data->page->point, "UL%d",
  141. SPECIAL_HPGL_LINE_TYPE);
  142. _update_buffer (_plotter->data->page);
  143. for (i = 0; i < num_dashes; i++)
  144. {
  145. sprintf (_plotter->data->page->point, ",%.3f",
  146. /* dash length as frac of iteration interval */
  147. 100.0 * (odd_length ? 0.5 : 1.0)
  148. * dashbuf[i] / dash_cycle_length);
  149. _update_buffer (_plotter->data->page);
  150. }
  151. if (odd_length)
  152. /* if an odd number of dashes, emit the dash array twice
  153. (HP-GL/2 doesn't handle odd-length patterns the way that
  154. Postscript does, so an even-length pattern is better) */
  155. {
  156. for (i = 0; i < num_dashes; i++)
  157. {
  158. sprintf (_plotter->data->page->point, ",%.3f",
  159. /* dash length as frac of iteration interval */
  160. 100.0 * (odd_length ? 0.5 : 1.0)
  161. * dashbuf[i] / dash_cycle_length);
  162. _update_buffer (_plotter->data->page);
  163. }
  164. }
  165. sprintf (_plotter->data->page->point, ";");
  166. _update_buffer (_plotter->data->page);
  167. /* switch to new line type */
  168. {
  169. double width, height, diagonal_p1_p2_distance;
  170. double iter_interval;
  171. /* specify iteration interval as percentage of P1-P2 distance */
  172. width = (double)(HPGL_SCALED_DEVICE_RIGHT-HPGL_SCALED_DEVICE_LEFT);
  173. height = (double)(HPGL_SCALED_DEVICE_TOP-HPGL_SCALED_DEVICE_BOTTOM);
  174. diagonal_p1_p2_distance = sqrt (width * width + height * height);
  175. iter_interval = 100 * (odd_length ? 2 : 1) * (dash_cycle_length/diagonal_p1_p2_distance);
  176. sprintf (_plotter->data->page->point, "LT%d,%.4f;",
  177. SPECIAL_HPGL_LINE_TYPE, iter_interval);
  178. _update_buffer (_plotter->data->page);
  179. if (_plotter->drawstate->dash_array_in_effect)
  180. _plotter->hpgl_line_type = SPECIAL_HPGL_LINE_TYPE;
  181. else
  182. /* keep track of plotter's line type as if it were
  183. one of the built-in ones */
  184. _plotter->hpgl_line_type =
  185. _hpgl_line_type[_plotter->drawstate->line_type];
  186. }
  187. }
  188. free (dashbuf);
  189. }
  190. /* Not HP-GL/2, so the only line types at our disposal are HP-GL's
  191. traditional line types. Check whether we need to switch. */
  192. if (_plotter->hpgl_version < 2
  193. && ((_plotter->hpgl_line_type !=
  194. _hpgl_line_type[_plotter->drawstate->line_type])
  195. || /* special case #1, mapped to "shortdashed" */
  196. (_plotter->drawstate->dash_array_in_effect
  197. && _plotter->drawstate->dash_array_len == 2
  198. && (_plotter->drawstate->dash_array[1]
  199. == _plotter->drawstate->dash_array[0]))
  200. || /* special case #2, mapped to "dotted" */
  201. (_plotter->drawstate->dash_array_in_effect
  202. && _plotter->drawstate->dash_array_len == 2
  203. && (_plotter->drawstate->dash_array[1]
  204. > (3 - FUZZ) * _plotter->drawstate->dash_array[0])
  205. && (_plotter->drawstate->dash_array[1]
  206. < (3 + FUZZ) * _plotter->drawstate->dash_array[0]))))
  207. /* switch to one of HP-GL's traditional line types */
  208. {
  209. double dash_cycle_length, iter_interval;
  210. double min_sing_val, max_sing_val;
  211. int line_type;
  212. if (_plotter->drawstate->dash_array_in_effect
  213. && _plotter->drawstate->dash_array_len == 2
  214. && (_plotter->drawstate->dash_array[1]
  215. == _plotter->drawstate->dash_array[0]))
  216. /* special case #1, user-specified dashing (equal on/off lengths):
  217. treat effectively as "shortdashed" line mode */
  218. {
  219. /* Minimum singular value is the nominal device-frame line width
  220. divided by the actual user-frame line-width (see
  221. g_linewidth.c), so it's the user->device frame conversion
  222. factor. */
  223. _matrix_sing_vals (_plotter->drawstate->transform.m,
  224. &min_sing_val, &max_sing_val);
  225. dash_cycle_length =
  226. min_sing_val * 2.0 * _plotter->drawstate->dash_array[0];
  227. line_type = PL_L_SHORTDASHED;
  228. }
  229. else if (_plotter->drawstate->dash_array_in_effect
  230. && _plotter->drawstate->dash_array_len == 2
  231. && (_plotter->drawstate->dash_array[1]
  232. > (3 - FUZZ) * _plotter->drawstate->dash_array[0])
  233. && (_plotter->drawstate->dash_array[1]
  234. < (3 + FUZZ) * _plotter->drawstate->dash_array[0]))
  235. /* special case #2, user-specified dashing (dash on length = 1/4 of
  236. cycle length): treat effectively as "dotted" line mode */
  237. {
  238. /* Minimum singular value is the nominal device-frame line width
  239. divided by the actual user-frame line-width (see
  240. g_linewidth.c), so it's the user->device frame conversion
  241. factor. */
  242. _matrix_sing_vals (_plotter->drawstate->transform.m,
  243. &min_sing_val, &max_sing_val);
  244. dash_cycle_length =
  245. min_sing_val * 2.0 * 4.0 * _plotter->drawstate->dash_array[0];
  246. line_type = PL_L_DOTTED;
  247. }
  248. else
  249. /* general case: user must have changed canonical line types by
  250. invoking linemod(); will implement new line style as one of the
  251. traditional HP-GL line types. */
  252. {
  253. const int *dash_array;
  254. int i, num_dashes;
  255. double scale;
  256. dash_array = _pl_g_line_styles[_plotter->drawstate->line_type].dash_array;
  257. num_dashes =
  258. _pl_g_line_styles[_plotter->drawstate->line_type].dash_array_len;
  259. /* compute iter interval in device coors, scaling by floored line
  260. width (see comments at head of file) */
  261. scale = DMAX(MIN_DASH_UNIT,_plotter->drawstate->device_line_width);
  262. if (scale < 1.0)
  263. scale = 1.0;
  264. dash_cycle_length = 0.0;
  265. for (i = 0; i < num_dashes; i++)
  266. dash_cycle_length += scale * dash_array[i];
  267. line_type = _plotter->drawstate->line_type;
  268. }
  269. /* compute iteration interval as percentage of P1-P2 distance */
  270. {
  271. double width, height, diagonal_p1_p2_distance;
  272. width = (double)(HPGL_SCALED_DEVICE_RIGHT-HPGL_SCALED_DEVICE_LEFT);
  273. height = (double)(HPGL_SCALED_DEVICE_TOP-HPGL_SCALED_DEVICE_BOTTOM);
  274. diagonal_p1_p2_distance = sqrt (width * width + height * height);
  275. iter_interval = 100 * (dash_cycle_length/diagonal_p1_p2_distance);
  276. }
  277. switch (line_type)
  278. {
  279. case PL_L_SOLID:
  280. /* "solid" */
  281. strcpy (_plotter->data->page->point, "LT;");
  282. break;
  283. case PL_L_DOTTED:
  284. /* "dotted": emulate dots by selecting shortdashed pattern with a
  285. short iteration interval */
  286. sprintf (_plotter->data->page->point,
  287. "LT%d,%.4f;",
  288. HPGL_L_SHORTDASHED,
  289. 0.5 * iter_interval);
  290. break;
  291. case PL_L_DOTDOTDOTDASHED:
  292. /* not a native line type before HP-GL/2; use "dotdotdashed" */
  293. sprintf (_plotter->data->page->point,
  294. "LT%d,%.4f;",
  295. HPGL_L_DOTDOTDASHED,
  296. iter_interval);
  297. break;
  298. default:
  299. sprintf (_plotter->data->page->point,
  300. "LT%d,%.4f;",
  301. _hpgl_line_type[_plotter->drawstate->line_type],
  302. iter_interval);
  303. }
  304. _update_buffer (_plotter->data->page);
  305. _plotter->hpgl_line_type =
  306. _hpgl_line_type[_plotter->drawstate->line_type];
  307. }
  308. /* if plotter's line attributes don't agree with what they should be,
  309. adjust them (HP-GL/2 only) */
  310. if (_plotter->hpgl_version == 2)
  311. {
  312. if ((_plotter->hpgl_cap_style
  313. != _hpgl_cap_style[_plotter->drawstate->cap_type])
  314. || (_plotter->hpgl_join_style
  315. != _hpgl_join_style[_plotter->drawstate->join_type]))
  316. {
  317. sprintf (_plotter->data->page->point, "LA1,%d,2,%d;",
  318. _hpgl_cap_style[_plotter->drawstate->cap_type],
  319. _hpgl_join_style[_plotter->drawstate->join_type]);
  320. _update_buffer (_plotter->data->page);
  321. _plotter->hpgl_cap_style =
  322. _hpgl_cap_style[_plotter->drawstate->cap_type];
  323. _plotter->hpgl_join_style =
  324. _hpgl_join_style[_plotter->drawstate->join_type];
  325. }
  326. }
  327. /* if plotter's miter limit doesn't agree with what it should be, update
  328. it (HP-GL/2 only) */
  329. if (_plotter->hpgl_version == 2
  330. && _plotter->hpgl_miter_limit != _plotter->drawstate->miter_limit)
  331. {
  332. double new_limit = _plotter->drawstate->miter_limit;
  333. int new_limit_integer;
  334. if (new_limit > 32767.0) /* clamp */
  335. new_limit = 32767.0;
  336. else if (new_limit < 1.0)
  337. new_limit = 1.0;
  338. new_limit_integer = (int)new_limit; /* floor */
  339. sprintf (_plotter->data->page->point, "LA3,%d;", new_limit_integer);
  340. _update_buffer (_plotter->data->page);
  341. _plotter->hpgl_miter_limit = _plotter->drawstate->miter_limit;
  342. }
  343. /* if plotter's pen width doesn't agree with what it should be (i.e. the
  344. device-frame version of our line width), update it (HP-GL/2 only) */
  345. if (_plotter->hpgl_version == 2)
  346. {
  347. if (_plotter->hpgl_pen_width != desired_hpgl_pen_width)
  348. {
  349. sprintf (_plotter->data->page->point, "PW%.4f;",
  350. 100.0 * desired_hpgl_pen_width);
  351. _update_buffer (_plotter->data->page);
  352. _plotter->hpgl_pen_width = desired_hpgl_pen_width;
  353. }
  354. }
  355. }