Debian Codegraph @

freehep-graphicsio-cgm/src/main/java/org/freehep/graphicsio/cgm/CGMWriter.java

raw symbols SCIP
1 // Copyright 2001, FreeHEP.
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 * CGM Clear Text Writer. Tags written with this Writer will produce a clear
16 * text CGM file. The class also holds the state for the various precisions,
17 * defaults, etc...
18 *
19 * @author Mark Donszelmann
20 * @author Charles Loomis
21 * @version $Id: freehep-graphicsio-cgm/src/main/java/org/freehep/graphicsio/cgm/CGMWriter.java 83dd558e0284 2005/12/11 22:59:37 duns $
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 * Constructs a Clear Text CGM writer of a specified version.
59 *
60 * @param writer the underlying writer
61 * @param version the requested cgm version
62 */
63 public CGMWriter(Writer writer, int version) throws IOException {
64
65 super(new IndentPrintWriter(writer));
66 this.version = version;
67 }
68
69 /**
70 * Constructs a Clear Text CGM Writer of version 1.
71 *
72 * @param writer the underlying writer.
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 * Write a Fixed Point value (FX)
151 *
152 * @param d value to the written
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 * Writes a Floating Point value (FP)
168 *
169 * @param d value to be written
170 */
171 public void writeFloatingPoint(double d) throws IOException {
172 if (doublePrecision) {
173 // FIXME: ignored number of digits
174 print(d);
175 } else {
176 // FIXME: ignored number of digits
177 print((float) d);
178 }
179 }
180
181 /**
182 * Writes a Color Index (CI)
183 *
184 * @param i index to be written
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: // FIXME
196 case 32:
197 print(i);
198 break;
199 }
200 }
201
202 /**
203 * Writes a Color Component (CCO)
204 *
205 * @param c color component to be written
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: // FIXME
217 case 32:
218 print(c);
219 break;
220 }
221 }
222
223 /**
224 * Writes a Color Direct (CD)
225 *
226 * @param c color to be written
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 * Writes an Integer (I)
238 *
239 * @param i integer to be written
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: // FIXME
251 case 32:
252 print(i);
253 break;
254 }
255 }
256
257 /**
258 * Writes a Real (R)
259 *
260 * @param r value to be written
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 * Writes a String or String Fixed (S, SF)
272 *
273 * @param s string to be written
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 * Writes a chunk of Data (D)
290 *
291 * @param data data to be written
292 */
293 public void writeData(byte[] data) throws IOException {
294 // FIXME: good unless strings will do encoding
295 writeString(new String(data));
296 }
297
298 /**
299 * Writes a Virtual Display Coordinate (VDC)
300 *
301 * @param d VDC to be written
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: // FIXME
321 case 32:
322 print(d);
323 break;
324 }
325 }
326 }
327
328 /**
329 * Writes a Point (P)
330 *
331 * @param p value to be written
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 * Writes a Color (CO)
343 *
344 * @param c color to be written
345 */
346 public void writeColor(Color c) throws IOException {
347 if (direct) {
348 writeColorDirect(c);
349 } else {
350 // FIXME: we should look up the color
351 writeColorIndex((c.getRed() << 16) + (c.getGreen() << 8)
352 + c.getBlue());
353 }
354 }
355
356 /**
357 * Writes a Name (N)
358 *
359 * @param name name to be written
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: // FIXME
371 case 32:
372 print(name);
373 break;
374 }
375 }
376
377 /**
378 * Writes a Tag and a Terminator (;)
379 *
380 * @param tag tag to be written
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 * @return the version of this CGM Writer
391 */
392 public int getVersion() {
393 return version;
394 }
395
396 // Delegations to IndentPrintWriter to avoid "strange" compilation error in jdk 1.5
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 }