| 1 |
// Copyright 2000-2007 FreeHEP |
| 2 |
package org.freehep.graphicsio.ps; |
| 3 |
|
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Graphics; |
| 7 |
import java.awt.Rectangle; |
| 8 |
import java.io.File; |
| 9 |
import java.io.FileNotFoundException; |
| 10 |
import java.io.FileOutputStream; |
| 11 |
import java.io.IOException; |
| 12 |
import java.io.OutputStream; |
| 13 |
import java.io.PrintStream; |
| 14 |
|
| 15 |
import org.freehep.graphicsio.MultiPageDocument; |
| 16 |
|
| 17 |
/** |
| 18 |
* @author Charles Loomis |
| 19 |
* @author Mark Donszelmann |
| 20 |
* @version $Id: freehep-graphicsio-ps/src/main/java/org/freehep/graphicsio/ps/PSGraphics2D.java bed5e3a39f35 2007/09/10 18:13:00 duns $ |
| 21 |
*/ |
| 22 |
public class PSGraphics2D extends AbstractPSGraphics2D implements |
| 23 |
MultiPageDocument { |
| 24 |
|
| 25 |
private boolean multiPage; |
| 26 |
|
| 27 |
private int currentPage; |
| 28 |
|
| 29 |
public PSGraphics2D(File file, Dimension size) throws FileNotFoundException { |
| 30 |
this(new FileOutputStream(file), size); |
| 31 |
} |
| 32 |
|
| 33 |
public PSGraphics2D(File file, Component component) |
| 34 |
throws FileNotFoundException { |
| 35 |
this(new FileOutputStream(file), component); |
| 36 |
} |
| 37 |
|
| 38 |
public PSGraphics2D(OutputStream os, Dimension size) { |
| 39 |
super(size, false); |
| 40 |
init(os); |
| 41 |
} |
| 42 |
|
| 43 |
public PSGraphics2D(OutputStream os, Component component) { |
| 44 |
super(component, false); |
| 45 |
init(os); |
| 46 |
} |
| 47 |
|
| 48 |
protected PSGraphics2D(PSGraphics2D graphics, boolean doRestoreOnDispose) { |
| 49 |
super(graphics, doRestoreOnDispose); |
| 50 |
multiPage = graphics.multiPage; |
| 51 |
currentPage = graphics.currentPage; |
| 52 |
} |
| 53 |
|
| 54 |
protected void init(OutputStream os) { |
| 55 |
super.init(os); |
| 56 |
this.multiPage = false; |
| 57 |
this.currentPage = 0; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
public void setMultiPage(boolean multiPage) { |
| 62 |
this.multiPage = multiPage; |
| 63 |
} |
| 64 |
|
| 65 |
public boolean isMultiPage() { |
| 66 |
return multiPage; |
| 67 |
} |
| 68 |
|
| 69 |
public void openPage(Component component) throws IOException { |
| 70 |
openPage(component.getSize(), component.getName(), component); |
| 71 |
} |
| 72 |
|
| 73 |
public void openPage(Dimension size, String title) throws IOException { |
| 74 |
openPage(size, title, null); |
| 75 |
} |
| 76 |
|
| 77 |
protected void openPage(Dimension size, String title, Component component) { |
| 78 |
currentPage++; |
| 79 |
title = (title == null) ? "" + currentPage : "(" + title + ")"; |
| 80 |
if (isMultiPage()) |
| 81 |
os.println("%%Page: " + title + " " + currentPage); |
| 82 |
|
| 83 |
os.println(isMultiPage() ? "%%BeginPageSetup" : "%%BeginSetup"); |
| 84 |
|
| 85 |
super.openPage(size, title, component); |
| 86 |
|
| 87 |
os.println(isMultiPage() ? "%%EndPageSetup" : "%%EndSetup"); |
| 88 |
os.println(); |
| 89 |
|
| 90 |
try { |
| 91 |
writeGraphicsState(); |
| 92 |
writeBackground(); |
| 93 |
} catch (Exception e) { |
| 94 |
handleException(e); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public void closePage() { |
| 99 |
if (isMultiPage()) |
| 100 |
os.println("%%PageTrailer"); |
| 101 |
} |
| 102 |
|
| 103 |
public void writeHeader() throws IOException { |
| 104 |
if (!isMultiPage()) { |
| 105 |
Dimension size = getSize(); |
| 106 |
// moved to openPage for multiPage |
| 107 |
resetClip(new Rectangle(0, 0, size.width, size.height)); |
| 108 |
} |
| 109 |
|
| 110 |
os = new PrintStream(ros, true); |
| 111 |
os.println("%!PS-Adobe-3.0"); |
| 112 |
|
| 113 |
super.writeHeader(); |
| 114 |
|
| 115 |
if (!isMultiPage()) |
| 116 |
openPage(getSize(), null, getComponent()); |
| 117 |
} |
| 118 |
|
| 119 |
public void writeTrailer() throws IOException { |
| 120 |
if (!isMultiPage()) |
| 121 |
closePage(); |
| 122 |
super.writeTrailer(); |
| 123 |
if (isMultiPage()) |
| 124 |
os.println("%%Pages: " + currentPage); |
| 125 |
os.println("%%EOF"); |
| 126 |
} |
| 127 |
|
| 128 |
public Graphics create() { |
| 129 |
try { |
| 130 |
writeGraphicsSave(); |
| 131 |
} catch (IOException e) { |
| 132 |
handleException(e); |
| 133 |
} |
| 134 |
return new PSGraphics2D(this, true); |
| 135 |
} |
| 136 |
|
| 137 |
public Graphics create(double x, double y, double width, double height) { |
| 138 |
try { |
| 139 |
writeGraphicsSave(); |
| 140 |
} catch (IOException e) { |
| 141 |
handleException(e); |
| 142 |
} |
| 143 |
PSGraphics2D graphics = new PSGraphics2D(this, true); |
| 144 |
graphics.translate(x, y); |
| 145 |
graphics.clipRect(0, 0, width, height); |
| 146 |
return graphics; |
| 147 |
} |
| 148 |
} |