FileDocCategorySizeDatePackage
PSFormatter.javaAPI DocExample3124Sun Mar 07 11:46:00 GMT 2004None

PSFormatter

public class PSFormatter extends Object
Text to PS

Fields Summary
protected BufferedReader
br
The current input source
protected int
pageNum
The current page number
protected int
curX
The current X and Y on the page
protected int
curY
protected int
lineNum
The current line number on page
protected int
tabPos
The current tab setting
public static final int
INCH
protected int
leftMargin
The left margin indent
protected int
topMargin
The top of page indent
protected int
botMargin
The bottom of page indent
protected int
points
protected int
leading
Constructors Summary
public PSFormatter(String fileName)

		br = new BufferedReader(new FileReader(fileName));
	
public PSFormatter(Reader in)

		if (in instanceof BufferedReader)
			br = (BufferedReader)in;
		else
			br = new BufferedReader(in);
	
Methods Summary
protected voiddoLine(java.lang.String line)
Process one line from the current input

		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();
	
public static voidmain(java.lang.String[] av)


	       
		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();
		}
	
protected voidmoveTo(int x, int y)

		curX = x;
		curY = y;
		println(x + " " + y + " " + "moveto");
	
protected voidprintln(java.lang.String s)

		System.out.println(s);
	
protected voidprocess()
Main processing of the current input source.


		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");
	
voidprologue()

		println("%!PS-Adobe");
		println("/Courier findfont " + points + " scalefont setfont ");
	
protected voidstartPage()
Handle start of page details.

		if (pageNum++ > 0)
			println("showpage");
		lineNum = 0;
		moveTo(leftMargin, topMargin);
	
protected java.lang.StringtoPSString(java.lang.String o)
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();