1 /*
2  * $Id: PdfRectangle.java 3694 2009-02-17 19:29:05Z mstorer $
3  *
4  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf;
51
52 import com.lowagie.text.Rectangle;
53
54 /**
55  * <CODE>PdfRectangle</CODE> is the PDF Rectangle object.
56  * <P>
57  * Rectangles are used to describe locations on the page and bounding boxes for several
58  * objects in PDF, such as fonts. A rectangle is represented as an <CODE>array</CODE> of
59  * four numbers, specifying the lower left <I>x</I>, lower left <I>y</I>, upper right <I>x</I>,
60  * and upper right <I>y</I> coordinates of the rectangle, in that order.<BR>
61  * This object is described in the 'Portable Document Format Reference Manual version 1.3'
62  * section 7.1 (page 183).
63  *
64  * @see        com.lowagie.text.Rectangle
65  * @see        PdfArray
66  */

67
68 public class PdfRectangle extends PdfArray {
69
70     // membervariables
71
72 /** lower left x */
73     private float llx = 0;
74
75 /** lower left y */
76     private float lly = 0;
77
78 /** upper right x */
79     private float urx = 0;
80
81 /** upper right y */
82     private float ury = 0;
83
84     // constructors
85
86 /**
87  * Constructs a <CODE>PdfRectangle</CODE>-object.
88  *
89  * @param        llx            lower left x
90  * @param        lly            lower left y
91  * @param        urx            upper right x
92  * @param        ury            upper right y
93  *
94  * @since        rugPdf0.10
95  */

96
97     public PdfRectangle(float llx, float lly, float urx, float ury, int rotation) {
98         super();
99         if (rotation == 90 || rotation == 270) {
100             this.llx = lly;
101             this.lly = llx;
102             this.urx = ury;
103             this.ury = urx;
104         }
105         else {
106             this.llx = llx;
107             this.lly = lly;
108             this.urx = urx;
109             this.ury = ury;
110         }
111         super.add(new PdfNumber(this.llx));
112         super.add(new PdfNumber(this.lly));
113         super.add(new PdfNumber(this.urx));
114         super.add(new PdfNumber(this.ury));
115     }
116
117     public PdfRectangle(float llx, float lly, float urx, float ury) {
118         this(llx, lly, urx, ury, 0);
119     }
120
121 /**
122  * Constructs a <CODE>PdfRectangle</CODE>-object starting from the origin (0, 0).
123  *
124  * @param        urx            upper right x
125  * @param        ury            upper right y
126  */

127
128     public PdfRectangle(float urx, float ury, int rotation) {
129         this(0, 0, urx, ury, rotation);
130     }
131
132     public PdfRectangle(float urx, float ury) {
133         this(0, 0, urx, ury, 0);
134     }
135
136 /**
137  * Constructs a <CODE>PdfRectangle</CODE>-object with a <CODE>Rectangle</CODE>-object.
138  *
139  * @param    rectangle    a <CODE>Rectangle</CODE>
140  */

141
142     public PdfRectangle(Rectangle rectangle, int rotation) {
143         this(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), rectangle.getTop(), rotation);
144     }
145
146     public PdfRectangle(Rectangle rectangle) {
147         this(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), rectangle.getTop(), 0);
148     }
149
150     // methods
151     /**
152      * Returns the high level version of this PdfRectangle
153      * @return this PdfRectangle translated to class Rectangle
154      */

155     public Rectangle getRectangle() {
156         return new Rectangle(left(), bottom(), right(), top());
157     }
158
159 /**
160  * Overrides the <CODE>add</CODE>-method in <CODE>PdfArray</CODE> in order to prevent the adding of extra object to the array.
161  *
162  * @param        object            <CODE>PdfObject</CODE> to add (will not be added here)
163  * @return        <CODE>false</CODE>
164  */

165
166     public boolean add(PdfObject object) {
167         return false;
168     }
169
170     /**
171      * Block changes to the underlying PdfArray
172      * @param values stuff we'll ignore.  Ha!
173      * @return false.  You can't add anything to a PdfRectangle
174      * @since 2.1.5
175      */

176
177     public boolean add( float values[] ) {
178         return false;
179     }
180
181     /**
182      * Block changes to the underlying PdfArray
183      * @param values stuff we'll ignore.  Ha!
184      * @return false.  You can't add anything to a PdfRectangle
185      * @since 2.1.5
186      */

187
188     public boolean add( int values[] ) {
189         return false;
190     }
191
192     /**
193      * Block changes to the underlying PdfArray
194      * @param object Ignored.
195      * @since 2.1.5
196      */

197
198     public void addFirst( PdfObject object ) {
199     }
200 /**
201  * Returns the lower left x-coordinate.
202  *
203  * @return        the lower left x-coordinate
204  */

205
206     public float left() {
207         return llx;
208     }
209
210 /**
211  * Returns the upper right x-coordinate.
212  *
213  * @return        the upper right x-coordinate
214  */

215
216     public float right() {
217         return urx;
218     }
219
220 /**
221  * Returns the upper right y-coordinate.
222  *
223  * @return        the upper right y-coordinate
224  */

225
226     public float top() {
227         return ury;
228     }
229
230 /**
231  * Returns the lower left y-coordinate.
232  *
233  * @return        the lower left y-coordinate
234  */

235
236     public float bottom() {
237         return lly;
238     }
239
240 /**
241  * Returns the lower left x-coordinate, considering a given margin.
242  *
243  * @param        margin        a margin
244  * @return        the lower left x-coordinate
245  */

246
247     public float left(int margin) {
248         return llx + margin;
249     }
250
251 /**
252  * Returns the upper right x-coordinate, considering a given margin.
253  *
254  * @param        margin        a margin
255  * @return        the upper right x-coordinate
256  */

257
258     public float right(int margin) {
259         return urx - margin;
260     }
261
262 /**
263  * Returns the upper right y-coordinate, considering a given margin.
264  *
265  * @param        margin        a margin
266  * @return        the upper right y-coordinate
267  */

268
269     public float top(int margin) {
270         return ury - margin;
271     }
272
273 /**
274  * Returns the lower left y-coordinate, considering a given margin.
275  *
276  * @param        margin        a margin
277  * @return        the lower left y-coordinate
278  */

279
280     public float bottom(int margin) {
281         return lly + margin;
282     }
283
284 /**
285  * Returns the width of the rectangle.
286  *
287  * @return        a width
288  */

289
290     public float width() {
291         return urx - llx;
292     }
293
294 /**
295  * Returns the height of the rectangle.
296  *
297  * @return        a height
298  */

299
300     public float height() {
301         return ury - lly;
302     }
303
304 /**
305  * Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
306  *
307  * @return        a <CODE>PdfRectangle</CODE>
308  */

309
310     public PdfRectangle rotate() {
311         return new PdfRectangle(lly, llx, ury, urx, 0);
312     }
313 }