📄 postprint.c
字号:
/* * * Handles things that must be done after the options are read but before the * input files are processed. linespp (lines per page) can be set using the -l * option. If it's not positive we calculate a reasonable value using the * requested point size - assuming LINESPP lines fit on a page in point size * POINTSIZE. * */ 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); if ( linespp <= 0 ) linespp = LINESPP * POINTSIZE / pointsize;} /* End of setup *//*****************************************************************************/arguments(){/* * * Makes sure all the non-option command line arguments are processed. If we get * here and there aren't any arguments left, or if '-' is one of the input files * we'll translate stdin. * */ if ( argc < 1 ) text(); else { /* at least one argument is left */ while ( argc > 0 ) { if ( strcmp(*argv, "-") == 0 ) fp_in = stdin; else if ( (fp_in = fopen(*argv, "r")) == NULL ) error(FATAL, "can't open %s", *argv); text(); if ( fp_in != stdin ) fclose(fp_in); argc--; argv++; } /* End while */ } /* End else */} /* End of arguments *//*****************************************************************************/done(){/* * * Finished with all the input files, so mark the end of the pages with a TRAILER * comment, make sure the last page prints, and add things like the PAGES comment * that can only be determined after all the input files have been read. * */ fprintf(stdout, "%s", TRAILER); fprintf(stdout, "done\n"); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, fontname); fprintf(stdout, "%s %d\n", PAGES, printed);} /* End of done *//*****************************************************************************/account(){/* * * Writes an accounting record to *fp_acct provided it's not NULL. Accounting is * requested using the -A or -J options. * */ if ( fp_acct != NULL ) fprintf(fp_acct, " print %d\n copies %d\n", printed, copies);} /* End of account *//*****************************************************************************/text(){ int ch; /* next input character *//* * * Translates *fp_in into PostScript. Intercepts space, tab, backspace, newline, * return, and formfeed. Everything else goes to oput(), which handles quoting * (if needed) and escapes for nonascii characters if extended is TRUE. The * redirect(-1) call forces the initial output to go to /dev/null - so stuff * that formfeed() does at the end of each page goes to /dev/null rather than * the real output file. * */ redirect(-1); /* get ready for the first page */ formfeed(); /* force PAGE comment etc. */ while ( (ch = getc(fp_in)) != EOF ) switch ( ch ) { case '\n': newline(); break; case '\t': case '\b': case ' ': spaces(ch); break; case '\014': formfeed(); break; case '\r': if ( crmode == 1 ) spaces(ch); else if ( crmode == 2 ) newline(); break; default: oput(ch); break; } /* End switch */ formfeed(); /* next file starts on a new page? */} /* End of text *//*****************************************************************************/formfeed(){/* * * Called whenever we've finished with the last page and want to get ready for the * next one. Also used at the beginning and end of each input file, so we have to * be careful about what's done. The first time through (up to the redirect() call) * output goes to /dev/null. * * Adobe now recommends that the showpage operator occur after the page level * restore so it can be easily redefined to have side-effects in the printer's VM. * Although it seems reasonable I haven't implemented it, because it makes other * things, like selectively setting manual feed or choosing an alternate paper * tray, clumsy - at least on a per page basis. * */ if ( fp_out == stdout ) /* count the last page */ printed++; endline(); /* print the last line */ fprintf(fp_out, "cleartomark\n"); fprintf(fp_out, "showpage\n"); fprintf(fp_out, "saveobj restore\n"); fprintf(fp_out, "%s %d %d\n", ENDPAGE, page, printed); if ( ungetc(getc(fp_in), fp_in) == EOF ) redirect(-1); else redirect(++page); fprintf(fp_out, "%s %d %d\n", PAGE, page, printed+1); fprintf(fp_out, "/saveobj save def\n"); fprintf(fp_out, "mark\n"); writerequest(printed+1, fp_out); fprintf(fp_out, "%d pagesetup\n", printed+1); line = 1;} /* End of formfeed *//*****************************************************************************/newline(){/* * * Called when we've read a newline character. The call to startline() ensures * that at least an empty string is on the stack. * */ startline(); endline(); /* print the current line */ if ( ++line > linespp ) /* done with this page */ formfeed();} /* End of newline *//*****************************************************************************/spaces(ch) int ch; /* next input character */{ int endcol; /* ending column */ int i; /* final distance - in spaces *//* * * Counts consecutive spaces, tabs, and backspaces and figures out where the next * string should start. Once that's been done we try to choose an efficient way * to output the required number of spaces. The choice is between using procedure * l with a single string on the stack and L with several string and column pairs. * We usually break even, in terms of the size of the output file, if we need four * consecutive spaces. More means using L decreases the size of the file. For now * if there are less than 6 consecutive spaces we just add them to the current * string, otherwise we end that string, follow it by its starting position, and * begin a new one that starts at endcol. Backspacing is always handled this way. * */ startline(); /* so col makes sense */ endcol = col; do { if ( ch == ' ' ) endcol++; else if ( ch == '\t' ) endcol += tabstops - ((endcol - 1) % tabstops); else if ( ch == '\b' ) endcol--; else if ( ch == '\r' ) endcol = 1; else break; } while ( ch = getc(fp_in) ); /* if ch is 0 we'd quit anyway */ ungetc(ch, fp_in); /* wasn't a space, tab, or backspace */ if ( endcol < 1 ) /* can't move past left edge */ endcol = 1; if ( (i = endcol - col) >= 0 && i < 6 ) for ( ; i > 0; i-- ) oput((int)' '); else { endstring(); col = stringstart = endcol; } /* End else */} /* End of spaces *//*****************************************************************************/startline(){/* * * Called whenever we want to be certain we're ready to start pushing characters * into an open string on the stack. If stringcount is positive we've already * started, so there's nothing to do. The first string starts in column 1. * */ if ( stringcount < 1 ) { putc('(', fp_out); stringstart = col = 1; stringcount = 1; } /* End if */} /* End of startline *//*****************************************************************************/endstring(){/* * * End the current string and start a new one. * */ if ( stringcount > 100 ) { /* don't put too much on the stack */ fprintf(fp_out, ")%d LL\n(", stringstart-1); stringcount = 2; /* kludge - don't let endline() use l */ } else { fprintf(fp_out, ")%d(", stringstart-1); stringcount++; } /* End else */} /* End of endstring *//*****************************************************************************/endline(){/* * * Generates a call to the PostScript procedure that processes all the text on * the stack - provided stringcount is positive. If one string is on the stack * the fast procedure (ie. l) is used to print the line, otherwise the slower * one that processes string and column pairs is used. * */ if ( stringcount == 1 ) fprintf(fp_out, ")l\n"); else if ( stringcount > 1 ) fprintf(fp_out, ")%d L\n", stringstart-1); stringcount = 0;} /* End of endline *//*****************************************************************************/oput(ch) int ch; /* next output character */{/* * * Responsible for adding all printing characters from the input file to the * open string on top of the stack. * */ if ( isascii(ch) && isprint(ch) ) { startline(); if ( ch == '(' || ch == ')' || ch == '\\' ) putc('\\', fp_out); putc(ch, fp_out); col++; } else if ( extended == TRUE ) { startline(); fprintf(fp_out, "\\%.3o", ch & 0377); col++; } /* End if */} /* End of oput *//*****************************************************************************/redirect(pg) int pg; /* next page we're printing */{ static FILE *fp_null = NULL; /* if output is turned off *//* * * If we're not supposed to print page pg, fp_out will be directed to /dev/null, * otherwise output goes to stdout. * */ if ( pg >= 0 && in_olist(pg) == ON ) fp_out = stdout; else if ( (fp_out = fp_null) == NULL ) fp_out = fp_null = fopen("/dev/null", "w");} /* End of redirect *//*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -