From 199e2f9a7669a900449915529dbadb6ad27705cb Mon Sep 17 00:00:00 2001
From: Christopher Mason <cgmason14@users.sf.net>
Date: Sun, 8 Feb 2015 04:55:32 +0000
Subject: [PATCH] Add support to Imagewriter for multiple page sizes and banner
 printing.

---
 src/config.c          |  8 +++++++-
 src/imagewriter.cpp   | 28 ++++++++++++++++++----------
 src/imagewriter.h     |  6 +++---
 src/iw_charmaps.h     | 16 ++++++++++++++++
 src/scc_imagewriter.c |  6 ++++--
 src/sim65816.c        |  2 ++
 6 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/src/config.c b/src/config.c
index eac3cc9..17b9d09 100644
--- a/src/config.c
+++ b/src/config.c
@@ -105,6 +105,8 @@ extern int g_imagewriter_multipage;
 extern int g_imagewriter_timeout;
 extern char* g_imagewriter_fixed_font;
 extern char* g_imagewriter_prop_font;
+extern int g_imagewriter_paper;
+extern int g_imagewriter_banner;
 
 #if defined(_WIN32) || defined(__CYGWIN__)
 extern int g_win_show_console_request;
@@ -307,8 +309,12 @@ Cfg_menu g_cfg_imagewriter_menu[] = {
 { "Virtual Imagewriter Configuration", g_cfg_imagewriter_menu, 0, 0, CFGTYPE_MENU },
 { "Virtual Printer Type,0,Imagewriter II,1,Imagewriter LQ",
 		KNMP(g_imagewriter), CFGTYPE_INT },
+{ "Paper Size,0,US Letter (8.5x11in),1,US Legal (8.5x14in),2,ISO A4 (210 x 297mm),3,ISO B5 (176 x 250mm),4,Wide Fanfold (14 x 11in),5,Ledger (11 x 17in),6,ISO A3 (297 x 420mm)",
+		KNMP(g_imagewriter_paper), CFGTYPE_INT },
 { "Printer DPI,360,360x360 dpi (Best for 8-bit software),720,720x720 dpi (Best for GS/OS & IW LQ Modes),1440,1440x1440 dpi",
 		KNMP(g_imagewriter_dpi), CFGTYPE_INT },
+{ "Banner Printing (Limited To 144x144 dpi Output),0,Banner Printing Off,3,3 Pages Long,4,4 Pages Long,5,5 Pages Long,6,6 Pages Long,7,7 Pages Long,8,8 Pages Long,9,9 Pages Long,10,10 Pages Long",
+		KNMP(g_imagewriter_banner), CFGTYPE_INT },
 { "Printer Output Type,bmp,Windows Bitmap,ps,Postscript (B&W),colorps,Postscript (Color),printer,Direct to host printer,text,Text file",
 		KNMP(g_imagewriter_output), CFGTYPE_STR },
 { "Multipage Files? (PS Only),0,No,1,Yes",
@@ -613,7 +619,7 @@ cfg_iwreset()
 {
 		imagewriter_feed();
 		imagewriter_close();
-		imagewriter_init(g_imagewriter_dpi,612,792,g_imagewriter_output,g_imagewriter_multipage, 0);
+		imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner, g_imagewriter_output,g_imagewriter_multipage);
 		return;
 }
 #ifdef HAVE_TFE
diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp
index 099a006..46a9c55 100644
--- a/src/imagewriter.cpp
+++ b/src/imagewriter.cpp
@@ -101,7 +101,7 @@ void Imagewriter::FillPalette(Bit8u redmax, Bit8u greenmax, Bit8u bluemax, Bit8u
 }
 #endif // HAVE_SDL
 
-Imagewriter::Imagewriter(Bit16u dpi, Bit16u width, Bit16u height, char* output, bool multipageOutput, int port) 
+Imagewriter::Imagewriter(Bit16u dpi, Bit16u paperSize, Bit16u bannerSize, char* output, bool multipageOutput)
 {
 #ifdef HAVE_SDL
 	if (FT_Init_FreeType(&FTlib))
@@ -111,14 +111,22 @@ Imagewriter::Imagewriter(Bit16u dpi, Bit16u width, Bit16u height, char* output,
 	else
 	{
 		SDL_Init(SDL_INIT_EVERYTHING);
-		this->dpi = dpi;
 		this->output = output;
 		this->multipageOutput = multipageOutput;
 		this->port = port;
 
-		defaultPageWidth = (Real64)width/(Real64)72;
-		defaultPageHeight = (Real64)height/(Real64)72;
-
+		if (bannerSize)
+		{
+			defaultPageWidth = ((Real64)paperSizes[0][0]/(Real64)72);
+			defaultPageHeight = (((Real64)paperSizes[0][1]*bannerSize)/(Real64)72);
+			dpi = 144;
+		}
+		else
+		{
+			defaultPageWidth = ((Real64)paperSizes[paperSize][0]/(Real64)72);
+			defaultPageHeight = ((Real64)paperSizes[paperSize][1]/(Real64)72);
+		}
+		this->dpi = dpi;
 		// Create page
 		page = SDL_CreateRGBSurface(
 						SDL_SWSURFACE, 
@@ -1626,8 +1634,8 @@ SDL_FreeSurface(image);*/
 	{
 #if defined (WIN32)
 
-		Bit16u physW = GetDeviceCaps(printerDC, PHYSICALWIDTH);
-		Bit16u physH = GetDeviceCaps(printerDC, PHYSICALHEIGHT);
+		Bit32u physW = GetDeviceCaps(printerDC, PHYSICALWIDTH);
+		Bit32u physH = GetDeviceCaps(printerDC, PHYSICALHEIGHT);
 		Bit16u printeroffsetW = GetDeviceCaps(printerDC, PHYSICALOFFSETX);  //printer x offset in actual pixels
 		Bit16u printeroffsetH = GetDeviceCaps(printerDC, PHYSICALOFFSETY); //printer y offset in actual pixels
 		Bit16u deviceDPIW = GetDeviceCaps(printerDC, LOGPIXELSX);
@@ -2174,12 +2182,12 @@ Bit8u Imagewriter::getPixel(Bit32u num) {
 //Interfaces to C code
 
 
-extern "C" void imagewriter_init(int pdpi, int pwidth, int pheight, char* poutput, bool mpage, int port)
+extern "C" void imagewriter_init(int pdpi, int ppaper, int banner, char* poutput, bool mpage)
 {
 	if (defaultImagewriter != NULL) return; //if Imagewriter on this port is initialized, reuse it
-	defaultImagewriter = new Imagewriter(pdpi, pwidth,pheight, poutput, mpage, port);
+	defaultImagewriter = new Imagewriter(pdpi, ppaper, banner, poutput, mpage);
 }
-extern "C" void imagewriter_loop(Bit8u pchar, int port)
+extern "C" void imagewriter_loop(Bit8u pchar)
 {
     if (defaultImagewriter == NULL) return;
 	defaultImagewriter->printChar(pchar);
diff --git a/src/imagewriter.h b/src/imagewriter.h
index c58cd55..2bc0f13 100644
--- a/src/imagewriter.h
+++ b/src/imagewriter.h
@@ -123,7 +123,7 @@ typedef struct {
 class Imagewriter {
 public:
 
-	Imagewriter (Bit16u dpi, Bit16u width, Bit16u height, char* output, bool multipageOutput, int port);
+	Imagewriter (Bit16u dpi, Bit16u paperSize, Bit16u bannerSize, char* output, bool multipageOutput);
 	virtual ~Imagewriter();
 
 	// Process one character sent to virtual printer
@@ -305,8 +305,8 @@ extern "C"
 typedef unsigned char Bit8u;
 #endif
 
-void imagewriter_init(int pdpi, int pwidth, int pheight, char* poutput, bool mpage, int port);
-void imagewriter_loop(Bit8u pchar,int port);
+void imagewriter_init(int pdpi, int ppaper, int banner, char* poutput, bool mpage);
+void imagewriter_loop(Bit8u pchar);
 void imagewriter_close();
 void imagewriter_feed();
 #ifdef __cplusplus
diff --git a/src/iw_charmaps.h b/src/iw_charmaps.h
index c7a2f8d..a496721 100644
--- a/src/iw_charmaps.h
+++ b/src/iw_charmaps.h
@@ -58,3 +58,19 @@ static const Bit16u intCharSets[8][10] =
 	{0x00a3, 0x00e0, 0x00b0, 0x00e7, 0x00a7, 0x0060, 0x00e9, 0x00f9, 0x00e8, 0x00a8}, // France
 	{0x00a3, 0x00a7, 0x00a1, 0x00d1, 0x00bf, 0x0060, 0x00b0, 0x00f1, 0x00e7, 0x007e}, // Spanish
 };
+
+/*
+Paper size definitions. Units are Postscript points (1/72in).
+This list is based on the options available in the GS/OS
+Imagewriter LQ driver.
+*/
+static const Bit16u paperSizes[7][2] =
+{
+	{612, 792}, //US Letter 8.5 x 11in
+	{612, 1008}, //US Legal 8.5 x 14in
+	{595, 842}, //ISO A4 210 x 297mm
+	{499, 709}, //ISO B5 176 x 250mm
+	{1071, 792}, //Wide Fanfold 14 x 11in
+	{792, 1224}, //Ledger/Tabloid 11 x 17in
+	{842, 1191}, // ISO A3 297 x 420mm
+};
diff --git a/src/scc_imagewriter.c b/src/scc_imagewriter.c
index 3f040bc..0b68c5e 100644
--- a/src/scc_imagewriter.c
+++ b/src/scc_imagewriter.c
@@ -31,6 +31,8 @@ extern int g_imagewriter_dpi;
 extern char* g_imagewriter_output;
 extern int g_imagewriter_multipage;
 extern int g_imagewriter_timeout;
+extern int g_imagewriter_paper;
+extern int g_imagewriter_banner;
 
 word32 imagewriter_vbl_count = 0;
 int imagewriter_port_block = 0;
@@ -40,7 +42,7 @@ int scc_imagewriter_init(int port)
 {
 	Scc	*scc_ptr;
 	scc_ptr = &(scc_stat[port]);
-	imagewriter_init(g_imagewriter_dpi,612,792,g_imagewriter_output,g_imagewriter_multipage != 0, port);
+	imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner,g_imagewriter_output,g_imagewriter_multipage);
 	scc_ptr->state = 4;
 	return 4;
 }
@@ -122,7 +124,7 @@ void scc_imagewriter_empty_writebuf(int port, double dcycs)
 		}
 		bytes_written = 1;
 		imagewriter_port_block = 1;
-		imagewriter_loop(scc_ptr->out_buf[rdptr],port);
+		imagewriter_loop(scc_ptr->out_buf[rdptr]);
 		imagewriter_vbl_count = g_vbl_count+(g_imagewriter_timeout*60);
 		imagewriter_port_block = 0;
 		//printf("Write Imagewriter ret: %d, bytes_written:%d, len:%d\n", ret,
diff --git a/src/sim65816.c b/src/sim65816.c
index 0c64a5c..8c09aa9 100644
--- a/src/sim65816.c
+++ b/src/sim65816.c
@@ -152,6 +152,8 @@ int g_imagewriter_multipage = 0;
 int g_imagewriter_timeout = 2;
 char* g_imagewriter_fixed_font = "lib/letgothl.ttf";
 char* g_imagewriter_prop_font = "lib/letgothl.ttf";
+int g_imagewriter_paper = 0;
+int g_imagewriter_banner = 0;
 
 int	g_config_iwm_vbl_count = 0;
 extern const char g_gsport_version_str[] = "0.31";