// GRID.C  FreeWare by Scott R. Houck    10 March 91    MSC 6.00A
//
// This program prints a 25x80 grid with column and row numbers on an HP
// laser printer.  It is intended for use in designing display screens.
//
// Usage:  GRID [?] [1] [7]
//
//    ?   Display usage and exit
//    1   Rows and columns start with 1 (default is 0)
//    7   Use 7 point (default is Line Printer 8.5 point)
//
// Complaints, suggestions and/or accolades can be forwarded to:
//
//     Scott R. Houck
//     1880 Riggs Place, NW
//     Washington, DC  20009
//
// If you like the program, send me a postcard!

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define VERSION       "1.0"
#define ESC           "\033"
#define NUMROWS       25      // Number of rows
#define NUMCOLS       80      // Number of columns
#define PHYSPGLENGTH  3300    // Landscape physical page length
#define PHYSPGWIDTH   2550    // Landscape physical page width
#define LEFTNONPRINT  60      // Nonprintable area on left
#define TOPNONPRINT   50      // Nonprintable area on top
#define FUDGE         115     // Seems to be needed for centering
#define DOTSPERCOL    32      // Dots per column
#define DOTSPERROW    64      // Dots per row
#define BORDERWIDTH   4       // Border width for 75 dpi resolution
#define GRIDWIDTH     (NUMCOLS * DOTSPERCOL + BORDERWIDTH)
#define GRIDHEIGHT    (NUMROWS * DOTSPERROW + BORDERWIDTH)
#define LEFTMARGIN    ((PHYSPGLENGTH - GRIDWIDTH)  / 2 - LEFTNONPRINT)
#define TOPMARGIN     ((PHYSPGWIDTH  - GRIDHEIGHT) / 2 - TOPNONPRINT - FUDGE)
#define RIGHTX        (GRIDWIDTH  + LEFTMARGIN - 1)
#define BOTTOMY       (GRIDHEIGHT + TOPMARGIN  - BORDERWIDTH)
#define LEFTX         (LEFTMARGIN - 1)

void Print(char *, ...);

main(int argc, char *argv[])
{
   int offset = 0, sevenpt = 0, adjust1 = 43, adjust2 = 46;
   int i, row, column, xpos, remainder, quotient;

   fprintf(stderr, "GRID v" VERSION " by Scott R. Houck\n\n");

   while (--argc)
      switch (argv[argc][0])
         {
         case '?':
            fprintf(stderr,
               "Usage:  GRID [?] [1] [7]\n\n"
               "   ?   Display usage and exit\n"
               "   1   Rows and columns start with 1 (default is 0)\n"
               "   7   Use 7 point (default is Line Printer 8.5 point)\n");
            exit(0);
            break;

         case '1':
            offset = 1;
            break;

         case '7':
            sevenpt = 1;
            adjust1 = 37;
            adjust2 = 42;
            break;
         }

   if (sevenpt)
      printf("Using 7 point condensed font\n");
   else
      printf("Using 8.5 point Line Printer font\n");

   printf("Rows and columns start with %d\n", offset);

   // Reset the printer
   Print(ESC "E");

   // Set landscape orientation
   Print(ESC "&l1O");

   // Specify the raster graphics resolution (75 dpi)
   Print(ESC "*t75R");

   printf("Printing row   ");
   for (row = 0; row < NUMROWS; row++)
      {
      printf("\b\b%2d", row + offset);

      // Position the cursor
      Print(ESC "*p%dx%dY", RIGHTX, TOPMARGIN + row * DOTSPERROW);

      // Specify the left raster graphics margin (current X position)
      Print(ESC "*r1A");

      // Transfer the raster data
      for (column = 0; column < NUMCOLS; column++)
         {
         Print(ESC "*b2W%c%c", 0xff, 0xff);     // Right border
         for (i = 0; i < 7; i++)
            Print(ESC "*b2W%c%c", 0x80, 0x00);  // Top border
         }

      // Print the left border
      Print(ESC "*b2W%c%c", 0xff, 0xff);

      // Signify the end of the raster graphic image transfer
      Print(ESC "*rB");
      }

   // Print the bottom border
   Print(ESC "*p%dx%dY", RIGHTX, BOTTOMY);
   Print(ESC "*r1A");
   for (i = 0; i < 8 * NUMCOLS + 1; i++)
      Print(ESC "*b2W%c%c", 0x80, 0x00);
   Print(ESC "*rB");

   // Use the correct symbol set
   Print(ESC "(%dU", sevenpt ? 0 : 8);

   // Set condensed print
   Print(ESC "(s%sv16.66H", sevenpt ? "7" : "8.5");

   // Print the column numbers
   for (column = 0; column < NUMCOLS; column++)
      {
      xpos = LEFTX + column * DOTSPERCOL + 7;
      quotient  = (column + offset) / 10;
      remainder = (column + offset) % 10;
      Print(ESC "*p%dx%dY%d", xpos, TOPMARGIN - 10, remainder);
      if (remainder == 0 && quotient > 0)
         Print(ESC "*p%dx%dY%d", xpos, TOPMARGIN - adjust1, quotient);
      }

   // Print the row numbers
   for (row = 0; row < NUMROWS; row++)
      Print(ESC "*p%dx%dY%2d", LEFTX - 45,
         TOPMARGIN + row * DOTSPERROW + adjust2, row + offset);

   // Send a form feed and reset the printer
   Print("\f" ESC "E");

   printf("\nDone!\n");
}

void Print(char *output, ...)
{
   va_list argptr;
   va_start(argptr, output);
   vfprintf(stdprn, output, argptr);
   va_end(argptr);
}
