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

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » PDF PostScriptScreenshots 
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[] avthrows 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 fileNamethrows IOException {
    br = new BufferedReader(new FileReader(fileName));
  }

  public PSFormatter(Reader inthrows 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.