| 1 |
// Copyright 2001-2006, FreeHEP. |
| 2 |
package org.freehep.graphicsio.emf; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Point; |
| 7 |
import java.awt.Rectangle; |
| 8 |
import java.awt.geom.AffineTransform; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.OutputStream; |
| 11 |
|
| 12 |
import org.freehep.util.io.ActionHeader; |
| 13 |
import org.freehep.util.io.Tag; |
| 14 |
import org.freehep.util.io.TagHeader; |
| 15 |
import org.freehep.util.io.TaggedOutputStream; |
| 16 |
|
| 17 |
/** |
| 18 |
* EMF Binary Output Stream. Tags written with this OutputStream will produce a |
| 19 |
* binary EMF file. |
| 20 |
* |
| 21 |
* @author Mark Donszelmann |
| 22 |
* @version $Id: freehep-graphicsio-emf/src/main/java/org/freehep/graphicsio/emf/EMFOutputStream.java eb6e9c24f3e4 2006/03/02 00:56:03 duns $ |
| 23 |
*/ |
| 24 |
public class EMFOutputStream extends TaggedOutputStream { |
| 25 |
|
| 26 |
private String application; |
| 27 |
|
| 28 |
private String name; |
| 29 |
|
| 30 |
private int recordCount; |
| 31 |
|
| 32 |
private Rectangle imageBounds; |
| 33 |
|
| 34 |
private int version; |
| 35 |
|
| 36 |
private EMFHandleManager handles; |
| 37 |
|
| 38 |
private Dimension device; |
| 39 |
|
| 40 |
public EMFOutputStream(OutputStream os, Rectangle imageBounds, |
| 41 |
EMFHandleManager handles, String application, String name, |
| 42 |
Dimension device, int version) throws IOException { |
| 43 |
|
| 44 |
// EMF is little-endian |
| 45 |
super(os, new EMFTagSet(version), null, true); |
| 46 |
this.recordCount = 0; |
| 47 |
this.version = version; |
| 48 |
this.imageBounds = imageBounds; |
| 49 |
this.handles = handles; |
| 50 |
this.application = application; |
| 51 |
this.name = name; |
| 52 |
this.device = device; |
| 53 |
|
| 54 |
// will be popped by close() |
| 55 |
pushBuffer(); |
| 56 |
} |
| 57 |
|
| 58 |
public EMFOutputStream(OutputStream os, Rectangle imageBounds, |
| 59 |
EMFHandleManager handles, String application, String name, |
| 60 |
Dimension device) throws IOException { |
| 61 |
|
| 62 |
this(os, imageBounds, handles, application, name, device, 1); |
| 63 |
} |
| 64 |
|
| 65 |
public void close() throws IOException { |
| 66 |
int len = popBuffer(); |
| 67 |
recordCount++; |
| 68 |
EMFHeader header = new EMFHeader(imageBounds, getVersion(), 0, len, |
| 69 |
recordCount, handles.maxHandlesUsed(), application, name, |
| 70 |
device); |
| 71 |
writeHeader(header); |
| 72 |
append(); |
| 73 |
|
| 74 |
super.close(); |
| 75 |
} |
| 76 |
|
| 77 |
// DWORD |
| 78 |
public void writeDWORD(int i) throws IOException { |
| 79 |
writeUnsignedInt(i); |
| 80 |
} |
| 81 |
|
| 82 |
// DWORD [] |
| 83 |
public void writeDWORD(int[] w) throws IOException { |
| 84 |
for (int i = 0; i < w.length; i++) { |
| 85 |
writeDWORD(w[i]); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// WORD |
| 90 |
public void writeWORD(int s) throws IOException { |
| 91 |
writeUnsignedShort(s); |
| 92 |
} |
| 93 |
|
| 94 |
public void writeFLOAT(float f) throws IOException { |
| 95 |
writeFloat(f); |
| 96 |
} |
| 97 |
|
| 98 |
public void writeCOLORREF(Color c) throws IOException { |
| 99 |
writeByte(c.getRed()); |
| 100 |
writeByte(c.getGreen()); |
| 101 |
writeByte(c.getBlue()); |
| 102 |
// NOTE: if not 0x00 EMF does not display correctly in full screen mode. |
| 103 |
writeByte(0x00); |
| 104 |
} |
| 105 |
|
| 106 |
// COLOR16 |
| 107 |
public void writeCOLOR16(Color c) throws IOException { |
| 108 |
writeShort(c.getRed() << 8); |
| 109 |
writeShort(c.getGreen() << 8); |
| 110 |
writeShort(c.getBlue() << 8); |
| 111 |
writeShort(c.getAlpha() << 8); |
| 112 |
} |
| 113 |
|
| 114 |
public void writeXFORM(AffineTransform t) throws IOException { |
| 115 |
writeFLOAT((float) t.getScaleX()); |
| 116 |
writeFLOAT((float) t.getShearY()); |
| 117 |
writeFLOAT((float) t.getShearX()); |
| 118 |
writeFLOAT((float) t.getScaleY()); |
| 119 |
writeFLOAT((float) t.getTranslateX()); |
| 120 |
writeFLOAT((float) t.getTranslateY()); |
| 121 |
} |
| 122 |
|
| 123 |
// POINTS [] |
| 124 |
public void writePOINTS(Point[] p) throws IOException { |
| 125 |
writePOINTS(p.length, p); |
| 126 |
} |
| 127 |
|
| 128 |
public void writePOINTS(int n, Point[] p) throws IOException { |
| 129 |
for (int i = 0; i < n; i++) { |
| 130 |
writePOINTS(p[i]); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public void writePOINTS(Point p) throws IOException { |
| 135 |
writeSHORT((short) p.x); |
| 136 |
writeSHORT((short) p.y); |
| 137 |
} |
| 138 |
|
| 139 |
// POINTL [] |
| 140 |
public void writePOINTL(Point[] p) throws IOException { |
| 141 |
writePOINTL(p.length, p); |
| 142 |
} |
| 143 |
|
| 144 |
public void writePOINTL(int n, Point[] p) throws IOException { |
| 145 |
for (int i = 0; i < n; i++) { |
| 146 |
writePOINTL(p[i]); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// POINTL |
| 151 |
public void writePOINTL(Point p) throws IOException { |
| 152 |
writeLONG(p.x); |
| 153 |
writeLONG(p.y); |
| 154 |
} |
| 155 |
|
| 156 |
// RECTL |
| 157 |
public void writeRECTL(Rectangle r) throws IOException { |
| 158 |
writeLONG(r.x); |
| 159 |
writeLONG(r.y); |
| 160 |
writeLONG(r.x + r.width); |
| 161 |
writeLONG(r.y + r.height); |
| 162 |
} |
| 163 |
|
| 164 |
// SIZEL |
| 165 |
public void writeSIZEL(Dimension d) throws IOException { |
| 166 |
writeLONG(d.width); |
| 167 |
writeLONG(d.height); |
| 168 |
} |
| 169 |
|
| 170 |
// UINT |
| 171 |
public void writeUINT(int i) throws IOException { |
| 172 |
writeUnsignedInt(i); |
| 173 |
} |
| 174 |
|
| 175 |
// ULONG |
| 176 |
public void writeULONG(int i) throws IOException { |
| 177 |
writeUnsignedInt(i); |
| 178 |
} |
| 179 |
|
| 180 |
// LONG |
| 181 |
public void writeLONG(int i) throws IOException { |
| 182 |
writeInt(i); |
| 183 |
} |
| 184 |
|
| 185 |
public void writeSHORT(short i) throws IOException { |
| 186 |
writeShort(i); |
| 187 |
} |
| 188 |
|
| 189 |
// BYTE [] |
| 190 |
public void writeBYTE(byte[] b) throws IOException { |
| 191 |
writeByte(b); |
| 192 |
} |
| 193 |
|
| 194 |
// BYTE |
| 195 |
public void writeBYTE(byte b) throws IOException { |
| 196 |
writeByte(b); |
| 197 |
} |
| 198 |
|
| 199 |
// BYTE |
| 200 |
public void writeBYTE(int b) throws IOException { |
| 201 |
writeByte(b); |
| 202 |
} |
| 203 |
|
| 204 |
public void writeBYTE(boolean b) throws IOException { |
| 205 |
writeBYTE((b) ? 1 : 0); |
| 206 |
} |
| 207 |
|
| 208 |
public void writeWORD(boolean b) throws IOException { |
| 209 |
writeWORD((b) ? 1 : 0); |
| 210 |
} |
| 211 |
|
| 212 |
public void writeDWORD(boolean b) throws IOException { |
| 213 |
writeDWORD((b) ? 1 : 0); |
| 214 |
} |
| 215 |
|
| 216 |
public void writeWCHAR(String s) throws IOException { |
| 217 |
writeByte(s.getBytes("UTF-16LE")); |
| 218 |
} |
| 219 |
|
| 220 |
public void writeWCHAR(String s, int size) throws IOException { |
| 221 |
writeWCHAR(s); |
| 222 |
for (int i = size - s.length(); i > 0; i--) { |
| 223 |
writeWORD(0); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
protected int getTagAlignment() { |
| 228 |
return 4; |
| 229 |
} |
| 230 |
|
| 231 |
protected void writeTagHeader(TagHeader header) throws IOException { |
| 232 |
|
| 233 |
int tagID = header.getTag(); |
| 234 |
long length = header.getLength(); |
| 235 |
writeUnsignedInt(tagID); |
| 236 |
writeUnsignedInt(length + 8); |
| 237 |
} |
| 238 |
|
| 239 |
public void writeTag(Tag tag) throws IOException { |
| 240 |
|
| 241 |
recordCount++; |
| 242 |
super.writeTag(tag); |
| 243 |
} |
| 244 |
|
| 245 |
protected void writeActionHeader(ActionHeader header) throws IOException { |
| 246 |
// empty |
| 247 |
} |
| 248 |
|
| 249 |
public void writeHeader(EMFHeader header) throws IOException { |
| 250 |
header.write(this); |
| 251 |
} |
| 252 |
|
| 253 |
public int getVersion() { |
| 254 |
return version; |
| 255 |
} |
| 256 |
} |