1. /*
  2. * @(#)PageFormat.java 1.21 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.print;
  8. import java.awt.geom.AffineTransform;
  9. import java.awt.geom.Point2D;
  10. import java.awt.geom.Rectangle2D;
  11. /**
  12. * The <code>PageFormat</code> class describes the size and
  13. * orientation of a page to be printed.
  14. */
  15. public class PageFormat implements Cloneable
  16. {
  17. /* Class Constants */
  18. /**
  19. * The origin is at the bottom left of the paper with
  20. * x running bottom to top and y running left to right.
  21. * Note that this is not the Macintosh landscape but
  22. * is the Window's and PostScript landscape.
  23. */
  24. public static final int LANDSCAPE = 0;
  25. /**
  26. * The origin is at the top left of the paper with
  27. * x running to the right and y running down the
  28. * paper.
  29. */
  30. public static final int PORTRAIT = 1;
  31. /**
  32. * The origin is at the top right of the paper with x
  33. * running top to bottom and y running right to left.
  34. * Note that this is the Macintosh landscape.
  35. */
  36. public static final int REVERSE_LANDSCAPE = 2;
  37. /* Instance Variables */
  38. /**
  39. * A description of the physical piece of paper.
  40. */
  41. private Paper mPaper;
  42. /**
  43. * The orientation of the current page. This will be
  44. * one of the constants: PORTRIAT, LANDSCAPE, or
  45. * REVERSE_LANDSCAPE,
  46. */
  47. private int mOrientation = PORTRAIT;
  48. /* Constructors */
  49. /**
  50. * Creates a default, portrait-oriented
  51. * <code>PageFormat</code>.
  52. */
  53. public PageFormat()
  54. {
  55. mPaper = new Paper();
  56. }
  57. /* Instance Methods */
  58. /**
  59. * Makes a copy of this <code>PageFormat</code> with the same
  60. * contents as this <code>PageFormat</code>.
  61. * @return a copy of this <code>PageFormat</code>.
  62. */
  63. public Object clone() {
  64. PageFormat newPage;
  65. try {
  66. newPage = (PageFormat) super.clone();
  67. newPage.mPaper = (Paper)mPaper.clone();
  68. } catch (CloneNotSupportedException e) {
  69. e.printStackTrace();
  70. newPage = null; // should never happen.
  71. }
  72. return newPage;
  73. }
  74. /**
  75. * Returns the width, in 1/72nds of an inch, of the page.
  76. * This method takes into account the orientation of the
  77. * page when determining the width.
  78. * @return the width of the page.
  79. */
  80. public double getWidth() {
  81. double width;
  82. int orientation = getOrientation();
  83. if (orientation == PORTRAIT) {
  84. width = mPaper.getWidth();
  85. } else {
  86. width = mPaper.getHeight();
  87. }
  88. return width;
  89. }
  90. /**
  91. * Returns the height, in 1/72nds of an inch, of the page.
  92. * This method takes into account the orientation of the
  93. * page when determining the height.
  94. * @return the height of the page.
  95. */
  96. public double getHeight() {
  97. double height;
  98. int orientation = getOrientation();
  99. if (orientation == PORTRAIT) {
  100. height = mPaper.getHeight();
  101. } else {
  102. height = mPaper.getWidth();
  103. }
  104. return height;
  105. }
  106. /**
  107. * Returns the x coordinate of the upper left point of the
  108. * imageable area of the <code>Paper</code> object
  109. * associated with this <code>PageFormat</code>.
  110. * This method takes into account the
  111. * orientation of the page.
  112. * @return the x coordinate of the upper left point of the
  113. * imageable area of the <code>Paper</code> object
  114. * associated with this <code>PageFormat</code>.
  115. */
  116. public double getImageableX() {
  117. double x;
  118. switch (getOrientation()) {
  119. case LANDSCAPE:
  120. x = mPaper.getHeight()
  121. - (mPaper.getImageableY() + mPaper.getImageableHeight());
  122. break;
  123. case PORTRAIT:
  124. x = mPaper.getImageableX();
  125. break;
  126. case REVERSE_LANDSCAPE:
  127. x = mPaper.getImageableY();
  128. break;
  129. default:
  130. /* This should never happen since it signifies that the
  131. * PageFormat is in an invalid orientation.
  132. */
  133. throw new InternalError("unrecognized orientation");
  134. }
  135. return x;
  136. }
  137. /**
  138. * Returns the y coordinate of the upper left point of the
  139. * imageable area of the <code>Paper</code> object
  140. * associated with this <code>PageFormat</code>.
  141. * This method takes into account the
  142. * orientation of the page.
  143. * @return the y coordinate of the upper left point of the
  144. * imageable area of the <code>Paper</code> object
  145. * associated with this <code>PageFormat</code>.
  146. */
  147. public double getImageableY() {
  148. double y;
  149. switch (getOrientation()) {
  150. case LANDSCAPE:
  151. y = mPaper.getImageableX();
  152. break;
  153. case PORTRAIT:
  154. y = mPaper.getImageableY();
  155. break;
  156. case REVERSE_LANDSCAPE:
  157. y = mPaper.getWidth()
  158. - (mPaper.getImageableX() + mPaper.getImageableWidth());
  159. break;
  160. default:
  161. /* This should never happen since it signifies that the
  162. * PageFormat is in an invalid orientation.
  163. */
  164. throw new InternalError("unrecognized orientation");
  165. }
  166. return y;
  167. }
  168. /**
  169. * Returns the width, in 1/72nds of an inch, of the imageable
  170. * area of the page. This method takes into account the orientation
  171. * of the page.
  172. * @return the width of the page.
  173. */
  174. public double getImageableWidth() {
  175. double width;
  176. if (getOrientation() == PORTRAIT) {
  177. width = mPaper.getImageableWidth();
  178. } else {
  179. width = mPaper.getImageableHeight();
  180. }
  181. return width;
  182. }
  183. /**
  184. * Return the height, in 1/72nds of an inch, of the imageable
  185. * area of the page. This method takes into account the orientation
  186. * of the page.
  187. * @return the height of the page.
  188. */
  189. public double getImageableHeight() {
  190. double height;
  191. if (getOrientation() == PORTRAIT) {
  192. height = mPaper.getImageableHeight();
  193. } else {
  194. height = mPaper.getImageableWidth();
  195. }
  196. return height;
  197. }
  198. /**
  199. * Returns a copy of the {@link Paper} object associated
  200. * with this <code>PageFormat</code>. Changes made to the
  201. * <code>Paper</code> object returned from this method do not
  202. * affect the <code>Paper</code> object of this
  203. * <code>PageFormat</code>. To update the <code>Paper</code>
  204. * object of this <code>PageFormat</code>, create a new
  205. * <code>Paper</code> object and set it into this
  206. * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
  207. * method.
  208. * @return a copy of the <code>Paper</code> object associated
  209. * with this <code>PageFormat</code>.
  210. * @see #setPaper
  211. */
  212. public Paper getPaper() {
  213. return (Paper)mPaper.clone();
  214. }
  215. /**
  216. * Sets the <code>Paper</code> object for this
  217. * <code>PageFormat</code>.
  218. * @param paper the <code>Paper</code> object to which to set
  219. * the <code>Paper</code> object for this <code>PageFormat</code>.
  220. * @exception <code>NullPointerException</code>
  221. * a null paper instance was passed as a parameter.
  222. * @see #getPaper
  223. */
  224. public void setPaper(Paper paper) {
  225. mPaper = (Paper)paper.clone();
  226. }
  227. /**
  228. * Sets the page orientation. <code>orientation</code> must be
  229. * one of the constants: PORTRAIT, LANDSCAPE,
  230. * or REVERSE_LANDSCAPE.
  231. * @param orientation the new orientation for the page
  232. * @throws IllegalArgumentException if
  233. * an unknown orientation was requested
  234. * @see #getOrientation
  235. */
  236. public void setOrientation(int orientation) throws IllegalArgumentException
  237. {
  238. if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
  239. mOrientation = orientation;
  240. } else {
  241. throw new IllegalArgumentException();
  242. }
  243. }
  244. /**
  245. * Returns the orientation of this <code>PageFormat</code>.
  246. * @return this <code>PageFormat</code> object's orientation.
  247. * @see #setOrientation
  248. */
  249. public int getOrientation() {
  250. return mOrientation;
  251. }
  252. /**
  253. * Returns a transformation matrix that translates user
  254. * space rendering to the requested orientation
  255. * of the page. The values are placed into the
  256. * array as
  257. * { m00, m10, m01, m11, m02, m12} in
  258. * the form required by the {@link AffineTransform}
  259. * constructor.
  260. * @return the matrix used to translate user space rendering
  261. * to the orientation of the page.
  262. * @see java.awt.geom.AffineTransform
  263. */
  264. public double[] getMatrix() {
  265. double[] matrix = new double[6];
  266. switch (mOrientation) {
  267. case LANDSCAPE:
  268. matrix[0] = 0; matrix[1] = -1;
  269. matrix[2] = 1; matrix[3] = 0;
  270. matrix[4] = 0; matrix[5] = mPaper.getHeight();
  271. break;
  272. case PORTRAIT:
  273. matrix[0] = 1; matrix[1] = 0;
  274. matrix[2] = 0; matrix[3] = 1;
  275. matrix[4] = 0; matrix[5] = 0;
  276. break;
  277. case REVERSE_LANDSCAPE:
  278. matrix[0] = 0; matrix[1] = 1;
  279. matrix[2] = -1; matrix[3] = 0;
  280. matrix[4] = mPaper.getWidth(); matrix[5] = 0;
  281. break;
  282. default:
  283. throw new IllegalArgumentException();
  284. }
  285. return matrix;
  286. }
  287. }