a_text.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 AI-driver-specific version of the low-level
  16. paint_text_string() method, which is called to plot a label in the
  17. current font (either PS or PCL), at the current fontsize and textangle.
  18. The label is just a string: no control codes (font switching or
  19. sub/superscripts).
  20. The width of the string in user units is returned. */
  21. #include "sys-defines.h"
  22. #include "extern.h"
  23. #define GOOD_PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))
  24. /* This prints a single-font, single-font-size label. When this is called,
  25. the current point is on the intended baseline of the label. */
  26. /* ARGS: h_just = horiz justification, PL_JUST_LEFT, CENTER, or RIGHT
  27. v_just = vert justificattion, PL_JUST_TOP, BASE, or BOTTOM */
  28. double
  29. _pl_a_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
  30. {
  31. int i, master_font_index;
  32. int justify_code;
  33. double width;
  34. unsigned char *ptr;
  35. double theta, costheta, sintheta;
  36. double norm;
  37. double dx0,dy0,dx1,dy1,dx2,dy2,dx3,dy3;
  38. double font_ascent, font_descent, up, down;
  39. double user_font_size = _plotter->drawstate->true_font_size;
  40. double device_font_size;
  41. double user_text_transformation_matrix[6];
  42. double text_transformation_matrix[6];
  43. double lshift;
  44. bool pcl_font;
  45. /* sanity check; this routine supports only baseline positioning */
  46. if (v_just != PL_JUST_BASE)
  47. return 0.0;
  48. /* if empty string, nothing to do */
  49. if (*s == (unsigned char)'\0')
  50. return 0.0;
  51. /* sanity check */
  52. if (_plotter->drawstate->font_type != PL_F_POSTSCRIPT
  53. && _plotter->drawstate->font_type != PL_F_PCL)
  54. return 0.0;
  55. pcl_font = (_plotter->drawstate->font_type == PL_F_PCL ? true : false);
  56. /* compute index of font in master table of PS [or PCL] fonts, in
  57. g_fontdb.c */
  58. if (pcl_font) /* one of the 45 standard PCL fonts */
  59. master_font_index =
  60. (_pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  61. else /* one of the 35 standard PS fonts */
  62. master_font_index =
  63. (_pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  64. /* font ascent and descent (taken from the font's bounding box) */
  65. if (pcl_font)
  66. {
  67. font_ascent = (double)((_pl_g_pcl_font_info[master_font_index]).font_ascent);
  68. font_descent = (double)((_pl_g_pcl_font_info[master_font_index]).font_descent);
  69. }
  70. else /* PS font */
  71. {
  72. font_ascent = (double)((_pl_g_ps_font_info[master_font_index]).font_ascent);
  73. font_descent = (double)((_pl_g_ps_font_info[master_font_index]).font_descent);
  74. }
  75. up = user_font_size * font_ascent / 1000.0;
  76. down = user_font_size * font_descent / 1000.0;
  77. /* label rotation angle in radians, in user frame */
  78. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  79. sintheta = sin (theta);
  80. costheta = cos (theta);
  81. /* this transformation matrix rotates, and translates; it maps (0,0) to
  82. the origin of the string, in user coordinates */
  83. user_text_transformation_matrix[0] = costheta;
  84. user_text_transformation_matrix[1] = sintheta;
  85. user_text_transformation_matrix[2] = - sintheta;
  86. user_text_transformation_matrix[3] = costheta;
  87. user_text_transformation_matrix[4] = _plotter->drawstate->pos.x;
  88. user_text_transformation_matrix[5] = _plotter->drawstate->pos.y;
  89. /* Construct a temporary matrix that rotates, translates, and then maps
  90. to device coordinates. This matrix transforms from a frame in which
  91. nominal character sizes are roughly 1 unit in the horizontal and
  92. vertical directions, to device coordinates. */
  93. _matrix_product (user_text_transformation_matrix,
  94. _plotter->drawstate->transform.m,
  95. text_transformation_matrix);
  96. /* We need to extract a quantity we can call a font size in device
  97. coordinates, for the benefit of AI. (AI needs to retrieve a font, and
  98. transform it.)
  99. We define this to be user_font_size (the nominal font size in user
  100. coordinates), times the norm of the linear tranformation contained in
  101. the temporary matrix we just constructed (the magnitude of its larger
  102. singular value). Recall that for any square matrix M, the singular
  103. values are the square roots of the eigenvalues of the symmetric matrix
  104. M^t M. */
  105. norm = _matrix_norm (text_transformation_matrix);
  106. if (norm == 0.0) /* avoid division by zero */
  107. return 0.0;
  108. device_font_size = norm * user_font_size;
  109. /* Now scale the text transformation matrix so that the linear
  110. transformation contained in it has unit norm (if there is no shearing,
  111. it will just be a rotation; if there is no rotation either, it will be
  112. the identity matrix). */
  113. for (i = 0; i < 4; i++)
  114. text_transformation_matrix[i] /= norm;
  115. /* AI directive: begin `point text' object */
  116. strcpy (_plotter->data->page->point, "0 To\n");
  117. _update_buffer (_plotter->data->page);
  118. /* output text transformation matrix */
  119. for (i = 0; i < 6; i++)
  120. {
  121. sprintf (_plotter->data->page->point, "%.4f ",
  122. text_transformation_matrix[i]);
  123. _update_buffer (_plotter->data->page);
  124. }
  125. strcpy (_plotter->data->page->point, "0 Tp\nTP\n");
  126. _update_buffer (_plotter->data->page);
  127. /* set render mode: fill text, rather than several other possibilities */
  128. strcpy (_plotter->data->page->point, "0 Tr\n");
  129. _update_buffer (_plotter->data->page);
  130. /* set AI's fill color to be the same as libplot's notion of pen color
  131. (since letters in label will be drawn as filled outlines) */
  132. _pl_a_set_fill_color (R___(_plotter) true);
  133. /* set AI's pen color also, in particular set it to be the same as
  134. libplot's notion of pen color (even though we'll be filling, not
  135. stroking); this is a convenience for AI users who may wish e.g. to
  136. switch from filling letter outlines to stroking them */
  137. _pl_a_set_pen_color (S___(_plotter)); /* emit AI directive */
  138. /* AI directive: set font name and size */
  139. {
  140. const char *ps_name;
  141. if (pcl_font) /* one of the 45 PCL fonts */
  142. ps_name = _pl_g_pcl_font_info[master_font_index].ps_name;
  143. else /* one of the 35 PS fonts */
  144. ps_name = _pl_g_ps_font_info[master_font_index].ps_name;
  145. /* specify font name (underscore indicates reencoding), font size */
  146. sprintf (_plotter->data->page->point, "/_%s %.4f Tf\n",
  147. ps_name, device_font_size);
  148. _update_buffer (_plotter->data->page);
  149. }
  150. /* set line horizontal expansion factor, in percent */
  151. strcpy (_plotter->data->page->point, "100 Tz\n");
  152. _update_buffer (_plotter->data->page);
  153. /* NO track kerning, please */
  154. strcpy (_plotter->data->page->point, "0 Tt\n");
  155. _update_buffer (_plotter->data->page);
  156. /* turn off pairwise kerning (currently, a libplot convention) */
  157. strcpy (_plotter->data->page->point, "0 TA\n");
  158. _update_buffer (_plotter->data->page);
  159. /* turn off ALL inter-character spacing */
  160. strcpy (_plotter->data->page->point, "0 0 0 TC\n");
  161. _update_buffer (_plotter->data->page);
  162. /* use the default inter-word spacing; no more, no less */
  163. strcpy (_plotter->data->page->point, "100 100 100 TW\n");
  164. _update_buffer (_plotter->data->page);
  165. /* no indentation at beginning of `paragraphs' */
  166. strcpy (_plotter->data->page->point, "0 0 0 Ti\n");
  167. _update_buffer (_plotter->data->page);
  168. /* specify justification */
  169. switch (h_just)
  170. {
  171. case PL_JUST_LEFT:
  172. default:
  173. justify_code = 0;
  174. break;
  175. case PL_JUST_CENTER:
  176. justify_code = 1;
  177. break;
  178. case PL_JUST_RIGHT:
  179. justify_code = 2;
  180. break;
  181. }
  182. sprintf (_plotter->data->page->point, "%d Ta\n", justify_code);
  183. _update_buffer (_plotter->data->page);
  184. /* no hanging quotation marks */
  185. strcpy (_plotter->data->page->point, "0 Tq\n");
  186. _update_buffer (_plotter->data->page);
  187. /* no leading between lines of a paragraph or between paragraphs */
  188. strcpy (_plotter->data->page->point, "0 0 Tl\n");
  189. _update_buffer (_plotter->data->page);
  190. /* compute width of the substring in user units (used below in
  191. constructing a bounding box) */
  192. width = _plotter->get_text_width (R___(_plotter) s);
  193. /* for computing bounding box, compute justification-dependent leftward
  194. shift, as fraction of label width */
  195. switch (h_just)
  196. {
  197. case PL_JUST_LEFT:
  198. default:
  199. lshift = 0.0;
  200. break;
  201. case PL_JUST_CENTER:
  202. lshift = 0.5;
  203. break;
  204. case PL_JUST_RIGHT:
  205. lshift = 1.0;
  206. break;
  207. }
  208. /* to compute an EPS-style bounding box, first compute offsets to the
  209. four vertices of the smallest rectangle containing the string */
  210. dx0 = costheta * (- lshift) * width - sintheta * (-down);
  211. dy0 = sintheta * (- lshift) * width + costheta * (-down);
  212. dx1 = costheta * (- lshift) * width - sintheta * up;
  213. dy1 = sintheta * (- lshift) * width + costheta * up;
  214. dx2 = costheta * (1.0 - lshift) * width - sintheta * (-down);
  215. dy2 = sintheta * (1.0 - lshift) * width + costheta * (-down);
  216. dx3 = costheta * (1.0 - lshift) * width - sintheta * up;
  217. dy3 = sintheta * (1.0 - lshift) * width + costheta * up;
  218. /* record that we're using all four vertices (args of _update_bbox() are in
  219. device units, not user units) */
  220. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0),
  221. YD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0));
  222. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1),
  223. YD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1));
  224. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2),
  225. YD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2));
  226. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3),
  227. YD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3));
  228. /* output string as a PS string (i.e. surrounded by parentheses) */
  229. ptr = (unsigned char *)_plotter->data->page->point;
  230. *ptr++ = '(';
  231. while (*s)
  232. {
  233. switch (*s)
  234. {
  235. case '(': /* for PS, escape ()/ */
  236. case ')':
  237. case '\\':
  238. *ptr++ = (unsigned char)'\\';
  239. *ptr++ = *s++;
  240. break;
  241. default:
  242. if GOOD_PRINTABLE_ASCII (*s)
  243. *ptr++ = *s++;
  244. else
  245. {
  246. sprintf ((char *)ptr, "\\%03o", (unsigned int)*s);
  247. ptr += 4;
  248. s++;
  249. }
  250. break;
  251. }
  252. }
  253. *ptr++ = ')';
  254. *ptr = (unsigned char)'\0';
  255. _update_buffer (_plotter->data->page);
  256. /* AI directive: this is the text to be rendered */
  257. strcpy (_plotter->data->page->point, " Tx\n");
  258. _update_buffer (_plotter->data->page);
  259. /* AI directive: end of text object */
  260. strcpy (_plotter->data->page->point, "TO\n");
  261. _update_buffer (_plotter->data->page);
  262. /* flag current PS or PCL font as used */
  263. if (pcl_font)
  264. _plotter->data->page->pcl_font_used[master_font_index] = true;
  265. else
  266. _plotter->data->page->ps_font_used[master_font_index] = true;
  267. return width;
  268. }