📄 postbgi.c
字号:
/* * * postbgi - BGI (Basic Graphical Instructions) to PostScript translator. * * A simple program that translates BGI files into PostScript. Probably only * useful in Computer Centers that support STARE or PRISM plotters. Most of the * code was borrowed from the corresponding program that was written for printers * that understand Impress. * * Extending the original program to handle PRISM jobs was not trivial. Graphics * packages that support PRISM occasionally use BGI commands that I ignored in the * STARE implementation. Subroutines, color requests, patterns (for filling), and * filled trapeziods were the most important omissions. All are now implemented, * and at present only repeats, filled slices, and raster rectangles are missing. * * Pattern filling results were not always predictable or even good, unless the * halftone screen definitions were changed and scaling was adjusted so one pixel * in user space mapped into an integral number of device space pixels. Doing that * makes the resulting PostScript output device dependent, but was often necessary. * I've added two booleans to the PostScript prologue (fixscreen and scaletodevice) * that control what's done. By default both are false (check postbgi.ps) but can * be set to true on the command line using the -P option or by hand by changing * the definitions in the prologue. A command line that would set fixscreen and * scaletodevice true would look like, * * postbgi -P"/fixscreen true" -P"/scaletodevice true" file >file.ps * * Several other approaches are available if you want to have your spooler handle * STARE and PRISM jobs differently. A boolean called prism is defined in the * prologue (postbgi.ps) and if it's set to true PostScript procedure setup will * set fixscreen and scaletodevice to true before anything important is done. That * means the following command line, * * postbgi -P"/prism true" file >file.ps * * accomplishes the same things as the last example. Two different prologue files, * one for STARE jobs and the other for PRISM, could be used and the spooler could * point postbgi to the appropriate one using the -L option. In that case the only * important difference in the two prologues would be the definition of prism. The * prologue used for PRISM jobs would have prism set to true, while the STARE * prologue would have it set to false. * * Also included is code that ties lines to device space coordinates. What you get * is a consistent line thickness, but placement of lines won't be exact. It's a * trade-off that should be right for most jobs. Everything is implemented in the * prologue (postbgi.ps) and nothing will be done if the linewidth is zero or if * the boolean fixlinewidth (again in postbgi.ps) is false. Once again the -P * option can be used to set fixlinewidth to whatever you choose. * * BGI supports color mixing but PostScript doesn't. BGI files that expect to mix * colors won't print properly. PostScript's fill operator overlays whatever has * already been put down. Implementing color mixing would have been a terribly * difficult job - not worth the effort! * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following PostScript procedures * are defined in that file: * * setup * * mark ... setup - * * Handles special initialization stuff that depends on how the program * was called. Expects to find a mark followed by key/value pairs on the * stack. The def operator is applied to each pair up to the mark, then * the default state is set up. * * pagesetup * * page pagesetup - * * Does whatever is needed to set things up for the next page. Expects * to find the current page number on the stack. * * v * * dx1 dy1 ... dxn dyn x y v - * * Draws the vector described by the numbers on the stack. The top two * numbers are the coordinates of the starting point. The rest of the * numbers are relative displacements from the preceeding point. * * pp * * x1 y1 ... xn yn string pp - * * Prints string, which is always a single character, at the points * represented by the rest of the numbers on the stack. * * R * * n deltax deltay x y R - * * Creates a rectangular path with its lower left corner at (x, y) and * sides of length deltax and deltay. The resulting path is stroked if * n is 0 and filled otherwise. * * T * * dx3 dy3 dx2 dy2 dx1 dy1 x y T - * * Fills a trapezoid starting at (x, y) and having relative displacements * given by the (dx, dy) pairs. * * t * * angle x y string t - * * Prints string starting at (x, y) using an orientation of angle degrees. * The PostScript procedure can handle any angle, but BGI files will only * request 0 or 90 degrees. Text printed at any other orientation will be * vector generated. * * p * * x y p - * * Called to mark the point (x, y). It fills a small circle, that right * now has a constant radius. This stuff could probably be much more * efficient? * * l * * array l - * * Sets the line drawing mode according to the description given in * array. The arrays that describe the different line styles are declared * in STYLES (file posttek.h), although it would be better to have them * defined in the prologue. * * c * * red green blue c - * * Sets the current PostScript RGB color using setrgbcolor. Also used for * selecting appropriate patterns as colors. * * f * * bgisize f - * * Changes the size of the font that's used to print text. bgisize is a * grid separation in a 5 by 7 array in which characters are assumed to * be built. * * done * * done * * Makes sure the last page is printed. Only needed when we're printing * more than one page on each sheet of paper. * * The default line width is zero, which forces lines to be one pixel wide. That * works well for 'write to black' engines but won't be right for 'write to white' * engines. The line width can be changed using the -w option, or you can change * the initialization of linewidth in the prologue. Code in the prologue supports * the generation of uniform width lines when linewidth is non-zero and boolean * fixlinewidth is true. * * Many default values, like the magnification and orientation, are defined in * the prologue, which is where they belong. If they're changed (by options), an * appropriate definition is made after the prologue is added to the output file. * The -P option passes arbitrary PostScript through to the output file. Among * other things it can be used to set (or change) values that can't be accessed by * other options. * */#include <stdio.h>#include <sys/types.h>#include <fcntl.h>#include <signal.h>#include <math.h>#include <ctype.h>#ifdef plan9#define isascii(c) ((unsigned char)(c)<=0177)#endif#include "comments.h" /* PostScript file structuring comments */#include "gen.h" /* general purpose definitions */#include "path.h" /* for the prologue */#include "ext.h" /* external variable declarations */#include "postbgi.h" /* a few definitions just used here */char *optnames = "a:c:f:m:n:o:p:w:x:y:A:C:E:J:L:P:R:DI";char *prologue = POSTBGI; /* default PostScript prologue */char *formfile = FORMFILE; /* stuff for multiple pages per sheet */int formsperpage = 1; /* page images on each piece of paper */int copies = 1; /* and this many copies of each sheet */char *styles[] = STYLES; /* descriptions of line styles */int hpos = 0; /* current horizontal */int vpos = 0; /* and vertical position */int bgisize = BGISIZE; /* just the character grid spacing */int linespace; /* distance between lines of text */int bgimode; /* character or graph mode */int in_subr = FALSE; /* currently defining a subroutine */int in_global = FALSE; /* to save space with subroutine defs */int subr_id = 0; /* defining this subroutine */int shpos = 0; /* starting horizontal */int svpos = 0; /* and vertical positions - subroutines */Disp displacement[64]; /* dx and dy after a subroutine call */Fontmap fontmap[] = FONTMAP; /* for translating font names */char *fontname = "Courier"; /* use this PostScript font */int page = 0; /* page we're working on */int printed = 0; /* printed this many pages */FILE *fp_in = stdin; /* read from this file */FILE *fp_out = NULL; /* and write stuff here */FILE *fp_acct = NULL; /* for accounting data *//*****************************************************************************/main(agc, agv) int agc; char *agv[];{/* * * A program that converts BGI (Basic Graphical Instructions) files generated by * packages like GRAFPAC and DISSPLA into PostScript. It does an adequate job but * is far from perfect. A few things still haven't been implemented (eg. repeats * and raster rectangles), but what's here should be good enough for most of our * STARE and PRISM jobs. Color mixing (in PRISM jobs) won't work on PostScript * printers, and there's no chance I'll implement it! * */ argc = agc; /* global so everyone can use them */ argv = agv; prog_name = argv[0]; /* just for error messages */ init_signals(); /* set up interrupt handling */ header(); /* PostScript header comments */ options(); /* command line options */ setup(); /* for PostScript */ arguments(); /* followed by each input file */ done(); /* print the last page etc. */ account(); /* job accounting data */ exit(x_stat); /* everything probably went OK */} /* End of main *//*****************************************************************************/init_signals(){/* * * Make sure we handle interrupts. * */ if ( signal(SIGINT, interrupt) == SIG_IGN ) { signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } else { signal(SIGHUP, interrupt); signal(SIGQUIT, interrupt); } /* End else */ signal(SIGTERM, interrupt);} /* End of init_signals *//*****************************************************************************/header(){ int ch; /* return value from getopt() */ int old_optind = optind; /* for restoring optind - should be 1 *//* * * Scans the option list looking for things, like the prologue file, that we need * right away but could be changed from the default. Doing things this way is an * attempt to conform to Adobe's latest file structuring conventions. In particular * they now say there should be nothing executed in the prologue, and they have * added two new comments that delimit global initialization calls. Once we know * where things really are we write out the job header, follow it by the prologue, * and then add the ENDPROLOG and BEGINSETUP comments. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) if ( ch == 'L' ) prologue = optarg; else if ( ch == '?' ) error(FATAL, ""); optind = old_optind; /* get ready for option scanning */ fprintf(stdout, "%s", CONFORMING); fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, ATEND); fprintf(stdout, "%s %s\n", PAGES, ATEND); fprintf(stdout, "%s", ENDCOMMENTS); if ( cat(prologue) == FALSE ) error(FATAL, "can't read %s", prologue); fprintf(stdout, "%s", ENDPROLOG); fprintf(stdout, "%s", BEGINSETUP); fprintf(stdout, "mark\n");} /* End of header *//*****************************************************************************/options(){ int ch; /* option name - from getopt() *//* * * Reads and processes the command line options. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) { switch ( ch ) { case 'a': /* aspect ratio */ fprintf(stdout, "/aspectratio %s def\n", optarg); break; case 'c': /* copies */ copies = atoi(optarg); fprintf(stdout, "/#copies %s def\n", optarg); break; case 'f': /* new font */ fontname = get_font(optarg); fprintf(stdout, "/font /%s def\n", fontname); break; case 'm': /* magnification */ fprintf(stdout, "/magnification %s def\n", optarg); break; case 'n': /* forms per page */ formsperpage = atoi(optarg); fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg); fprintf(stdout, "/formsperpage %s def\n", optarg); break; case 'o': /* output page list */ out_list(optarg); break; case 'p': /* landscape or portrait mode */ if ( *optarg == 'l' ) fprintf(stdout, "/landscape true def\n"); else fprintf(stdout, "/landscape false def\n"); break; case 'w': /* line width */ fprintf(stdout, "/linewidth %s def\n", optarg); break; case 'x': /* shift horizontally */ fprintf(stdout, "/xoffset %s def\n", optarg); break; case 'y': /* and vertically on the page */ fprintf(stdout, "/yoffset %s def\n", optarg); break; case 'A': /* force job accounting */ case 'J': if ( (fp_acct = fopen(optarg, "a")) == NULL ) error(FATAL, "can't open accounting file %s", optarg); break; case 'C': /* copy file straight to output */ if ( cat(optarg) == FALSE ) error(FATAL, "can't read %s", optarg); break; case 'E': /* text font encoding */ fontencoding = optarg; break; case 'L': /* Postscript prologue file */ prologue = optarg; break; case 'P': /* PostScript pass through */ fprintf(stdout, "%s\n", optarg); break; case 'R': /* special global or page level request */ saverequest(optarg); break; case 'D': /* debug flag */ debug = ON; break; case 'I': /* ignore FATAL errors */ ignore = ON; break; case '?': /* don't know the option */ error(FATAL, ""); break; default: /* don't know what to do for ch */ error(FATAL, "missing case for option %c", ch); break; } /* End switch */ } /* End while */ argc -= optind; /* get ready for non-option args */ argv += optind;} /* End of options *//*****************************************************************************/char *get_font(name) char *name; /* name the user asked for */{ int i; /* for looking through fontmap[] *//* * * Called from options() to map a user's font name into a legal PostScript name. * If the lookup fails *name is returned to the caller. That should let you choose * any PostScript font. * */ for ( i = 0; fontmap[i].name != NULL; i++ ) if ( strcmp(name, fontmap[i].name) == 0 ) return(fontmap[i].val); return(name);} /* End of get_font *//*****************************************************************************/setup(){/* * * Handles things that must be done after the options are read but before the * input files are processed. * */ writerequest(0, stdout); /* global requests eg. manual feed */ setencoding(fontencoding); fprintf(stdout, "setup\n"); if ( formsperpage > 1 ) { if ( cat(formfile) == FALSE ) error(FATAL, "can't read %s", formfile); fprintf(stdout, "%d setupforms\n", formsperpage); } /* End if */ fprintf(stdout, "%s", ENDSETUP);} /* End of setup *//*****************************************************************************/arguments(){/* * * Makes sure all the non-option command line options are processed. If we get * here and there aren't any arguments left, or if '-' is one of the input files * we'll process stdin. * */ if ( argc < 1 ) conv(); else while ( argc > 0 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -