001/*
002 * $Id: PdfRectangle.java 4793 2011-04-05 15:28:41Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045
046import com.itextpdf.text.Rectangle;
047
048/**
049 * <CODE>PdfRectangle</CODE> is the PDF Rectangle object.
050 * <P>
051 * Rectangles are used to describe locations on the page and bounding boxes for several
052 * objects in PDF, such as fonts. A rectangle is represented as an <CODE>array</CODE> of
053 * four numbers, specifying the lower left <I>x</I>, lower left <I>y</I>, upper right <I>x</I>,
054 * and upper right <I>y</I> coordinates of the rectangle, in that order.<BR>
055 * This object is described in the 'Portable Document Format Reference Manual version 1.3'
056 * section 7.1 (page 183).
057 *
058 * @see         com.itextpdf.text.Rectangle
059 * @see         PdfArray
060 */
061
062public class PdfRectangle extends NumberArray {
063
064    // membervariables
065
066/** lower left x */
067    private float llx = 0;
068
069/** lower left y */
070    private float lly = 0;
071
072/** upper right x */
073    private float urx = 0;
074
075/** upper right y */
076    private float ury = 0;
077
078    // constructors
079
080/**
081 * Constructs a <CODE>PdfRectangle</CODE>-object.
082 *
083 * @param               llx                     lower left x
084 * @param               lly                     lower left y
085 * @param               urx                     upper right x
086 * @param               ury                     upper right y
087 *
088 * @since               rugPdf0.10
089 */
090
091    public PdfRectangle(float llx, float lly, float urx, float ury, int rotation) {
092        super();
093        if (rotation == 90 || rotation == 270) {
094            this.llx = lly;
095            this.lly = llx;
096            this.urx = ury;
097            this.ury = urx;
098        }
099        else {
100            this.llx = llx;
101            this.lly = lly;
102            this.urx = urx;
103            this.ury = ury;
104        }
105        super.add(new PdfNumber(this.llx));
106        super.add(new PdfNumber(this.lly));
107        super.add(new PdfNumber(this.urx));
108        super.add(new PdfNumber(this.ury));
109    }
110
111    public PdfRectangle(float llx, float lly, float urx, float ury) {
112        this(llx, lly, urx, ury, 0);
113    }
114
115/**
116 * Constructs a <CODE>PdfRectangle</CODE>-object starting from the origin (0, 0).
117 *
118 * @param               urx                     upper right x
119 * @param               ury                     upper right y
120 */
121
122    public PdfRectangle(float urx, float ury, int rotation) {
123        this(0, 0, urx, ury, rotation);
124    }
125
126    public PdfRectangle(float urx, float ury) {
127        this(0, 0, urx, ury, 0);
128    }
129
130/**
131 * Constructs a <CODE>PdfRectangle</CODE>-object with a <CODE>Rectangle</CODE>-object.
132 *
133 * @param       rectangle       a <CODE>Rectangle</CODE>
134 */
135
136    public PdfRectangle(Rectangle rectangle, int rotation) {
137        this(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), rectangle.getTop(), rotation);
138    }
139
140    public PdfRectangle(Rectangle rectangle) {
141        this(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), rectangle.getTop(), 0);
142    }
143
144    // methods
145    /**
146     * Returns the high level version of this PdfRectangle
147     * @return this PdfRectangle translated to class Rectangle
148     */
149    public Rectangle getRectangle() {
150        return new Rectangle(left(), bottom(), right(), top());
151    }
152
153/**
154 * Overrides the <CODE>add</CODE>-method in <CODE>PdfArray</CODE> in order to prevent the adding of extra object to the array.
155 *
156 * @param               object                  <CODE>PdfObject</CODE> to add (will not be added here)
157 * @return              <CODE>false</CODE>
158 */
159
160    public boolean add(PdfObject object) {
161        return false;
162    }
163
164    /**
165     * Block changes to the underlying PdfArray
166     * @param values stuff we'll ignore.  Ha!
167     * @return false.  You can't add anything to a PdfRectangle
168     * @since 2.1.5
169     */
170
171    public boolean add( float values[] ) {
172        return false;
173    }
174
175    /**
176     * Block changes to the underlying PdfArray
177     * @param values stuff we'll ignore.  Ha!
178     * @return false.  You can't add anything to a PdfRectangle
179     * @since 2.1.5
180     */
181
182    public boolean add( int values[] ) {
183        return false;
184    }
185
186    /**
187     * Block changes to the underlying PdfArray
188     * @param object Ignored.
189     * @since 2.1.5
190     */
191
192    public void addFirst( PdfObject object ) {
193    }
194/**
195 * Returns the lower left x-coordinate.
196 *
197 * @return              the lower left x-coordinate
198 */
199
200    public float left() {
201        return llx;
202    }
203
204/**
205 * Returns the upper right x-coordinate.
206 *
207 * @return              the upper right x-coordinate
208 */
209
210    public float right() {
211        return urx;
212    }
213
214/**
215 * Returns the upper right y-coordinate.
216 *
217 * @return              the upper right y-coordinate
218 */
219
220    public float top() {
221        return ury;
222    }
223
224/**
225 * Returns the lower left y-coordinate.
226 *
227 * @return              the lower left y-coordinate
228 */
229
230    public float bottom() {
231        return lly;
232    }
233
234/**
235 * Returns the lower left x-coordinate, considering a given margin.
236 *
237 * @param               margin          a margin
238 * @return              the lower left x-coordinate
239 */
240
241    public float left(int margin) {
242        return llx + margin;
243    }
244
245/**
246 * Returns the upper right x-coordinate, considering a given margin.
247 *
248 * @param               margin          a margin
249 * @return              the upper right x-coordinate
250 */
251
252    public float right(int margin) {
253        return urx - margin;
254    }
255
256/**
257 * Returns the upper right y-coordinate, considering a given margin.
258 *
259 * @param               margin          a margin
260 * @return              the upper right y-coordinate
261 */
262
263    public float top(int margin) {
264        return ury - margin;
265    }
266
267/**
268 * Returns the lower left y-coordinate, considering a given margin.
269 *
270 * @param               margin          a margin
271 * @return              the lower left y-coordinate
272 */
273
274    public float bottom(int margin) {
275        return lly + margin;
276    }
277
278/**
279 * Returns the width of the rectangle.
280 *
281 * @return              a width
282 */
283
284    public float width() {
285        return urx - llx;
286    }
287
288/**
289 * Returns the height of the rectangle.
290 *
291 * @return              a height
292 */
293
294    public float height() {
295        return ury - lly;
296    }
297
298/**
299 * Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
300 *
301 * @return              a <CODE>PdfRectangle</CODE>
302 */
303
304    public PdfRectangle rotate() {
305        return new PdfRectangle(lly, llx, ury, urx, 0);
306    }
307}