File: a_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 (309 lines) | stat: -rw-r--r-- 11,291 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
/* This file contains the AI-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. */

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

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

/* This prints a single-font, single-font-size label.  When this is called,
   the current point is on the intended baseline of the label.  */

double
#ifdef _HAVE_PROTOS
_a_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
#else
_a_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;
  int justify_code;
  double width;
  unsigned char *ptr;
  double theta, costheta, sintheta;
  double norm;
  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];
  double lshift;
  bool pcl_font;
  
  /* sanity check; this routine supports only baseline positioning */
  if (v_just != JUST_BASE)
    return 0.0;

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

  /* sanity check */
  if (_plotter->drawstate->font_type != F_POSTSCRIPT
      && _plotter->drawstate->font_type != F_PCL)
    return 0.0;
  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];

  /* 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;

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

  /* 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;

  /* 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 AI.  (AI needs to retrieve a font, and
     transform it.)

     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;

  /* 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;

  /* AI directive: begin `point text' object */
  strcpy (_plotter->data->page->point, "0 To\n");
  _update_buffer (_plotter->data->page);

  /* output text transformation matrix */
  for (i = 0; i < 6; i++)
    {
      sprintf (_plotter->data->page->point, "%.4f ", 
	       text_transformation_matrix[i]);
      _update_buffer (_plotter->data->page);      
    }
  strcpy (_plotter->data->page->point, "0 Tp\nTP\n");
  _update_buffer (_plotter->data->page);
  
  /* set render mode: fill text, rather than several other possibilities */
  strcpy (_plotter->data->page->point, "0 Tr\n");
  _update_buffer (_plotter->data->page);

  /* set AI's fill color to be the same as libplot's notion of pen color
     (since letters in label will be drawn as filled outlines) */
  _a_set_fill_color (R___(_plotter) true);

  /* set AI's pen color also, in particular set it to be the same as
     libplot's notion of pen color (even though we'll be filling, not
     stroking); this is a convenience for AI users who may wish e.g. to
     switch from filling letter outlines to stroking them */
  _a_set_pen_color (S___(_plotter)); /* emit AI directive */

  /* AI directive: set font name and size */
  {
    const char *ps_name;

    if (pcl_font)			/* one of the 45 PCL fonts */
      ps_name = _pcl_font_info[master_font_index].ps_name;
    else				/* one of the 35 PS fonts */
      ps_name = _ps_font_info[master_font_index].ps_name;
    
    /* specify font name (underscore indicates reencoding), font size */
    sprintf (_plotter->data->page->point, "/_%s %.4f Tf\n", 
	     ps_name, device_font_size);
    _update_buffer (_plotter->data->page);
  }
  
  /* set line horizontal expansion factor, in percent */
  strcpy (_plotter->data->page->point, "100 Tz\n");
  _update_buffer (_plotter->data->page);

  /* NO track kerning, please */
  strcpy (_plotter->data->page->point, "0 Tt\n");
  _update_buffer (_plotter->data->page);

  /* turn off pairwise kerning (currently, a libplot convention) */
  strcpy (_plotter->data->page->point, "0 TA\n");
  _update_buffer (_plotter->data->page);

  /* turn off ALL inter-character spacing */
  strcpy (_plotter->data->page->point, "0 0 0 TC\n");
  _update_buffer (_plotter->data->page);

  /* use the default inter-word spacing; no more, no less */
  strcpy (_plotter->data->page->point, "100 100 100 TW\n");
  _update_buffer (_plotter->data->page);

  /* no indentation at beginning of `paragraphs' */
  strcpy (_plotter->data->page->point, "0 0 0 Ti\n");
  _update_buffer (_plotter->data->page);

  /* specify justification */
  switch (h_just)
    {
    case JUST_LEFT:
    default:
      justify_code = 0;
      break;
    case JUST_CENTER:
      justify_code = 1;
      break;
    case JUST_RIGHT:
      justify_code = 2;
      break;
    }
  sprintf (_plotter->data->page->point, "%d Ta\n", justify_code);
  _update_buffer (_plotter->data->page);

  /* no hanging quotation marks */
  strcpy (_plotter->data->page->point, "0 Tq\n");
  _update_buffer (_plotter->data->page);

  /* no leading between lines of a paragraph or between paragraphs */
  strcpy (_plotter->data->page->point, "0 0 Tl\n");
  _update_buffer (_plotter->data->page);

  /* compute width of the substring in user units (used below in
     constructing a bounding box) */
  width = _plotter->get_text_width (R___(_plotter) s);

  /* for computing bounding box, compute justification-dependent leftward
     shift, as fraction of label width */
  switch (h_just)
    {
    case JUST_LEFT:
    default:
      lshift = 0.0;
      break;
    case JUST_CENTER:
      lshift = 0.5;
      break;
    case JUST_RIGHT:
      lshift = 1.0;
      break;
    }

  /* to compute an EPS-style bounding box, first compute offsets to the
     four vertices of the smallest rectangle containing the string */

  dx0 = costheta * (- lshift) * width - sintheta * (-down);
  dy0 = sintheta * (- lshift) * width + costheta * (-down);
  
  dx1 = costheta * (- lshift) * width - sintheta * up;
  dy1 = sintheta * (- lshift) * width + costheta * up;
  
  dx2 = costheta * (1.0 - lshift) * width - sintheta * (-down);
  dy2 = sintheta * (1.0 - lshift) * width + costheta * (-down);
  
  dx3 = costheta * (1.0 - lshift) * width - sintheta * up;
  dy3 = sintheta * (1.0 - lshift) * 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));

  /* output string as a PS string (i.e. surrounded by parentheses) */
  ptr = (unsigned char *)_plotter->data->page->point;
  *ptr++ = '(';
  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++ = ')';
  *ptr = (unsigned char)'\0';
  _update_buffer (_plotter->data->page);

  /* AI directive: this is the text to be rendered */
  strcpy (_plotter->data->page->point, " Tx\n");
  _update_buffer (_plotter->data->page);

  /* AI directive: end of text object */
  strcpy (_plotter->data->page->point, "TO\n");
  _update_buffer (_plotter->data->page);

  /* flag current PS or PCL font as used */
  if (pcl_font)
    _plotter->data->page->pcl_font_used[master_font_index] = true;
  else
    _plotter->data->page->ps_font_used[master_font_index] = true;

  return width;
}