001: /**
002: * Copyright (c) 2003-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.common;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSFloat;
035: import org.pdfbox.cos.COSNumber;
036:
037: import org.fontbox.util.BoundingBox;
038:
039: import java.awt.Dimension;
040:
041: /**
042: * This represents a rectangle in a PDF document.
043: *
044: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
045: * @version $Revision: 1.12 $
046: */
047: public class PDRectangle implements COSObjectable {
048: private COSArray rectArray;
049:
050: /**
051: * Constructor.
052: *
053: * Initializes to 0,0,0,0
054: */
055: public PDRectangle() {
056: rectArray = new COSArray();
057: rectArray.add(new COSFloat(0.0f));
058: rectArray.add(new COSFloat(0.0f));
059: rectArray.add(new COSFloat(0.0f));
060: rectArray.add(new COSFloat(0.0f));
061: }
062:
063: /**
064: * Constructor.
065: *
066: * @param width The width of the rectangle.
067: * @param height The height of the rectangle.
068: */
069: public PDRectangle(float width, float height) {
070: rectArray = new COSArray();
071: rectArray.add(new COSFloat(0.0f));
072: rectArray.add(new COSFloat(0.0f));
073: rectArray.add(new COSFloat(width));
074: rectArray.add(new COSFloat(height));
075: }
076:
077: /**
078: * Constructor.
079: *
080: * @param box The non PD bouding box.
081: */
082: public PDRectangle(BoundingBox box) {
083: rectArray = new COSArray();
084: rectArray.add(new COSFloat(box.getLowerLeftX()));
085: rectArray.add(new COSFloat(box.getLowerLeftY()));
086: rectArray.add(new COSFloat(box.getUpperRightX()));
087: rectArray.add(new COSFloat(box.getUpperRightY()));
088: }
089:
090: /**
091: * Constructor.
092: *
093: * @param array An array of numbers as specified in the PDF Reference for a rectangle type.
094: */
095: public PDRectangle(COSArray array) {
096: rectArray = array;
097: }
098:
099: /**
100: * Method to determine if the x/y point is inside this rectangle.
101: * @param x The x-coordinate to test.
102: * @param y The y-coordinate to test.
103: * @return True if the point is inside this rectangle.
104: */
105: public boolean contains(float x, float y) {
106: float llx = getLowerLeftX();
107: float urx = getUpperRightX();
108: float lly = getLowerLeftY();
109: float ury = getUpperRightY();
110: return x >= llx && x <= urx && y >= lly && y <= ury;
111: }
112:
113: /**
114: * This will create a translated rectangle based off of this rectangle, such
115: * that the new rectangle retains the same dimensions(height/width), but the
116: * lower left x,y values are zero. <br />
117: * 100, 100, 400, 400 (llx, lly, urx, ury ) <br />
118: * will be translated to 0,0,300,300
119: *
120: * @return A new rectangle that has been translated back to the origin.
121: */
122: public PDRectangle createRetranslatedRectangle() {
123: PDRectangle retval = new PDRectangle();
124: retval.setUpperRightX(getWidth());
125: retval.setUpperRightY(getHeight());
126: return retval;
127: }
128:
129: /**
130: * This will get the underlying array for this rectangle.
131: *
132: * @return The cos array.
133: */
134: public COSArray getCOSArray() {
135: return rectArray;
136: }
137:
138: /**
139: * This will get the lower left x coordinate.
140: *
141: * @return The lower left x.
142: */
143: public float getLowerLeftX() {
144: return ((COSNumber) rectArray.get(0)).floatValue();
145: }
146:
147: /**
148: * This will set the lower left x coordinate.
149: *
150: * @param value The lower left x.
151: */
152: public void setLowerLeftX(float value) {
153: rectArray.set(0, new COSFloat(value));
154: }
155:
156: /**
157: * This will get the lower left y coordinate.
158: *
159: * @return The lower left y.
160: */
161: public float getLowerLeftY() {
162: return ((COSNumber) rectArray.get(1)).floatValue();
163: }
164:
165: /**
166: * This will set the lower left y coordinate.
167: *
168: * @param value The lower left y.
169: */
170: public void setLowerLeftY(float value) {
171: rectArray.set(1, new COSFloat(value));
172: }
173:
174: /**
175: * This will get the upper right x coordinate.
176: *
177: * @return The upper right x .
178: */
179: public float getUpperRightX() {
180: return ((COSNumber) rectArray.get(2)).floatValue();
181: }
182:
183: /**
184: * This will set the upper right x coordinate.
185: *
186: * @param value The upper right x .
187: */
188: public void setUpperRightX(float value) {
189: rectArray.set(2, new COSFloat(value));
190: }
191:
192: /**
193: * This will get the upper right y coordinate.
194: *
195: * @return The upper right y.
196: */
197: public float getUpperRightY() {
198: return ((COSNumber) rectArray.get(3)).floatValue();
199: }
200:
201: /**
202: * This will set the upper right y coordinate.
203: *
204: * @param value The upper right y.
205: */
206: public void setUpperRightY(float value) {
207: rectArray.set(3, new COSFloat(value));
208: }
209:
210: /**
211: * This will get the width of this rectangle as calculated by
212: * upperRightX - lowerLeftX.
213: *
214: * @return The width of this rectangle.
215: */
216: public float getWidth() {
217: return getUpperRightX() - getLowerLeftX();
218: }
219:
220: /**
221: * This will get the height of this rectangle as calculated by
222: * upperRightY - lowerLeftY.
223: *
224: * @return The height of this rectangle.
225: */
226: public float getHeight() {
227: return getUpperRightY() - getLowerLeftY();
228: }
229:
230: /**
231: * A convenience method to create a dimension object for AWT operations.
232: *
233: * @return A dimension that matches the width and height of this rectangle.
234: */
235: public Dimension createDimension() {
236: return new Dimension((int) getWidth(), (int) getHeight());
237: }
238:
239: /**
240: * This will move the rectangle the given relative amount.
241: *
242: * @param horizontalAmount positive values will move rectangle to the right, negative's to the left.
243: * @param verticalAmount positive values will move the rectangle up, negative's down.
244: */
245: public void move(float horizontalAmount, float verticalAmount) {
246: setUpperRightX(getUpperRightX() + horizontalAmount);
247: setLowerLeftX(getLowerLeftX() + horizontalAmount);
248: setUpperRightY(getUpperRightY() + verticalAmount);
249: setLowerLeftY(getLowerLeftY() + verticalAmount);
250: }
251:
252: /**
253: * Convert this standard java object to a COS object.
254: *
255: * @return The cos object that matches this Java object.
256: */
257: public COSBase getCOSObject() {
258: return rectArray;
259: }
260:
261: /**
262: * This will return a string representation of this rectangle.
263: *
264: * @return This object as a string.
265: */
266: public String toString() {
267: return "[" + getLowerLeftX() + "," + getLowerLeftY() + ","
268: + getUpperRightX() + "," + getUpperRightY() + "]";
269: }
270: }
|