From 57f005bf98d1f93aff30bf99046694962d2b2a3a Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:32:22 +0200 Subject: [PATCH 1/6] Fix possible out-of-bounds array access --- src/imagewriter.cpp | 5 +++++ src/iw_charmaps.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index a60119d..5bb789a 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -109,6 +109,11 @@ Imagewriter::Imagewriter(Bit16u dpi, Bit16u paperSize, Bit16u bannerSize, char* } else { + if (paperSize >= N_PAPER_SIZES) + { + printf("Printer: unsupported paper size %d\n", paperSize); + paperSize = 0; + } defaultPageWidth = ((Real64)paperSizes[paperSize][0]/(Real64)72); defaultPageHeight = ((Real64)paperSizes[paperSize][1]/(Real64)72); } diff --git a/src/iw_charmaps.h b/src/iw_charmaps.h index 6037afe..3cba5a0 100644 --- a/src/iw_charmaps.h +++ b/src/iw_charmaps.h @@ -45,12 +45,13 @@ static const Bit16u intCharSets[8][10] = {0x00a3, 0x00a7, 0x00a1, 0x00d1, 0x00bf, 0x0060, 0x00b0, 0x00f1, 0x00e7, 0x007e}, // Spanish }; +#define N_PAPER_SIZES 7 /* 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] = +static const Bit16u paperSizes[N_PAPER_SIZES][2] = { {612, 792}, //US Letter 8.5 x 11in {612, 1008}, //US Legal 8.5 x 14in From bcd06c5d68d26a27ec49eb703ad53b85abda925b Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:36:16 +0200 Subject: [PATCH 2/6] Fix reading switcha --- src/imagewriter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index 5bb789a..e46b2af 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -420,7 +420,7 @@ void Imagewriter::updateFont() void Imagewriter::updateSwitch() { //Set international character mappping (Switches A-1 to A3) - int charmap = switcha &= 7; + int charmap = (switcha & 7); curMap[0x23] = intCharSets[charmap][0]; curMap[0x40] = intCharSets[charmap][1]; curMap[0x5b] = intCharSets[charmap][2]; @@ -432,7 +432,7 @@ void Imagewriter::updateSwitch() curMap[0x7d] = intCharSets[charmap][8]; curMap[0x7e] = intCharSets[charmap][9]; //MSB control (Switch B-6) - if (!(switchb&32)) + if (!(switchb & 32)) { msb = 255; } @@ -1129,7 +1129,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) return true; case 0x0d: // Carriage Return (CR) curX = leftMargin; - if ((switcha&=128)) curY += lineSpacing; // If switch A-8 is set, send a LF after CR + if ((switcha & 128)) curY += lineSpacing; // If switch A-8 is set, send a LF after CR if (!autoFeed) return true; case 0x0a: // Line feed @@ -1280,7 +1280,7 @@ void Imagewriter::printChar(Bit8u ch) // For line printing Bit16u lineStart = PIXX; // Print a slashed zero if the softswitch B-1 is set - if(switchb&1 && ch=='0') slashzero(penX,penY); + if(switchb & 1 && ch=='0') slashzero(penX,penY); // advance the cursor to the right Real64 x_advance; if (style & STYLE_PROP) From 699701d39dadd7e22f3dadc27109de1b05b9543b Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:40:34 +0200 Subject: [PATCH 3/6] Make DIP switch 1 handling clearer --- src/imagewriter.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index e46b2af..eb2739a 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -70,6 +70,19 @@ extern "C" char* g_imagewriter_prop_font; extern "C" int iw_scc_write; #include "iw_charmaps.h" +#define SWITCHA_CHARSET_MASK 0x07 +#define SWITCHA_CHARSET_US 0x00 +#define SWITCHA_CHARSET_IT 0x01 +#define SWITCHA_CHARSET_DK 0x02 +#define SWITCHA_CHARSET_UK 0x03 +#define SWITCHA_CHARSET_DE 0x04 +#define SWITCHA_CHARSET_SE 0x05 +#define SWITCHA_CHARSET_FR 0x06 +#define SWITCHA_CHARSET_ES 0x07 + +#define SWITCHA_PERFORATIONSKIP 0x10 +#define SWITCHA_LFAFTERCR 0x80 + #ifdef HAVE_SDL void Imagewriter::FillPalette(Bit8u redmax, Bit8u greenmax, Bit8u bluemax, Bit8u colorID, SDL_Palette* pal) { @@ -274,7 +287,7 @@ void Imagewriter::resetPrinter() multiPointSize = 0.0; multicpi = 0.0; hmi = -1.0; - switcha = 0; + switcha = SWITCHA_CHARSET_US; switchb = ' '; numPrintAsChar = 0; LQtypeFace = fixed; @@ -420,7 +433,7 @@ void Imagewriter::updateFont() void Imagewriter::updateSwitch() { //Set international character mappping (Switches A-1 to A3) - int charmap = (switcha & 7); + int charmap = (switcha & SWITCHA_CHARSET_MASK); curMap[0x23] = intCharSets[charmap][0]; curMap[0x40] = intCharSets[charmap][1]; curMap[0x5b] = intCharSets[charmap][2]; @@ -1129,7 +1142,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) return true; case 0x0d: // Carriage Return (CR) curX = leftMargin; - if ((switcha & 128)) curY += lineSpacing; // If switch A-8 is set, send a LF after CR + if ((switcha & SWITCHA_LFAFTERCR)) curY += lineSpacing; // If switch A-8 is set, send a LF after CR if (!autoFeed) return true; case 0x0a: // Line feed From 436be75460b1afd51363a4ca596066b042d43252 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:45:40 +0200 Subject: [PATCH 4/6] First style/score init --- src/imagewriter.cpp | 4 ++-- src/imagewriter.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index eb2739a..334cd38 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -270,10 +270,10 @@ void Imagewriter::resetPrinter() lineSpacing = (Real64)1/6; cpi = 12.0; printRes = 2; - style &= (0xffff - STYLE_PROP); definedUnit = 96; curCharTable = 1; - style = 0; + style = STYLE_BASE; + score = SCORE_NONE; extraIntraSpace = 0.0; printUpperContr = true; bitGraph.remBytes = 0; diff --git a/src/imagewriter.h b/src/imagewriter.h index a6ef9ca..4ea6971 100644 --- a/src/imagewriter.h +++ b/src/imagewriter.h @@ -73,6 +73,7 @@ #include #endif +#define STYLE_BASE 0x00 #define STYLE_PROP 0x01 #define STYLE_CONDENSED 0x02 #define STYLE_BOLD 0x04 From 4bc00ba6234064fc745cdfa44b8fda9478c610be Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:46:41 +0200 Subject: [PATCH 5/6] Fix off-by-one-line at end of page --- src/imagewriter.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index 334cd38..db3f2ed 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -1065,7 +1065,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) while (x < paramc(0)) { curY += lineSpacing; - if (curY > bottomMargin) + if (curY > bottomMargin - lineSpacing) newPage(true,false); x++; } @@ -1119,7 +1119,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) { curX = leftMargin; curY += lineSpacing; - if (curY > bottomMargin) + if (curY > bottomMargin - lineSpacing) newPage(true,false); } else @@ -1131,7 +1131,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) moveTo = verttabs[i]; // Nothing found => Act like FF - if (moveTo > bottomMargin || moveTo < 0) + if (moveTo > bottomMargin - lineSpacing || moveTo < 0) newPage(true,false); else curY = moveTo; @@ -1148,7 +1148,7 @@ bool Imagewriter::processCommandChar(Bit8u ch) case 0x0a: // Line feed //curX = leftMargin; curY += lineSpacing; - if (curY > bottomMargin) + if (curY > bottomMargin - lineSpacing) newPage(true,false); return true; case 0x0e: //Select double width printing (SO) IW @@ -1324,7 +1324,7 @@ void Imagewriter::printChar(Bit8u ch) if((curX + x_advance) > rightMargin) { curX = leftMargin; curY += lineSpacing; - if (curY > bottomMargin) newPage(true,false); + if (curY > bottomMargin - lineSpacing) newPage(true,false); } #endif // HAVE_SDL } From 89d099794c517f1a86966548a34d701f4f0cf547 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 18 Oct 2024 08:50:11 +0200 Subject: [PATCH 6/6] Handle Perforation Skip softswitch --- src/imagewriter.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index db3f2ed..61ca970 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -444,6 +444,18 @@ void Imagewriter::updateSwitch() curMap[0x7c] = intCharSets[charmap][7]; curMap[0x7d] = intCharSets[charmap][8]; curMap[0x7e] = intCharSets[charmap][9]; + + if (switcha & SWITCHA_PERFORATIONSKIP) + { + topMargin = 0.25; + bottomMargin = pageHeight - 0.25; + } + else + { + topMargin = 0.0; + bottomMargin = pageHeight - 0.0; + } + //MSB control (Switch B-6) if (!(switchb & 32)) { @@ -842,8 +854,8 @@ bool Imagewriter::processCommandChar(Bit8u ch) x++; } pageHeight = (Real64)PARAM4(0)/144; - bottomMargin = pageHeight; - topMargin = 0.0; + // trigger margins computation + updateSwitch(); break; } case 0x21: // Select bold font (ESC !) IW