~ubuntu-branches/ubuntu/gutsy/plotutils/gutsy

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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 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 internal _pl_g_set_font method, which is called
   when the font_name, font_size, and textangle fields of the current
   drawing state have been filled in.  It retrieves the specified font, and
   fills in the true_font_name, true_font_size, font_type, typeface_index,
   font_index, font_is_iso8858_1, font_ascent, font_descent, and
   font_cap_height fields of the drawing state.

   _pl_g_set_font is invoked by _API_alabel() and _API_flabelwidth(), and
   also by _API_fontname(), _API_fontsize(), and _API_textangle() (only
   because those three functions in the API must return a font size).

   _pl_g_set_font does the following, in order:

   1. If font_name is the name of a supported Hershey font, i.e., one in
   libplot's database, it fills in the fields itself.

   2. If the font name is the name of a supported font of a non-Hershey
   type (i.e. PS/PCL/Stick), it _tentatively_ fills in the fields.  If the
   font name doesn't appear in libplot's internal database, it simply sets
   the font_type to `PL_F_OTHER'.

   3a. If the font name isn't the name of a supported Hershey font, it
   invokes the Plotter-specific `retrieve_font' method.  In most Plotters
   this simply returns true.  (See the stub _pl_g_retrieve_font() below.)
   But in principle it may modify the fields arbitrarily, perhaps consult
   an external database, etc.

   3b. If retrieve_font returns false, a default Hershey font is
   substituted, and the fields are filled in.  This feature is used e.g. by
   Fig Plotters, which support any of the 35 PS fonts in libplot's
   database, except when the user frame -> device frame map is anisotropic
   (i.e. non-uniform).  It is also used by X Drawable Plotters, which may
   or may not have a PS font available, and in fact may or may not have any
   given font of type `PL_F_OTHER' (see above) available. */

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

#define MAP_HERSHEY_UNITS_TO_USER_UNITS(size, drawstate) \
	((size)*(drawstate->true_font_size)/(HERSHEY_EM))

/* forward references */
static bool _match_hershey_font (plDrawState *drawstate);
static bool _match_ps_font (plDrawState *drawstate);
static bool _match_pcl_font (plDrawState *drawstate);
static bool _match_stick_font (plDrawState *drawstate, bool have_extra_stick_fonts);

void
_pl_g_set_font (S___(Plotter *_plotter))
{
  plDrawState *drawstate = _plotter->drawstate;
  plPlotterData *data = _plotter->data;
  const char *default_font_name;
  bool matched;

  /* try to match font name in our database */

  if (_match_hershey_font (drawstate))
    /* Matched a Hershey font name in our database, and all fields filled
       in definitively, so return without invoking Plotter-specific
       `retrieve_font', which knows nothing about Hershey fonts anyway */
    return;

  matched = false;

  /* Try to match the font name with the name of a PCL font or a PS font in
     the database, and tentatively fill in fields.  But there is a
     namespace collision: "Courier" etc. and "Symbol" occur on both lists.
     Which list we search first depends on what type of Plotter this is. */

  if (data->pcl_before_ps)
    {
      /* search PCL font list first */      
      if ((data->have_pcl_fonts 
	   && _match_pcl_font (drawstate))
	  ||
          (data->have_ps_fonts 
	   && _match_ps_font (drawstate)))
	matched = true;
    }
  else
    {
      /* search PS font list first */
      if ((data->have_ps_fonts 
	   && _match_ps_font (drawstate))
	  ||
	  (data->have_pcl_fonts 
	   && _match_pcl_font (drawstate)))
	matched = true;
    }

  /* if unmatched, search through Stick font list too */
  if (matched == false && data->have_stick_fonts
      && _match_stick_font (drawstate, 
			    data->have_extra_stick_fonts ? true : false))
    matched = true;

  if (matched == false)
    /* fill in the only fields we can */
    {
      free ((char *)drawstate->true_font_name);
      drawstate->true_font_name = 
	(const char *)_pl_xmalloc (strlen (drawstate->font_name) + 1);
      strcpy ((char *)drawstate->true_font_name, drawstate->font_name);
      drawstate->true_font_size = drawstate->font_size;

      drawstate->font_type = PL_F_OTHER;
      drawstate->typeface_index = 0;
      drawstate->font_index = 1;

      /* NOT filled in yet: font_is_iso8859_1, font_ascent, font_descent,
	 and font_cap_height; they're left to Plotter-specific retrieval
	 routine, if it can supply them (in the case of a Metafile Plotter,
	 it surely can't). */
    }

  /* If we got here, font name isn't that of a Hershey font, and `matched'
     indicates whether or not it's listed in our font name database, as one
     of the types of font this Plotter can handle.  So invoke low-level
     Plotter-specific retrieval routine.  Do it even if we haven't matched
     the font, if this Plotter claims to be able to handle `other' fonts,
     i.e., ones not listed in our database. */

  if (matched || (!matched && data->have_other_fonts))
    /* try to invoke low-level Plotter-specific routine to finish the job
       of filling in fields, including Plotter-specific ones */
    {
      if (_plotter->retrieve_font (S___(_plotter)))
	/* all finished... */
	return;
    }

  /* Plotter-specific retrieval failed: it doesn't like the font name or
     other drawstate parameters (size, textangle, transformation matrix?)  */

  /* Via a recursive call, try to retrieve default font for this Plotter
     type (if it's different from the one we just failed to retrieve;
     otherwise retrieve the default Hershey font).  */

  switch (data->default_font_type)
    {
    case PL_F_POSTSCRIPT:
      default_font_name = PL_DEFAULT_POSTSCRIPT_FONT;
      break;
    case PL_F_PCL:
      default_font_name = PL_DEFAULT_PCL_FONT;
      break;
    case PL_F_STICK:
      default_font_name = PL_DEFAULT_STICK_FONT;
      break;
    case PL_F_HERSHEY:		/* Hershey shouldn't happen */
    default:
      default_font_name = PL_DEFAULT_HERSHEY_FONT;
      break;

      /* N.B. No support yet for a Plotter-specific default font that is of
	 PL_F_OTHER type, i.e., isn't listed in the libplot font database. */
    }

  if (strcmp (drawstate->font_name, default_font_name) == 0
      || strcmp (drawstate->true_font_name, default_font_name) == 0)
    /* default font is the one we just failed to retrieve: so use Hershey:
       internal and guaranteed to be available */
    default_font_name = PL_DEFAULT_HERSHEY_FONT;
  
  /* stash fontname we failed to retrieve; then do recursive call, turning
     off font warnings for the duration; restore fontname field */
  {
    const char *saved_font_name;
    bool saved_font_warning_issued;

    saved_font_name = drawstate->font_name;
    drawstate->font_name = default_font_name;
    saved_font_warning_issued = _plotter->data->font_warning_issued;
    _plotter->data->font_warning_issued = true;	/* turn off warnings */
    _pl_g_set_font (S___(_plotter));
    _plotter->data->font_warning_issued = saved_font_warning_issued;
    drawstate->font_name = saved_font_name;
  }

  if (data->issue_font_warning && !_plotter->data->font_warning_issued)
    /* squawk */
    {
      char *buf;
      
      buf = (char *)_pl_xmalloc (strlen (drawstate->font_name) + strlen (drawstate->true_font_name) + 100);
      sprintf (buf, "cannot retrieve font \"%s\", using default \"%s\"", 
	       drawstate->font_name, drawstate->true_font_name);
      _plotter->warning (R___(_plotter) buf);
      free (buf);
      _plotter->data->font_warning_issued = true;
    }
}

