Text to Postscript : PDF PostScript « 2D Graphics GUI « Java






Text to Postscript

Text to Postscript

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */
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" */
  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 ");
  }
}


           
       








Related examples in the same category

1.Output to PDF fileOutput to PDF file