--- gdevgdi.c.orig	Fri Oct 20 06:25:03 2000
+++ gdevgdi.c	Wed Jul  2 20:03:06 2003
@@ -126,7 +126,7 @@
 private int
 gdi_open(gx_device *pdev)
 {	/* Change the margins if necessary. */
-	const float _ds *m = 0;
+	const float *m = 0;
 	bool move_origin = true;
 
 	static const float m_a4[4] = { GDI_MARGINS_A4 };
@@ -240,7 +240,63 @@
 	    //compression_type =  (ul_scan_size > ul_tiff_size) ? GDI_COMP_MODITIFF : GDI_COMP_SCANLINE;
             switch (compression_type) {
 	    case GDI_COMP_MODITIFF:
+#define FUDGE_BIG_BANDS
+#ifndef FUDGE_BIG_BANDS
                 ul_comp_size = FrameTiffComp(obp, ibp, band_height, band_width_bytes, GDI_REAL_COMP);
+#else
+	      {
+		/* Very ugly.  The printer will hose if the compressed
+                   band size is over 65536, so we "fudge" the data in
+                   this case repeatedly until we get what we want. 
+
+		   The fudge algorithm is simple, this is kinda-sorta
+		   RLE, so we just round groups of bits in groups of
+		   2, then 3, then 4, etc until the thing works.  */
+#define MAXBAND 0xffff
+#define ASSERT(x)
+		int fudge=0;
+		byte *use_band=ibp;
+		do {
+		  ul_comp_size = FrameTiffComp(obp, use_band, 
+					       band_height, band_width_bytes, 
+					       GDI_REAL_COMP);
+		  if (ul_comp_size > MAXBAND-8) {
+		    int f, g, h;
+		    if (!fudge) {
+		      ASSERT(use_band == ibp);
+		      use_band = (byte*)gs_malloc(ul_band_size, 1, "gdi_print_page/fudge");
+		      fudge=1;
+		    }
+		    memcpy(use_band, ibp, ul_band_size);
+		    fudge++;
+		    ASSERT(fudge>=2);
+		    {
+#define FUDGE2(x) ( (((((x)>>6)&0x3)?3:0)<<6)	\
+		    | (((((x)>>4)&0x3)?3:0)<<4)	\
+		    | (((((x)>>2)&0x3)?3:0)<<2)	\
+		    | (((((x)>>0)&0x3)?3:0)) )
+#define FUDGE4(x) ( (((((x)>>4)&0xf)?0xf:0)<<4)	\
+		    | (((((x)>>0)&0xf)?0xf:0)) )
+#define FUDGE8(x) ( (((((x)>>0)&0xff)?0xf:0)) )
+#define FUDGE(fudge, x) ( (fudge == 2 ? FUDGE2(x) 	\
+			   : fudge == 3 ? FUDGE4(x)	\
+			   : fudge == 4 ? FUDGE8(x)	\
+			   : 0 ) )
+
+		      for(f=0;f<ul_band_size; f++) {
+			use_band[f] = FUDGE(fudge, ibp[f]);
+		      }
+		    }
+		  }
+		} while (ul_comp_size > MAXBAND-8);
+	      oh_well:
+		if (fudge > 1) {
+		  ASSERT(use_band != ibp);
+		  gs_free(use_band, ul_band_size, 1, "gdi_print_page/fudge");
+		  /*fprintf(stderr, "smartgdi: band %d fudge factor is %d\n", i, fudge);*/
+		}
+	      }
+#endif
 		break;
 	    case GDI_COMP_SCANLINE:
                 ul_comp_size = bmp2run(obp, ibp, band_height, band_width_bytes, GDI_REAL_COMP);
@@ -271,45 +327,45 @@
   unsigned char buffer[300];
   int dots_per_inch = (int)pdev->y_pixels_per_inch;
 
-  strcpy(buffer, "\x1b%-12345X");
+  strcpy(buffer, "\033%-12345X");
   
   // Paper Type
-  strcat(buffer, "@PJL SET PAPERTYPE = NORMAL ON\x0d\x0a");
+  strcat(buffer, "@PJL SET PAPERTYPE = NORMAL ON\015\012");
   //Density
-  strcat(buffer, "@PJL SET DENSITY = 1\x0d\x0a");
+  strcat(buffer, "@PJL SET DENSITY = 1\015\012");
   // Toner Save
-  strcat(buffer, "@PJL SET TONERSAVE = OFF\x0d\x0a");
+  strcat(buffer, "@PJL SET TONERSAVE = OFF\015\012");
   // Enter Language SMART
-  strcat(buffer, "@PJL ENTER LANGUAGE = SMART\x0d\x0a");
+  strcat(buffer, "@PJL ENTER LANGUAGE = SMART\015\012");
   // JobStart