static bool
_match_hershey_font (plDrawState *drawstate)
{
  int i;

  /* is font a Hershey font? */
  i = -1;
  while (_pl_g_hershey_font_info[++i].name)
    {
      if (_pl_g_hershey_font_info[i].visible) /* i.e. font not internal-only */
	if (strcasecmp (_pl_g_hershey_font_info[i].name, 
			drawstate->font_name) == 0
	    || (_pl_g_hershey_font_info[i].othername
		&& strcasecmp (_pl_g_hershey_font_info[i].othername, 
			       drawstate->font_name) == 0))
	  /* fill in fields */
	  {
	    free ((char *)drawstate->true_font_name);
	    drawstate->true_font_name = 
	      (const char *)_pl_xmalloc (strlen (_pl_g_hershey_font_info[i].name) + 1);
	    strcpy ((char *)drawstate->true_font_name, _pl_g_hershey_font_info[i].name);
	    drawstate->true_font_size = drawstate->font_size;

	    drawstate->font_type = PL_F_HERSHEY;
	    drawstate->typeface_index = _pl_g_hershey_font_info[i].typeface_index;
	    drawstate->font_index = _pl_g_hershey_font_info[i].font_index;
	    drawstate->font_is_iso8859_1 = _pl_g_hershey_font_info[i].iso8859_1;

	    /* N.B. this macro uses true_font_size */
	    drawstate->font_cap_height = 
	      MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_CAPHEIGHT, drawstate);
	    drawstate->font_ascent = 
	      MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_ASCENT, drawstate);
	    drawstate->font_descent = 
	      MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_DESCENT, drawstate);
	    
	    return true;
	  }
    }
  return false;
}

