1
2 package org.freehep.graphicsio.emf.gdi;
3
4 import java.awt.Image;
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7
8 import javax.imageio.ImageIO;
9
10 import org.freehep.graphicsio.emf.EMFConstants;
11 import org.freehep.graphicsio.emf.EMFInputStream;
12 import org.freehep.graphicsio.emf.EMFOutputStream;
13 import org.freehep.graphicsio.emf.EMFRenderer;
14 import org.freehep.graphicsio.emf.EMFTag;
15
16
17
18
19
20
21
22 public class GDIComment extends EMFTag {
23
24 private int type;
25
26 private String comment = "";
27
28 private Image image;
29
30 public GDIComment() {
31 super(70, 1);
32 }
33
34 public GDIComment(String comment) {
35 this();
36 this.comment = comment;
37 }
38
39 public EMFTag read(int tagID, EMFInputStream emf, int len)
40 throws IOException {
41
42 GDIComment result = new GDIComment();
43
44 int l = emf.readDWORD();
45
46 result.type = emf.readDWORD();
47
48
49
50 if (result.type == 726027589) {
51
52 if (l % 4 != 0) {
53 emf.readBYTE(4 - l % 4);
54 }
55 } else if (result.type == EMFConstants.GDICOMMENT_BEGINGROUP) {
56
57
58
59
60
61
62
63
64 int nDescription = emf.readDWORD();
65
66
67 if (nDescription > 0) {
68 result.comment = new String(emf.readByte(nDescription));
69 }
70 } else if (result.type == EMFConstants.GDICOMMENT_ENDGROUP) {
71
72 } else if (result.type == EMFConstants.GDICOMMENT_MULTIFORMATS) {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 l = l - 4 - 8;
98
99 result.comment = new String(emf.readBYTE(l));
100
101 if (l % 4 != 0)
102 emf.readBYTE(4 - l % 4);
103
104 } else if (result.type == EMFConstants.GDICOMMENT_WINDOWS_METAFILE) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 int size = emf.readDWORD();
124
125
126 byte[] bytes = emf.readByte(size);
127 result.image = ImageIO.read(new ByteArrayInputStream(bytes));
128 return this;
129
130 } else {
131 l = l - 4;
132
133 if (l > 0) {
134 result.comment = new String(emf.readBYTE(l));
135
136 if (l % 4 != 0) {
137 emf.readBYTE(4 - l % 4);
138 }
139 } else {
140 comment = "";
141 }
142 }
143 return result;
144 }
145
146 public void write(int tagID, EMFOutputStream emf) throws IOException {
147 byte[] b = comment.getBytes();
148 emf.writeDWORD(b.length);
149 emf.writeBYTE(b);
150 if (b.length % 4 != 0)
151 for (int i = 0; i < 4 - b.length % 4; i++)
152 emf.writeBYTE(0);
153 }
154
155 public String toString() {
156 return super.toString() + "\n length: " + comment.length();
157 }
158
159
160
161
162
163
164 public void render(EMFRenderer renderer) {
165
166 }
167 }