-  strcat(buffer, "$PJL JOB START\x0d\x0a");
+  strcat(buffer, "$PJL JOB START\015\012");
 
   // Resolution
   if (dots_per_inch == 600)
-      strcat(buffer, "$PJL RESOLUTION = 600\x0d\x0a");
+      strcat(buffer, "$PJL RESOLUTION = 600\015\012");
   else
-      strcat(buffer, "$PJL RESOLUTION = 300\x0d\x0a");
+      strcat(buffer, "$PJL RESOLUTION = 300\015\012");
 
   // Copies
-  strcat(buffer, "$PJL COPIES = 1\x0d\x0a");
+  strcat(buffer, "$PJL COPIES = 1\015\012");
   // Paper Size
   switch (gdev_pcl_paper_size((gx_device*)pdev)) 
   {
 	case PAPER_SIZE_A4:
-  			strcat(buffer, "$PJL PAGE A4 AUTO\x0d\x0a");
+  			strcat(buffer, "$PJL PAGE A4 AUTO\015\012");
 			break;
 	case PAPER_SIZE_LETTER:
-  			strcat(buffer, "$PJL PAGE LETTER AUTO\x0d\x0a");
+  			strcat(buffer, "$PJL PAGE LETTER AUTO\015\012");
 			break;
 	case PAPER_SIZE_LEGAL:
-  			strcat(buffer, "$PJL PAGE LEGAL AUTO\x0d\x0a");
+  			strcat(buffer, "$PJL PAGE LEGAL AUTO\015\012");
 			break;
 	default:
-  			strcat(buffer, "$PJL PAGE LETTER AUTO\x0d\x0a");
+  			strcat(buffer, "$PJL PAGE LETTER AUTO\015\012");
 			break;
   }
   // bitmap start
-  strcat(buffer, "$PJL BITMAP START\x0d\x0a");
+  strcat(buffer, "$PJL BITMAP START\015\012");
   // write buffer to file.
   ulSize = strlen(buffer);
   fwrite(buffer, 1, ulSize, fp );
@@ -369,12 +425,12 @@
   unsigned long buffer[200];
 
   memset((char*)buffer, 0x00, 200);
-  strcpy((char*)buffer, "$PJL PRINT 4\x0d\x0a");
-  strcat((char*)buffer, "$PJL EOJ\x0d\x0a");
-  strcat((char*)buffer, "$PJL SYNC\x0d\x0a");
-  strcat((char*)buffer, "$PJL RELEASE 0 2047\x0d\x0a");
-  strcat((char*)buffer, "$PJL GARBAGE\x0d\x0a");
-  strcat((char*)buffer, "\x1b%-12345X\x0d\x0a");
+  strcpy((char*)buffer, "$PJL PRINT 4\015\012");
+  strcat((char*)buffer, "$PJL EOJ\015\012");
+  strcat((char*)buffer, "$PJL SYNC\015\012");
+  strcat((char*)buffer, "$PJL RELEASE 0 2047\015\012");
+  strcat((char*)buffer, "$PJL GARBAGE\015\012");
+  strcat((char*)buffer, "\033%-12345X\015\012");
 
   ulSize = strlen((char*)buffer);
   fwrite(buffer, 1, ulSize, fp);
@@ -817,16 +873,16 @@
   lDis  = ((long)usDy * lWarp) + (long)sDx;
 
   // 1st, 2nd, 3rd & 4th byte
-  ultmp_dat = 0xc0000000;
+  ultmp_dat = 0xc0000000ul;
   if (lDis < 0)
   {
-    ultmp_dat |= 0x20000000;
+    ultmp_dat |= 0x20000000ul;
   }
-  ultmp_dat |= (lDis & 0x1fffffff);
-  *out_buf++ = (unsigned char)((ultmp_dat & 0xff000000) >> 24);
-  *out_buf++ = (unsigned char)((ultmp_dat & 0xff0000) >> 16);
-  *out_buf++ = (unsigned char)((ultmp_dat & 0xff00) >> 8);
-  *out_buf++ = (unsigned char)(ultmp_dat & 0xff);
+  ultmp_dat |= (lDis & 0x1ffffffful);
+  *out_buf++ = (unsigned char)((ultmp_dat & 0xff000000ul) >> 24);
+  *out_buf++ = (unsigned char)((ultmp_dat & 0xff0000ul) >> 16);
+  *out_buf++ = (unsigned char)((ultmp_dat & 0xff00ul) >> 8);
+  *out_buf++ = (unsigned char)(ultmp_dat & 0xfful);
 
   // 5th & 6th byte
   ustmp_dat = 0xc000;