static bool
_match_pcl_font (plDrawState *drawstate)
{
  int i = -1;

  /* is font a PCL font in libplot's database? */
  while (_pl_g_pcl_font_info[++i].ps_name)
    {
      if (strcasecmp (_pl_g_pcl_font_info[i].ps_name, 
		      drawstate->font_name) == 0
	  /* try alternative PS font name if any */
	  || (_pl_g_pcl_font_info[i].ps_name_alt != NULL
	      && strcasecmp (_pl_g_pcl_font_info[i].ps_name_alt, 
			     drawstate->font_name) == 0)
	  /* try X font name too */
	  || strcasecmp (_pl_g_pcl_font_info[i].x_name, 
			 drawstate->font_name) == 0)
	{
	  free ((char *)drawstate->true_font_name);
	  drawstate->true_font_name = 
	    (const char *)_pl_xmalloc (strlen (_pl_g_pcl_font_info[i].ps_name) + 1);
	  strcpy ((char *)drawstate->true_font_name, _pl_g_pcl_font_info[i].ps_name);
	  drawstate->true_font_size = drawstate->font_size;

	  drawstate->font_type = PL_F_PCL;
	  drawstate->typeface_index = _pl_g_pcl_font_info[i].typeface_index;
	  drawstate->font_index = _pl_g_pcl_font_info[i].font_index;
	  drawstate->font_is_iso8859_1 = _pl_g_pcl_font_info[i].iso8859_1;

	  drawstate->font_ascent 
	    = drawstate->true_font_size
	      * (double)(_pl_g_pcl_font_info[i].font_ascent)/1000.0;
	  drawstate->font_descent 
	    = drawstate->true_font_size
	      * (double)(_pl_g_pcl_font_info[i].font_descent)/1000.0;
	  drawstate->font_cap_height 
	    = drawstate->true_font_size
	      * (double)(_pl_g_pcl_font_info[i].font_cap_height)/1000.0;
	  return true;
	}
    }
  return false;
}

