File: p_text.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (325 lines) | stat: -rw-r--r-- 12,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* This file contains the PS-driver-specific version of the low-level
   paint_text_string method, which is called to plot a label in the current
   font (either PS or PCL), at the current fontsize and textangle.  The
   label is just a string: no control codes (font switching or
   sub/superscripts).

   The width of the string in user units is returned.

   This version does not support center-justification and
   right-justification; only the default left-justification.  That is
   all right, since label justification is handled at a higher level. */

#include "sys-defines.h"
#include "extern.h"

#define GOOD_PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))

double
#ifdef _HAVE_PROTOS
_p_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
#else
_p_paint_text_string (R___(_plotter) s, h_just, v_just)
     S___(Plotter *_plotter;)
     const unsigned char *s;
     int h_just;  /* horizontal justification: JUST_LEFT, CENTER, or RIGHT */
     int v_just;  /* vertical justification: JUST_TOP, HALF, BASE, BOTTOM */
#endif
{
  int i, master_font_index;
  double width;
  unsigned char *ptr;
  double theta, costheta, sintheta;
  double norm;
  double crockshift_x, crockshift_y;
  double dx0,dy0,dx1,dy1,dx2,dy2,dx3,dy3;
  double font_ascent, font_descent, up, down;
  double user_font_size = _plotter->drawstate->true_font_size;
  double device_font_size;
  double user_text_transformation_matrix[6];
  double text_transformation_matrix[6];
  bool pcl_font;
  
  /* sanity check; this routine supports only baseline positioning */
  if (v_just != JUST_BASE)
    return 0.0;

  /* similarly for horizontal justification */
  if (h_just != JUST_LEFT)
    /* shouldn't happen */
    return 0.0;

  /* if empty string, nothing to do */
  if (*s == (unsigned char)'\0')
    return 0.0;

  /* sanity check */
#ifndef USE_LJ_FONTS_IN_PS
  if (_plotter->drawstate->font_type != F_POSTSCRIPT)
    return 0.0;
#else  /* USE_LJ_FONTS_IN_PS */
  if (_plotter->drawstate->font_type != F_POSTSCRIPT
      && _plotter->drawstate->font_type != F_PCL)
    return 0.0;
#endif
  pcl_font = (_plotter->drawstate->font_type == F_PCL ? true : false);

  /* compute index of font in master table of PS [or PCL] fonts, in
     g_fontdb.c */
  if (pcl_font)			/* one of the 45 standard PCL fonts */
    master_font_index =
      (_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  else				/* one of the 35 standard PS fonts */
    master_font_index =
      (_ps_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];

  /* label rotation angle in radians, in user frame */
  theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  sintheta = sin (theta);
  costheta = cos (theta);

  /* font ascent and descent (taken from the font's bounding box) */
  if (pcl_font)
    {
      font_ascent = (double)((_pcl_font_info[master_font_index]).font_ascent);
      font_descent = (double)((_pcl_font_info[master_font_index]).font_descent);
    }
  else				/* PS font */
    {
      font_ascent = (double)((_ps_font_info[master_font_index]).font_ascent);
      font_descent = (double)((_ps_font_info[master_font_index]).font_descent);
    }
  up = user_font_size * font_ascent / 1000.0;
  down = user_font_size * font_descent / 1000.0;

  /* Current point is on the baseline, but the rendering logic of the PS
     code in the idraw prologue requires us to perform a vertical shift at
     this point.  (We'll undo the vertical shift immediately.) */
  _plotter->drawstate->pos.x -= (user_font_size - down) * sintheta;
  _plotter->drawstate->pos.y += (user_font_size - down) * costheta;

  /* the idraw PS prologue (see p_header.c) performs an additional
     [gratuitous] vertical shift by 1 unit, which we must compensate for */
  {
    double ctm_norm = _matrix_norm (_plotter->drawstate->transform.m);
    
    crockshift_x = sintheta / ctm_norm;
    crockshift_y = costheta / ctm_norm;
  }
  _plotter->drawstate->pos.x += crockshift_x;
  _plotter->drawstate->pos.y -= crockshift_y;

  /* this transformation matrix rotates, and translates: it maps (0,0) to
     the origin of the string, in user coordinates */
  user_text_transformation_matrix[0] = costheta;
  user_text_transformation_matrix[1] = sintheta;
  user_text_transformation_matrix[2] = - sintheta;
  user_text_transformation_matrix[3] = costheta;
  user_text_transformation_matrix[4] = _plotter->drawstate->pos.x;
  user_text_transformation_matrix[5] = _plotter->drawstate->pos.y;

  /* undo vertical shifts performed above */
  _plotter->drawstate->pos.x += (user_font_size - down) * sintheta;
  _plotter->drawstate->pos.y -= (user_font_size - down) * costheta;
  _plotter->drawstate->pos.x -= crockshift_x;
  _plotter->drawstate->pos.y += crockshift_y;

  /* Construct a temporary matrix that rotates, translates, and then maps
     to device coordinates.  This matrix transforms from a frame in which
     nominal character sizes are roughly 1 unit in the horizontal and
     vertical directions, to device coordinates. */
  _matrix_product (user_text_transformation_matrix, 
		   _plotter->drawstate->transform.m,
		   text_transformation_matrix);

  /* We need to extract a quantity we can call a font size in device
     coordinates, for the benefit of idraw.  (Idraw needs to retrieve an X
     font, and scale it.  Idraw does not make use of modern [X11R6+] font
     scaling technology; it does its own scaling of bitmaps.)

     We define this to be user_font_size (the nominal font size in user
     coordinates), times the norm of the linear tranformation contained in
     the temporary matrix we just constructed (the magnitude of its larger
     singular value).  Recall that for any square matrix M, the singular
     values are the square roots of the eigenvalues of the symmetric matrix
     M^t M. */

  norm = _matrix_norm (text_transformation_matrix);

  if (norm == 0.0)		/* avoid division by zero */
    return 0.0;

  device_font_size = norm * user_font_size;

  /* Many PS interpreters can't handle zero font size.  So bail if the font
     size we'll emit is zero. */
  {
    char charbuf[64];
    double emitted_device_font_size;
    
    sprintf (charbuf, "%f", device_font_size);
    sscanf (charbuf, "%lf", &emitted_device_font_size);
    if (emitted_device_font_size == 0.0)
      return 0.0;
  }

  /* Now scale the text transformation matrix so that the linear
     transformation contained in it has unit norm (if there is no shearing,
     it will just be a rotation; if there is no rotation either, it will be
     the identity matrix). */
  for (i = 0; i < 4; i++)
    text_transformation_matrix[i] /= norm;

  /* prologue instruction, plus idraw directive: start of Text */
  strcpy (_plotter->data->page->point, "Begin %I Text\n");
  _update_buffer (_plotter->data->page);

  /* idraw directive, plus prologue instruction: set foreground color */
  _p_set_pen_color (S___(_plotter));	/* invoked lazily, i.e. when needed */
  sprintf (_plotter->data->page->point, "%%I cfg %s\n%g %g %g SetCFg\n",
	   _idraw_stdcolornames[_plotter->drawstate->ps_idraw_fgcolor],
	   _plotter->drawstate->ps_fgcolor_red,
	   _plotter->drawstate->ps_fgcolor_green,
	   _plotter->drawstate->ps_fgcolor_blue);
  _update_buffer (_plotter->data->page);

  /* idraw directive: X Windows font name, which incorporates the X font
     size.  We use our primary X font name (the `x_name' field, not the
     `x_name_alt' field if any). */

  /* N.B. this directive sets the _pixel_ size of the X font to be our
     current point size.  That would really be appropriate only if the
     screen resolution is 72 dpi.  But idraw seems to prefer setting the
     pixel size to setting the point size. */

  if (pcl_font)			/* one of the 45 PCL fonts */
    {
      const char *ps_name;
      
      /* this is to support the Tidbits-is-Wingdings botch */
      if (_pcl_font_info[master_font_index].substitute_ps_name)
	ps_name = _pcl_font_info[master_font_index].substitute_ps_name;
      else
	ps_name = _pcl_font_info[master_font_index].ps_name;

      sprintf (_plotter->data->page->point,
	       "%%I f -*-%s-*-%d-*-*-*-*-*-*-*\n", 
	       (_pcl_font_info[master_font_index]).x_name, 
	       IROUND(device_font_size));
      _update_buffer (_plotter->data->page);

      /* prolog instruction: PS font name and size */
      sprintf (_plotter->data->page->point, "/%s %f SetF\n", 
	       ps_name,
	       device_font_size);
      _update_buffer (_plotter->data->page);
    }
  else				/* one of the 35 PS fonts */
    {
      sprintf (_plotter->data->page->point,
	       "%%I f -*-%s-*-%d-*-*-*-*-*-*-*\n", 
	       (_ps_font_info[master_font_index]).x_name, 
	       IROUND(device_font_size));
      _update_buffer (_plotter->data->page);

      /* prolog instruction: PS font name and size */
      sprintf (_plotter->data->page->point, "/%s %f SetF\n", 
	       _ps_font_info[master_font_index].ps_name,
	       device_font_size);
      _update_buffer (_plotter->data->page);
    }

  /* idraw directive and prologue instruction: text transformation matrix */
  strcpy (_plotter->data->page->point, "%I t\n[ ");
  _update_buffer (_plotter->data->page);

  for (i = 0; i < 6; i++)
    {
      sprintf (_plotter->data->page->point, "%.7g ", 
	       text_transformation_matrix[i]);
      _update_buffer (_plotter->data->page);      
    }
  
  /* width of the string in user units (used below in constructing a
     bounding box, and as return value) */
  width = _plotter->get_text_width (R___(_plotter) s);

  /* to compute an EPS-style bounding box, first compute offsets to the
     four vertices of the smallest rectangle containing the string */
  dx0 = - sintheta * (-down);
  dy0 =   costheta * (-down);
  
  dx1 = - sintheta * up;
  dy1 =   costheta * up;
  
  dx2 = costheta * width - sintheta * (-down);
  dy2 = sintheta * width + costheta * (-down);
  
  dx3 = costheta * width - sintheta * up;
  dy3 = sintheta * width + costheta * up;

  /* record that we're using all four vertices (args of _update_bbox() are in
     device units, not user units) */
  _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0),
	      YD ((_plotter->drawstate->pos).x + dx0, (_plotter->drawstate->pos).y + dy0));
  _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1), 
	      YD ((_plotter->drawstate->pos).x + dx1, (_plotter->drawstate->pos).y + dy1));
  _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2), 
	      YD ((_plotter->drawstate->pos).x + dx2, (_plotter->drawstate->pos).y + dy2));
  _update_bbox (_plotter->data->page, XD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3), 
	      YD ((_plotter->drawstate->pos).x + dx3, (_plotter->drawstate->pos).y + dy3));

  /* Finish outputting transformation matrix; begin outputting string. */
  /* Escape all backslashes etc. in the text string, before output. */
  strcpy (_plotter->data->page->point, " ] concat\n\
%I\n\
[\n\
(");
  _update_buffer (_plotter->data->page);

  ptr = (unsigned char *)_plotter->data->page->point;
  while (*s)
    {
      switch (*s)
	{
	case '(':		/* for PS, escape ()/ */
	case ')':
	case '\\':
	  *ptr++ = (unsigned char)'\\';
	  *ptr++ = *s++;
          break;
	default:
          if GOOD_PRINTABLE_ASCII (*s)
	    *ptr++ = *s++;
          else
            {	    
               sprintf ((char *)ptr, "\\%03o", (unsigned int)*s);
               ptr += 4;
               s++;
            }
          break;
	}
    }
  *ptr = (unsigned char)'\0';
  _update_buffer (_plotter->data->page);

  /* prologue instruction: end of text */
  strcpy (_plotter->data->page->point, ")\n\
] Text\n\
End\n\
\n");
  _update_buffer (_plotter->data->page);

  /* flag current PS or PCL font as used on this page */
#ifdef USE_LJ_FONTS_IN_PS
  if (pcl_font)
    _plotter->data->page->pcl_font_used[master_font_index] = true;

  else
    _plotter->data->page->ps_font_used[master_font_index] = true;
#else
    _plotter->data->page->ps_font_used[master_font_index] = true;
#endif

  return width;
}