1 /*
2  * $Id: PdfRectangle.java,v 1.26 2002/07/09 11:28:24 blowagie Exp $
3  * $Name:  $
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  *
8  * The Original Code is 'iText, a free JAVA-PDF library'.
9  *
10  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
11  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
12  * All Rights Reserved.
13  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
14  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
15  *
16  * Contributor(s): all the names of the contributors are added in the source code
17  * where applicable.
18  *
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Library General Public
22  * License as published by the Free Software Foundation; either
23  * version 2 of the License, or (at your option) any later version.
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License along with this library; if not, write to the
32  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
33  * Boston, MA  02110-1301, USA.
34  *
35  *
36  * This library is free software; you can redistribute it and/or
37  * modify it under the terms of the GNU Library General Public
38  * License as published by the Free Software Foundation; either
39  * version 2 of the License, or (at your option) any later version.
40  *
41  * This library is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
44  * Library General Public License for more details.
45  *
46  * You should have received a copy of the GNU Library General Public
47  * License along with this library; if not, write to the
48  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
49  * Boston, MA  02110-1301, USA.
50  *
51  *
52  * If you didn't download this code from the following link, you should check if
53  * you aren't using an obsolete version:
54  * http://www.lowagie.com/iText/
55  */
56 
57 package com.gitlab.pdftk_java.com.lowagie.text.pdf;
58 
59 import com.gitlab.pdftk_java.com.lowagie.text.Rectangle;
60 
61 /**
62  * <CODE>PdfRectangle</CODE> is the PDF Rectangle object.
63  * <P>
64  * Rectangles are used to describe locations on the page and bounding boxes for several
65  * objects in PDF, such as fonts. A rectangle is represented as an <CODE>array</CODE> of
66  * four numbers, specifying the lower lef <I>x</I>, lower left <I>y</I>, upper right <I>x</I>,
67  * and upper right <I>y</I> coordinates of the rectangle, in that order.<BR>
68  * This object is described in the 'Portable Document Format Reference Manual version 1.3'
69  * section 7.1 (page 183).
70  *
71  * @see		com.gitlab.pdftk_java.com.lowagie.text.Rectangle
72  * @see		PdfArray
73  */
74 
75 public class PdfRectangle extends PdfArray {
76 
77     // membervariables
78 
79 /** lower left x */
80     private float llx = 0;
81 
82 /** lower left y */
83     private float lly = 0;
84 
85 /** upper right x */
86     private float urx = 0;
87 
88 /** upper right y */
89     private float ury = 0;
90 
91     // constructors
92 
93 /**
94  * Constructs a <CODE>PdfRectangle</CODE>-object.
95  *
96  * @param		llx			lower left x
97  * @param		lly			lower left y
98  * @param		urx			upper right x
99  * @param		ury			upper right y
100  *
101  * @since		rugPdf0.10
102  */
103 
PdfRectangle(float llx, float lly, float urx, float ury, int rotation)104     public PdfRectangle(float llx, float lly, float urx, float ury, int rotation) {
105         super();
106         if (rotation == 90 || rotation == 270) {
107             this.llx = lly;
108             this.lly = llx;
109             this.urx = ury;
110             this.ury = urx;
111         }
112         else {
113             this.llx = llx;
114             this.lly = lly;
115             this.urx = urx;
116             this.ury = ury;
117         }
118         super.add(new PdfNumber(this.llx));
119         super.add(new PdfNumber(this.lly));
120         super.add(new PdfNumber(this.urx));
121         super.add(new PdfNumber(this.ury));
122     }
123 
PdfRectangle(float llx, float lly, float urx, float ury)124     public PdfRectangle(float llx, float lly, float urx, float ury) {
125         this(llx, lly, urx, ury, 0);
126     }
127 
128 /**
129  * Constructs a <CODE>PdfRectangle</CODE>-object starting from the origin (0, 0).
130  *
131  * @param		urx			upper right x
132  * @param		ury			upper right y
133  */
134 
PdfRectangle(float urx, float ury, int rotation)135     public PdfRectangle(float urx, float ury, int rotation) {
136         this(0, 0, urx, ury, rotation);
137     }
138 
PdfRectangle(float urx, float ury)139     public PdfRectangle(float urx, float ury) {
140         this(0, 0, urx, ury, 0);
141     }
142 
143 /**
144  * Constructs a <CODE>PdfRectangle</CODE>-object with a <CODE>Rectangle</CODE>-object.
145  *
146  * @param	rectangle	a <CODE>Rectangle</CODE>
147  */
148 
PdfRectangle(Rectangle rectangle, int rotation)149     public PdfRectangle(Rectangle rectangle, int rotation) {
150         this(rectangle.left(), rectangle.bottom(), rectangle.right(), rectangle.top(), rotation);
151     }
152 
PdfRectangle(Rectangle rectangle)153     public PdfRectangle(Rectangle rectangle) {
154         this(rectangle.left(), rectangle.bottom(), rectangle.right(), rectangle.top(), 0);
155     }
156 
157     // methods
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 
add(PdfObject object)166     public boolean add(PdfObject object) {
167         return false;
168     }
169 
170 /**
171  * Returns the lower left x-coordinate.
172  *
173  * @return		the lower left x-coordinaat
174  */
175 
left()176     public float left() {
177         return llx;
178     }
179 
180 /**
181  * Returns the upper right x-coordinate.
182  *
183  * @return		the upper right x-coordinate
184  */
185 
right()186     public float right() {
187         return urx;
188     }
189 
190 /**
191  * Returns the upper right y-coordinate.
192  *
193  * @return		the upper right y-coordinate
194  */
195 
top()196     public float top() {
197         return ury;
198     }
199 
200 /**
201  * Returns the lower left y-coordinate.
202  *
203  * @return		the lower left y-coordinate
204  */
205 
bottom()206     public float bottom() {
207         return lly;
208     }
209 
210 /**
211  * Returns the lower left x-coordinate, considering a given margin.
212  *
213  * @param		margin		a margin
214  * @return		the lower left x-coordinate
215  */
216 
left(int margin)217     public float left(int margin) {
218         return llx + margin;
219     }
220 
221 /**
222  * Returns the upper right x-coordinate, considering a given margin.
223  *
224  * @param		margin		a margin
225  * @return		the upper right x-coordinate
226  */
227 
right(int margin)228     public float right(int margin) {
229         return urx - margin;
230     }
231 
232 /**
233  * Returns the upper right y-coordinate, considering a given margin.
234  *
235  * @param		margin		a margin
236  * @return		the upper right y-coordinate
237  */
238 
top(int margin)239     public float top(int margin) {
240         return ury - margin;
241     }
242 
243 /**
244  * Returns the lower left y-coordinate, considering a given margin.
245  *
246  * @param		margin		a margin
247  * @return		the lower left y-coordinate
248  */
249 
bottom(int margin)250     public float bottom(int margin) {
251         return lly + margin;
252     }
253 
254 /**
255  * Returns the width of the rectangle.
256  *
257  * @return		a width
258  */
259 
width()260     public float width() {
261         return urx - llx;
262     }
263 
264 /**
265  * Returns the height of the rectangle.
266  *
267  * @return		a height
268  */
269 
height()270     public float height() {
271         return ury - lly;
272     }
273 
274 /**
275  * Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle.
276  *
277  * @return		a <CODE>PdfRectangle</CODE>
278  */
279 
rotate()280     public PdfRectangle rotate() {
281         return new PdfRectangle(lly, llx, ury, urx, 0);
282     }
283 }