001: package com.lowagie.text.rtf.graphic;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.OutputStream;
006: import java.util.HashMap;
007: import java.util.Iterator;
008:
009: import com.lowagie.text.rtf.RtfAddableElement;
010:
011: /**
012: * The RtfShape provides the interface for adding shapes to
013: * the RTF document. This will only work for Word 97+, older
014: * Word versions are not supported by this class.<br /><br />
015: *
016: * Only very simple shapes are directly supported by the RtfShape.
017: * For more complex shapes you will have to read the RTF
018: * specification (iText follows the 1.6 specification) and add
019: * the desired properties via the RtfShapeProperty.<br /><br />
020: *
021: * One thing to keep in mind is that distances are not expressed
022: * in the standard iText point, but in EMU where 1 inch = 914400 EMU
023: * or 1 cm = 360000 EMU.
024: *
025: * @version $Id: RtfShape.java 2844 2007-06-19 11:41:07Z psoares33 $
026: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
027: * @author Thomas Bickel (tmb99@inode.at)
028: */
029: public class RtfShape extends RtfAddableElement {
030: /**
031: * Constant for a Picture Frame.
032: */
033: public static final int SHAPE_PICTURE_FRAME = 75;
034: /**
035: * Constant for a free form shape. The shape verticies must
036: * be specified with an array of Point objects in a
037: * RtfShapeProperty with the name PROPERTY_VERTICIES.
038: */
039: public static final int SHAPE_FREEFORM = 0;
040: /**
041: * Constant for a rectangle.
042: */
043: public static final int SHAPE_RECTANGLE = 1;
044: /**
045: * Constant for a rounded rectangle. The roundness is
046: * set via a RtfShapeProperty with the name PROPERTY_ADJUST_VALUE.
047: */
048: public static final int SHAPE_ROUND_RECTANGLE = 2;
049: /**
050: * Constant for an ellipse. Use this to create circles.
051: */
052: public static final int SHAPE_ELLIPSE = 3;
053: /**
054: * Constant for a diamond.
055: */
056: public static final int SHAPE_DIAMOND = 4;
057: /**
058: * Constant for a isoscelle triangle.
059: */
060: public static final int SHAPE_TRIANGLE_ISOSCELES = 5;
061: /**
062: * Constant for a right triangle.
063: */
064: public static final int SHAPE_TRIANGLE_RIGHT = 6;
065: /**
066: * Constant for a parallelogram.
067: */
068: public static final int SHAPE_PARALLELOGRAM = 7;
069: /**
070: * Constant for a trapezoid.
071: */
072: public static final int SHAPE_TRAPEZOID = 8;
073: /**
074: * Constant for a hexagon.
075: */
076: public static final int SHAPE_HEXAGON = 9;
077: /**
078: * Constant for an ocatagon.
079: */
080: public static final int SHAPE_OCTAGON = 10;
081: /**
082: * Constant for a star.
083: */
084: public static final int SHAPE_STAR = 12;
085: /**
086: * Constant for an arrow.
087: */
088: public static final int SHAPE_ARROW = 13;
089: /**
090: * Constant for a thick arrow.
091: */
092: public static final int SHAPE_ARROR_THICK = 14;
093: /**
094: * Constant for a home plate style shape.
095: */
096: public static final int SHAPE_HOME_PLATE = 15;
097: /**
098: * Constant for a cube shape.
099: */
100: public static final int SHAPE_CUBE = 16;
101: /**
102: * Constant for a balloon shape.
103: */
104: public static final int SHAPE_BALLOON = 17;
105: /**
106: * Constant for a seal shape.
107: */
108: public static final int SHAPE_SEAL = 18;
109: /**
110: * Constant for an arc shape.
111: */
112: public static final int SHAPE_ARC = 19;
113: /**
114: * Constant for a line shape.
115: */
116: public static final int SHAPE_LINE = 20;
117: /**
118: * Constant for a can shape.
119: */
120: public static final int SHAPE_CAN = 22;
121: /**
122: * Constant for a donut shape.
123: */
124: public static final int SHAPE_DONUT = 23;
125:
126: /**
127: * Text is not wrapped around the shape.
128: */
129: public static final int SHAPE_WRAP_NONE = 0;
130: /**
131: * Text is wrapped to the top and bottom.
132: */
133: public static final int SHAPE_WRAP_TOP_BOTTOM = 1;
134: /**
135: * Text is wrapped on the left and right side.
136: */
137: public static final int SHAPE_WRAP_BOTH = 2;
138: /**
139: * Text is wrapped on the left side.
140: */
141: public static final int SHAPE_WRAP_LEFT = 3;
142: /**
143: * Text is wrapped on the right side.
144: */
145: public static final int SHAPE_WRAP_RIGHT = 4;
146: /**
147: * Text is wrapped on the largest side.
148: */
149: public static final int SHAPE_WRAP_LARGEST = 5;
150: /**
151: * Text is tightly wrapped on the left and right side.
152: */
153: public static final int SHAPE_WRAP_TIGHT_BOTH = 6;
154: /**
155: * Text is tightly wrapped on the left side.
156: */
157: public static final int SHAPE_WRAP_TIGHT_LEFT = 7;
158: /**
159: * Text is tightly wrapped on the right side.
160: */
161: public static final int SHAPE_WRAP_TIGHT_RIGHT = 8;
162: /**
163: * Text is tightly wrapped on the largest side.
164: */
165: public static final int SHAPE_WRAP_TIGHT_LARGEST = 9;
166: /**
167: * Text is wrapped through the shape.
168: */
169: public static final int SHAPE_WRAP_THROUGH = 10;
170:
171: /**
172: * The shape nr is a random unique id.
173: */
174: private int shapeNr = 0;
175: /**
176: * The shape type.
177: */
178: private int type = 0;
179: /**
180: * The RtfShapePosition that defines position settings for this RtfShape.
181: */
182: private RtfShapePosition position = null;
183: /**
184: * A HashMap with RtfShapePropertys that define further shape properties.
185: */
186: private HashMap properties = null;
187: /**
188: * The wrapping mode. Defaults to SHAPE_WRAP_NONE;
189: */
190: private int wrapping = SHAPE_WRAP_NONE;
191: /**
192: * Text that is contained in the shape.
193: */
194: private String shapeText = "";
195:
196: /**
197: * Constructs a new RtfShape of a given shape at the given RtfShapePosition.
198: *
199: * @param type The type of shape to create.
200: * @param position The RtfShapePosition to create this RtfShape at.
201: */
202: public RtfShape(int type, RtfShapePosition position) {
203: this .type = type;
204: this .position = position;
205: this .properties = new HashMap();
206: }
207:
208: /**
209: * Sets a property.
210: *
211: * @param property The property to set for this RtfShape.
212: */
213: public void setProperty(RtfShapeProperty property) {
214: this .properties.put(property.getName(), property);
215: }
216:
217: /**
218: * Sets the text to display in this RtfShape.
219: *
220: * @param shapeText The text to display.
221: */
222: public void setShapeText(String shapeText) {
223: this .shapeText = shapeText;
224: }
225:
226: /**
227: * Set the wrapping mode.
228: *
229: * @param wrapping The wrapping mode to use for this RtfShape.
230: */
231: public void setWrapping(int wrapping) {
232: this .wrapping = wrapping;
233: }
234:
235: /**
236: * Writes the RtfShape. Some settings are automatically translated into
237: * or require other properties and these are set first.
238: *
239: * @deprecated replaced by {@link #writeContent(OutputStream)}
240: */
241: public byte[] write() {
242:
243: ByteArrayOutputStream result = new ByteArrayOutputStream();
244: try {
245: writeContent(result);
246: } catch (IOException ioe) {
247: ioe.printStackTrace();
248: }
249: return result.toByteArray();
250: }
251:
252: /**
253: * Writes the RtfShape. Some settings are automatically translated into
254: * or require other properties and these are set first.
255: */
256: public void writeContent(final OutputStream result)
257: throws IOException {
258: this .shapeNr = this .doc.getRandomInt();
259:
260: this .properties.put("ShapeType", new RtfShapeProperty(
261: "ShapeType", this .type));
262: if (this .position.isShapeBelowText()) {
263: this .properties.put("fBehindDocument",
264: new RtfShapeProperty("fBehindDocument", true));
265: }
266: if (this .inTable) {
267: this .properties.put("fLayoutInCell", new RtfShapeProperty(
268: "fLayoutInCell", true));
269: }
270: if (this .properties.containsKey("posh")) {
271: this .position.setIgnoreXRelative(true);
272: }
273: if (this .properties.containsKey("posv")) {
274: this .position.setIgnoreYRelative(true);
275: }
276:
277: result.write(OPEN_GROUP);
278: result.write("\\shp".getBytes());
279: result.write("\\shplid".getBytes());
280: result.write(intToByteArray(this .shapeNr));
281: //.result.write(this.position.write());
282: this .position.writeContent(result);
283: switch (this .wrapping) {
284: case SHAPE_WRAP_NONE:
285: result.write("\\shpwr3".getBytes());
286: break;
287: case SHAPE_WRAP_TOP_BOTTOM:
288: result.write("\\shpwr1".getBytes());
289: break;
290: case SHAPE_WRAP_BOTH:
291: result.write("\\shpwr2".getBytes());
292: result.write("\\shpwrk0".getBytes());
293: break;
294: case SHAPE_WRAP_LEFT:
295: result.write("\\shpwr2".getBytes());
296: result.write("\\shpwrk1".getBytes());
297: break;
298: case SHAPE_WRAP_RIGHT:
299: result.write("\\shpwr2".getBytes());
300: result.write("\\shpwrk2".getBytes());
301: break;
302: case SHAPE_WRAP_LARGEST:
303: result.write("\\shpwr2".getBytes());
304: result.write("\\shpwrk3".getBytes());
305: break;
306: case SHAPE_WRAP_TIGHT_BOTH:
307: result.write("\\shpwr4".getBytes());
308: result.write("\\shpwrk0".getBytes());
309: break;
310: case SHAPE_WRAP_TIGHT_LEFT:
311: result.write("\\shpwr4".getBytes());
312: result.write("\\shpwrk1".getBytes());
313: break;
314: case SHAPE_WRAP_TIGHT_RIGHT:
315: result.write("\\shpwr4".getBytes());
316: result.write("\\shpwrk2".getBytes());
317: break;
318: case SHAPE_WRAP_TIGHT_LARGEST:
319: result.write("\\shpwr4".getBytes());
320: result.write("\\shpwrk3".getBytes());
321: break;
322: case SHAPE_WRAP_THROUGH:
323: result.write("\\shpwr5".getBytes());
324: break;
325: default:
326: result.write("\\shpwr3".getBytes());
327: }
328: if (this .inHeader) {
329: result.write("\\shpfhdr1".getBytes());
330: }
331: if (this .doc.getDocumentSettings().isOutputDebugLineBreaks()) {
332: result.write('\n');
333: }
334: result.write(OPEN_GROUP);
335: result.write("\\*\\shpinst".getBytes());
336: Iterator it = this .properties.values().iterator();
337: while (it.hasNext()) {
338: RtfShapeProperty rsp = (RtfShapeProperty) it.next();
339: //.result.write(rsp.write());
340: rsp.writeContent(result);
341: }
342: if (!this .shapeText.equals("")) {
343: result.write(OPEN_GROUP);
344: result.write("\\shptxt".getBytes());
345: result.write(DELIMITER);
346: result.write(this .shapeText.getBytes());
347: result.write(CLOSE_GROUP);
348: }
349: result.write(CLOSE_GROUP);
350: if (this .doc.getDocumentSettings().isOutputDebugLineBreaks()) {
351: result.write('\n');
352: }
353: result.write(CLOSE_GROUP);
354:
355: }
356:
357: }
|