View Javadoc

1   // Copyright 2001, FreeHEP.
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   * GDIComment TAG.
18   * 
19   * @author Mark Donszelmann
20   * @version $Id: GDIComment.java 12753 2007-06-12 22:32:31Z duns $
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          // not documented, but embedded GIF / PNG images
49          // start with that tag
50          if (result.type == 726027589) {
51              /*byte[] bytes = */ emf.readByte(l - 4);
52              if (l % 4 != 0) {
53                  emf.readBYTE(4 - l % 4);
54              }
55          } else if (result.type == EMFConstants.GDICOMMENT_BEGINGROUP) {
56              // This is the bounding rectangle for the
57              // object in logical coordinates.
58              /* Rectangle rclOutput = */ emf.readRECTL();
59  
60              // This is the number of characters in the
61              // optional Unicode description string that
62              // follows. This is zero if there is no
63              // description string.
64              int nDescription = emf.readDWORD();
65  
66              // read the description
67              if (nDescription > 0) {
68                  result.comment = new String(emf.readByte(nDescription));
69              }
70          } else if (result.type == EMFConstants.GDICOMMENT_ENDGROUP) {
71              // nothing to to
72          } else if (result.type == EMFConstants.GDICOMMENT_MULTIFORMATS) {
73              // This is the bounding rectangle for the
74              // picture in logical coordinates.
75              /* Rectangle rclOutput = */ emf.readRECTL();
76  
77              // This contains the number of formats in
78              // the comment.
79              /* nFormats = */ emf.readDWORD();
80              
81              // This is an array of EMRFORMAT structures
82              // in the order of preference.  The data
83              // for each format follows the last
84              // EMRFORMAT structure.
85  
86              // TODO read tagEMRFORMAT
87  
88              /*
89              typedef struct tagEMRFORMAT {
90                    DWORD   dSignature;
91                    DWORD   nVersion;
92                    DWORD   cbData;
93                    DWORD   offData;
94                  } EMRFORMAT;
95              */
96  
97              l = l - 4 - 8;
98  
99              result.comment = new String(emf.readBYTE(l));
100             // Align to 4-byte boundary
101             if (l % 4 != 0)
102                 emf.readBYTE(4 - l % 4);
103 
104         } else if (result.type == EMFConstants.GDICOMMENT_WINDOWS_METAFILE) {
105             // This contains the version number of the
106             // Windows-format metafile.
107             /*int version =*/ emf.readDWORD();
108 
109             // This is the additive DWORD checksum for
110             // the enhanced metafile.  The checksum
111             // for the enhanced metafile data including
112             // this comment record must be zero.
113             // Otherwise, the enhanced metafile has been
114             //  modified and the Windows-format
115             // metafile is no longer valid.
116             /*int checksum =*/ emf.readDWORD();
117 
118             // This must be zero.
119             /* int fFlags = */ emf.readDWORD();
120 
121             // This is the size, in bytes. of the
122             // Windows-format metafile data that follows.
123             int size = emf.readDWORD();
124 
125             // read the image data
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                 // Align to 4-byte boundary
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      * displays the tag using the renderer
161      *
162      * @param renderer EMFRenderer storing the drawing session data
163      */
164     public void render(EMFRenderer renderer) {
165         // do nothing
166     }
167 }