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