Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2013, Oracle and/or its affiliates. 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. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.print; 027 028import static javafx.print.PageOrientation.*; 029 030/** 031 * 032 * A PageLayout encapsulates the information needed to 033 * lay out content. The reported width and height can be 034 * considered equivalent to the clip enforced by a Window. 035 * Applications that obtain a PageLayout instance will 036 * need to inspect the width and height to perform layout and pagination. 037 * Other information such as orientation and the Paper being used 038 * and margins outside of this area are not needed for page rendering. 039 * <p> 040 * Printers usually have hardware margins where they cannot print. 041 * A PageLayout instance obtained from a PrinterJob in the context 042 * of a specific printer will be correctly set up to print over 043 * the whole of that area. If an application adjusts the printable 044 * area outside of this bounds, rendering to those areas will be 045 * clipped by the device. 046 * <p> 047 * Within those hardware margins, the application may define any 048 * printable area it needs. The resulting printable area will 049 * define the effective dimensions 050 * of the page available to the application at printing time. 051 * <p> 052 * Applying a PageLayout configured based on one printer, 053 * to a job on a different printer may not work correctly, 054 * as the second printer may not support the same margins, and may not 055 * even support the same Paper. In such a case, the PageLayout must 056 * be validated against the new printer. 057 * <p> 058 * A PageLayout is immutable. 059 * 060 * @since JavaFX 8 061 */ 062 063public final class PageLayout { 064 065 private PageOrientation orient; 066 private Paper paper; 067 private double lMargin, rMargin, tMargin, bMargin; 068 069 /** 070 * Create a PageLayout using the specified Paper size and orientation. 071 * Default margins are 0.75 inch which is 56 points. 072 * If the paper dimension is smaller than this, 073 * the margins will be reduced. 074 * @param paper the paper to use 075 * @param orient orientation of the layout 076 * @throws IllegalArgumentException if paper or orient is null. 077 */ 078 PageLayout(Paper paper, PageOrientation orient) { 079 this(paper, orient, 56, 56, 56, 56); 080 } 081 082 /** 083 * Note that the margins are to be specified as applying after 084 * the rotation due to the orientation. Thus the left margin 085 * always defines the x origin of the printable area, 086 * and the top margin always defines its y origin. 087 * @param paper the paper to use 088 * @param orient orientation of the layout 089 * @param leftMargin the left margin in points. 090 * @param rightMargin the left margin in points. 091 * @param topMargin the top margin in points. 092 * @param bottomMargin the bottom margin in points. 093 * @throws IllegalArgumentException if the margins exceed the 094 * corresponding paper dimension, or are negative, or if 095 * paper or orient is null. 096 */ 097 PageLayout(Paper paper, PageOrientation orient, 098 double leftMargin, double rightMargin, 099 double topMargin, double bottomMargin) { 100 101 if (paper == null || orient == null || 102 leftMargin < 0 || rightMargin < 0 || 103 topMargin < 0 || bottomMargin < 0) { 104 throw new IllegalArgumentException("Illegal parameters"); 105 } 106 if (orient == PORTRAIT || orient == REVERSE_PORTRAIT) { 107 if (leftMargin+rightMargin > paper.getWidth() || 108 topMargin+bottomMargin > paper.getHeight()) { 109 throw new IllegalArgumentException("Bad margins"); 110 } 111 } else if (leftMargin+rightMargin > paper.getHeight() || 112 topMargin+bottomMargin > paper.getWidth()) { 113 throw new IllegalArgumentException("Bad margins"); 114 } 115 this.paper = paper; 116 this.orient = orient; 117 this.lMargin = leftMargin; 118 this.rMargin = rightMargin; 119 this.tMargin = topMargin; 120 this.bMargin = bottomMargin; 121 } 122 123 public PageOrientation getPageOrientation() { 124 return orient; 125 } 126 127 /** 128 * The paper used. 129 * @return the Paper used for this <code>PageLayout</code>. 130 */ 131 public Paper getPaper() { 132 return paper; 133 } 134 135 /** 136 * Returns the width dimension of the printable area of the page, 137 * in 1/72 of an inch points, taking into account the orientation. 138 * <p> 139 * The printable area is width or height reduced by the 140 * requested margins on each side. If the requested margins 141 * are smaller than the the hardware margins, rendering may 142 * be clipped by the device. 143 * <p> 144 * Since the returned value accounts for orientation, this means if 145 * if the orientation is LANDSCAPE or REVERSE_LANDSCAPE, then 146 * the left and right margins are subtracted from the height of 147 * the underlying paper, since it is rotated 90 degrees. 148 * @return printable width in points. 149 */ 150 public double getPrintableWidth() { 151 double pw = 0; 152 if (orient == PORTRAIT || orient == REVERSE_PORTRAIT) { 153 pw = paper.getWidth(); 154 } else { 155 pw = paper.getHeight(); 156 } 157 pw -= (lMargin+rMargin); 158 if (pw < 0) { 159 pw = 0; 160 } 161 return pw; 162 } 163 164 /** 165 * Returns the height dimension of the printable area of the page, 166 * in 1/72 of an inch, taking into account the orientation. 167 * <p> 168 * The printable area is width or height reduced by the 169 * requested margins on each side. If the requested margins 170 * are smaller than the the hardware margins, rendering may 171 * be clipped by the device. 172 * <p> 173 * Since the returned value accounts for orientation, this means if 174 * if the orientation is LANDSCAPE or REVERSE_LANDSCAPE, then 175 * the top and bottom margins are subtracted from the height of 176 * the underlying paper, since it is rotated 90 degrees. 177 * @return printable height in points. 178 */ 179 public double getPrintableHeight() { 180 double ph = 0; 181 if (orient == PORTRAIT || orient == REVERSE_PORTRAIT) { 182 ph = paper.getHeight(); 183 } else { 184 ph = paper.getWidth(); 185 } 186 ph -= (tMargin+bMargin); 187 if (ph < 0) { 188 ph = 0; 189 } 190 return ph; 191 } 192 193 /** 194 * Returns the left margin of the page layout in points. 195 * This value is in the orientation of the PageLayout. 196 * @return left margin in points. 197 */ 198 public double getLeftMargin() { 199 return lMargin; 200 } 201 202 /** 203 * Returns the right margin of the page layout in points. 204 * This value is in the orientation of the PageLayout. 205 * @return right margin in points. 206 */ 207 public double getRightMargin() { 208 return rMargin; 209 } 210 211 /** 212 * Returns the top margin of the page layout in points. 213 * This value is in the orientation of the PageLayout. 214 * @return top margin in points. 215 */ 216 public double getTopMargin() { 217 return tMargin; 218 } 219 220 /** 221 * Returns the bottom margin of the page layout in points. 222 * This value is in the orientation of the PageLayout. 223 * @return bottom margin in points. 224 */ 225 public double getBottomMargin() { 226 return bMargin; 227 } 228 229 @Override public boolean equals(Object o) { 230 if (o instanceof PageLayout) { 231 PageLayout other = (PageLayout)o; 232 return 233 paper.equals(other.paper) && 234 orient.equals(other.orient) && 235 tMargin == other.tMargin && 236 bMargin == other.bMargin && 237 rMargin == other.rMargin && 238 lMargin == other.lMargin; 239 } else { 240 return false; 241 } 242 } 243 244 @Override public int hashCode() { 245 return paper.hashCode() + orient.hashCode()+ 246 (int)(tMargin+bMargin+lMargin+rMargin); 247 } 248 249 @Override public String toString() { 250 return 251 "Paper="+paper+ 252 " Orient="+orient+ 253 " leftMargin="+lMargin+ 254 " rightMargin="+rMargin+ 255 " topMargin="+tMargin+ 256 " bottomMargin="+bMargin; 257 } 258}