1. /*
  2. * @(#)PageFormat.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 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>.
  201. * @return a copy of the <code>Paper</code> object associated
  202. * with this <code>PageFormat</code>.
  203. */
  204. public Paper getPaper() {
  205. return (Paper)mPaper.clone();
  206. }
  207. /**
  208. * Sets the <code>Paper</code> object for this
  209. * <code>PageFormat</code>.
  210. * @param paper the <code>Paper</code> object to which to set
  211. * the <code>Paper</code> object for this <code>PageFormat</code>.
  212. */
  213. public void setPaper(Paper paper) {
  214. mPaper = paper;
  215. }
  216. /**
  217. * Sets the page orientation. <code>orientation</code> must be
  218. * one of the constants: PORTRAIT, LANDSCAPE,
  219. * or REVERSE_LANDSCAPE.
  220. * @param orientation the new orientation for the page
  221. * @exception <code>IllegalArgumentException</code>
  222. * an unknown orientation was requested
  223. */
  224. public void setOrientation(int orientation) throws IllegalArgumentException
  225. {
  226. if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
  227. mOrientation = orientation;
  228. } else {
  229. throw new IllegalArgumentException();
  230. }
  231. }
  232. /**
  233. * Returns the orientation of this <code>PageFormat</code>.
  234. * @return this <code>PageFormat</code> object's orientation.
  235. */
  236. public int getOrientation() {
  237. return mOrientation;
  238. }
  239. /**
  240. * Returns a transformation matrix that translates user
  241. * space rendering to the requested orientation
  242. * of the page. The values are placed into the
  243. * array as
  244. * { m00, m10, m01, m11, m02, m12} in
  245. * the form required by the {@link AffineTransform}
  246. * constructor.
  247. * @return the matrix used to translate user space rendering
  248. * to the orientation of the page.
  249. * @see java.awt.geom.AffineTransform
  250. */
  251. public double[] getMatrix() {
  252. double[] matrix = new double[6];
  253. switch (mOrientation) {
  254. case LANDSCAPE:
  255. matrix[0] = 0; matrix[1] = -1;
  256. matrix[2] = 1; matrix[3] = 0;
  257. matrix[4] = 0; matrix[5] = mPaper.getHeight();
  258. break;
  259. case PORTRAIT:
  260. matrix[0] = 1; matrix[1] = 0;
  261. matrix[2] = 0; matrix[3] = 1;
  262. matrix[4] = 0; matrix[5] = 0;
  263. break;
  264. case REVERSE_LANDSCAPE:
  265. matrix[0] = 0; matrix[1] = 1;
  266. matrix[2] = -1; matrix[3] = 0;
  267. matrix[4] = 0; matrix[5] = 0;
  268. break;
  269. default:
  270. throw new IllegalArgumentException();
  271. }
  272. return matrix;
  273. }
  274. }