| 1 |
|
| 2 |
package org.freehep.graphicsio.cgm; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.geom.Point2D; |
| 6 |
import java.io.FilterWriter; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.Writer; |
| 9 |
|
| 10 |
import org.freehep.util.io.IndentPrintWriter; |
| 11 |
import org.freehep.util.io.Tag; |
| 12 |
import org.freehep.util.io.TaggedOutput; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class CGMWriter extends FilterWriter implements TaggedOutput { |
| 24 |
|
| 25 |
private int version; |
| 26 |
|
| 27 |
private boolean direct = true; |
| 28 |
|
| 29 |
private int colorIndexPrecision = 8; |
| 30 |
|
| 31 |
private int directColorPrecision = 8; |
| 32 |
|
| 33 |
private int integerPrecision = 16; |
| 34 |
|
| 35 |
private boolean fixedPrecision = true; |
| 36 |
|
| 37 |
private boolean doublePrecision = false; |
| 38 |
|
| 39 |
private boolean vdcReal = false; |
| 40 |
|
| 41 |
private boolean vdcFixedPrecision = true; |
| 42 |
|
| 43 |
private boolean vdcDoublePrecision = false; |
| 44 |
|
| 45 |
private int vdcIntegerPrecision = 16; |
| 46 |
|
| 47 |
private int namePrecision = 16; |
| 48 |
|
| 49 |
private int lineWidthSpecificationMode = LineWidthSpecificationMode.ABSOLUTE; |
| 50 |
|
| 51 |
private int markerSizeSpecificationMode = MarkerSizeSpecificationMode.ABSOLUTE; |
| 52 |
|
| 53 |
private int edgeWidthSpecificationMode = EdgeWidthSpecificationMode.ABSOLUTE; |
| 54 |
|
| 55 |
private int interiorStyleSpecificationMode = InteriorStyleSpecificationMode.ABSOLUTE; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public CGMWriter(Writer writer, int version) throws IOException { |
| 64 |
|
| 65 |
super(new IndentPrintWriter(writer)); |
| 66 |
this.version = version; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public CGMWriter(Writer writer) throws IOException { |
| 75 |
|
| 76 |
this(writer, 1); |
| 77 |
} |
| 78 |
|
| 79 |
void setColorMode(boolean direct) { |
| 80 |
this.direct = direct; |
| 81 |
} |
| 82 |
|
| 83 |
void setColorIndexPrecision(int precision) { |
| 84 |
colorIndexPrecision = precision; |
| 85 |
} |
| 86 |
|
| 87 |
void setDirectColorPrecision(int precision) { |
| 88 |
directColorPrecision = precision; |
| 89 |
} |
| 90 |
|
| 91 |
void setIntegerPrecision(int precision) { |
| 92 |
integerPrecision = precision; |
| 93 |
} |
| 94 |
|
| 95 |
void setRealPrecision(boolean fixedPrecision, boolean doublePrecision) { |
| 96 |
this.fixedPrecision = fixedPrecision; |
| 97 |
this.doublePrecision = doublePrecision; |
| 98 |
} |
| 99 |
|
| 100 |
void setVDCReal(boolean real) { |
| 101 |
vdcReal = real; |
| 102 |
} |
| 103 |
|
| 104 |
void setVDCIntegerPrecision(int precision) { |
| 105 |
vdcIntegerPrecision = precision; |
| 106 |
} |
| 107 |
|
| 108 |
void setVDCRealPrecision(boolean fixedPrecision, boolean doublePrecision) { |
| 109 |
this.vdcFixedPrecision = fixedPrecision; |
| 110 |
this.vdcDoublePrecision = doublePrecision; |
| 111 |
} |
| 112 |
|
| 113 |
void setNamePrecision(int precision) { |
| 114 |
namePrecision = precision; |
| 115 |
} |
| 116 |
|
| 117 |
void setLineWidthSpecificationMode(int mode) { |
| 118 |
lineWidthSpecificationMode = mode; |
| 119 |
} |
| 120 |
|
| 121 |
int getLineWidthSpecificationMode() { |
| 122 |
return lineWidthSpecificationMode; |
| 123 |
} |
| 124 |
|
| 125 |
void setMarkerSizeSpecificationMode(int mode) { |
| 126 |
markerSizeSpecificationMode = mode; |
| 127 |
} |
| 128 |
|
| 129 |
int getMarkerSizeSpecificationMode() { |
| 130 |
return markerSizeSpecificationMode; |
| 131 |
} |
| 132 |
|
| 133 |
void setEdgeWidthSpecificationMode(int mode) { |
| 134 |
edgeWidthSpecificationMode = mode; |
| 135 |
} |
| 136 |
|
| 137 |
int getEdgeWidthSpecificationMode() { |
| 138 |
return edgeWidthSpecificationMode; |
| 139 |
} |
| 140 |
|
| 141 |
void setInteriorStyleSpecificationMode(int mode) { |
| 142 |
interiorStyleSpecificationMode = mode; |
| 143 |
} |
| 144 |
|
| 145 |
int getInteriorStyleSpecificationMode() { |
| 146 |
return interiorStyleSpecificationMode; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void writeFixedPoint(double d) throws IOException { |
| 155 |
boolean negative = d < 0; |
| 156 |
d = Math.abs(d); |
| 157 |
int whole = (int) Math.floor(d); |
| 158 |
int frac = (int) Math.floor((d - whole) * 10000); |
| 159 |
if (negative) |
| 160 |
print("-"); |
| 161 |
print(whole); |
| 162 |
print("."); |
| 163 |
print(frac); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void writeFloatingPoint(double d) throws IOException { |
| 172 |
if (doublePrecision) { |
| 173 |
|
| 174 |
print(d); |
| 175 |
} else { |
| 176 |
|
| 177 |
print((float) d); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void writeColorIndex(int i) throws IOException { |
| 187 |
switch (colorIndexPrecision) { |
| 188 |
case 8: |
| 189 |
print(Math.max(0, Math.min(255, i))); |
| 190 |
break; |
| 191 |
case 16: |
| 192 |
print(Math.max(0, Math.min(65535, i))); |
| 193 |
break; |
| 194 |
default: |
| 195 |
case 24: |
| 196 |
case 32: |
| 197 |
print(i); |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void writeColorComponent(int c) throws IOException { |
| 208 |
switch (directColorPrecision) { |
| 209 |
default: |
| 210 |
case 8: |
| 211 |
print(Math.max(0, Math.min(255, c))); |
| 212 |
break; |
| 213 |
case 16: |
| 214 |
print(Math.max(0, Math.min(65535, c))); |
| 215 |
break; |
| 216 |
case 24: |
| 217 |
case 32: |
| 218 |
print(c); |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public void writeColorDirect(Color c) throws IOException { |
| 229 |
writeColorComponent(c.getRed()); |
| 230 |
print(", "); |
| 231 |
writeColorComponent(c.getGreen()); |
| 232 |
print(", "); |
| 233 |
writeColorComponent(c.getBlue()); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public void writeInteger(int i) throws IOException { |
| 242 |
switch (integerPrecision) { |
| 243 |
case 8: |
| 244 |
print((byte) i); |
| 245 |
break; |
| 246 |
default: |
| 247 |
case 16: |
| 248 |
print((short) i); |
| 249 |
break; |
| 250 |
case 24: |
| 251 |
case 32: |
| 252 |
print(i); |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public void writeReal(double r) throws IOException { |
| 263 |
if (fixedPrecision) { |
| 264 |
writeFixedPoint(r); |
| 265 |
} else { |
| 266 |
writeFloatingPoint(r); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public void writeString(String s) throws IOException { |
| 276 |
StringBuffer b = new StringBuffer(); |
| 277 |
b.append('"'); |
| 278 |
for (int i = 0; i < s.length(); i++) { |
| 279 |
b.append(s.charAt(i)); |
| 280 |
if (s.charAt(i) == '"') { |
| 281 |
b.append('"'); |
| 282 |
} |
| 283 |
} |
| 284 |
b.append('"'); |
| 285 |
print(b.toString()); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public void writeData(byte[] data) throws IOException { |
| 294 |
|
| 295 |
writeString(new String(data)); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public void writeVDC(double d) throws IOException { |
| 304 |
if (vdcReal) { |
| 305 |
if (vdcFixedPrecision) { |
| 306 |
writeFixedPoint(d); |
| 307 |
} else { |
| 308 |
if (vdcDoublePrecision) { |
| 309 |
print(d); |
| 310 |
} else { |
| 311 |
print((float) d); |
| 312 |
} |
| 313 |
} |
| 314 |
} else { |
| 315 |
switch (vdcIntegerPrecision) { |
| 316 |
default: |
| 317 |
case 16: |
| 318 |
print((short) d); |
| 319 |
break; |
| 320 |
case 24: |
| 321 |
case 32: |
| 322 |
print(d); |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public void writePoint(Point2D p) throws IOException { |
| 334 |
print("("); |
| 335 |
writeVDC(p.getX()); |
| 336 |
print(", "); |
| 337 |
writeVDC(p.getY()); |
| 338 |
print(")"); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public void writeColor(Color c) throws IOException { |
| 347 |
if (direct) { |
| 348 |
writeColorDirect(c); |
| 349 |
} else { |
| 350 |
|
| 351 |
writeColorIndex((c.getRed() << 16) + (c.getGreen() << 8) |
| 352 |
+ c.getBlue()); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public void writeName(int name) throws IOException { |
| 362 |
switch (namePrecision) { |
| 363 |
case 8: |
| 364 |
print(Math.max(0, Math.min(255, name))); |
| 365 |
break; |
| 366 |
default: |
| 367 |
case 16: |
| 368 |
print(Math.max(0, Math.min(65535, name))); |
| 369 |
break; |
| 370 |
case 24: |
| 371 |
case 32: |
| 372 |
print(name); |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public void writeTag(Tag tag) throws IOException { |
| 383 |
|
| 384 |
CGMTag cgm = (CGMTag) tag; |
| 385 |
cgm.write(cgm.getTag(), this); |
| 386 |
println(";"); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public int getVersion() { |
| 393 |
return version; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
public void indent() { |
| 398 |
((IndentPrintWriter)out).indent(); |
| 399 |
} |
| 400 |
|
| 401 |
public void outdent() { |
| 402 |
((IndentPrintWriter)out).outdent(); |
| 403 |
} |
| 404 |
|
| 405 |
public void print(byte b) { |
| 406 |
((IndentPrintWriter)out).print(b); |
| 407 |
} |
| 408 |
|
| 409 |
public void print(double d) { |
| 410 |
((IndentPrintWriter)out).print(d); |
| 411 |
} |
| 412 |
|
| 413 |
public void print(String s) { |
| 414 |
((IndentPrintWriter)out).print(s); |
| 415 |
} |
| 416 |
|
| 417 |
public void println(String s) { |
| 418 |
((IndentPrintWriter)out).println(s); |
| 419 |
} |
| 420 |
|
| 421 |
public void println() { |
| 422 |
((IndentPrintWriter)out).println(); |
| 423 |
} |
| 424 |
} |