| 1 |
// Copyright 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.Insets; |
| 8 |
import java.awt.Rectangle; |
| 9 |
import java.io.File; |
| 10 |
import java.io.FileNotFoundException; |
| 11 |
import java.io.FileOutputStream; |
| 12 |
import java.io.IOException; |
| 13 |
import java.io.OutputStream; |
| 14 |
import java.io.PrintStream; |
| 15 |
|
| 16 |
import org.freehep.graphicsio.PageConstants; |
| 17 |
|
| 18 |
/** |
| 19 |
* Writes Encapsulated PostScript with bounding box. |
| 20 |
* @author duns |
| 21 |
* @version $Id: freehep-graphicsio-ps/src/main/java/org/freehep/graphicsio/ps/EPSGraphics2D.java bed5e3a39f35 2007/09/10 18:13:00 duns $ |
| 22 |
*/ |
| 23 |
public class EPSGraphics2D extends AbstractPSGraphics2D { |
| 24 |
|
| 25 |
public EPSGraphics2D(File file, Dimension size) throws FileNotFoundException { |
| 26 |
this(new FileOutputStream(file), size); |
| 27 |
} |
| 28 |
|
| 29 |
public EPSGraphics2D(File file, Component component) |
| 30 |
throws FileNotFoundException { |
| 31 |
this(new FileOutputStream(file), component); |
| 32 |
} |
| 33 |
|
| 34 |
public EPSGraphics2D(OutputStream os, Dimension size) { |
| 35 |
super(size, false); |
| 36 |
init(os); |
| 37 |
} |
| 38 |
|
| 39 |
public EPSGraphics2D(OutputStream os, Component component) { |
| 40 |
super(component, false); |
| 41 |
init(os); |
| 42 |
} |
| 43 |
|
| 44 |
protected EPSGraphics2D(EPSGraphics2D graphics, boolean doRestoreOnDispose) { |
| 45 |
super(graphics, doRestoreOnDispose); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the bounding box for this page. |
| 50 |
* |
| 51 |
* @return pageSize - margins |
| 52 |
*/ |
| 53 |
private Rectangle getBoundingBox() { |
| 54 |
// determine page size |
| 55 |
Dimension pageSize = getPageSize(); |
| 56 |
|
| 57 |
// Our PS Header has internal page orientation mode, all sizes given in |
| 58 |
// portrait |
| 59 |
Insets margins = getPropertyInsets(PAGE_MARGINS); |
| 60 |
boolean isPortrait = getProperty(ORIENTATION).equals( |
| 61 |
PageConstants.PORTRAIT); |
| 62 |
|
| 63 |
// Available width and height. |
| 64 |
double awidth = (pageSize.width - margins.left - margins.right); |
| 65 |
double aheight = (pageSize.height - margins.top - margins.bottom); |
| 66 |
|
| 67 |
// Image width and height (adjusted for portrait or landscape) |
| 68 |
Dimension size = getSize(); |
| 69 |
double iwidth = (isPortrait ? size.width : size.height); |
| 70 |
double iheight = (isPortrait ? size.height : size.width); |
| 71 |
|
| 72 |
// Choose the minimum scale factor. |
| 73 |
double sf = Math.min(awidth / iwidth, aheight / iheight); |
| 74 |
if (!isProperty(FIT_TO_PAGE)) { |
| 75 |
sf = Math.min(sf, 1.); |
| 76 |
} |
| 77 |
|
| 78 |
// Lower left corner. |
| 79 |
double x0 = awidth / 2. + margins.left - sf * iwidth / 2.; |
| 80 |
double y0 = aheight / 2. + margins.bottom - sf * iheight / 2.; |
| 81 |
|
| 82 |
// Upper right corner. |
| 83 |
double x1 = x0 + sf * iwidth; |
| 84 |
double y1 = y0 + sf * iheight; |
| 85 |
|
| 86 |
int llx = (int) x0; |
| 87 |
int lly = (int) y0; |
| 88 |
int urx = (int) Math.ceil(x1); |
| 89 |
int ury = (int) Math.ceil(y1); |
| 90 |
|
| 91 |
// Convert these back to integer values. |
| 92 |
return new Rectangle(llx, lly, urx - llx, ury - lly); |
| 93 |
} |
| 94 |
|
| 95 |
public void writeHeader() throws IOException { |
| 96 |
Dimension size = getSize(); |
| 97 |
// moved to openPage for multiPage |
| 98 |
resetClip(new Rectangle(0, 0, size.width, size.height)); |
| 99 |
|
| 100 |
os = new PrintStream(ros, true); |
| 101 |
os.println("%!PS-Adobe-3.0" + " EPSF-3.0"); |
| 102 |
Rectangle bbox = getBoundingBox(); |
| 103 |
os.println("%%BoundingBox: " + bbox.x + " " + bbox.y + " " |
| 104 |
+ (bbox.x + bbox.width) + " " + (bbox.y + bbox.height)); |
| 105 |
|
| 106 |
super.writeHeader(); |
| 107 |
|
| 108 |
os.println("%%BeginSetup"); |
| 109 |
|
| 110 |
super.openPage(getSize(), null, getComponent()); |
| 111 |
|
| 112 |
os.println("%%EndSetup"); |
| 113 |
os.println(); |
| 114 |
|
| 115 |
try { |
| 116 |
writeGraphicsState(); |
| 117 |
writeBackground(); |
| 118 |
} catch (Exception e) { |
| 119 |
handleException(e); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public void writeTrailer() throws IOException { |
| 124 |
super.writeTrailer(); |
| 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 EPSGraphics2D(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 |
EPSGraphics2D graphics = new EPSGraphics2D(this, true); |
| 144 |
graphics.translate(x, y); |
| 145 |
graphics.clipRect(0, 0, width, height); |
| 146 |
return graphics; |
| 147 |
} |
| 148 |
} |