001: package com.lowagie.text.rtf.graphic;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.OutputStream;
006:
007: import com.lowagie.text.rtf.RtfAddableElement;
008:
009: /**
010: * The RtfShapePosition stores position and ordering
011: * information for one RtfShape.
012: *
013: * @version $Id: RtfShapePosition.java 2776 2007-05-23 20:01:40Z hallm $
014: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
015: * @author Thomas Bickel (tmb99@inode.at)
016: */
017: public class RtfShapePosition extends RtfAddableElement {
018: /**
019: * Constant for horizontal positioning relative to the page.
020: */
021: public static final int POSITION_X_RELATIVE_PAGE = 0;
022: /**
023: * Constant for horizontal positioning relative to the margin.
024: */
025: public static final int POSITION_X_RELATIVE_MARGIN = 1;
026: /**
027: * Constant for horizontal positioning relative to the column.
028: */
029: public static final int POSITION_X_RELATIVE_COLUMN = 2;
030: /**
031: * Constant for vertical positioning relative to the page.
032: */
033: public static final int POSITION_Y_RELATIVE_PAGE = 0;
034: /**
035: * Constant for vertical positioning relative to the margin.
036: */
037: public static final int POSITION_Y_RELATIVE_MARGIN = 1;
038: /**
039: * Constant for vertical positioning relative to the paragraph.
040: */
041: public static final int POSITION_Y_RELATIVE_PARAGRAPH = 2;
042:
043: /**
044: * The top coordinate of this RtfShapePosition.
045: */
046: private int top = 0;
047: /**
048: * The left coordinate of this RtfShapePosition.
049: */
050: private int left = 0;
051: /**
052: * The right coordinate of this RtfShapePosition.
053: */
054: private int right = 0;
055: /**
056: * The bottom coordinate of this RtfShapePosition.
057: */
058: private int bottom = 0;
059: /**
060: * The z order of this RtfShapePosition.
061: */
062: private int zOrder = 0;
063: /**
064: * The horizontal relative position.
065: */
066: private int xRelativePos = POSITION_X_RELATIVE_PAGE;
067: /**
068: * The vertical relative position.
069: */
070: private int yRelativePos = POSITION_Y_RELATIVE_PAGE;
071: /**
072: * Whether to ignore the horizontal relative position.
073: */
074: private boolean ignoreXRelative = false;
075: /**
076: * Whether to ignore the vertical relative position.
077: */
078: private boolean ignoreYRelative = false;
079: /**
080: * Whether the shape is below the text.
081: */
082: private boolean shapeBelowText = false;
083:
084: /**
085: * Constructs a new RtfShapePosition with the four bounding coordinates.
086: *
087: * @param top The top coordinate.
088: * @param left The left coordinate.
089: * @param right The right coordinate.
090: * @param bottom The bottom coordinate.
091: */
092: public RtfShapePosition(int top, int left, int right, int bottom) {
093: this .top = top;
094: this .left = left;
095: this .right = right;
096: this .bottom = bottom;
097: }
098:
099: /**
100: * Gets whether the shape is below the text.
101: *
102: * @return <code>True</code> if the shape is below, <code>false</code> if the text is below.
103: */
104: public boolean isShapeBelowText() {
105: return shapeBelowText;
106: }
107:
108: /**
109: * Sets whether the shape is below the text.
110: *
111: * @param shapeBelowText <code>True</code> if the shape is below, <code>false</code> if the text is below.
112: */
113: public void setShapeBelowText(boolean shapeBelowText) {
114: this .shapeBelowText = shapeBelowText;
115: }
116:
117: /**
118: * Sets the relative horizontal position. Use one of the constants
119: * provided in this class.
120: *
121: * @param relativePos The relative horizontal position to use.
122: */
123: public void setXRelativePos(int relativePos) {
124: xRelativePos = relativePos;
125: }
126:
127: /**
128: * Sets the relative vertical position. Use one of the constants
129: * provides in this class.
130: *
131: * @param relativePos The relative vertical position to use.
132: */
133: public void setYRelativePos(int relativePos) {
134: yRelativePos = relativePos;
135: }
136:
137: /**
138: * Sets the z order to use.
139: *
140: * @param order The z order to use.
141: */
142: public void setZOrder(int order) {
143: zOrder = order;
144: }
145:
146: /**
147: * Set whether to ignore the horizontal relative position.
148: *
149: * @param ignoreXRelative <code>True</code> to ignore the horizontal relative position, <code>false</code> otherwise.
150: */
151: protected void setIgnoreXRelative(boolean ignoreXRelative) {
152: this .ignoreXRelative = ignoreXRelative;
153: }
154:
155: /**
156: * Set whether to ignore the vertical relative position.
157: *
158: * @param ignoreYRelative <code>True</code> to ignore the vertical relative position, <code>false</code> otherwise.
159: */
160: protected void setIgnoreYRelative(boolean ignoreYRelative) {
161: this .ignoreYRelative = ignoreYRelative;
162: }
163:
164: /**
165: * Write this RtfShapePosition.
166: * @deprecated replaced by {@link #writeContent(OutputStream)}
167: */
168: public byte[] write() {
169: ByteArrayOutputStream result = new ByteArrayOutputStream();
170: try {
171: writeContent(result);
172: } catch (IOException ioe) {
173: ioe.printStackTrace();
174: }
175: return result.toByteArray();
176: }
177:
178: /**
179: * Write this RtfShapePosition.
180: */
181: public void writeContent(final OutputStream result)
182: throws IOException {
183: result.write("\\shpleft".getBytes());
184: result.write(intToByteArray(this .left));
185: result.write("\\shptop".getBytes());
186: result.write(intToByteArray(this .top));
187: result.write("\\shpright".getBytes());
188: result.write(intToByteArray(this .right));
189: result.write("\\shpbottom".getBytes());
190: result.write(intToByteArray(this .bottom));
191: result.write("\\shpz".getBytes());
192: result.write(intToByteArray(this .zOrder));
193: switch (this .xRelativePos) {
194: case POSITION_X_RELATIVE_PAGE:
195: result.write("\\shpbxpage".getBytes());
196: break;
197: case POSITION_X_RELATIVE_MARGIN:
198: result.write("\\shpbxmargin".getBytes());
199: break;
200: case POSITION_X_RELATIVE_COLUMN:
201: result.write("\\shpbxcolumn".getBytes());
202: break;
203: }
204: if (this .ignoreXRelative) {
205: result.write("\\shpbxignore".getBytes());
206: }
207: switch (this .yRelativePos) {
208: case POSITION_Y_RELATIVE_PAGE:
209: result.write("\\shpbypage".getBytes());
210: break;
211: case POSITION_Y_RELATIVE_MARGIN:
212: result.write("\\shpbymargin".getBytes());
213: break;
214: case POSITION_Y_RELATIVE_PARAGRAPH:
215: result.write("\\shpbypara".getBytes());
216: break;
217: }
218: if (this .ignoreYRelative) {
219: result.write("\\shpbyignore".getBytes());
220: }
221: if (this .shapeBelowText) {
222: result.write("\\shpfblwtxt1".getBytes());
223: } else {
224: result.write("\\shpfblwtxt0".getBytes());
225: }
226: }
227:
228: }
|