static bool
_match_ps_font (plDrawState *drawstate)
{
  int i = -1;

  /* is font a PS font in libplot's database ? */
  while (_pl_g_ps_font_info[++i].ps_name)
    {
      if (strcasecmp (_pl_g_ps_font_info[i].ps_name, 
		      drawstate->font_name) == 0
	  /* try alternative PS font name if any */
	  || (_pl_g_ps_font_info[i].ps_name_alt != NULL
	      && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt, 
			     drawstate->font_name) == 0)
	  /* try 2nd alternative PS font name if any */
	  || (_pl_g_ps_font_info[i].ps_name_alt2 != NULL
	      && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt2, 
			     drawstate->font_name) == 0)
	  /* try X font name too */
	  || strcasecmp (_pl_g_ps_font_info[i].x_name, 
			 drawstate->font_name) == 0
	  /* try alternative X font name if any */
	  || (_pl_g_ps_font_info[i].x_name_alt != NULL
	      && strcasecmp (_pl_g_ps_font_info[i].x_name_alt,
			     drawstate->font_name) == 0))
	{
	  free ((char *)drawstate->true_font_name);
	  drawstate->true_font_name = 
	    (const char *)_pl_xmalloc (strlen (_pl_g_ps_font_info[i].ps_name) + 1);
	  strcpy ((char *)drawstate->true_font_name, _pl_g_ps_font_info[i].ps_name);

	  drawstate->true_font_size = drawstate->font_size;

	  drawstate->font_type = PL_F_POSTSCRIPT;
	  drawstate->typeface_index = _pl_g_ps_font_info[i].typeface_index;
	  drawstate->font_index = _pl_g_ps_font_info[i].font_index;
	  drawstate->font_is_iso8859_1 = _pl_g_ps_font_info[i].iso8859_1;

	  drawstate->font_ascent 
	    = drawstate->true_font_size
	      * (double)(_pl_g_ps_font_info[i].font_ascent)/1000.0;
	  drawstate->font_descent 
	    = drawstate->true_font_size
	      * (double)(_pl_g_ps_font_info[i].font_descent)/1000.0;
	  drawstate->font_cap_height
	    = drawstate->true_font_size
	      * (double)(_pl_g_ps_font_info[i].font_cap_height)/1000.0;
	  return true;
	}
    }
  return false;
}

static bool
_match_stick_font (plDrawState *drawstate, bool have_extra_stick_fonts)
{
  int i = -1;

  /* is font a PCL font in libplot's database? */
  while (_pl_g_stick_font_info[++i].ps_name)
    {
      if (_pl_g_stick_font_info[i].basic == false
	  && !have_extra_stick_fonts)
	/* not a basic Stick font, and this Plotter supports only the basic
	   ones */
	continue;
      
      if (strcasecmp (_pl_g_stick_font_info[i].ps_name, 
		      drawstate->font_name) == 0)
	/* fill in fields */
	{
	  free ((char *)drawstate->true_font_name);
	  drawstate->true_font_name = 
	    (const char *)_pl_xmalloc (strlen (_pl_g_stick_font_info[i].ps_name) + 1);
	  strcpy ((char *)drawstate->true_font_name, _pl_g_stick_font_info[i].ps_name);
	  drawstate->true_font_size = drawstate->font_size;
	  drawstate->true_font_size = drawstate->font_size;
	  drawstate->true_font_size = drawstate->font_size;

	  drawstate->font_type = PL_F_STICK;
	  drawstate->typeface_index = _pl_g_stick_font_info[i].typeface_index;
	  drawstate->font_index = _pl_g_stick_font_info[i].font_index;
	  drawstate->font_is_iso8859_1 = _pl_g_stick_font_info[i].iso8859_1;

	  drawstate->font_ascent 
	    = drawstate->true_font_size
	    * (double)(_pl_g_stick_font_info[i].font_ascent)/1000.0;
	  drawstate->font_descent 
	    = drawstate->true_font_size
	    * (double)(_pl_g_stick_font_info[i].font_descent)/1000.0;
	  drawstate->font_cap_height
	    /* The `0.7' is undocumented HP magic; see comments in
	       h_font.c and g_fontd2.c */
	    = 0.7 * drawstate->true_font_size;
	  
	  return true;
	}
    }
  return false;
}

/* This is the generic version of the _retrieve_font method, which does
   nothing.  Many types of Plotter use this, but some, e.g., FigPlotters
   and XDrawablePlotters (and X Plotters) override it.  See
   _pl_f_retrieve_font() and _pl_x_retrieve_font(). */

bool
_pl_g_retrieve_font (S___(Plotter *_plotter))
{
  return true;
}