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 Recipes Section 12.11 and Section 12.12. However, sometimes you simply want to convert text into a form that will display nicely on a printer that isn’t capable of handling raw text on its own (such as most of the many PostScript devices on the market). The program in Example 9-5 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 9-5. 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)=='	')
                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(  );
    }

    protected String toPSString(String o) {
        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, Recipes Section 12.11 and Section 12.12 contain “better” recipes for printing under Java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128