Recipe 10.22 Program: Text to PostScript


There are several approaches to printing in Java. In a GUI application, or if you want to use the graphical facilities that Java offers (fonts, colors, drawing primitives, and the like), you should refer to Recipe 13.11. However, sometimes you simply want to convert text into a form that prints nicely on a printer that isn't capable of handling raw text on its own (such as most of the PostScript devices on the market). The program in Example 10-9 shows code for reading one or more text files and outputting each of them in a plain font with PostScript around it. Because of the nature of PostScript, certain characters must be escaped; this is handled in toPsString( ), which in turn is called from doLine( ). There is also code for keeping track of the current position on the page. The output of this program can be sent directly to a PostScript printer.

Example 10-9. PSFormatter.java
import java.io.*; /** Text to PS */ public class PSFormatter {     /** The current input source */     protected BufferedReader br;     /** The current page number */     protected int pageNum;     /** The current X and Y on the page */     protected int curX, curY;     /** The current line number on page */     protected int lineNum;     /** The current tab setting */     protected int tabPos = 0;     public static final int INCH = 72;    // PS constant: 72 pts/inch     // Page parameters     /** The left margin indent */     protected int leftMargin = 50;     /** The top of page indent */     protected int topMargin = 750;     /** The bottom of page indent */     protected int botMargin = 50;     // FORMATTING PARAMETERS     protected int points = 12;     protected int leading = 14;     public static void main(String[] av) throws IOException {         if (av.length == 0)              new PSFormatter(                 new InputStreamReader(System.in)).process( );         else for (int i = 0; i < av.length; i++) {             new PSFormatter(av[i]).process( );         }     }     public PSFormatter(String fileName) throws IOException {         br = new BufferedReader(new FileReader(fileName));     }     public PSFormatter(Reader in) throws IOException {         if (in instanceof BufferedReader)             br = (BufferedReader)in;         else             br = new BufferedReader(in);     }     /** Main processing of the current input source. */     protected void process( ) throws IOException {         String line;         prologue( );            // emit PostScript prologue, once.         startPage( );        // emit top-of-page (ending previous)         while ((line = br.readLine( )) != null) {             if (line.startsWith("\f") || line.trim( ).equals(".bp")) {                 startPage( );                 continue;             }             doLine(line);         }         // finish last page, if not already done.         if (lineNum != 0)             println("showpage");     }     /** Handle start of page details. */     protected void startPage( ) {         if (pageNum++ > 0)             println("showpage");         lineNum = 0;         moveTo(leftMargin, topMargin);     }     /** Process one line from the current input */     protected void doLine(String line) {         tabPos = 0;         // count leading (not imbedded) tabs.         for (int i=0; i<line.length( ); i++) {             if (line.charAt(i)=='\t')                 tabPos++;             else                 break;         }         String l = line.trim( ); // removes spaces AND tabs         if (l.length( ) == 0) {             ++lineNum;             return;         }         moveTo(leftMargin + (tabPos * INCH),             topMargin-(lineNum++ * leading));         println('(' + toPSString(l)+ ") show");         // If we just hit the bottom, start a new page         if (curY <= botMargin)             startPage( );     }     /** Overly-simplistic conversion to PS, e.g., breaks on "foo\)bar" */         StringBuffer sb = new StringBuffer( );         for (int i=0; i<o.length( ); i++) {             char c = o.charAt(i);             switch(c) {                 case '(':    sb.append("\\("); break;                 case ')':    sb.append("\\)"); break;                 default:    sb.append(c); break;             }         }         return sb.toString( );     }     protected void println(String s) {         System.out.println(s);     }     protected void moveTo(int x, int y) {         curX = x;         curY = y;         println(x + " " + y + " " + "moveto");     }     void prologue( ) {         println("%!PS-Adobe");         println("/Courier findfont " + points + " scalefont setfont ");     } }

The program could certainly be generalized more, and certain features (such as wrapping long lines) could be handled. I could also wade into the debate among PostScript experts as to how much of the formatting should be done on the main computer and how much should be done by the PostScript program interpreter running in the printer. But perhaps I won't get into that discussion. At least, not today.

See Also

As mentioned, Recipe 13.11 contains "better" recipes for printing under Java.

For all topics in this chapter, Elliotte Rusty Harold's book Java I/O should be considered the antepenultimate documentation. The penultimate reference is the Javadoc documentation, while the ultimate reference is, if you really need it, the source code for the Java API, to which I have not needed to make a single reference in writing this chapter.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net