File: x_text.c

package info (click to toggle)
plotutils 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 13,156 kB
  • ctags: 7,141
  • sloc: ansic: 68,670; sh: 20,082; cpp: 12,382; yacc: 2,588; makefile: 889; lex: 137
file content (270 lines) | stat: -rw-r--r-- 11,470 bytes parent folder | download | duplicates (7)
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
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.

   The GNU plotutils package is free software.  You may redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software foundation; either version 2, or (at your
   option) any later version.

   The GNU plotutils package is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with the GNU plotutils package; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
   Boston, MA 02110-1301, USA. */

/* This file contains the XDrawablePlotter (and XPlotter) version of the
   low-level paint_text_string() method, which is called to plot a label in
   the current (non-Hershey) font, 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 not does support center-justification and right
   justification; only the default left-justification.  That is all
   right, since justification is handled at a higher level. */

/* This file also contains the XDrawablePlotter (and XPlotter) version of
   the flabelwidth_other() method, which is called to compute the width, in
   user coordinates, of a label. */

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

/* When this is called in g_alabel.c, the X font has already been
   retrieved, in whole or in part (by calling "_set_font()", which in turn
   calls "_plotter->retrieve_font()", which is bound to the
   _pl_x_retrieve_font() routine in x_retrieve.c).  I.e., whatever portion of
   the X font was required to be retrieved in order to return font metrics,
   has previously been retrieved.

   To retrieve a larger part, we call _pl_x_retrieve_font() again.  But this
   time, we pass the label to be rendered to _pl_x_retrieve_font() as a
   "hint", i.e., as the x_label data member of (the driver-specific part
   of) the drawing state.  That tells _pl_x_retrieve_font how much more of the
   font to retrieve.  This scheme is a hack, but it works (and doesn't
   violate layering).  */

#include "x_afftext.h"

double
_pl_x_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
{
  const char *saved_font_name;
  char *temp_font_name;
  bool ok;
  double x, y;
  double width = 0.0;		/* width of string in user units */
  double rot[4];		/* user-frame rotation matrix */
  double a[4];		   /* transformation matrix for XAffDrawString() */
  int i, ix, iy;
  
  /* sanity check; this routine supports only baseline positioning */
  if (v_just != PL_JUST_BASE)
    return 0.0;

  /* similarly for horizontal justification */
  if (h_just != PL_JUST_LEFT)
    return 0.0;

  if (*s == (unsigned char)'\0')
    return 0.0;

  /* Do retrieval, fill in the X-specific field x_font_struct of the
     drawing state.  (We've previously retrieved a small subset of the
     font, to obtain metrics used for text positioning, as mentioned above;
     so retrieving a larger portion should go smoothly.)   

     We retrieve not `font_name' but rather `true_font_name', because the
     latter may have been what was retrieved, if a default X font had to be
     substituted; see g_retrieve.c. */

  if (_plotter->drawstate->true_font_name == NULL) /* shouldn't happen */
    return 0.0;

  saved_font_name = _plotter->drawstate->font_name;
  temp_font_name = 
    (char *)_pl_xmalloc (strlen (_plotter->drawstate->true_font_name) + 1);
  strcpy (temp_font_name, _plotter->drawstate->true_font_name);
  _plotter->drawstate->font_name = temp_font_name;

  _plotter->drawstate->x_label = s; /* pass label hint */
  ok = _pl_x_retrieve_font (S___(_plotter));
  _plotter->drawstate->x_label = NULL; /* restore label hint to default */

  _plotter->drawstate->font_name = saved_font_name;
  free (temp_font_name);

  if (!ok)			/* shouldn't happen */
    return 0.0;

  /* set font in GC used for drawing (the other GC, used for filling, is
     left alone) */
  XSetFont (_plotter->x_dpy, _plotter->drawstate->x_gc_fg,
	    _plotter->drawstate->x_font_struct->fid);

  /* select our pen color as foreground color in X GC used for drawing */
  _pl_x_set_pen_color (S___(_plotter));
  
  /* compute position in device coordinates */
  x = XD(_plotter->drawstate->pos.x, _plotter->drawstate->pos.y);
  y = YD(_plotter->drawstate->pos.x, _plotter->drawstate->pos.y);
  
  /* X11 protocol OOB check */
  ix = IROUND(x);
  iy = IROUND(y);
  if (X_OOB_INT(ix) || X_OOB_INT(iy))
    {
      _plotter->warning (R___(_plotter) 
			 "not drawing a text string that is positioned too far for X11");
      return 0.0;
    }
    
  /* Draw the text string by calling XAffDrawString() in x_afftext.c, which
     operates by affinely transform a bitmap generated by XDrawString() in
     the following way: it pulls it back from the server as an image,
     transforms the image, and then sends the image back to the server. */
  
  /* First, compute a 2x2 matrix a[] that would, in the jargon of the
     matrix extension to the XLFD (X Logical Font Description) scheme, be
     called a `pixel matrix'.  It specifies how XAffDrawAffString should
     `anamorphically transform' the text bitmap produced by XDrawString(),
     to yield the bitmap we want.  It's essentially the product of (i) the
     user-frame text rotation matrix, and (ii) the user_space->device_space
     transformation matrix.  But see additional comments below. */

  /* user-frame rotation matrix */
  rot[0] = cos (M_PI * _plotter->drawstate->text_rotation / 180.0);
  rot[1] = sin (M_PI * _plotter->drawstate->text_rotation / 180.0);
  rot[2] = - sin (M_PI * _plotter->drawstate->text_rotation / 180.0);
  rot[3] = cos (M_PI * _plotter->drawstate->text_rotation / 180.0);
  
  /* Compute matrix product.  But note flipped-y convention affecting a[1]
     and a[3].  Sign flipping is because the pixel matrix (as used in the
     XLFD matrix extension and hence, for consistency, by our code by
     XAffDrawAffString()) is expressed with respect to a right-handed
     coordinate system, in which y grows upward, rather than X11's default
     left-handed coordinate system, in which y grows downward. */

  a[0] =  (rot[0] * _plotter->drawstate->transform.m[0] 
	   + rot[1] * _plotter->drawstate->transform.m[2]);
  a[1] =  - (rot[0] * _plotter->drawstate->transform.m[1] 
	     + rot[1] * _plotter->drawstate->transform.m[3]);
  a[2] =  (rot[2] * _plotter->drawstate->transform.m[0] 
	   + rot[3] * _plotter->drawstate->transform.m[2]);
  a[3] =  - (rot[2] * _plotter->drawstate->transform.m[1] 
	     + rot[3] * _plotter->drawstate->transform.m[3]);
  
  /* Apply an overall scaling.  We want the text string to appear at a
     certain font size in the user frame; and the font that XDrawString
     will use was retrieved at a certain pixel size in the device frame.
     So we compensate on both sides, so to speak.  We multiply by
     true_font_size / x_font_pixel_size, where the numerator refers to the
     user frame, and the denominator to the device frame. */

  for (i = 0; i < 4; i++)
    a[i] = a[i] 
      * (_plotter->drawstate->true_font_size / _plotter->drawstate->x_font_pixel_size);
    
  if (_plotter->x_double_buffering != X_DBL_BUF_NONE)
    /* double buffering, have a `x_drawable3' to draw into */
    XAffDrawAffString (_plotter->x_dpy, _plotter->x_drawable3, 
		       _plotter->drawstate->x_gc_fg, 
		       _plotter->drawstate->x_font_struct,
		       ix, iy, a, (char *)s);
  else
    {
      /* not double buffering, have no `x_drawable3' */
      if (_plotter->x_drawable1)
	XAffDrawAffString (_plotter->x_dpy, _plotter->x_drawable1, 
			   _plotter->drawstate->x_gc_fg, 
			   _plotter->drawstate->x_font_struct,
			   ix, iy, a, (char *)s);
      if (_plotter->x_drawable2)
	XAffDrawAffString (_plotter->x_dpy, _plotter->x_drawable2, 
			   _plotter->drawstate->x_gc_fg, 
			   _plotter->drawstate->x_font_struct,
			   ix, iy, a, (char *)s);
    }
    
  /* compute width of just-drawn string in user units */
  width = (((XTextWidth (_plotter->drawstate->x_font_struct, 
			 (char *)s, 
			 (int)(strlen((char *)s)))
	     *_plotter->drawstate->true_font_size))
	   / _plotter->drawstate->x_font_pixel_size);
  
  /* maybe flush X output buffer and handle X events (a no-op for
     XDrawablePlotters, which is overridden for XPlotters) */
  _maybe_handle_x_events (S___(_plotter));

  return width;
}

/* Compute width, in user coordinates, of label in the currently selected
   font (no escape sequences!).  Current font is assumed to be a
   non-Hershey font (so we have an X font structure for it).  This is
   installed as an internal class method, invoked if the current font is
   non-Hershey (which means Postscript, PCL, or `other' [i.e. any non-PS,
   non-PCL, retrievable X font].  */

/* When this is called in g_alabel.c, the X font has already been
   retrieved, in whole or in part (by calling "_set_font()", which in turn
   calls "_plotter->retrieve_font()", i.e., which calls the
   _pl_x_retrieve_font() routine in x_retrieve.c).  I.e., whatever portion of
   the X font was required to be retrieved in order to return font metrics,
   has previously been retrieved.

   To retrieve a larger part, we call _pl_x_retrieve_font() again.  But this
   time, we pass the label to be rendered to _pl_x_retrieve_font() as a
   "hint", i.e., as a data member of (the driver-specific part of) the
   drawing state.  That tells _pl_x_retrieve_font how much more of the font to
   retrieve.  This scheme is an ugly hack, but it works (and doesn't
   violate layering). */

double
_pl_x_get_text_width (R___(Plotter *_plotter) const unsigned char *s)
{
  const char *saved_font_name;
  char *temp_font_name;
  bool ok;
  double width;

  /* Do retrieval, but use current `true_font_name' as our font name (see
     above; we've previously retrieved a subset of it). */

  if (_plotter->drawstate->true_font_name == NULL) /* shouldn't happen */
    return 0.0;

  saved_font_name = _plotter->drawstate->font_name;
  temp_font_name = 
    (char *)_pl_xmalloc (strlen (_plotter->drawstate->true_font_name) + 1);
  strcpy (temp_font_name, _plotter->drawstate->true_font_name);
  _plotter->drawstate->font_name = temp_font_name;

  _plotter->drawstate->x_label = s; /* pass label hint */
  ok = _pl_x_retrieve_font (S___(_plotter));
  _plotter->drawstate->x_label = NULL; /* restore label hint to default */

  _plotter->drawstate->font_name = saved_font_name;
  free (temp_font_name);

  if (!ok)			/* shouldn't happen */
    return 0.0;

  /* compute width of string in user units; see above comments on
     `compensating on both sides' */
  width = ((XTextWidth (_plotter->drawstate->x_font_struct, 
			(char *)s, 
			(int)(strlen((char *)s)))
	    *_plotter->drawstate->true_font_size)
	   / _plotter->drawstate->x_font_pixel_size);
  
  /* maybe flush X output buffer and handle X events (a no-op for
     XDrawablePlotters, which is overridden for XPlotters) */
  _maybe_handle_x_events (S___(_plotter));

  return width;
}