// hpbar.cpp //////////////////////////////////////////////////////////////// // // FUNCTION: print code 3 of 9 barcodes on HP Laserjet printers // // NOTES: revised from EPSONBAR.PAS which was based on Barcode.bas // - original notes are below due to modification from BASIC // // Translated to C by Keith D. McCroan 13-May-1991. // // To print code 3 of 9 Bar codes on H.P. Laserjet printer. // // Modified by Craig Feied 4/16/87 to add sequencial counting and produced // // Developed from LASERBAR.BAS by Bill Baines, Enfield, CT. // ///////////////////////////////////////////////////////////////////////////// #include #include #include #include "hpbar.h" #include "code39.h" #include "text39.h" typedef unsigned short word; typedef unsigned char byte; //#define hi(w) ((byte)((w)>>8)) //#define lo(w) ((byte)((w)&0xff)) inline byte hi(word w) { return static_cast(w >> 8); } inline byte lo(word w) { return static_cast(w & 0xff); } static int vpos, hpos; static int res; ////////////////////////////////////////////////////////////////////////// // initprinter ////////////////////////////////////////////////////////////////////////// // Initialize the printer void initprinter(FILE *fp) { res = 300; fprintf(fp, "\033E\033&l0O\033&l1E\033&a1L\033&a90M\033*t%dR", res); } ////////////////////////////////////////////////////////////////////////// // graphon ////////////////////////////////////////////////////////////////////////// // Enter graphics mode void graphon(FILE *fp) { fprintf(fp, "\033*r1A"); } ////////////////////////////////////////////////////////////////////////// // graphoff ////////////////////////////////////////////////////////////////////////// // Exit graphics mode void graphoff(FILE *fp) { fprintf(fp, "\033*rB"); } ////////////////////////////////////////////////////////////////////////// // setres ////////////////////////////////////////////////////////////////////////// // Set graphics resolution (dots per inch) void setres (FILE* fp, int r) { #if 0 if (r < 85) r = 75; else if (r < 125) r = 100; else if (r < 225) r = 150; else if (r < 450) r = 300; else if (r < 900) r = 600; else r = 1200; #endif fprintf(fp, "\033*t%dR", r); res = r; } ////////////////////////////////////////////////////////////////////////// // setpos ////////////////////////////////////////////////////////////////////////// // Set cursor position #define LEFTM 0.1 // left margin #define TOPM 0.125 // top margin // Parameter I/O Description // --------- --- --------------------------------- // fp IO The output file // y I Vertical coordinate (inches from top) // x I Horizontal coordinate (inches from left) void setpos(FILE *fp, float y, float x) { int row, col; row = (int)((y - TOPM) * 300.0); col = (int)((x - LEFTM) * 300.0); if (row < 0) row = 0; if (col < 0) col = 0; fprintf(fp, "\033*p%dY\033*p%dX", ++row, ++col); vpos = row; hpos = col; } ////////////////////////////////////////////////////////////////////////////// // printbar ////////////////////////////////////////////////////////////////////////////// // Print a barcode on an HP Laserjet, n pixels high, with text at the bottom // (if possible). Where the text is printed, it replaces any bars that would // otherwise have appeared. bool printbar(FILE* fp, const char* psz, int n) { int i, k, ls, // Length of text string (chars) lth, // Length of barcode (bytes) k0, // Barcode column where text begins start, // Row at which barcode text begins last; // Row at which bars end static byte bc[204]; ls = strlen(psz); k0 = ls/2 + 2; if ((lth = Encode39(psz, bc)) == 0) return false; setres(fp, 100); graphon(fp); // Enter graphics mode // Is there room for text at the bottom? if (n >= 20) { start = n - 10; // Row where text starts last = n - 5; // Row where bars stop } else { last = start = n; // No room for text label } for (i = 0; i < n; ++i) { fprintf(fp, "\033*b%dW", lth); if (i == last) // Stop printing bars now? for (k = 0; k < lth; ++k) bc[k] = 0; if (i >= start) // Printing the text label? Text39(psz, bc + k0, i - start); // Get a row of pixels for text for (k = 0; k < lth; ++k) fputc(bc[k], fp); // Binary output } graphoff(fp); // Exit graphics mode return true; } //////////////////////////// end of hpbar.cpp ////////////////////////////////