KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > graphic > RtfShape


1 package com.lowagie.text.rtf.graphic;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import com.lowagie.text.rtf.RtfAddableElement;
10
11 /**
12  * The RtfShape provides the interface for adding shapes to
13  * the RTF document. This will only work for Word 97+, older
14  * Word versions are not supported by this class.<br /><br />
15  *
16  * Only very simple shapes are directly supported by the RtfShape.
17  * For more complex shapes you will have to read the RTF
18  * specification (iText follows the 1.6 specification) and add
19  * the desired properties via the RtfShapeProperty.<br /><br />
20  *
21  * One thing to keep in mind is that distances are not expressed
22  * in the standard iText point, but in EMU where 1 inch = 914400 EMU
23  * or 1 cm = 360000 EMU.
24  *
25  * @version $Id: RtfShape.java 2776 2007-05-23 20:01:40Z hallm $
26  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
27  * @author Thomas Bickel (tmb99@inode.at)
28  */

29 public class RtfShape extends RtfAddableElement {
30     /**
31      * Constant for a free form shape. The shape verticies must
32      * be specified with an array of Point objects in a
33      * RtfShapeProperty with the name PROPERTY_VERTICIES.
34      */

35     public static final int SHAPE_FREEFORM = 0;
36     /**
37      * Constant for a rectangle.
38      */

39     public static final int SHAPE_RECTANGLE = 1;
40     /**
41      * Constant for a rounded rectangle. The roundness is
42      * set via a RtfShapeProperty with the name PROPERTY_ADJUST_VALUE.
43      */

44     public static final int SHAPE_ROUND_RECTANGLE = 2;
45     /**
46      * Constant for an ellipse. Use this to create circles.
47      */

48     public static final int SHAPE_ELLIPSE = 3;
49     /**
50      * Constant for a diamond.
51      */

52     public static final int SHAPE_DIAMOND = 4;
53     /**
54      * Constant for a isoscelle triangle.
55      */

56     public static final int SHAPE_TRIANGLE_ISOSCELES = 5;
57     /**
58      * Constant for a right triangle.
59      */

60     public static final int SHAPE_TRIANGLE_RIGHT = 6;
61     /**
62      * Constant for a parallelogram.
63      */

64     public static final int SHAPE_PARALLELOGRAM = 7;
65     /**
66      * Constant for a trapezoid.
67      */

68     public static final int SHAPE_TRAPEZOID = 8;
69     /**
70      * Constant for a hexagon.
71      */

72     public static final int SHAPE_HEXAGON = 9;
73     /**
74      * Constant for an ocatagon.
75      */

76     public static final int SHAPE_OCTAGON = 10;
77     /**
78      * Constant for a star.
79      */

80     public static final int SHAPE_STAR = 12;
81     /**
82      * Constant for an arrow.
83      */

84     public static final int SHAPE_ARROW = 13;
85     /**
86      * Constant for a thick arrow.
87      */

88     public static final int SHAPE_ARROR_THICK = 14;
89     /**
90      * Constant for a home plate style shape.
91      */

92     public static final int SHAPE_HOME_PLATE = 15;
93     /**
94      * Constant for a cube shape.
95      */

96     public static final int SHAPE_CUBE = 16;
97     /**
98      * Constant for a balloon shape.
99      */

100     public static final int SHAPE_BALLOON = 17;
101     /**
102      * Constant for a seal shape.
103      */

104     public static final int SHAPE_SEAL = 18;
105     /**
106      * Constant for an arc shape.
107      */

108     public static final int SHAPE_ARC = 19;
109     /**
110      * Constant for a line shape.
111      */

112     public static final int SHAPE_LINE = 20;
113     /**
114      * Constant for a can shape.
115      */

116     public static final int SHAPE_CAN = 22;
117     /**
118      * Constant for a donut shape.
119      */

120     public static final int SHAPE_DONUT = 23;
121     
122     /**
123      * Text is not wrapped around the shape.
124      */

125     public static final int SHAPE_WRAP_NONE = 0;
126     /**
127      * Text is wrapped to the top and bottom.
128      */

129     public static final int SHAPE_WRAP_TOP_BOTTOM = 1;
130     /**
131      * Text is wrapped on the left and right side.
132      */

133     public static final int SHAPE_WRAP_BOTH = 2;
134     /**
135      * Text is wrapped on the left side.
136      */

137     public static final int SHAPE_WRAP_LEFT = 3;
138     /**
139      * Text is wrapped on the right side.
140      */

141     public static final int SHAPE_WRAP_RIGHT = 4;
142     /**
143      * Text is wrapped on the largest side.
144      */

145     public static final int SHAPE_WRAP_LARGEST = 5;
146     /**
147      * Text is tightly wrapped on the left and right side.
148      */

149     public static final int SHAPE_WRAP_TIGHT_BOTH = 6;
150     /**
151      * Text is tightly wrapped on the left side.
152      */

153     public static final int SHAPE_WRAP_TIGHT_LEFT = 7;
154     /**
155      * Text is tightly wrapped on the right side.
156      */

157     public static final int SHAPE_WRAP_TIGHT_RIGHT = 8;
158     /**
159      * Text is tightly wrapped on the largest side.
160      */

161     public static final int SHAPE_WRAP_TIGHT_LARGEST = 9;
162     /**
163      * Text is wrapped through the shape.
164      */

165     public static final int SHAPE_WRAP_THROUGH = 10;
166     
167     /**
168      * The shape nr is a random unique id.
169      */

170     private int shapeNr = 0;
171     /**
172      * The shape type.
173      */

174     private int type = 0;
175     /**
176      * The RtfShapePosition that defines position settings for this RtfShape.
177      */

178     private RtfShapePosition position = null;
179     /**
180      * A HashMap with RtfShapePropertys that define further shape properties.
181      */

182     private HashMap JavaDoc properties = null;
183     /**
184      * The wrapping mode. Defaults to SHAPE_WRAP_NONE;
185      */

186     private int wrapping = SHAPE_WRAP_NONE;
187     /**
188      * Text that is contained in the shape.
189      */

190     private String JavaDoc shapeText = "";
191     
192     /**
193      * Constructs a new RtfShape of a given shape at the given RtfShapePosition.
194      *
195      * @param type The type of shape to create.
196      * @param position The RtfShapePosition to create this RtfShape at.
197      */

198     public RtfShape(int type, RtfShapePosition position) {
199         this.type = type;
200         this.position = position;
201         this.properties = new HashMap JavaDoc();
202     }
203
204     /**
205      * Sets a property.
206      *
207      * @param property The property to set for this RtfShape.
208      */

209     public void setProperty(RtfShapeProperty property) {
210         this.properties.put(property.getName(), property);
211     }
212     
213     /**
214      * Sets the text to display in this RtfShape.
215      *
216      * @param shapeText The text to display.
217      */

218     public void setShapeText(String JavaDoc shapeText) {
219         this.shapeText = shapeText;
220     }
221
222     /**
223      * Set the wrapping mode.
224      *
225      * @param wrapping The wrapping mode to use for this RtfShape.
226      */

227     public void setWrapping(int wrapping) {
228         this.wrapping = wrapping;
229     }
230
231     /**
232      * Writes the RtfShape. Some settings are automatically translated into
233      * or require other properties and these are set first.
234      *
235      * @deprecated replaced by {@link #writeContent(OutputStream)}
236      */

237     public byte[] write() {
238         
239         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
240         try {
241             writeContent(result);
242         } catch(IOException JavaDoc ioe) {
243             ioe.printStackTrace();
244         }
245         return result.toByteArray();
246     }
247     
248     /**
249      * Writes the RtfShape. Some settings are automatically translated into
250      * or require other properties and these are set first.
251      */

252     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
253     {
254         this.shapeNr = this.doc.getRandomInt();
255         
256         this.properties.put("ShapeType", new RtfShapeProperty("ShapeType", this.type));
257         if(this.position.isShapeBelowText()) {
258             this.properties.put("fBehindDocument", new RtfShapeProperty("fBehindDocument", true));
259         }
260         if(this.inTable) {
261             this.properties.put("fLayoutInCell", new RtfShapeProperty("fLayoutInCell", true));
262         }
263         if(this.properties.containsKey("posh")) {
264             this.position.setIgnoreXRelative(true);
265         }
266         if(this.properties.containsKey("posv")) {
267             this.position.setIgnoreYRelative(true);
268         }
269         
270         result.write(OPEN_GROUP);
271         result.write("\\shp".getBytes());
272         result.write("\\shplid".getBytes());
273         result.write(intToByteArray(this.shapeNr));
274         //.result.write(this.position.write());
275
this.position.writeContent(result);
276         switch(this.wrapping) {
277         case SHAPE_WRAP_NONE:
278             result.write("\\shpwr3".getBytes());
279             break;
280         case SHAPE_WRAP_TOP_BOTTOM:
281             result.write("\\shpwr1".getBytes());
282             break;
283         case SHAPE_WRAP_BOTH:
284             result.write("\\shpwr2".getBytes());
285             result.write("\\shpwrk0".getBytes());
286             break;
287         case SHAPE_WRAP_LEFT:
288             result.write("\\shpwr2".getBytes());
289             result.write("\\shpwrk1".getBytes());
290             break;
291         case SHAPE_WRAP_RIGHT:
292             result.write("\\shpwr2".getBytes());
293             result.write("\\shpwrk2".getBytes());
294             break;
295         case SHAPE_WRAP_LARGEST:
296             result.write("\\shpwr2".getBytes());
297             result.write("\\shpwrk3".getBytes());
298             break;
299         case SHAPE_WRAP_TIGHT_BOTH:
300             result.write("\\shpwr4".getBytes());
301             result.write("\\shpwrk0".getBytes());
302             break;
303         case SHAPE_WRAP_TIGHT_LEFT:
304             result.write("\\shpwr4".getBytes());
305             result.write("\\shpwrk1".getBytes());
306             break;
307         case SHAPE_WRAP_TIGHT_RIGHT:
308             result.write("\\shpwr4".getBytes());
309             result.write("\\shpwrk2".getBytes());
310             break;
311         case SHAPE_WRAP_TIGHT_LARGEST:
312             result.write("\\shpwr4".getBytes());
313             result.write("\\shpwrk3".getBytes());
314             break;
315         case SHAPE_WRAP_THROUGH:
316             result.write("\\shpwr5".getBytes());
317             break;
318         default:
319             result.write("\\shpwr3".getBytes());
320         }
321         if(this.inHeader) {
322             result.write("\\shpfhdr1".getBytes());
323         }
324         if(this.doc.getDocumentSettings().isOutputDebugLineBreaks()) {
325             result.write('\n');
326         }
327         result.write(OPEN_GROUP);
328         result.write("\\*\\shpinst".getBytes());
329         Iterator JavaDoc it = this.properties.values().iterator();
330         while(it.hasNext()) {
331             RtfShapeProperty rsp = (RtfShapeProperty) it.next();
332             //.result.write(rsp.write());
333
rsp.writeContent(result);
334         }
335         if(!this.shapeText.equals("")) {
336             result.write(OPEN_GROUP);
337             result.write("\\shptxt".getBytes());
338             result.write(DELIMITER);
339             result.write(this.shapeText.getBytes());
340             result.write(CLOSE_GROUP);
341         }
342         result.write(CLOSE_GROUP);
343         if(this.doc.getDocumentSettings().isOutputDebugLineBreaks()) {
344             result.write('\n');
345         }
346         result.write(CLOSE_GROUP);
347         
348     }
349     
350 }
351
Popular Tags