f_text.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 Fig-driver-specific version of the low-level
  16. paint_text_string() method, which is called to plot a label in the
  17. current PS font, at the current fontsize and textangle. The label is
  18. just a string: no control codes (font switching or sub/superscripts).
  19. The width of the string in user units is returned.
  20. This version does not support `sheared' fonts, since xfig does not
  21. support them. But it does support center-justification and right
  22. justification as well as the default left-justification, since the xfig
  23. format supports them. */
  24. #include "sys-defines.h"
  25. #include "extern.h"
  26. #define FONT_TYPE_LATEX 0 /* don't support LaTeX fonts currently */
  27. #define FONT_TYPE_PS 4
  28. #define GOOD_PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))
  29. /* Fig horizontal alignment styles, indexed by internal number
  30. (left/center/right) */
  31. #define FIG_ALIGN_LEFT 0
  32. #define FIG_ALIGN_CENTER 1
  33. #define FIG_ALIGN_RIGHT 2
  34. static const int fig_horizontal_alignment_style[PL_NUM_HORIZ_JUST_TYPES] =
  35. { FIG_ALIGN_LEFT, FIG_ALIGN_CENTER, FIG_ALIGN_RIGHT };
  36. /* This prints a single-font, single-font-size label. When this is called,
  37. the current point is on the intended baseline of the label. */
  38. /* ARGS: h_just,v_just are PL_JUST_{LEFT|CENTER|RIGHT}, PL_JUST_{TOP etc.} */
  39. double
  40. _pl_f_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
  41. {
  42. int len, master_font_index;
  43. unsigned char *ptr, *t;
  44. double theta, costheta, sintheta;
  45. double label_width, label_ascent;
  46. double initial_x, initial_y;
  47. double horizontal_x, horizontal_y, vertical_x, vertical_y;
  48. double horizontal_fig_length, vertical_fig_length;
  49. double horizontal_fig_x, vertical_fig_x;
  50. double horizontal_fig_y, vertical_fig_y;
  51. double angle_device;
  52. /* sanity check */
  53. if (_plotter->drawstate->font_type != PL_F_POSTSCRIPT)
  54. return 0.0;
  55. /* sanity check; this routine supports only baseline positioning */
  56. if (v_just != PL_JUST_BASE)
  57. return 0.0;
  58. /* if empty string, nothing to do */
  59. if (*s == (unsigned char)'\0')
  60. return 0.0;
  61. /* if font (previously retrieved) has a font size of zero in terms of
  62. integer `Fig points', bail out right now, since xfig can't handle text
  63. strings with zero point size */
  64. if (_plotter->drawstate->fig_font_point_size == 0)
  65. return 0.0;
  66. /* label rotation angle in radians */
  67. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  68. sintheta = sin (theta);
  69. costheta = cos (theta);
  70. /* compute index of font in master table of PS fonts, in g_fontdb.h */
  71. master_font_index =
  72. (_pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  73. /* compute label height and width, in user units */
  74. label_width = _plotter->get_text_width (R___(_plotter) s);
  75. label_ascent = _plotter->drawstate->true_font_size * (_pl_g_ps_font_info[master_font_index]).font_ascent / 1000.0;
  76. /* vector along baseline of label, and an orthogonal vector which is the
  77. other side of a rectangle containing the portion of the string above
  78. the baseline (both in the user frame) */
  79. horizontal_x = costheta * label_width;
  80. horizontal_y = sintheta * label_width;
  81. vertical_x = - sintheta * label_ascent;
  82. vertical_y = costheta * label_ascent;
  83. /* Convert two above orthogonal vectors to the device frame, and compute
  84. their lengths. In the device frame they may no longer be orthogonal.
  85. But xfig supports setting up only rectangular `hot regions', so we'll
  86. use their lengths as the sides of a rectangle. If user coor->device
  87. coor map is highly sheared, this would be inappropriate.
  88. Incidentally, the height of the rectangular hot region should really
  89. be the string ascent (from its bounding box), not the font ascent.
  90. But since we don't include the bounding boxes of individual characters
  91. in our g_fontdb.c, we have no way of computing the former. */
  92. horizontal_fig_x = XDV(horizontal_x, horizontal_y);
  93. horizontal_fig_y = YDV(horizontal_x, horizontal_y);
  94. horizontal_fig_length = sqrt(horizontal_fig_x * horizontal_fig_x
  95. + horizontal_fig_y * horizontal_fig_y);
  96. /* text angle in device frame (note flipped-y convention) */
  97. angle_device = - _xatan2 (horizontal_fig_y, horizontal_fig_x);
  98. if (angle_device == 0.0)
  99. angle_device = 0.0; /* remove sign bit if any */
  100. /* avoid triggering a bug in xfig, which as of release 3.2.2 can't handle
  101. rotated text strings consisting of a single space character (they
  102. cause it to crash) */
  103. if (angle_device != 0.0 && strcmp ((const char *)s, " ") == 0)
  104. return _plotter->get_text_width (R___(_plotter) s);
  105. vertical_fig_x = XDV(vertical_x, vertical_y);
  106. vertical_fig_y = YDV(vertical_x, vertical_y);
  107. vertical_fig_length = sqrt(vertical_fig_x * vertical_fig_x
  108. + vertical_fig_y * vertical_fig_y);
  109. /* where we should start from, in device frame (i.e. in Fig units) */
  110. initial_x = XD((_plotter->drawstate->pos).x, (_plotter->drawstate->pos).y);
  111. initial_y = YD((_plotter->drawstate->pos).x, (_plotter->drawstate->pos).y);
  112. /* evaluate fig colors lazily, i.e. only when needed */
  113. _pl_f_set_pen_color (S___(_plotter));
  114. /* escape all backslashes in the text string, before output */
  115. len = strlen ((char *)s);
  116. ptr = (unsigned char *)_pl_xmalloc ((4 * len + 1) * sizeof(char));
  117. t = ptr;
  118. while (*s)
  119. {
  120. switch (*s)
  121. {
  122. case '\\':
  123. *ptr++ = (unsigned char)'\\';
  124. *ptr++ = *s++;
  125. break;
  126. default:
  127. if GOOD_PRINTABLE_ASCII (*s)
  128. *ptr++ = *s++;
  129. else
  130. {
  131. sprintf ((char *)ptr, "\\%03o", (unsigned int)*s);
  132. ptr += 4;
  133. s++;
  134. }
  135. break;
  136. }
  137. }
  138. *ptr = (unsigned char)'\0';
  139. /* update xfig's `depth' attribute */
  140. if (_plotter->fig_drawing_depth > 0)
  141. (_plotter->fig_drawing_depth)--;
  142. sprintf(_plotter->data->page->point,
  143. "#TEXT\n%d %d %d %d %d %d %.3f %.3f %d %.3f %.3f %d %d %s\\001\n",
  144. 4, /* text object */
  145. /* xfig supports 3 justification types: left, center, or right. */
  146. fig_horizontal_alignment_style[h_just],/* horizontal just. type */
  147. _plotter->drawstate->fig_fgcolor, /* pen color */
  148. _plotter->fig_drawing_depth, /* depth */
  149. 0, /* pen style, ignored */
  150. _pl_g_ps_font_info[master_font_index].fig_id, /* Fig font id */
  151. (double)_plotter->drawstate->fig_font_point_size, /* point size (float) */
  152. angle_device, /* text rotation in radians (float) */
  153. FONT_TYPE_PS, /* Fig font type */
  154. /* these next two are used only for setting up `hot spots' */
  155. vertical_fig_length, /* string height, Fig units (float) */
  156. horizontal_fig_length, /* string width, Fig units (float) */
  157. /* coors of origin of label, in Fig units */
  158. IROUND(initial_x),
  159. IROUND(initial_y),
  160. t); /* munged string */
  161. free (t);
  162. _update_buffer (_plotter->data->page);
  163. return label_width;
  164. }