From d67a359907b827541e922eec1c789d60bebeb3ec Mon Sep 17 00:00:00 2001
From: Jim Dishaw <jim@dishaw.org>
Date: Sun, 15 Sep 2019 13:22:26 -0400
Subject: [PATCH] Cleanup of the postscript driver   -- Changed unnecessary
 calls of fprintf() to fputs.     -- The fputs() call is more efficient with
 fixed strings   -- Changed the TRMFLT() from a macro to an inline static
 function     -- Preprocessor macros can have unintended side effects   --
 Eliminated the the shared ouput buffer (outbuf) variable     -- Dynamically
 allocating the output buffer variable on the        stack on function entry
 has negligible performance impact     -- The shared output buffer would cause
 problems when threads        are implemented

---
 drivers/ps.c | 52 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/drivers/ps.c b/drivers/ps.c
index dd4b2892b..da4d04897 100644
--- a/drivers/ps.c
+++ b/drivers/ps.c
@@ -40,9 +40,11 @@
 #include "plunicode-type1.h"
 #include "plfci-type1.h"
 
-// Define macro to truncate small values to zero - prevents
-// printf printing -0.000
-#define TRMFLT( a )    ( ( fabs( a ) < 5.0e-4 ) ? 0.0 : ( a ) )
+// truncate small values to zero - prevents printf printing -0.000
+inline static PLFLT TRMFLT(PLFLT a)
+{
+	return ( fabs(a) < 5.0e-4 ) ? 0.0 : a;
+}
 
 // Device info
 
@@ -72,8 +74,11 @@ static void esc_purge( unsigned char *, unsigned char * );
 static void ps_dispatch_init_helper( PLDispatchTable *pdt,
                                      const char *menustr, const char *devnam,
                                      int type, int seq, plD_init_fp init );
-#define OUTBUF_LEN    128
-static char   outbuf[OUTBUF_LEN];
+
+// Size of the string buffer used to format commands
+#define OUTBUF_LEN      128
+
+// Driver settings
 static int    text = 1;
 static int    color;
 static int    hrshsym = 1;
@@ -420,6 +425,7 @@ ps_init( PLStream *pls )
 void
 plD_line_ps( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
 {
+    char outbuf[OUTBUF_LEN];
     PSDev *dev = (PSDev *) pls->dev;
     PLINT x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
 
@@ -444,7 +450,7 @@ plD_line_ps( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
     }
     else
     {
-        fprintf( OF, " Z\n" );
+        fputs( " Z\n", OF );
         pls->linepos = 0;
 
         if ( x1 == x2 && y1 == y2 ) // must be a single dot, draw a circle
@@ -463,7 +469,7 @@ plD_line_ps( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
     dev->urx = MAX( dev->urx, x2 );
     dev->ury = MAX( dev->ury, y2 );
 
-    fprintf( OF, "%s", outbuf );
+    fputs( outbuf, OF );
     pls->bytecnt += 1 + (PLINT) strlen( outbuf );
     dev->xold     = x2;
     dev->yold     = y2;
@@ -493,7 +499,7 @@ plD_polyline_ps( PLStream *pls, short *xa, short *ya, PLINT npts )
 void
 plD_eop_ps( PLStream *pls )
 {
-    fprintf( OF, " S\neop\n" );
+    fputs( " S\neop\n", OF );
 }
 
 //--------------------------------------------------------------------------
@@ -521,7 +527,7 @@ plD_bop_ps( PLStream *pls )
     else
         fprintf( OF, "%%%%Page: %d %d\n", (int) pls->page, (int) pls->page );
 
-    fprintf( OF, "bop\n" );
+    fputs( "bop\n", OF );
     if ( pls->color )
     {
         PLFLT r, g, b;
@@ -556,7 +562,7 @@ plD_tidy_ps( PLStream *pls )
 {
     PSDev *dev = (PSDev *) pls->dev;
 
-    fprintf( OF, "\n%%%%Trailer\n" );
+    fputs( "\n%%%%Trailer\n", OF );
 
     dev->llx /= ENLARGE;
     dev->lly /= ENLARGE;
@@ -578,8 +584,8 @@ plD_tidy_ps( PLStream *pls )
     else
         fprintf( OF, "%%%%Pages: %d\n", (int) pls->page );
 
-    fprintf( OF, "@end\n" );
-    fprintf( OF, "%%%%EOF\n" );
+    fputs( "@end\n", OF );
+    fputs( "%%%%EOF\n", OF );
 
 // Backtrack to write the BoundingBox at the beginning
 // Some applications don't like it atend
@@ -676,11 +682,12 @@ plD_esc_ps( PLStream *pls, PLINT op, void *ptr )
 static void
 fill_polygon( PLStream *pls )
 {
+    char outbuf[OUTBUF_LEN];
     PSDev *dev = (PSDev *) pls->dev;
     PLINT n, ix = 0, iy = 0;
     PLINT x, y;
 
-    fprintf( OF, " Z\n" );
+    fputs( " Z\n", OF );
 
     for ( n = 0; n < pls->dev_npts; n++ )
     {
@@ -700,7 +707,7 @@ fill_polygon( PLStream *pls )
             dev->lly = MIN( dev->lly, y );
             dev->urx = MAX( dev->urx, x );
             dev->ury = MAX( dev->ury, y );
-            fprintf( OF, "%s", outbuf );
+            fputs( outbuf, OF );
             pls->bytecnt += (PLINT) strlen( outbuf );
             continue;
         }
@@ -721,13 +728,13 @@ fill_polygon( PLStream *pls )
         dev->urx = MAX( dev->urx, x );
         dev->ury = MAX( dev->ury, y );
 
-        fprintf( OF, "%s", outbuf );
+        fputs( outbuf, OF );
         pls->bytecnt += (PLINT) strlen( outbuf );
         pls->linepos += 21;
     }
     dev->xold = PL_UNDEFINED;
     dev->yold = PL_UNDEFINED;
-    fprintf( OF, " F " );
+    fputs( " F ", OF );
 }
 
 //--------------------------------------------------------------------------
@@ -953,7 +960,12 @@ proc_str( PLStream *pls, EscText *args )
             &clipx[2], &clipy[2] );
         plRotPhy( ORIENTATION, dev->xmin, dev->ymin, dev->xmax, dev->ymax,
             &clipx[3], &clipy[3] );
-        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] );
+        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] );
 
         // move to string reference point
         fprintf( OF, " %d %d M\n", args->x, args->y );
@@ -982,7 +994,7 @@ proc_str( PLStream *pls, EscText *args )
                 fprintf( OF, "%c", str[i] );
             i++;
         }
-        fprintf( OF, ") SW\n" );
+        fputs( ") SW\n", OF );
 
 
         // Parse string for PLplot escape sequences and print everything out
@@ -1091,8 +1103,8 @@ proc_str( PLStream *pls, EscText *args )
                 fprintf( OF, "grestore (%s) stringwidth rmoveto\n", str );
         } while ( *cur_strp );
 
-        fprintf( OF, "grestore\n" );
-        fprintf( OF, "grestore\n" );
+        fputs( "grestore\n", OF );
+        fputs( "grestore\n", OF );
 
         //
         // keep driver happy -- needed for background and orientation.
-- 
2.22.0.windows.1

