a_attribs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Illustrator Plotter before drawing
  16. any object. It sets the relevant attributes (fill rule [if filling],
  17. cap type, join type, miter limit, line width) to what they should be. */
  18. #include "sys-defines.h"
  19. #include "extern.h"
  20. /* Pseudo line type, which we use internally when AI's line type,
  21. i.e. dashing style, is set to agree with what the user specifies with
  22. linedash(), rather than with what the user specifies with linemod().
  23. Should not equal any of our canonical line types, i.e. PL_L_SOLID etc. */
  24. #define SPECIAL_AI_LINE_TYPE 100
  25. /* AI fill rule types (in version 5 and later), indexed by our internal
  26. fill rule number (PL_FILL_ODD_WINDING/PL_FILL_NONZERO_WINDING) */
  27. static const int _ai_fill_rule[PL_NUM_FILL_RULES] =
  28. { AI_FILL_ODD_WINDING, AI_FILL_NONZERO_WINDING };
  29. /* AI (i.e. PS) join styles, indexed by internal number
  30. (miter/rd./bevel/triangular) */
  31. static const int _ai_join_style[PL_NUM_JOIN_TYPES] =
  32. { AI_LINE_JOIN_MITER, AI_LINE_JOIN_ROUND, AI_LINE_JOIN_BEVEL, AI_LINE_JOIN_ROUND };
  33. /* AI (i.e. PS) cap styles, indexed by internal number
  34. (butt/rd./project/triangular) */
  35. static const int _ai_cap_style[PL_NUM_CAP_TYPES] =
  36. { AI_LINE_CAP_BUTT, AI_LINE_CAP_ROUND, AI_LINE_CAP_PROJECT, AI_LINE_CAP_ROUND };
  37. void
  38. _pl_a_set_attributes (S___(Plotter *_plotter))
  39. {
  40. bool changed_width = false;
  41. int desired_fill_rule = _ai_fill_rule[_plotter->drawstate->fill_rule_type];
  42. double desired_ai_line_width = _plotter->drawstate->device_line_width;
  43. int desired_ai_cap_style = _ai_cap_style[_plotter->drawstate->cap_type];
  44. int desired_ai_join_style = _ai_join_style[_plotter->drawstate->join_type];
  45. double desired_ai_miter_limit = _plotter->drawstate->miter_limit;
  46. int desired_ai_line_type = _plotter->drawstate->line_type;
  47. int i;
  48. double display_size_in_points, min_dash_unit;
  49. if (_plotter->ai_version >= AI_VERSION_5
  50. && _plotter->drawstate->fill_type > 0
  51. && _plotter->ai_fill_rule_type != desired_fill_rule)
  52. {
  53. sprintf (_plotter->data->page->point, "%d XR\n", desired_fill_rule);
  54. _update_buffer (_plotter->data->page);
  55. _plotter->ai_fill_rule_type = desired_fill_rule;
  56. }
  57. if (_plotter->ai_cap_style != desired_ai_cap_style)
  58. {
  59. sprintf (_plotter->data->page->point, "%d J\n", desired_ai_cap_style);
  60. _update_buffer (_plotter->data->page);
  61. _plotter->ai_cap_style = desired_ai_cap_style;
  62. }
  63. if (_plotter->ai_join_style != desired_ai_join_style)
  64. {
  65. sprintf (_plotter->data->page->point, "%d j\n", desired_ai_join_style);
  66. _update_buffer (_plotter->data->page);
  67. _plotter->ai_join_style = desired_ai_join_style;
  68. }
  69. if (_plotter->drawstate->join_type == PL_JOIN_MITER
  70. && _plotter->ai_miter_limit != desired_ai_miter_limit)
  71. {
  72. sprintf (_plotter->data->page->point, "%.4g M\n", desired_ai_miter_limit);
  73. _update_buffer (_plotter->data->page);
  74. _plotter->ai_miter_limit = desired_ai_miter_limit;
  75. }
  76. if (_plotter->ai_line_width != desired_ai_line_width)
  77. {
  78. sprintf (_plotter->data->page->point, "%.4f w\n", desired_ai_line_width);
  79. _update_buffer (_plotter->data->page);
  80. _plotter->ai_line_width = desired_ai_line_width;
  81. changed_width = true;
  82. }
  83. if (_plotter->drawstate->dash_array_in_effect
  84. || _plotter->ai_line_type != desired_ai_line_type
  85. || (changed_width && desired_ai_line_type != PL_L_SOLID))
  86. /* must tell AI which dash array to use */
  87. {
  88. double *dashbuf;
  89. int num_dashes;
  90. double offset;
  91. if (_plotter->drawstate->dash_array_in_effect)
  92. /* have user-specified dash array */
  93. {
  94. num_dashes = _plotter->drawstate->dash_array_len;
  95. if (num_dashes > 0)
  96. /* non-solid line type */
  97. {
  98. double min_sing_val, max_sing_val;
  99. /* compute minimum singular value of user->device coordinate
  100. map, which we use as a multiplicative factor to convert
  101. line widths (cf. g_linewidth.c), dash lengths, etc. */
  102. _matrix_sing_vals (_plotter->drawstate->transform.m,
  103. &min_sing_val, &max_sing_val);
  104. dashbuf = (double *)_pl_xmalloc (num_dashes * sizeof(double));
  105. for (i = 0; i < num_dashes; i++)
  106. {
  107. double dashlen;
  108. dashlen =
  109. min_sing_val * _plotter->drawstate->dash_array[i];
  110. dashbuf[i] = dashlen;
  111. }
  112. offset = min_sing_val * _plotter->drawstate->dash_offset;
  113. }
  114. else
  115. /* zero-length dash array, i.e. solid line type */
  116. {
  117. dashbuf = NULL;
  118. offset = 0;
  119. }
  120. /* we'll keep track of the fact that AI is using a special
  121. user-specified dash array by setting the `hpgl_line_type' data
  122. member to this bogus value */
  123. desired_ai_line_type = SPECIAL_AI_LINE_TYPE;
  124. }
  125. else
  126. /* dash array not in effect, have a canonical line type instead */
  127. {
  128. if (desired_ai_line_type == PL_L_SOLID)
  129. {
  130. num_dashes = 0;
  131. dashbuf = NULL;
  132. offset = 0.0;
  133. }
  134. else
  135. {
  136. const int *dash_array;
  137. double scale;
  138. num_dashes =
  139. _pl_g_line_styles[_plotter->drawstate->line_type].dash_array_len;
  140. dashbuf = (double *)_pl_xmalloc (num_dashes * sizeof(double));
  141. /* compute PS dash array for this line type */
  142. dash_array = _pl_g_line_styles[_plotter->drawstate->line_type].dash_array;
  143. /* scale the array of integers by line width (actually by
  144. floored line width; see comments at head of file) */
  145. display_size_in_points =
  146. DMIN(_plotter->data->xmax - _plotter->data->xmin,
  147. _plotter->data->ymax - _plotter->data->ymin);
  148. min_dash_unit = (PL_MIN_DASH_UNIT_AS_FRACTION_OF_DISPLAY_SIZE
  149. * display_size_in_points);
  150. scale = DMAX(min_dash_unit,
  151. _plotter->drawstate->device_line_width);
  152. for (i = 0; i < num_dashes; i++)
  153. dashbuf[i] = scale * dash_array[i];
  154. offset = 0.0;
  155. }
  156. }
  157. /* emit dash array */
  158. sprintf (_plotter->data->page->point, "[");
  159. _update_buffer (_plotter->data->page);
  160. for (i = 0; i < num_dashes; i++)
  161. {
  162. if (i == 0)
  163. sprintf (_plotter->data->page->point, "%.4f", dashbuf[i]);
  164. else
  165. sprintf (_plotter->data->page->point, " %.4f", dashbuf[i]);
  166. _update_buffer (_plotter->data->page);
  167. }
  168. sprintf (_plotter->data->page->point, "] %.4f d\n", offset);
  169. _update_buffer (_plotter->data->page);
  170. /* Update our knowledge of AI's line type (i.e. dashing style).
  171. This new value will be one of PL_L_SOLID etc., or the pseudo value
  172. SPECIAL_AI_LINE_TYPE. */
  173. _plotter->ai_line_type = desired_ai_line_type;
  174. free (dashbuf);
  175. }
  176. return;
  177. }