KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfRectangle


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

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

68
69 public class PdfRectangle extends PdfArray {
70     
71     // membervariables
72

73 /** lower left x */
74     private float llx = 0;
75     
76 /** lower left y */
77     private float lly = 0;
78     
79 /** upper right x */
80     private float urx = 0;
81     
82 /** upper right y */
83     private float ury = 0;
84     
85     // constructors
86

87 /**
88  * Constructs a <CODE>PdfRectangle</CODE>-object.
89  *
90  * @param llx lower left x
91  * @param lly lower left y
92  * @param urx upper right x
93  * @param ury upper right y
94  *
95  * @since rugPdf0.10
96  */

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

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

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

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

166     
167     public boolean add(PdfObject object) {
168         return false;
169     }
170     
171 /**
172  * Returns the lower left x-coordinate.
173  *
174  * @return the lower left x-coordinaat
175  */

176     
177     public float left() {
178         return llx;
179     }
180     
181 /**
182  * Returns the upper right x-coordinate.
183  *
184  * @return the upper right x-coordinate
185  */

186     
187     public float right() {
188         return urx;
189     }
190     
191 /**
192  * Returns the upper right y-coordinate.
193  *
194  * @return the upper right y-coordinate
195  */

196     
197     public float top() {
198         return ury;
199     }
200     
201 /**
202  * Returns the lower left y-coordinate.
203  *
204  * @return the lower left y-coordinate
205  */

206     
207     public float bottom() {
208         return lly;
209     }
210     
211 /**
212  * Returns the lower left x-coordinate, considering a given margin.
213  *
214  * @param margin a margin
215  * @return the lower left x-coordinate
216  */

217     
218     public float left(int margin) {
219         return llx + margin;
220     }
221     
222 /**
223  * Returns the upper right x-coordinate, considering a given margin.
224  *
225  * @param margin a margin
226  * @return the upper right x-coordinate
227  */

228     
229     public float right(int margin) {
230         return urx - margin;
231     }
232     
233 /**
234  * Returns the upper right y-coordinate, considering a given margin.
235  *
236  * @param margin a margin
237  * @return the upper right y-coordinate
238  */

239     
240     public float top(int margin) {
241         return ury - margin;
242     }
243     
244 /**
245  * Returns the lower left y-coordinate, considering a given margin.
246  *
247  * @param margin a margin
248  * @return the lower left y-coordinate
249  */

250     
251     public float bottom(int margin) {
252         return lly + margin;
253     }
254     
255 /**
256  * Returns the width of the rectangle.
257  *
258  * @return a width
259  */

260     
261     public float width() {
262         return urx - llx;
263     }
264     
265 /**
266  * Returns the height of the rectangle.
267  *
268  * @return a height
269  */

270     
271     public float height() {
272         return ury - lly;
273     }
274     
275 /**
276  * Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
277  *
278  * @return a <CODE>PdfRectangle</CODE>
279  */

280     
281     public PdfRectangle rotate() {
282         return new PdfRectangle(lly, llx, ury, urx, 0);
283     }
284 }
Popular Tags