p_text.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 PS-driver-specific version of the low-level
  16. paint_text_string method, which is called to plot a label in the current
  17. font (either PS or PCL), at the current fontsize and textangle. The
  18. 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. This version does not support center-justification and
  22. right-justification; only the default left-justification. That is
  23. all right, since label justification is handled at a higher level. */
  24. #include "sys-defines.h"
  25. #include "extern.h"
  26. #define GOOD_PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))
  27. double
  28. _pl_p_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
  29. {
  30. int i, master_font_index;
  31. double width;
  32. unsigned char *ptr;
  33. double theta, costheta, sintheta;
  34. double norm;
  35. double crockshift_x, crockshift_y;
  36. double dx0,dy0,dx1,dy1,dx2,dy2,dx3,dy3;
  37. double font_ascent, font_descent, up, down;
  38. double user_font_size = _plotter->drawstate->true_font_size;
  39. double device_font_size;
  40. double user_text_transformation_matrix[6];
  41. double text_transformation_matrix[6];
  42. bool pcl_font;
  43. /* sanity check; this routine supports only baseline positioning */
  44. if (v_just != PL_JUST_BASE)
  45. return 0.0;
  46. /* similarly for horizontal justification */
  47. if (h_just != PL_JUST_LEFT)
  48. /* shouldn't happen */
  49. return 0.0;
  50. /* if empty string, nothing to do */
  51. if (*s == (unsigned char)'\0')
  52. return 0.0;
  53. /* sanity check */
  54. #ifndef USE_LJ_FONTS_IN_PS
  55. if (_plotter->drawstate->font_type != PL_F_POSTSCRIPT)
  56. return 0.0;
  57. #else /* USE_LJ_FONTS_IN_PS */
  58. if (_plotter->drawstate->font_type != PL_F_POSTSCRIPT
  59. && _plotter->drawstate->font_type != PL_F_PCL)
  60. return 0.0;
  61. #endif
  62. pcl_font = (_plotter->drawstate->font_type == PL_F_PCL ? true : false);
  63. /* compute index of font in master table of PS [or PCL] fonts, in
  64. g_fontdb.c */
  65. if (pcl_font) /* one of the 45 standard PCL fonts */
  66. master_font_index =
  67. (_pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  68. else /* one of the 35 standard PS fonts */
  69. master_font_index =
  70. (_pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  71. /* label rotation angle in radians, in user frame */
  72. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  73. sintheta = sin (theta);
  74. costheta = cos (theta);
  75. /* font ascent and descent (taken from the font's bounding box) */
  76. if (pcl_font)
  77. {
  78. font_ascent = (double)((_pl_g_pcl_font_info[master_font_index]).font_ascent);
  79. font_descent = (double)((_pl_g_pcl_font_info[master_font_index]).font_descent);
  80. }
  81. else /* PS font */
  82. {
  83. font_ascent = (double)((_pl_g_ps_font_info[master_font_index]).font_ascent);
  84. font_descent = (double)((_pl_g_ps_font_info[master_font_index]).font_descent);
  85. }
  86. up = user_font_size * font_ascent / 1000.0;
  87. down = user_font_size * font_descent / 1000.0;
  88. /* Current point is on the baseline, but the rendering logic of the PS
  89. code in the idraw prologue requires us to perform a vertical shift at
  90. this point. (We'll undo the vertical shift immediately.) */
  91. _plotter->drawstate->pos.x -= (user_font_size - down) * sintheta;
  92. _plotter->drawstate->pos.y += (user_font_size - down) * costheta;
  93. /* the idraw PS prologue (see p_header.c) performs an additional
  94. [gratuitous] vertical shift by 1 unit, which we must compensate for */
  95. {
  96. double ctm_norm = _matrix_norm (_plotter->drawstate->transform.m);
  97. crockshift_x = sintheta / ctm_norm;
  98. crockshift_y = costheta / ctm_norm;
  99. }
  100. _plotter->drawstate->pos.x += crockshift_x;
  101. _plotter->drawstate->pos.y -= crockshift_y;
  102. /* this transformation matrix rotates, and translates: it maps (0,0) to
  103. the origin of the string, in user coordinates */
  104. user_text_transformation_matrix[0] = costheta;
  105. user_text_transformation_matrix[1] = sintheta;
  106. user_text_transformation_matrix[2] = - sintheta;
  107. user_text_transformation_matrix[3] = costheta;
  108. user_text_transformation_matrix[4] = _plotter->drawstate->pos.x;
  109. user_text_transformation_matrix[5] = _plotter->drawstate->pos.y;
  110. /* undo vertical shifts performed above */
  111. _plotter->drawstate->pos.x += (user_font_size - down) * sintheta;
  112. _plotter->drawstate->pos.y -= (user_font_size - down) * costheta;
  113. _plotter->drawstate->pos.x -= crockshift_x;
  114. _plotter->drawstate->pos.y += crockshift_y;
  115. /* Construct a temporary matrix that rotates, translates, and then maps
  116. to device coordinates. This matrix transforms from a frame in which
  117. nominal character sizes are roughly 1 unit in the horizontal and
  118. vertical directions, to device coordinates. */
  119. _matrix_product (user_text_transformation_matrix,
  120. _plotter->drawstate->transform.m,
  121. text_transformation_matrix);
  122. /* We need to extract a quantity we can call a font size in device
  123. coordinates, for the benefit of idraw. (Idraw needs to retrieve an X
  124. font, and scale it. Idraw does not make use of modern [X11R6+] font
  125. scaling technology; it does its own scaling of bitmaps.)
  126. We define this to be user_font_size (the nominal font size in user
  127. coordinates), times the norm of the linear tranformation contained in
  128. the temporary matrix we just constructed (the magnitude of its larger
  129. singular value). Recall that for any square matrix M, the singular
  130. values are the square roots of the eigenvalues of the symmetric matrix
  131. M^t M. */
  132. norm = _matrix_norm (text_transformation_matrix);
  133. if (norm == 0.0) /* avoid division by zero */
  134. return 0.0;
  135. device_font_size = norm * user_font_size;
  136. /* Many PS interpreters can't handle zero font size. So bail if the font
  137. size we'll emit is zero. */
  138. {
  139. char charbuf[64];
  140. double emitted_device_font_size;
  141. sprintf (charbuf, "%f", device_font_size);
  142. sscanf (charbuf, "%lf", &emitted_device_font_size);
  143. if (emitted_device_font_size == 0.0)
  144. return 0.0;
  145. }
  146. /* Now scale the text transformation matrix so that the linear
  147. transformation contained in it has unit norm (if there is no shearing,
  148. it will just be a rotation; if there is no rotation either, it will be
  149. the identity matrix). */
  150. for (i = 0; i < 4; i++)
  151. text_transformation_matrix[i] /= norm;
  152. /* prologue instruction, plus idraw directive: start of Text */
  153. strcpy (_plotter->data->page->point, "Begin %I Text\n");
  154. _update_buffer (_plotter->data->page);
  155. /* idraw directive, plus prologue instruction: set foreground color */
  156. _pl_p_set_pen_color (S___(_plotter)); /* invoked lazily, i.e. when needed */
  157. sprintf (_plotter->data->page->point, "%%I cfg %s\n%g %g %g SetCFg\n",
  158. _pl_p_idraw_stdcolornames[_plotter->drawstate->ps_idraw_fgcolor],
  159. _plotter->drawstate->ps_fgcolor_red,
  160. _plotter->drawstate->ps_fgcolor_green,
  161. _plotter->drawstate->ps_fgcolor_blue);
  162. _update_buffer (_plotter->data->page);
  163. /* idraw directive: X Windows font name, which incorporates the X font
  164. size. We use our primary X font name (the `x_name' field, not the
  165. `x_name_alt' field if any). */
  166. /* N.B. this directive sets the _pixel_ size of the X font to be our
  167. current point size. That would really be appropriate only if the
  168. screen resolution is 72 dpi. But idraw seems to prefer setting the
  169. pixel size to setting the point size. */
  170. if (pcl_font) /* one of the 45 PCL fonts */
  171. {
  172. const char *ps_name;
  173. /* this is to support the Tidbits-is-Wingdings botch */
  174. if (_pl_g_pcl_font_info[master_font_index].substitute_ps_name)
  175. ps_name = _pl_g_pcl_font_info[master_font_index].substitute_ps_name;
  176. else
  177. ps_name = _pl_g_pcl_font_info[master_font_index].ps_name;
  178. sprintf (_plotter->data->page->point,
  179. "%%I f -*-%s-*-%d-*-*-*-*-*-*-*\n",
  180. (_pl_g_pcl_font_info[master_font_index]).x_name,
  181. IROUND(device_font_size));
  182. _update_buffer (_plotter->data->page);
  183. /* prolog instruction: PS font name and size */
  184. sprintf (_plotter->data->page->point, "/%s %f SetF\n",
  185. ps_name,
  186. device_font_size);
  187. _update_buffer (_plotter->data->page);
  188. }
  189. else /* one of the 35 PS fonts */
  190. {
  191. sprintf (_plotter->data->page->point,
  192. "%%I f -*-%s-*-%d-*-*-*-*-*-*-*\n",
  193. (_pl_g_ps_font_info[master_font_index]).x_name,
  194. IROUND(device_font_size));
  195. _update_buffer (_plotter->data->page);
  196. /* prolog instruction: PS font name and size */
  197. sprintf (_plotter->data->page->point, "/%s %f SetF\n",
  198. _pl_g_ps_font_info[master_font_index].ps_name,
  199. device_font_size);
  200. _update_buffer (_plotter->data->page);
  201. }
  202. /* idraw directive and prologue instruction: text transformation matrix */
  203. strcpy (_plotter->data->page->point, "%I t\n[ ");
  204. _update_buffer (_plotter->data->page);
  205. for (i = 0; i < 6; i++)
  206. {
  207. sprintf (_plotter->data->page->point, "%.7g ",
  208. text_transformation_matrix[i]);
  209. _update_buffer (_plotter->data->page);
  210. }
  211. /* width of the string in user units (used below in constructing a
  212. bounding box, and as return value) */
  213. width = _plotter->get_text_width (R___(_plotter) s);
  214. /* to compute an EPS-style bounding box, first compute offsets to the
  215. four vertices of the smallest rectangle containing the string */
  216. dx0 = - sintheta * (-down);
  217. dy0 = costheta * (-down);
  218. dx1 = - sintheta * up;
  219. dy1 = costheta * up;
  220. dx2 = costheta * width - sintheta * (-down);
  221. dy2 = sintheta * width + costheta * (-down);
  222. dx3 = costheta * width - sintheta * up;
  223. dy3 = sintheta * width + costheta * up;
  224. /* record that we're using all four vertices (args of _update_bbox() are in
  225. device units, not user units) */
  226. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0),
  227. YD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0));
  228. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1),
  229. YD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1));
  230. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2),
  231. YD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2));
  232. _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3),
  233. YD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3));
  234. /* Finish outputting transformation matrix; begin outputting string. */
  235. /* Escape all backslashes etc. in the text string, before output. */
  236. strcpy (_plotter->data->page->point, " ] concat\n\
  237. %I\n\
  238. [\n\
  239. (");
  240. _update_buffer (_plotter->data->page);
  241. ptr = (unsigned char *)_plotter->data->page->point;
  242. while (*s)
  243. {
  244. switch (*s)
  245. {
  246. case '(': /* for PS, escape ()/ */
  247. case ')':
  248. case '\\':
  249. *ptr++ = (unsigned char)'\\';
  250. *ptr++ = *s++;
  251. break;
  252. default:
  253. if GOOD_PRINTABLE_ASCII (*s)
  254. *ptr++ = *s++;
  255. else
  256. {
  257. sprintf ((char *)ptr, "\\%03o", (unsigned int)*s);
  258. ptr += 4;
  259. s++;
  260. }
  261. break;
  262. }
  263. }
  264. *ptr = (unsigned char)'\0';
  265. _update_buffer (_plotter->data->page);
  266. /* prologue instruction: end of text */
  267. strcpy (_plotter->data->page->point, ")\n\
  268. ] Text\n\
  269. End\n\
  270. \n");
  271. _update_buffer (_plotter->data->page);
  272. /* flag current PS or PCL font as used on this page */
  273. #ifdef USE_LJ_FONTS_IN_PS
  274. if (pcl_font)
  275. _plotter->data->page->pcl_font_used[master_font_index] = true;
  276. else
  277. _plotter->data->page->ps_font_used[master_font_index] = true;
  278. #else
  279. _plotter->data->page->ps_font_used[master_font_index] = true;
  280. #endif
  281. return width;
  282. }