blob: bbf033eae2f1df98c9e98c5d4eb4eca8cdfceb18 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.print;
27
28import java.awt.geom.AffineTransform;
29import java.awt.geom.Point2D;
30import java.awt.geom.Rectangle2D;
31
32/**
33 * The <code>PageFormat</code> class describes the size and
34 * orientation of a page to be printed.
35 */
36public class PageFormat implements Cloneable
37{
38
39 /* Class Constants */
40
41 /**
42 * The origin is at the bottom left of the paper with
43 * x running bottom to top and y running left to right.
44 * Note that this is not the Macintosh landscape but
45 * is the Window's and PostScript landscape.
46 */
47 public static final int LANDSCAPE = 0;
48
49 /**
50 * The origin is at the top left of the paper with
51 * x running to the right and y running down the
52 * paper.
53 */
54 public static final int PORTRAIT = 1;
55
56 /**
57 * The origin is at the top right of the paper with x
58 * running top to bottom and y running right to left.
59 * Note that this is the Macintosh landscape.
60 */
61 public static final int REVERSE_LANDSCAPE = 2;
62
63 /* Instance Variables */
64
65 /**
66 * A description of the physical piece of paper.
67 */
68 private Paper mPaper;
69
70 /**
71 * The orientation of the current page. This will be
72 * one of the constants: PORTRIAT, LANDSCAPE, or
73 * REVERSE_LANDSCAPE,
74 */
75 private int mOrientation = PORTRAIT;
76
77 /* Constructors */
78
79 /**
80 * Creates a default, portrait-oriented
81 * <code>PageFormat</code>.
82 */
83 public PageFormat()
84 {
85 mPaper = new Paper();
86 }
87
88 /* Instance Methods */
89
90 /**
91 * Makes a copy of this <code>PageFormat</code> with the same
92 * contents as this <code>PageFormat</code>.
93 * @return a copy of this <code>PageFormat</code>.
94 */
95 public Object clone() {
96 PageFormat newPage;
97
98 try {
99 newPage = (PageFormat) super.clone();
100 newPage.mPaper = (Paper)mPaper.clone();
101
102 } catch (CloneNotSupportedException e) {
103 e.printStackTrace();
104 newPage = null; // should never happen.
105 }
106
107 return newPage;
108 }
109
110
111 /**
112 * Returns the width, in 1/72nds of an inch, of the page.
113 * This method takes into account the orientation of the
114 * page when determining the width.
115 * @return the width of the page.
116 */
117 public double getWidth() {
118 double width;
119 int orientation = getOrientation();
120
121 if (orientation == PORTRAIT) {
122 width = mPaper.getWidth();
123 } else {
124 width = mPaper.getHeight();
125 }
126
127 return width;
128 }
129
130 /**
131 * Returns the height, in 1/72nds of an inch, of the page.
132 * This method takes into account the orientation of the
133 * page when determining the height.
134 * @return the height of the page.
135 */
136 public double getHeight() {
137 double height;
138 int orientation = getOrientation();
139
140 if (orientation == PORTRAIT) {
141 height = mPaper.getHeight();
142 } else {
143 height = mPaper.getWidth();
144 }
145
146 return height;
147 }
148
149 /**
150 * Returns the x coordinate of the upper left point of the
151 * imageable area of the <code>Paper</code> object
152 * associated with this <code>PageFormat</code>.
153 * This method takes into account the
154 * orientation of the page.
155 * @return the x coordinate of the upper left point of the
156 * imageable area of the <code>Paper</code> object
157 * associated with this <code>PageFormat</code>.
158 */
159 public double getImageableX() {
160 double x;
161
162 switch (getOrientation()) {
163
164 case LANDSCAPE:
165 x = mPaper.getHeight()
166 - (mPaper.getImageableY() + mPaper.getImageableHeight());
167 break;
168
169 case PORTRAIT:
170 x = mPaper.getImageableX();
171 break;
172
173 case REVERSE_LANDSCAPE:
174 x = mPaper.getImageableY();
175 break;
176
177 default:
178 /* This should never happen since it signifies that the
179 * PageFormat is in an invalid orientation.
180 */
181 throw new InternalError("unrecognized orientation");
182
183 }
184
185 return x;
186 }
187
188 /**
189 * Returns the y coordinate of the upper left point of the
190 * imageable area of the <code>Paper</code> object
191 * associated with this <code>PageFormat</code>.
192 * This method takes into account the
193 * orientation of the page.
194 * @return the y coordinate of the upper left point of the
195 * imageable area of the <code>Paper</code> object
196 * associated with this <code>PageFormat</code>.
197 */
198 public double getImageableY() {
199 double y;
200
201 switch (getOrientation()) {
202
203 case LANDSCAPE:
204 y = mPaper.getImageableX();
205 break;
206
207 case PORTRAIT:
208 y = mPaper.getImageableY();
209 break;
210
211 case REVERSE_LANDSCAPE:
212 y = mPaper.getWidth()
213 - (mPaper.getImageableX() + mPaper.getImageableWidth());
214 break;
215
216 default:
217 /* This should never happen since it signifies that the
218 * PageFormat is in an invalid orientation.
219 */
220 throw new InternalError("unrecognized orientation");
221
222 }
223
224 return y;
225 }
226
227 /**
228 * Returns the width, in 1/72nds of an inch, of the imageable
229 * area of the page. This method takes into account the orientation
230 * of the page.
231 * @return the width of the page.
232 */
233 public double getImageableWidth() {
234 double width;
235
236 if (getOrientation() == PORTRAIT) {
237 width = mPaper.getImageableWidth();
238 } else {
239 width = mPaper.getImageableHeight();
240 }
241
242 return width;
243 }
244
245 /**
246 * Return the height, in 1/72nds of an inch, of the imageable
247 * area of the page. This method takes into account the orientation
248 * of the page.
249 * @return the height of the page.
250 */
251 public double getImageableHeight() {
252 double height;
253
254 if (getOrientation() == PORTRAIT) {
255 height = mPaper.getImageableHeight();
256 } else {
257 height = mPaper.getImageableWidth();
258 }
259
260 return height;
261 }
262
263
264 /**
265 * Returns a copy of the {@link Paper} object associated
266 * with this <code>PageFormat</code>. Changes made to the
267 * <code>Paper</code> object returned from this method do not
268 * affect the <code>Paper</code> object of this
269 * <code>PageFormat</code>. To update the <code>Paper</code>
270 * object of this <code>PageFormat</code>, create a new
271 * <code>Paper</code> object and set it into this
272 * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
273 * method.
274 * @return a copy of the <code>Paper</code> object associated
275 * with this <code>PageFormat</code>.
276 * @see #setPaper
277 */
278 public Paper getPaper() {
279 return (Paper)mPaper.clone();
280 }
281
282 /**
283 * Sets the <code>Paper</code> object for this
284 * <code>PageFormat</code>.
285 * @param paper the <code>Paper</code> object to which to set
286 * the <code>Paper</code> object for this <code>PageFormat</code>.
287 * @exception <code>NullPointerException</code>
288 * a null paper instance was passed as a parameter.
289 * @see #getPaper
290 */
291 public void setPaper(Paper paper) {
292 mPaper = (Paper)paper.clone();
293 }
294
295 /**
296 * Sets the page orientation. <code>orientation</code> must be
297 * one of the constants: PORTRAIT, LANDSCAPE,
298 * or REVERSE_LANDSCAPE.
299 * @param orientation the new orientation for the page
300 * @throws IllegalArgumentException if
301 * an unknown orientation was requested
302 * @see #getOrientation
303 */
304 public void setOrientation(int orientation) throws IllegalArgumentException
305 {
306 if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
307 mOrientation = orientation;
308 } else {
309 throw new IllegalArgumentException();
310 }
311 }
312
313 /**
314 * Returns the orientation of this <code>PageFormat</code>.
315 * @return this <code>PageFormat</code> object's orientation.
316 * @see #setOrientation
317 */
318 public int getOrientation() {
319 return mOrientation;
320 }
321
322 /**
323 * Returns a transformation matrix that translates user
324 * space rendering to the requested orientation
325 * of the page. The values are placed into the
326 * array as
327 * {&nbsp;m00,&nbsp;m10,&nbsp;m01,&nbsp;m11,&nbsp;m02,&nbsp;m12} in
328 * the form required by the {@link AffineTransform}
329 * constructor.
330 * @return the matrix used to translate user space rendering
331 * to the orientation of the page.
332 * @see java.awt.geom.AffineTransform
333 */
334 public double[] getMatrix() {
335 double[] matrix = new double[6];
336
337 switch (mOrientation) {
338
339 case LANDSCAPE:
340 matrix[0] = 0; matrix[1] = -1;
341 matrix[2] = 1; matrix[3] = 0;
342 matrix[4] = 0; matrix[5] = mPaper.getHeight();
343 break;
344
345 case PORTRAIT:
346 matrix[0] = 1; matrix[1] = 0;
347 matrix[2] = 0; matrix[3] = 1;
348 matrix[4] = 0; matrix[5] = 0;
349 break;
350
351 case REVERSE_LANDSCAPE:
352 matrix[0] = 0; matrix[1] = 1;
353 matrix[2] = -1; matrix[3] = 0;
354 matrix[4] = mPaper.getWidth(); matrix[5] = 0;
355 break;
356
357 default:
358 throw new IllegalArgumentException();
359 }
360
361 return matrix;
362 }
363}