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.Paper.Units.*;
029
030/**
031 * A class which encapsulates the size of paper media as used by printers.
032 * <p>
033 * The paper sizes which are enumerated for a printer is the authoritative
034 * source of Paper sizes that may be selected for printing on that printer.
035 * <p>
036 * However for convenience, this class pre-defines some of the most
037 * common paper sizes so that an application may easily set up default
038 * parameters from code, eg by referring to <code>StandardPaper.A4</code>
039 * <p>
040 * The application is not limited to these sizes as it can create
041 * a Paper of any size, but in either case, it is up to the application
042 * to verify at run time that a particular size is supported by a device.
043 * <p>
044 * @since JavaFX 8
045 */
046
047public final class Paper {
048
049    /**
050     * An enumeration used to specify the Units used for Paper measurement
051     * @since JavaFX 8
052     */
053    public static enum Units {
054        /**
055         * Specify Paper size in millimetres.
056         */
057        MM,
058        /**
059         * Specify Paper size in inches.
060         */
061        INCH,
062        /**
063         * Specify Paper size in points (1/72 of an inch).
064         */
065        POINT
066    }
067
068    private String name;
069    private double width, height;
070    private Units units;
071
072    Paper(String paperName,
073          double paperWidth, double paperHeight, Units units)
074
075        throws IllegalArgumentException {
076
077        if (paperWidth <= 0 || paperHeight <= 0) {
078            throw new IllegalArgumentException("Illegal dimension");
079        }
080        if (paperName == null) {
081            throw new IllegalArgumentException("Null name");
082
083        }
084        name = paperName;
085        width = paperWidth;
086        height = paperHeight;
087        this.units = units;
088    }
089
090     /** Get the paper name.
091     * This may not be directly useful for user display as it is not localized.
092     */
093    public final String getName() {
094        return name;
095    }
096
097    /**
098     * Translate the internally stored dimension into points.
099     */
100    private double getSizeInPoints(double dim) {
101        switch (units) {
102        case POINT : return (int)(dim+0.5);
103        case INCH  : return (int)((dim * 72) + 0.5);
104        case MM    : return (int)(((dim * 72) / 25.4) + 0.5);
105        }
106        return dim;
107    }
108
109    /**
110     * Get the width of the paper in points (1/72 inch)
111     */
112    public final double getWidth() {
113        return getSizeInPoints(width);
114    }
115
116    /**
117     * Get the height of the paper in points (1/72 inch)
118     */
119    public final double getHeight() {
120        return getSizeInPoints(height);
121    }
122
123    @Override
124    public final int hashCode() {
125        return (int)width+((int)height<<16)+units.hashCode();
126    }
127
128    @Override
129    public final boolean equals(Object o) {
130        return (o != null &&
131                o instanceof Paper &&
132                this.name.equals(((Paper)o).name) &&
133                this.width == (((Paper)o).width) &&
134                this.height == (((Paper)o).height) &&
135                this.units == (((Paper)o).units));
136    }
137
138    @Override
139    public final String toString() {
140        return "Paper: " + name+" size="+width+"x"+height+" " + units;
141    }
142
143   /**
144     * Specifies the ISO A0 size, 841 mm by 1189 mm.
145     */
146    public static final Paper A0 = new Paper("A0", 841, 1189, MM);
147
148    /**
149     * Specifies the ISO A1 size, 594 mm by 841 mm.
150     */
151    public static final Paper A1 = new Paper("A1", 594, 841, MM);
152
153    /**
154     * Specifies the ISO A2 size, 420 mm by 594 mm.
155     */
156
157    public static final Paper A2 = new Paper("A2", 420, 594, MM);
158
159    /**
160     * Specifies the ISO A3 size, 297 mm by 420 mm.
161     */
162    public static final Paper A3 = new Paper("A3", 297, 420, MM);
163
164    /**
165     * Specifies the ISO A4 size, 210 mm by 297 mm.
166     */
167    public static final Paper A4 = new Paper("A4", 210, 297, MM);
168
169    /**
170     * Specifies the ISO A5 size, 148 mm by 210 mm.
171     */
172    public static final Paper A5 = new Paper("A5", 148, 210, MM);
173
174    /**
175     * Specifies the ISO A6 size, 105 mm by 148 mm.
176     */
177    public static final Paper A6 = new Paper("A6", 105, 148, MM);
178
179    /**
180     * Specifies the ISO Designated Long size, 110 mm by 220 mm.
181     */
182    public static final Paper
183        DESIGNATED_LONG = new Paper("Designated Long", 110, 220, MM);
184
185    /**
186     *Specifies the North American letter size, 8.5 inches by 11 inches
187     */
188    public static final Paper NA_LETTER = new Paper("Letter", 8.5, 11, INCH);
189
190    /**
191     * Specifies the North American legal size, 8.5 inches by 14 inches.
192     */
193    public static final Paper LEGAL = new Paper("Legal", 8.4, 14, INCH);
194
195    /**
196     * Specifies the tabloid size, 11 inches by 17 inches.
197     */
198    public static final Paper TABLOID = new Paper("Tabloid", 11.0, 17.0, INCH);
199
200    /**
201     * Specifies the executive size, 7.25 inches by 10.5 inches.
202     */
203    public static final Paper
204        EXECUTIVE = new Paper("Executive", 7.25, 10.5, INCH);
205
206    /**
207     * Specifies the North American 8 inch by 10 inch paper.
208     */
209    public static final Paper NA_8X10 = new Paper("8x10", 8, 10, INCH);
210
211    /**
212     * Specifies the Monarch envelope size, 3.87 inch by 7.5 inch.
213     */
214    public static final Paper
215        MONARCH_ENVELOPE = new Paper("Monarch Envelope", 3.87, 7.5, INCH);
216    /**
217     * Specifies the North American Number 10 business envelope size,
218     * 4.125 inches by 9.5 inches.
219     */
220    public static final Paper
221        NA_NUMBER_10_ENVELOPE = new Paper("Number 10 Envelope",
222                                          4.125, 9.5, INCH);
223     /**
224      * Specifies the engineering C size, 17 inch by 22 inch.
225      */
226    public static final Paper C = new Paper("C", 17.0, 22.0, INCH);
227
228    /**
229     * Specifies the JIS B4 size, 257 mm by 364 mm.
230     */
231    public static final Paper JIS_B4 = new Paper("B4", 257, 364, MM);
232
233    /**
234     * Specifies the JIS B5 size, 182 mm by 257 mm.
235     */
236
237    public static final Paper JIS_B5 = new Paper("B5", 182, 257, MM);
238
239    /**
240     * Specifies the JIS B6 size, 128 mm by 182 mm.
241     */
242    public static final Paper JIS_B6 = new Paper("B6", 128, 182, MM);
243
244    /**
245     * Specifies the Japanese postcard size, 100 mm by 148 mm.
246     */
247    public static final Paper
248        JAPANESE_POSTCARD = new Paper("Japanese Postcard", 100, 148, MM);
249
250}
251
252