PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ps.c
Go to the documentation of this file.
1 // PLplot PostScript device driver.
2 //
3 // Copyright (C) 1992, 2001 Geoffrey Furnish
4 // Copyright (C) 1992, 1993, 1994, 1995, 2001 Maurice LeBrun
5 // Copyright (C) 2000-2014 Alan W. Irwin
6 // Copyright (C) 2001, 2002 Joao Cardoso
7 // Copyright (C) 2001, 2003, 2004 Rafael Laboissiere
8 // Copyright (C) 2004, 2005 Thomas J. Duck
9 // Copyright (C) 2005 Andrew Ross
10 //
11 // This file is part of PLplot.
12 //
13 // PLplot is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU Library General Public License as published
15 // by the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // PLplot is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU Library General Public License
24 // along with PLplot; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 //
27 //
28 
29 #include "plDevs.h"
30 
31 #define DEBUG
32 
33 #ifdef PLD_ps
34 #define NEED_PLDEBUG
35 #include "plplotP.h"
36 #include "drivers.h"
37 #include "ps.h"
38 
39 #include <string.h>
40 #include <time.h>
41 #include "plunicode-type1.h"
42 #include "plfci-type1.h"
43 
44 // Define macro to truncate small values to zero - prevents
45 // printf printing -0.000
46 #define TRMFLT( a ) ( ( fabs( a ) < 5.0e-4 ) ? 0.0 : ( a ) )
47 
48 // Device info
49 
50 PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_ps =
51  "ps:PostScript File (monochrome):0:ps:29:psm\n"
52  "psc:PostScript File (color):0:ps:30:psc\n";
53 
54 
55 // Prototypes for functions in this file.
56 
59 
60 static char *ps_getdate( void );
61 static void ps_init( PLStream * );
62 static void fill_polygon( PLStream *pls );
63 static void proc_str( PLStream *, EscText * );
64 static void esc_purge( unsigned char *, unsigned char * );
65 
66 #define OUTBUF_LEN 128
67 static char outbuf[OUTBUF_LEN];
68 static int text = 1;
69 static int color;
70 static int hrshsym = 1;
71 
72 static DrvOpt ps_options[] = { { "text", DRV_INT, &text, "Use Postscript text (text=0|1)" },
73  { "color", DRV_INT, &color, "Use color (color=0|1)" },
74  { "hrshsym", DRV_INT, &hrshsym, "Use Hershey symbol set (hrshsym=0|1)" },
75  { NULL, DRV_INT, NULL, NULL } };
76 
77 static unsigned char
78 plunicode2type1( const PLUNICODE index,
79  const Unicode_to_Type1_table lookup[],
80  const int number_of_entries );
81 
82 static const char *
83 get_font( PSDev* dev, PLUNICODE fci );
84 
85 // text > 0 uses some postscript tricks, namely a transformation matrix
86 // that scales, rotates (with slanting) and offsets text strings.
87 // It has yet some bugs for 3d plots.
88 
89 
90 static void ps_dispatch_init_helper( PLDispatchTable *pdt,
91  const char *menustr, const char *devnam,
92  int type, int seq, plD_init_fp init )
93 {
94 #ifndef ENABLE_DYNDRIVERS
95  pdt->pl_MenuStr = (char *) menustr;
96  pdt->pl_DevName = (char *) devnam;
97 #else
98  (void) menustr; // Cast to void to silence compiler warnings about unused parameters
99  (void) devnam;
100 #endif
101  pdt->pl_type = type;
102  pdt->pl_seq = seq;
103  pdt->pl_init = init;
106  pdt->pl_eop = (plD_eop_fp) plD_eop_ps;
107  pdt->pl_bop = (plD_bop_fp) plD_bop_ps;
110  pdt->pl_esc = (plD_esc_fp) plD_esc_ps;
111 }
112 
114 {
115  ps_dispatch_init_helper( pdt,
116  "PostScript File (monochrome)", "ps",
119 }
120 
122 {
123  ps_dispatch_init_helper( pdt,
124  "PostScript File (color)", "psc",
127 }
128 
129 //--------------------------------------------------------------------------
130 // plD_init_ps()
131 //
132 // Initialize device.
133 //--------------------------------------------------------------------------
134 
135 void
137 {
138  color = 0;
139  pls->color = 0; // Not a color device
140 
141  plParseDrvOpts( ps_options );
142  if ( color )
143  pls->color = 1; // But user wants color
144  ps_init( pls );
145 }
146 
147 void
148 plD_init_psc( PLStream *pls )
149 {
150  color = 1;
151  pls->color = 1; // Is a color device
152  plParseDrvOpts( ps_options );
153 
154  if ( !color )
155  pls->color = 0; // But user does not want color
156  ps_init( pls );
157 }
158 
159 static void
160 ps_init( PLStream *pls )
161 {
162  PSDev *dev;
163 
164  PLFLT pxlx, pxly;
165 
166  // Set default values - 7.5 x 10 [inches] (72 points = 1 inch)
167  if ( pls->xlength <= 0 || pls->ylength <= 0 )
168  {
169  pls->xlength = 540;
170  pls->ylength = 720;
171  pls->xoffset = 32;
172  pls->yoffset = 32;
173  }
174  if ( pls->xdpi <= 0 )
175  pls->xdpi = 72.;
176  if ( pls->ydpi <= 0 )
177  pls->ydpi = 72.;
178 
179  pxlx = YPSSIZE / LPAGE_X;
180  pxly = XPSSIZE / LPAGE_Y;
181 
182  if ( text )
183  {
184  pls->dev_text = 1; // want to draw text
185  pls->dev_unicode = 1; // want unicode
186  if ( hrshsym )
187  pls->dev_hrshsym = 1; // want Hershey symbols
188  }
189 
190  pls->dev_fill0 = 1; // Can do solid fills
191 
192 // Initialize family file info
193 
194  plFamInit( pls );
195 
196 // Prompt for a file name if not already set
197 
198  plOpenFile( pls );
199 
200 // Allocate and initialize device-specific data
201 
202  if ( pls->dev != NULL )
203  free( (void *) pls->dev );
204 
205  pls->dev = calloc( 1, (size_t) sizeof ( PSDev ) );
206  if ( pls->dev == NULL )
207  plexit( "ps_init: Out of memory." );
208 
209  dev = (PSDev *) pls->dev;
210 
211  dev->xold = PL_UNDEFINED;
212  dev->yold = PL_UNDEFINED;
213 
214  plP_setpxl( pxlx, pxly );
215 
216  dev->llx = XPSSIZE;
217  dev->lly = YPSSIZE;
218  dev->urx = 0;
219  dev->ury = 0;
220  dev->ptcnt = 0;
221 
222 // Rotate by 90 degrees since portrait mode addressing is used
223 
224  dev->xmin = 0;
225  dev->ymin = 0;
226  dev->xmax = PSY;
227  dev->ymax = PSX;
228  dev->xlen = dev->xmax - dev->xmin;
229  dev->ylen = dev->ymax - dev->ymin;
230 
231  plP_setphy( dev->xmin, dev->xmax, dev->ymin, dev->ymax );
232 
233 // If portrait mode is specified, then set up an additional rotation
234 // transformation with aspect ratio allowed to adjust via freeaspect.
235 // Default orientation is landscape (ORIENTATION == 3 or 90 deg rotation
236 // counter-clockwise from portrait). (Legacy PLplot used seascape
237 // which was equivalent to ORIENTATION == 1 or 90 deg clockwise rotation
238 // from portrait.)
239 
240  if ( pls->portrait )
241  {
242  plsdiori( (PLFLT) ( 4 - ORIENTATION ) );
243  pls->freeaspect = 1;
244  }
245 
246 // Header comments into PostScript file
247 
248  fprintf( OF, "%%!PS-Adobe-2.0 EPSF-2.0\n" );
249  fprintf( OF, "%%%%BoundingBox: \n" );
250  fprintf( OF, "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" );
251 
252  fprintf( OF, "%%%%Title: PLplot Graph\n" );
253  fprintf( OF, "%%%%Creator: PLplot Version %s\n", PLPLOT_VERSION );
254  fprintf( OF, "%%%%CreationDate: %s\n", ps_getdate() );
255  fprintf( OF, "%%%%Pages: (atend)\n" );
256  fprintf( OF, "%%%%EndComments\n\n" );
257 
258 // Definitions
259 // Save VM state
260 
261  fprintf( OF, "/PSSave save def\n" );
262 
263 // Define a dictionary and start using it
264 
265  fprintf( OF, "/PSDict 200 dict def\n" );
266  fprintf( OF, "PSDict begin\n" );
267 
268  fprintf( OF, "/@restore /restore load def\n" );
269  fprintf( OF, "/restore\n" );
270  fprintf( OF, " {vmstatus pop\n" );
271  fprintf( OF, " dup @VMused lt {pop @VMused} if\n" );
272  fprintf( OF, " exch pop exch @restore /@VMused exch def\n" );
273  fprintf( OF, " } def\n" );
274  fprintf( OF, "/@pri\n" );
275  fprintf( OF, " {\n" );
276  fprintf( OF, " ( ) print\n" );
277  fprintf( OF, " ( ) cvs print\n" );
278  fprintf( OF, " } def\n" );
279 
280 // n @copies -
281 
282  fprintf( OF, "/@copies\n" );
283  fprintf( OF, " {\n" );
284  fprintf( OF, " /#copies exch def\n" );
285  fprintf( OF, " } def\n" );
286 
287 // - @start - -- start everything
288 
289  fprintf( OF, "/@start\n" );
290  fprintf( OF, " {\n" );
291  fprintf( OF, " vmstatus pop /@VMused exch def pop\n" );
292  fprintf( OF, " } def\n" );
293 
294 // - @end - -- finished
295 
296  fprintf( OF, "/@end\n" );
297  fprintf( OF, " {flush\n" );
298  fprintf( OF, " end\n" );
299  fprintf( OF, " PSSave restore\n" );
300  fprintf( OF, " } def\n" );
301 
302 // bop - -- begin a new page
303 // Only fill background if we are using color and if the bg isn't white
304 
305  fprintf( OF, "/bop\n" );
306  fprintf( OF, " {\n" );
307  fprintf( OF, " /SaveImage save def\n" );
308  fprintf( OF, " } def\n" );
309 
310 // - eop - -- end a page
311 
312  fprintf( OF, "/eop\n" );
313  fprintf( OF, " {\n" );
314  fprintf( OF, " showpage\n" );
315  fprintf( OF, " SaveImage restore\n" );
316  fprintf( OF, " } def\n" );
317 
318 // Set line parameters
319 
320  fprintf( OF, "/@line\n" );
321  fprintf( OF, " {0 setlinecap\n" );
322  fprintf( OF, " 0 setlinejoin\n" );
323  fprintf( OF, " 1 setmiterlimit\n" );
324  fprintf( OF, " } def\n" );
325 
326 // d @hsize - horizontal clipping dimension
327 
328  fprintf( OF, "/@hsize {/hs exch def} def\n" );
329  fprintf( OF, "/@vsize {/vs exch def} def\n" );
330 
331 // d @hoffset - shift for the plots
332 
333  fprintf( OF, "/@hoffset {/ho exch def} def\n" );
334  fprintf( OF, "/@voffset {/vo exch def} def\n" );
335 
336 // Set line width
337 
338  fprintf( OF, "/lw %d def\n", (int) (
339  ( pls->width < MIN_WIDTH ) ? DEF_WIDTH :
340  ( pls->width > MAX_WIDTH ) ? MAX_WIDTH : pls->width ) );
341 
342 // Setup user specified offsets, scales, sizes for clipping
343 
344  fprintf( OF, "/@SetPlot\n" );
345  fprintf( OF, " {\n" );
346  fprintf( OF, " ho vo translate\n" );
347  fprintf( OF, " XScale YScale scale\n" );
348  fprintf( OF, " lw setlinewidth\n" );
349  fprintf( OF, " } def\n" );
350 
351 // Setup x & y scales
352 
353  fprintf( OF, "/XScale\n" );
354  fprintf( OF, " {hs %d div} def\n", YPSSIZE );
355  fprintf( OF, "/YScale\n" );
356  fprintf( OF, " {vs %d div} def\n", XPSSIZE );
357 
358 // Macro definitions of common instructions, to keep output small
359 
360  fprintf( OF, "/M {moveto} def\n" );
361  fprintf( OF, "/D {lineto} def\n" );
362  fprintf( OF, "/A {0.5 0 360 arc} def\n" );
363  fprintf( OF, "/S {stroke} def\n" );
364  fprintf( OF, "/Z {stroke newpath} def\n" );
365  // Modify to use fill and stroke for better output with
366  // anti-aliasing
367  //fprintf(OF, "/F {fill} def\n");
368  if ( pls->dev_eofill )
369  fprintf( OF, "/F {closepath gsave eofill grestore stroke} def " );
370  else
371  fprintf( OF, "/F {closepath gsave fill grestore stroke} def " );
372  fprintf( OF, "/N {newpath} def" );
373  fprintf( OF, "/C {setrgbcolor} def\n" );
374  fprintf( OF, "/G {setgray} def\n" );
375  fprintf( OF, "/W {setlinewidth} def\n" );
376  fprintf( OF, "/SF {selectfont} def\n" );
377  fprintf( OF, "/R {rotate} def\n" );
378  fprintf( OF, "/SW {stringwidth 2 index mul exch 2 index mul exch rmoveto pop} bind def\n" );
379  fprintf( OF, "/B {Z %d %d M %d %d D %d %d D %d %d D %d %d closepath} def\n",
380  XMIN, YMIN, XMIN, YMAX, XMAX, YMAX, XMAX, YMIN, XMIN, YMIN );
381  fprintf( OF, "/CL {newpath M D D D closepath clip} def\n" );
382 
383 // End of dictionary definition
384 
385  fprintf( OF, "end\n\n" );
386 
387 // Set up the plots
388 
389  fprintf( OF, "PSDict begin\n" );
390  fprintf( OF, "@start\n" );
391  fprintf( OF, "%d @copies\n", COPIES );
392  fprintf( OF, "@line\n" );
393  fprintf( OF, "%d @hsize\n", YSIZE );
394  fprintf( OF, "%d @vsize\n", XSIZE );
395  fprintf( OF, "%d @hoffset\n", YOFFSET );
396  fprintf( OF, "%d @voffset\n", XOFFSET );
397 
398  fprintf( OF, "@SetPlot\n\n" );
399 }
400 
401 //--------------------------------------------------------------------------
402 // plD_line_ps()
403 //
404 // Draw a line in the current color from (x1,y1) to (x2,y2).
405 //--------------------------------------------------------------------------
406 
407 void
408 plD_line_ps( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
409 {
410  PSDev *dev = (PSDev *) pls->dev;
411  PLINT x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
412 
413 // Rotate by 90 degrees
414 
415  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax, &x1, &y1 );
416  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax, &x2, &y2 );
417 
418  if ( x1 == dev->xold && y1 == dev->yold && dev->ptcnt < 40 )
419  {
420  if ( pls->linepos + 12 > LINELENGTH )
421  {
422  putc( '\n', OF );
423  pls->linepos = 0;
424  }
425  else
426  putc( ' ', OF );
427 
428  snprintf( outbuf, OUTBUF_LEN, "%d %d D", x2, y2 );
429  dev->ptcnt++;
430  pls->linepos += 12;
431  }
432  else
433  {
434  fprintf( OF, " Z\n" );
435  pls->linepos = 0;
436 
437  if ( x1 == x2 && y1 == y2 ) // must be a single dot, draw a circle
438  snprintf( outbuf, OUTBUF_LEN, "%d %d A", x1, y1 );
439  else
440  snprintf( outbuf, OUTBUF_LEN, "%d %d M %d %d D", x1, y1, x2, y2 );
441  dev->llx = MIN( dev->llx, x1 );
442  dev->lly = MIN( dev->lly, y1 );
443  dev->urx = MAX( dev->urx, x1 );
444  dev->ury = MAX( dev->ury, y1 );
445  dev->ptcnt = 1;
446  pls->linepos += 24;
447  }
448  dev->llx = MIN( dev->llx, x2 );
449  dev->lly = MIN( dev->lly, y2 );
450  dev->urx = MAX( dev->urx, x2 );
451  dev->ury = MAX( dev->ury, y2 );
452 
453  fprintf( OF, "%s", outbuf );
454  pls->bytecnt += 1 + (PLINT) strlen( outbuf );
455  dev->xold = x2;
456  dev->yold = y2;
457 }
458 
459 //--------------------------------------------------------------------------
460 // plD_polyline_ps()
461 //
462 // Draw a polyline in the current color.
463 //--------------------------------------------------------------------------
464 
465 void
466 plD_polyline_ps( PLStream *pls, short *xa, short *ya, PLINT npts )
467 {
468  PLINT i;
469 
470  for ( i = 0; i < npts - 1; i++ )
471  plD_line_ps( pls, xa[i], ya[i], xa[i + 1], ya[i + 1] );
472 }
473 
474 //--------------------------------------------------------------------------
475 // plD_eop_ps()
476 //
477 // End of page.
478 //--------------------------------------------------------------------------
479 
480 void
481 plD_eop_ps( PLStream *pls )
482 {
483  fprintf( OF, " S\neop\n" );
484 }
485 
486 //--------------------------------------------------------------------------
487 // plD_bop_ps()
488 //
489 // Set up for the next page.
490 // Advance to next family file if necessary (file output).
491 //--------------------------------------------------------------------------
492 
493 void
494 plD_bop_ps( PLStream *pls )
495 {
496  PSDev *dev = (PSDev *) pls->dev;
497 
498  dev->xold = PL_UNDEFINED;
499  dev->yold = PL_UNDEFINED;
500 
501  if ( !pls->termin )
502  plGetFam( pls );
503 
504  pls->page++;
505 
506  if ( pls->family )
507  fprintf( OF, "%%%%Page: %d %d\n", (int) pls->page, 1 );
508  else
509  fprintf( OF, "%%%%Page: %d %d\n", (int) pls->page, (int) pls->page );
510 
511  fprintf( OF, "bop\n" );
512  if ( pls->color )
513  {
514  PLFLT r, g, b;
515  if ( pls->cmap0[0].r != 0xFF ||
516  pls->cmap0[0].g != 0xFF ||
517  pls->cmap0[0].b != 0xFF )
518  {
519  r = ( (PLFLT) pls->cmap0[0].r ) / 255.;
520  g = ( (PLFLT) pls->cmap0[0].g ) / 255.;
521  b = ( (PLFLT) pls->cmap0[0].b ) / 255.;
522 
523  fprintf( OF, "B %.4f %.4f %.4f C F\n", r, g, b );
524  }
525  }
526  pls->linepos = 0;
527 
528 // This ensures the color and line width are set correctly at the beginning of
529 // each page
530 
532  plD_state_ps( pls, PLSTATE_WIDTH );
533 }
534 
535 //--------------------------------------------------------------------------
536 // plD_tidy_ps()
537 //
538 // Close graphics file or otherwise clean up.
539 //--------------------------------------------------------------------------
540 
541 void
542 plD_tidy_ps( PLStream *pls )
543 {
544  PSDev *dev = (PSDev *) pls->dev;
545 
546  fprintf( OF, "\n%%%%Trailer\n" );
547 
548  dev->llx /= ENLARGE;
549  dev->lly /= ENLARGE;
550  dev->urx /= ENLARGE;
551  dev->ury /= ENLARGE;
552  dev->llx += XOFFSET;
553  dev->lly += YOFFSET;
554  dev->urx += XOFFSET;
555  dev->ury += YOFFSET;
556 
557 // changed for correct Bounding boundaries Jan Thorbecke okt 1993
558 // occurs from the integer truncation -- postscript uses fp arithmetic
559 
560  dev->urx += 1;
561  dev->ury += 1;
562 
563  if ( pls->family )
564  fprintf( OF, "%%%%Pages: %d\n", (int) 1 );
565  else
566  fprintf( OF, "%%%%Pages: %d\n", (int) pls->page );
567 
568  fprintf( OF, "@end\n" );
569  fprintf( OF, "%%%%EOF\n" );
570 
571 // Backtrack to write the BoundingBox at the beginning
572 // Some applications don't like it atend
573 
574  rewind( OF );
575  fprintf( OF, "%%!PS-Adobe-2.0 EPSF-2.0\n" );
576  fprintf( OF, "%%%%BoundingBox: %d %d %d %d\n",
577  dev->llx, dev->lly, dev->urx, dev->ury );
578  plCloseFile( pls );
579 }
580 
581 //--------------------------------------------------------------------------
582 // plD_state_ps()
583 //
584 // Handle change in PLStream state (color, pen width, fill attribute, etc).
585 //--------------------------------------------------------------------------
586 
587 void
588 plD_state_ps( PLStream *pls, PLINT op )
589 {
590  PSDev *dev = (PSDev *) pls->dev;
591 
592  switch ( op )
593  {
594  case PLSTATE_WIDTH: {
595  int width = (int) (
596  ( pls->width < MIN_WIDTH ) ? DEF_WIDTH :
597  ( pls->width > MAX_WIDTH ) ? MAX_WIDTH : pls->width );
598 
599  fprintf( OF, " S\n%d W", width );
600 
601  dev->xold = PL_UNDEFINED;
602  dev->yold = PL_UNDEFINED;
603  break;
604  }
605  case PLSTATE_COLOR0:
606  if ( !pls->color )
607  {
608  fprintf( OF, " S\n%.4f G", ( pls->icol0 ? 0.0 : 1.0 ) );
609  break;
610  }
611  // else fallthrough
612  case PLSTATE_COLOR1:
613  if ( pls->color )
614  {
615  PLFLT r = ( (PLFLT) pls->curcolor.r ) / 255.0;
616  PLFLT g = ( (PLFLT) pls->curcolor.g ) / 255.0;
617  PLFLT b = ( (PLFLT) pls->curcolor.b ) / 255.0;
618 
619  fprintf( OF, " S\n%.4f %.4f %.4f C", r, g, b );
620  }
621  else
622  {
623  PLFLT r = ( (PLFLT) pls->curcolor.r ) / 255.0;
624  fprintf( OF, " S\n%.4f G", 1.0 - r );
625  }
626  break;
627  }
628 
629 // Reinitialize current point location.
630 
631  if ( dev->xold != PL_UNDEFINED && dev->yold != PL_UNDEFINED )
632  {
633  fprintf( OF, " %d %d M \n", (int) dev->xold, (int) dev->yold );
634  }
635 }
636 
637 //--------------------------------------------------------------------------
638 // plD_esc_ps()
639 //
640 // Escape function.
641 //--------------------------------------------------------------------------
642 
643 void
644 plD_esc_ps( PLStream *pls, PLINT op, void *ptr )
645 {
646  switch ( op )
647  {
648  case PLESC_FILL:
649  fill_polygon( pls );
650  break;
651  case PLESC_HAS_TEXT:
652  proc_str( pls, (EscText *) ptr );
653  break;
654  }
655 }
656 
657 //--------------------------------------------------------------------------
658 // fill_polygon()
659 //
660 // Fill polygon described in points pls->dev_x[] and pls->dev_y[].
661 // Only solid color fill supported.
662 //--------------------------------------------------------------------------
663 
664 static void
665 fill_polygon( PLStream *pls )
666 {
667  PSDev *dev = (PSDev *) pls->dev;
668  PLINT n, ix = 0, iy = 0;
669  PLINT x, y;
670 
671  fprintf( OF, " Z\n" );
672 
673  for ( n = 0; n < pls->dev_npts; n++ )
674  {
675  x = pls->dev_x[ix++];
676  y = pls->dev_y[iy++];
677 
678 // Rotate by 90 degrees
679 
680  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax, &x, &y );
681 
682 // First time through start with a x y moveto
683 
684  if ( n == 0 )
685  {
686  snprintf( outbuf, OUTBUF_LEN, "N %d %d M", x, y );
687  dev->llx = MIN( dev->llx, x );
688  dev->lly = MIN( dev->lly, y );
689  dev->urx = MAX( dev->urx, x );
690  dev->ury = MAX( dev->ury, y );
691  fprintf( OF, "%s", outbuf );
692  pls->bytecnt += (PLINT) strlen( outbuf );
693  continue;
694  }
695 
696  if ( pls->linepos + 21 > LINELENGTH )
697  {
698  putc( '\n', OF );
699  pls->linepos = 0;
700  }
701  else
702  putc( ' ', OF );
703 
704  pls->bytecnt++;
705 
706  snprintf( outbuf, OUTBUF_LEN, "%d %d D", x, y );
707  dev->llx = MIN( dev->llx, x );
708  dev->lly = MIN( dev->lly, y );
709  dev->urx = MAX( dev->urx, x );
710  dev->ury = MAX( dev->ury, y );
711 
712  fprintf( OF, "%s", outbuf );
713  pls->bytecnt += (PLINT) strlen( outbuf );
714  pls->linepos += 21;
715  }
716  dev->xold = PL_UNDEFINED;
717  dev->yold = PL_UNDEFINED;
718  fprintf( OF, " F " );
719 }
720 
721 //--------------------------------------------------------------------------
722 // ps_getdate()
723 //
724 // Get the date and time
725 //--------------------------------------------------------------------------
726 
727 static char *
728 ps_getdate( void )
729 {
730  int len;
731  time_t t;
732  char *p;
733 
734  t = time( (time_t *) 0 );
735  p = ctime( &t );
736  len = (int) strlen( p );
737  *( p + len - 1 ) = '\0'; // zap the newline character
738  return p;
739 }
740 
741 
742 // 0.8 should mimic the offset of first superscript/subscript level
743 // implemented in plstr (plsym.c) for Hershey fonts. However, when
744 // comparing with -dev xwin and -dev xcairo results changing this
745 // factor to 0.6 appears to offset the centers of the letters
746 // appropriately while 0.8 gives much poorer agreement with the
747 // other devices.
748 # define RISE_FACTOR 0.6
749 
750 //--------------------------------------------------------------------------
751 // proc_str()
752 //
753 // Prints postscript strings.
754 // N.B. Now unicode only, no string access!
755 //
756 //--------------------------------------------------------------------------
757 
758 void
759 proc_str( PLStream *pls, EscText *args )
760 {
761  PLFLT *t = args->xform, tt[4]; // Transform matrices
762  PLFLT theta, shear, stride; // Rotation angle and shear from the matrix
763  PLFLT ft_ht, offset; // Font height and offset
764  PLFLT cs, sn, l1, l2;
765  PSDev *dev = (PSDev *) pls->dev;
766  const char *font;
767  char esc;
768  // Be generous. Used to store lots of font changes which take
769  // 3 characters per change.
770  #define PROC_STR_STRING_LENGTH 1000
771  unsigned char *strp, str[PROC_STR_STRING_LENGTH], *cur_strp,
772  cur_str[PROC_STR_STRING_LENGTH];
773  float font_factor = 1.4f;
774  PLINT clxmin, clxmax, clymin, clymax; // Clip limits
775  PLINT clipx[4], clipy[4]; // Current clip limits
776 
777  PLFLT scale = 1., up = 0.; // Font scaling and shifting parameters
778 
779  int i = 0; // String index
780 
781  // unicode only! so test for it.
782  if ( args->unicode_array_len > 0 )
783  {
784  int j, s, f;
785  const char *fonts[PROC_STR_STRING_LENGTH];
786  const PLUNICODE *cur_text;
787  PLUNICODE fci, fci_save;
788  PLFLT old_sscale, sscale, old_soffset, soffset, ddup;
789  PLINT level = 0;
790  // translate from unicode into type 1 font index.
791  //
792  // Choose the font family, style, variant, and weight using
793  // the FCI (font characterization integer).
794  //
795 
796  plgesc( &esc );
797  plgfci( &fci );
798  fci_save = fci;
799  font = get_font( dev, fci );
800  cur_text = args->unicode_array;
801  for ( f = s = j = 0; j < args->unicode_array_len; j++ )
802  {
803  if ( cur_text[j] & PL_FCI_MARK )
804  {
805  // process an FCI by saving it and escaping cur_str
806  // with an escff to make it a 2-character escape
807  // that is not used in legacy Hershey code
808  //
809  if ( ( f < PROC_STR_STRING_LENGTH ) && ( s + 3 < PROC_STR_STRING_LENGTH ) )
810  {
811  fci_save = cur_text[j];
812  fonts[f++] = get_font( dev, fci_save );
813  cur_str[s++] = (unsigned char) esc;
814  cur_str[s++] = 'f';
815  cur_str[s++] = 'f';
816  }
817  }
818  else if ( s + 4 < PROC_STR_STRING_LENGTH )
819  {
820 #undef PL_TEST_TYPE1
821 #ifdef PL_TEST_TYPE1
822  // Use this test case only to conveniently view Type1 font
823  // possibilities (as in test_type1.py example).
824  // This functionality is useless other than for this test case.
825  PLINT ifamily, istyle, iweight;
826  plgfont( &ifamily, &istyle, &iweight );
827  if ( 0 <= cur_text[j] && cur_text[j] < 256 )
828  cur_str[s++] = cur_text[j];
829  else
830  cur_str[s++] = 32;
831  // Overwrite font just for this special case.
832  if ( ifamily == PL_FCI_SYMBOL )
833  font = get_font( dev, 0 );
834  else
835  font = get_font( dev, fci );
836 #else
837  cur_str[s] = plunicode2type1( cur_text[j], dev->lookup, dev->nlookup );
838  if ( cur_text[j] != ' ' && cur_str[s] == ' ' )
839  {
840  // failed lookup.
841  if ( !dev->if_symbol_font )
842  {
843  // failed standard font lookup. Use symbol
844  // font instead which will return a blank if
845  // that fails as well.
846  fonts[f++] = get_font( dev, 0 );
847  cur_str[s++] = (unsigned char) esc;
848  cur_str[s++] = 'f';
849  cur_str[s++] = 'f';
850  cur_str[s++] = plunicode2type1( cur_text[j], dev->lookup, dev->nlookup );
851  }
852  else
853  {
854  // failed symbol font lookup. Use last standard
855  // font instead which will return a blank if
856  // that fails as well.
857  fonts[f++] = get_font( dev, fci_save );
858  cur_str[s++] = (unsigned char) esc;
859  cur_str[s++] = 'f';
860  cur_str[s++] = 'f';
861  cur_str[s++] = plunicode2type1( cur_text[j], dev->lookup, dev->nlookup );
862  }
863  }
864  else
865  {
866  // lookup succeeded.
867  s++;
868  }
869 #endif
870  pldebug( "proc_str", "unicode = 0x%x, type 1 code = %d\n",
871  cur_text[j], cur_str[s - 1] );
872  }
873  }
874  cur_str[s] = '\0';
875 
876  // finish previous polyline
877 
878  dev->xold = PL_UNDEFINED;
879  dev->yold = PL_UNDEFINED;
880 
881  // Determine the font height
882  ft_ht = pls->chrht * 72.0 / 25.4; // ft_ht in points, ht is in mm
883 
884 
885  // The transform matrix has only rotations and shears; extract them
886  plRotationShear( t, &theta, &shear, &stride );
887  cs = cos( theta );
888  sn = sin( theta );
889  tt[0] = t[0] * cs + t[2] * sn;
890  tt[1] = t[1] * cs + t[3] * sn;
891  tt[2] = -t[0] * sn + t[2] * cs;
892  tt[3] = -t[1] * sn + t[3] * cs;
893 
894  //
895  // Reference point conventions:
896  // If base = 0, it is aligned with the center of the text box
897  // If base = 1, it is aligned with the baseline of the text box
898  // If base = 2, it is aligned with the top of the text box
899  //
900  // Currently plplot only uses base=0
901  // Postscript uses base=1
902  //
903  // We must calculate the difference between the two and apply the offset.
904  //
905 
906  if ( args->base == 2 ) // not supported by plplot
907  offset = ENLARGE * ft_ht / 2.; // half font height
908  else if ( args->base == 1 )
909  offset = 0.;
910  else
911  offset = -ENLARGE * ft_ht / 2.;
912 
913  // Determine the adjustment for page orientation
914  theta -= PI / 2. * pls->diorot;
915  args->y += (PLINT) ( offset * cos( theta ) );
916  args->x -= (PLINT) ( offset * sin( theta ) );
917 
918  // ps driver is rotated by default
919  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
920  &( args->x ), &( args->y ) );
921 
922  // Correct for the fact ps driver uses landscape by default
923  theta += PI / 2.;
924 
925  // Output
926  // Set clipping
927  clipx[0] = pls->clpxmi;
928  clipx[2] = pls->clpxma;
929  clipy[0] = pls->clpymi;
930  clipy[2] = pls->clpyma;
931  clipx[1] = clipx[2];
932  clipy[1] = clipy[0];
933  clipx[3] = clipx[0];
934  clipy[3] = clipy[2];
935  difilt( clipx, clipy, 4, &clxmin, &clxmax, &clymin, &clymax );
936  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
937  &clipx[0], &clipy[0] );
938  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
939  &clipx[1], &clipy[1] );
940  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
941  &clipx[2], &clipy[2] );
942  plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
943  &clipx[3], &clipy[3] );
944  fprintf( OF, " gsave %d %d %d %d %d %d %d %d CL\n", clipx[0], clipy[0], clipx[1], clipy[1], clipx[2], clipy[2], clipx[3], clipy[3] );
945 
946  // move to string reference point
947  fprintf( OF, " %d %d M\n", args->x, args->y );
948 
949  // Save the current position and set the string rotation
950  fprintf( OF, "gsave %.3f R\n", TRMFLT( theta * 180. / PI ) );
951 
952  // Purge escape sequences from string, so that postscript can find it's
953  // length. The string length is computed with the current font, and can
954  // thus be wrong if there are font change escape sequences in the string
955  //
956 
957  esc_purge( str, cur_str );
958 
959  fprintf( OF, "/%s %.3f SF\n", font, TRMFLT( font_factor * ENLARGE * ft_ht ) );
960 
961  // Output string, while escaping the '(', ')' and '\' characters.
962  // this string is output for measurement purposes only.
963  //
964  fprintf( OF, "%.3f (", TRMFLT( -args->just ) );
965  while ( str[i] != '\0' )
966  {
967  if ( str[i] == '(' || str[i] == ')' || str[i] == '\\' )
968  fprintf( OF, "\\%c", str[i] );
969  else
970  fprintf( OF, "%c", str[i] );
971  i++;
972  }
973  fprintf( OF, ") SW\n" );
974 
975 
976  // Parse string for PLplot escape sequences and print everything out
977 
978  cur_strp = cur_str;
979  f = 0;
980  do
981  {
982  strp = str;
983 
984  if ( *cur_strp == esc )
985  {
986  cur_strp++;
987 
988  if ( *cur_strp == esc ) // <esc><esc>
989  {
990  *strp++ = *cur_strp++;
991  }
992  else if ( *cur_strp == 'f' )
993  {
994  cur_strp++;
995  if ( *cur_strp++ != 'f' )
996  {
997  // escff occurs because of logic above. But any suffix
998  // other than "f" should never happen.
999  plabort( "proc_str, internal PLplot logic error;"
1000  "wrong escf escape sequence" );
1001  return;
1002  }
1003  font = fonts[f++];
1004  pldebug( "proc_str", "string-specified fci = 0x%x, font name = %s\n", fci, font );
1005  continue;
1006  }
1007  else
1008  switch ( *cur_strp++ )
1009  {
1010  case 'd': //subscript
1011  case 'D':
1012  plP_script_scale( FALSE, &level,
1013  &old_sscale, &sscale, &old_soffset, &soffset );
1014  scale = sscale;
1015  // The correction for the difference in magnitude
1016  // between the baseline and middle coordinate systems
1017  // for subscripts should be
1018  // -0.5*(base font size - superscript/subscript font size).
1019  ddup = -0.5 * ( 1.0 - sscale );
1020  up = -font_factor * ENLARGE * ft_ht * ( RISE_FACTOR * soffset + ddup );
1021  break;
1022 
1023  case 'u': //superscript
1024  case 'U':
1025  plP_script_scale( TRUE, &level,
1026  &old_sscale, &sscale, &old_soffset, &soffset );
1027  scale = sscale;
1028  // The correction for the difference in magnitude
1029  // between the baseline and middle coordinate systems
1030  // for superscripts should be
1031  // 0.5*(base font size - superscript/subscript font size).
1032  ddup = 0.5 * ( 1.0 - sscale );
1033  up = font_factor * ENLARGE * ft_ht * ( RISE_FACTOR * soffset + ddup );
1034  break;
1035 
1036  // ignore the next sequences
1037 
1038  case '+':
1039  case '-':
1040  case 'b':
1041  case 'B':
1042  plwarn( "'+', '-', and 'b/B' text escape sequences not processed." );
1043  break;
1044  }
1045  }
1046 
1047  // copy from current to next token, adding a postscript escape
1048  // char '\' if necessary
1049  //
1050  while ( *cur_strp && *cur_strp != esc )
1051  {
1052  if ( *cur_strp == '(' || *cur_strp == ')' || *cur_strp == '\\' )
1053  *strp++ = '\\';
1054  *strp++ = *cur_strp++;
1055  }
1056  *strp = '\0';
1057 
1058  if ( fabs( up ) < 0.001 )
1059  up = 0.; // Watch out for small differences
1060 
1061  // Apply the scaling and the shear
1062  fprintf( OF, "/%s [%.3f %.3f %.3f %.3f 0 0] SF\n",
1063  font,
1064  TRMFLT( tt[0] * font_factor * ENLARGE * ft_ht * scale ),
1065  TRMFLT( tt[2] * font_factor * ENLARGE * ft_ht * scale ),
1066  TRMFLT( tt[1] * font_factor * ENLARGE * ft_ht * scale ),
1067  TRMFLT( tt[3] * font_factor * ENLARGE * ft_ht * scale ) );
1068 
1069  // if up/down escape sequences, save current point and adjust baseline;
1070  // take the shear into account
1071  if ( up != 0. )
1072  fprintf( OF, "gsave %.3f %.3f rmoveto\n", TRMFLT( up * tt[1] ), TRMFLT( up * tt[3] ) );
1073 
1074  // print the string
1075  fprintf( OF, "(%s) show\n", str );
1076 
1077  // back to baseline
1078  if ( up != 0. )
1079  fprintf( OF, "grestore (%s) stringwidth rmoveto\n", str );
1080  } while ( *cur_strp );
1081 
1082  fprintf( OF, "grestore\n" );
1083  fprintf( OF, "grestore\n" );
1084 
1085  //
1086  // keep driver happy -- needed for background and orientation.
1087  // arghhh! can't calculate it, as I only have the string reference
1088  // point, not its extent!
1089  // Still a hack - but at least it takes into account the string
1090  // length and justification. Character width is assumed to be
1091  // 0.6 * character height. Add on an extra 1.5 * character height
1092  // for safety.
1093  //
1094  cs = cos( theta );
1095  sn = sin( theta );
1096  l1 = -i * args->just;
1097  l2 = i * ( 1. - args->just );
1098  // Factor of 0.6 is an empirical fudge to convert character
1099  // height to average character width
1100  l1 *= 0.6;
1101  l2 *= 0.6;
1102 
1103  dev->llx = (int) ( MIN( dev->llx, args->x + ( MIN( l1 * cs, l2 * cs ) - 1.5 ) * font_factor * ft_ht * ENLARGE ) );
1104  dev->lly = (int) ( MIN( dev->lly, args->y + ( MIN( l1 * sn, l2 * sn ) - 1.5 ) * font_factor * ft_ht * ENLARGE ) );
1105  dev->urx = (int) ( MAX( dev->urx, args->x + ( MAX( l1 * cs, l2 * cs ) + 1.5 ) * font_factor * ft_ht * ENLARGE ) );
1106  dev->ury = (int) ( MAX( dev->ury, args->y + ( MAX( l1 * sn, l2 * sn ) + 1.5 ) * font_factor * ft_ht * ENLARGE ) );
1107  }
1108 }
1109 
1110 static void
1111 esc_purge( unsigned char *dstr, unsigned char *sstr )
1112 {
1113  char esc;
1114 
1115  plgesc( &esc );
1116 
1117  while ( *sstr )
1118  {
1119  if ( *sstr != esc )
1120  {
1121  *dstr++ = *sstr++;
1122  continue;
1123  }
1124 
1125  sstr++;
1126  if ( *sstr == esc )
1127  {
1128  *dstr++ = *sstr++;
1129  continue;
1130  }
1131 
1132  else
1133  {
1134  switch ( *sstr++ )
1135  {
1136  case 'f':
1137  sstr++;
1138  break; // two chars sequence
1139 
1140  default:
1141  break; // single char escape
1142  }
1143  }
1144  }
1145  *dstr = '\0';
1146 }
1147 
1148 //--------------------------------------------------------------------------
1149 // unsigned char plunicode2type1 (const PLUNICODE index,
1150 // const Unicode_to_Type1_table lookup[], const int number_of_entries)
1151 //
1152 // Function takes an input unicode index, looks through the lookup
1153 // table (which must be sorted by PLUNICODE Unicode), then returns the
1154 // corresponding Type1 code in the lookup table. If the Unicode index is
1155 // not present the returned value is 32 (which is normally a blank
1156 // for Type 1 fonts).
1157 //--------------------------------------------------------------------------
1158 
1159 static unsigned char
1160 plunicode2type1( const PLUNICODE index,
1161  const Unicode_to_Type1_table lookup[],
1162  const int nlookup )
1163 {
1164  int jlo = -1, jmid, jhi = nlookup;
1165  while ( jhi - jlo > 1 )
1166  {
1167  // Note that although jlo or jhi can be just outside valid
1168  // range (see initialization above) because of while condition
1169  // jlo < jmid < jhi and jmid must be in valid range.
1170  //
1171  jmid = ( jlo + jhi ) / 2;
1172  if ( index > lookup[jmid].Unicode )
1173  jlo = jmid;
1174  else if ( index < lookup[jmid].Unicode )
1175  jhi = jmid;
1176  else
1177  // We have found it!
1178  // index == lookup[jmid].Unicode
1179  //
1180  return ( lookup[jmid].Type1 );
1181  }
1182  // jlo is invalid or it is valid and index > lookup[jlo].Unicode.
1183  // jhi is invalid or it is valid and index < lookup[jhi].Unicode.
1184  // All these conditions together imply index cannot be found in lookup.
1185  // Mark with ' ' (which is normally the index for blank in type 1 fonts).
1186  //
1187  return ( ' ' );
1188 }
1189 
1190 //--------------------------------------------------------------------------
1191 // get_font( PSDev* dev, PLUNICODE fci )
1192 //
1193 // Sets the Type1 font.
1194 //--------------------------------------------------------------------------
1195 static const char *
1196 get_font( PSDev* dev, PLUNICODE fci )
1197 {
1198  const char *font;
1199  // fci = 0 is a special value indicating the Type 1 Symbol font
1200  // is desired. This value cannot be confused with a normal FCI value
1201  // because it doesn't have the PL_FCI_MARK.
1202  if ( fci == 0 )
1203  {
1204  font = "Symbol";
1207  dev->if_symbol_font = 1;
1208  }
1209  else
1210  {
1211  // convert the fci to Base14/Type1 font information
1212  font = plP_FCI2FontName( fci, Type1Lookup, N_Type1Lookup );
1215  dev->if_symbol_font = 0;
1216  }
1217  pldebug( "set_font", "fci = 0x%x, font name = %s\n", fci, font );
1218  return ( font );
1219 }
1220 
1221 #else
1222 int
1224 {
1225  return 0;
1226 }
1227 
1228 #endif // PLD_ps