001 /*
002 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.print;
027
028 import java.awt.geom.AffineTransform;
029 import java.awt.geom.Point2D;
030 import java.awt.geom.Rectangle2D;
031
032 /**
033 * The <code>PageFormat</code> class describes the size and
034 * orientation of a page to be printed.
035 */
036 public class PageFormat implements Cloneable {
037
038 /* Class Constants */
039
040 /**
041 * The origin is at the bottom left of the paper with
042 * x running bottom to top and y running left to right.
043 * Note that this is not the Macintosh landscape but
044 * is the Window's and PostScript landscape.
045 */
046 public static final int LANDSCAPE = 0;
047
048 /**
049 * The origin is at the top left of the paper with
050 * x running to the right and y running down the
051 * paper.
052 */
053 public static final int PORTRAIT = 1;
054
055 /**
056 * The origin is at the top right of the paper with x
057 * running top to bottom and y running right to left.
058 * Note that this is the Macintosh landscape.
059 */
060 public static final int REVERSE_LANDSCAPE = 2;
061
062 /* Instance Variables */
063
064 /**
065 * A description of the physical piece of paper.
066 */
067 private Paper mPaper;
068
069 /**
070 * The orientation of the current page. This will be
071 * one of the constants: PORTRIAT, LANDSCAPE, or
072 * REVERSE_LANDSCAPE,
073 */
074 private int mOrientation = PORTRAIT;
075
076 /* Constructors */
077
078 /**
079 * Creates a default, portrait-oriented
080 * <code>PageFormat</code>.
081 */
082 public PageFormat() {
083 mPaper = new Paper();
084 }
085
086 /* Instance Methods */
087
088 /**
089 * Makes a copy of this <code>PageFormat</code> with the same
090 * contents as this <code>PageFormat</code>.
091 * @return a copy of this <code>PageFormat</code>.
092 */
093 public Object clone() {
094 PageFormat newPage;
095
096 try {
097 newPage = (PageFormat) super .clone();
098 newPage.mPaper = (Paper) mPaper.clone();
099
100 } catch (CloneNotSupportedException e) {
101 e.printStackTrace();
102 newPage = null; // should never happen.
103 }
104
105 return newPage;
106 }
107
108 /**
109 * Returns the width, in 1/72nds of an inch, of the page.
110 * This method takes into account the orientation of the
111 * page when determining the width.
112 * @return the width of the page.
113 */
114 public double getWidth() {
115 double width;
116 int orientation = getOrientation();
117
118 if (orientation == PORTRAIT) {
119 width = mPaper.getWidth();
120 } else {
121 width = mPaper.getHeight();
122 }
123
124 return width;
125 }
126
127 /**
128 * Returns the height, in 1/72nds of an inch, of the page.
129 * This method takes into account the orientation of the
130 * page when determining the height.
131 * @return the height of the page.
132 */
133 public double getHeight() {
134 double height;
135 int orientation = getOrientation();
136
137 if (orientation == PORTRAIT) {
138 height = mPaper.getHeight();
139 } else {
140 height = mPaper.getWidth();
141 }
142
143 return height;
144 }
145
146 /**
147 * Returns the x coordinate of the upper left point of the
148 * imageable area of the <code>Paper</code> object
149 * associated with this <code>PageFormat</code>.
150 * This method takes into account the
151 * orientation of the page.
152 * @return the x coordinate of the upper left point of the
153 * imageable area of the <code>Paper</code> object
154 * associated with this <code>PageFormat</code>.
155 */
156 public double getImageableX() {
157 double x;
158
159 switch (getOrientation()) {
160
161 case LANDSCAPE:
162 x = mPaper.getHeight()
163 - (mPaper.getImageableY() + mPaper
164 .getImageableHeight());
165 break;
166
167 case PORTRAIT:
168 x = mPaper.getImageableX();
169 break;
170
171 case REVERSE_LANDSCAPE:
172 x = mPaper.getImageableY();
173 break;
174
175 default:
176 /* This should never happen since it signifies that the
177 * PageFormat is in an invalid orientation.
178 */
179 throw new InternalError("unrecognized orientation");
180
181 }
182
183 return x;
184 }
185
186 /**
187 * Returns the y coordinate of the upper left point of the
188 * imageable area of the <code>Paper</code> object
189 * associated with this <code>PageFormat</code>.
190 * This method takes into account the
191 * orientation of the page.
192 * @return the y coordinate of the upper left point of the
193 * imageable area of the <code>Paper</code> object
194 * associated with this <code>PageFormat</code>.
195 */
196 public double getImageableY() {
197 double y;
198
199 switch (getOrientation()) {
200
201 case LANDSCAPE:
202 y = mPaper.getImageableX();
203 break;
204
205 case PORTRAIT:
206 y = mPaper.getImageableY();
207 break;
208
209 case REVERSE_LANDSCAPE:
210 y = mPaper.getWidth()
211 - (mPaper.getImageableX() + mPaper
212 .getImageableWidth());
213 break;
214
215 default:
216 /* This should never happen since it signifies that the
217 * PageFormat is in an invalid orientation.
218 */
219 throw new InternalError("unrecognized orientation");
220
221 }
222
223 return y;
224 }
225
226 /**
227 * Returns the width, in 1/72nds of an inch, of the imageable
228 * area of the page. This method takes into account the orientation
229 * of the page.
230 * @return the width of the page.
231 */
232 public double getImageableWidth() {
233 double width;
234
235 if (getOrientation() == PORTRAIT) {
236 width = mPaper.getImageableWidth();
237 } else {
238 width = mPaper.getImageableHeight();
239 }
240
241 return width;
242 }
243
244 /**
245 * Return the height, in 1/72nds of an inch, of the imageable
246 * area of the page. This method takes into account the orientation
247 * of the page.
248 * @return the height of the page.
249 */
250 public double getImageableHeight() {
251 double height;
252
253 if (getOrientation() == PORTRAIT) {
254 height = mPaper.getImageableHeight();
255 } else {
256 height = mPaper.getImageableWidth();
257 }
258
259 return height;
260 }
261
262 /**
263 * Returns a copy of the {@link Paper} object associated
264 * with this <code>PageFormat</code>. Changes made to the
265 * <code>Paper</code> object returned from this method do not
266 * affect the <code>Paper</code> object of this
267 * <code>PageFormat</code>. To update the <code>Paper</code>
268 * object of this <code>PageFormat</code>, create a new
269 * <code>Paper</code> object and set it into this
270 * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
271 * method.
272 * @return a copy of the <code>Paper</code> object associated
273 * with this <code>PageFormat</code>.
274 * @see #setPaper
275 */
276 public Paper getPaper() {
277 return (Paper) mPaper.clone();
278 }
279
280 /**
281 * Sets the <code>Paper</code> object for this
282 * <code>PageFormat</code>.
283 * @param paper the <code>Paper</code> object to which to set
284 * the <code>Paper</code> object for this <code>PageFormat</code>.
285 * @exception <code>NullPointerException</code>
286 * a null paper instance was passed as a parameter.
287 * @see #getPaper
288 */
289 public void setPaper(Paper paper) {
290 mPaper = (Paper) paper.clone();
291 }
292
293 /**
294 * Sets the page orientation. <code>orientation</code> must be
295 * one of the constants: PORTRAIT, LANDSCAPE,
296 * or REVERSE_LANDSCAPE.
297 * @param orientation the new orientation for the page
298 * @throws IllegalArgumentException if
299 * an unknown orientation was requested
300 * @see #getOrientation
301 */
302 public void setOrientation(int orientation)
303 throws IllegalArgumentException {
304 if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
305 mOrientation = orientation;
306 } else {
307 throw new IllegalArgumentException();
308 }
309 }
310
311 /**
312 * Returns the orientation of this <code>PageFormat</code>.
313 * @return this <code>PageFormat</code> object's orientation.
314 * @see #setOrientation
315 */
316 public int getOrientation() {
317 return mOrientation;
318 }
319
320 /**
321 * Returns a transformation matrix that translates user
322 * space rendering to the requested orientation
323 * of the page. The values are placed into the
324 * array as
325 * { m00, m10, m01, m11, m02, m12} in
326 * the form required by the {@link AffineTransform}
327 * constructor.
328 * @return the matrix used to translate user space rendering
329 * to the orientation of the page.
330 * @see java.awt.geom.AffineTransform
331 */
332 public double[] getMatrix() {
333 double[] matrix = new double[6];
334
335 switch (mOrientation) {
336
337 case LANDSCAPE:
338 matrix[0] = 0;
339 matrix[1] = -1;
340 matrix[2] = 1;
341 matrix[3] = 0;
342 matrix[4] = 0;
343 matrix[5] = mPaper.getHeight();
344 break;
345
346 case PORTRAIT:
347 matrix[0] = 1;
348 matrix[1] = 0;
349 matrix[2] = 0;
350 matrix[3] = 1;
351 matrix[4] = 0;
352 matrix[5] = 0;
353 break;
354
355 case REVERSE_LANDSCAPE:
356 matrix[0] = 0;
357 matrix[1] = 1;
358 matrix[2] = -1;
359 matrix[3] = 0;
360 matrix[4] = mPaper.getWidth();
361 matrix[5] = 0;
362 break;
363
364 default:
365 throw new IllegalArgumentException();
366 }
367
368 return matrix;
369 }
370 }
|