1 package com.lowagie.text.rtf.graphic; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.io.OutputStream ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 9 import com.lowagie.text.rtf.RtfAddableElement; 10 11 29 public class RtfShape extends RtfAddableElement { 30 35 public static final int SHAPE_FREEFORM = 0; 36 39 public static final int SHAPE_RECTANGLE = 1; 40 44 public static final int SHAPE_ROUND_RECTANGLE = 2; 45 48 public static final int SHAPE_ELLIPSE = 3; 49 52 public static final int SHAPE_DIAMOND = 4; 53 56 public static final int SHAPE_TRIANGLE_ISOSCELES = 5; 57 60 public static final int SHAPE_TRIANGLE_RIGHT = 6; 61 64 public static final int SHAPE_PARALLELOGRAM = 7; 65 68 public static final int SHAPE_TRAPEZOID = 8; 69 72 public static final int SHAPE_HEXAGON = 9; 73 76 public static final int SHAPE_OCTAGON = 10; 77 80 public static final int SHAPE_STAR = 12; 81 84 public static final int SHAPE_ARROW = 13; 85 88 public static final int SHAPE_ARROR_THICK = 14; 89 92 public static final int SHAPE_HOME_PLATE = 15; 93 96 public static final int SHAPE_CUBE = 16; 97 100 public static final int SHAPE_BALLOON = 17; 101 104 public static final int SHAPE_SEAL = 18; 105 108 public static final int SHAPE_ARC = 19; 109 112 public static final int SHAPE_LINE = 20; 113 116 public static final int SHAPE_CAN = 22; 117 120 public static final int SHAPE_DONUT = 23; 121 122 125 public static final int SHAPE_WRAP_NONE = 0; 126 129 public static final int SHAPE_WRAP_TOP_BOTTOM = 1; 130 133 public static final int SHAPE_WRAP_BOTH = 2; 134 137 public static final int SHAPE_WRAP_LEFT = 3; 138 141 public static final int SHAPE_WRAP_RIGHT = 4; 142 145 public static final int SHAPE_WRAP_LARGEST = 5; 146 149 public static final int SHAPE_WRAP_TIGHT_BOTH = 6; 150 153 public static final int SHAPE_WRAP_TIGHT_LEFT = 7; 154 157 public static final int SHAPE_WRAP_TIGHT_RIGHT = 8; 158 161 public static final int SHAPE_WRAP_TIGHT_LARGEST = 9; 162 165 public static final int SHAPE_WRAP_THROUGH = 10; 166 167 170 private int shapeNr = 0; 171 174 private int type = 0; 175 178 private RtfShapePosition position = null; 179 182 private HashMap properties = null; 183 186 private int wrapping = SHAPE_WRAP_NONE; 187 190 private String shapeText = ""; 191 192 198 public RtfShape(int type, RtfShapePosition position) { 199 this.type = type; 200 this.position = position; 201 this.properties = new HashMap (); 202 } 203 204 209 public void setProperty(RtfShapeProperty property) { 210 this.properties.put(property.getName(), property); 211 } 212 213 218 public void setShapeText(String shapeText) { 219 this.shapeText = shapeText; 220 } 221 222 227 public void setWrapping(int wrapping) { 228 this.wrapping = wrapping; 229 } 230 231 237 public byte[] write() { 238 239 ByteArrayOutputStream result = new ByteArrayOutputStream (); 240 try { 241 writeContent(result); 242 } catch(IOException ioe) { 243 ioe.printStackTrace(); 244 } 245 return result.toByteArray(); 246 } 247 248 252 public void writeContent(final OutputStream result) throws IOException 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 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 it = this.properties.values().iterator(); 330 while(it.hasNext()) { 331 RtfShapeProperty rsp = (RtfShapeProperty) it.next(); 332 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 |