Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/attribute/standard/MediaPrintableArea.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.print.attribute.standard;2526import javax.print.attribute.Attribute;27import javax.print.attribute.DocAttribute;28import javax.print.attribute.PrintJobAttribute;29import javax.print.attribute.PrintRequestAttribute;3031/**32* Class MediaPrintableArea is a printing attribute used to distinguish33* the printable and non-printable areas of media.34* <p>35* The printable area is specified to be a rectangle, within the overall36* dimensions of a media.37* <p>38* Most printers cannot print on the entire surface of the media, due39* to printer hardware limitations. This class can be used to query40* the acceptable values for a supposed print job, and to request an area41* within the constraints of the printable area to be used in a print job.42* <p>43* To query for the printable area, a client must supply a suitable context.44* Without specifying at the very least the size of the media being used45* no meaningful value for printable area can be obtained.46* <p>47* The attribute is not described in terms of the distance from the edge48* of the paper, in part to emphasise that this attribute is not independent49* of a particular media, but must be described within the context of a50* choice of other attributes. Additionally it is usually more convenient51* for a client to use the printable area.52* <p>53* The hardware's minimum margins is not just a property of the printer,54* but may be a function of the media size, orientation, media type, and55* any specified finishings.56* <code>PrintService</code> provides the method to query the supported57* values of an attribute in a suitable context :58* See {@link javax.print.PrintService#getSupportedAttributeValues(Class,DocFlavor, AttributeSet) PrintService.getSupportedAttributeValues()}59* <p>60* The rectangular printable area is defined thus:61* The (x,y) origin is positioned at the top-left of the paper in portrait62* mode regardless of the orientation specified in the requesting context.63* For example a printable area for A4 paper in portrait or landscape64* orientation will have height {@literal >} width.65* <p>66* A printable area attribute's values are stored67* internally as integers in units of micrometers (µm), where 1 micrometer68* = 10<SUP>-6</SUP> meter = 1/1000 millimeter = 1/25400 inch. This permits69* dimensions to be represented exactly to a precision of 1/1000 mm (= 170* µm) or 1/100 inch (= 254 µm). If fractional inches are expressed in7172* negative powers of two, this permits dimensions to be represented exactly to73* a precision of 1/8 inch (= 3175 µm) but not 1/16 inch (because 1/16 inch7475* does not equal an integral number of µm).76* <p>77* <B>IPP Compatibility:</B> MediaPrintableArea is not an IPP attribute.78*/7980public final class MediaPrintableArea81implements DocAttribute, PrintRequestAttribute, PrintJobAttribute {8283private int x, y, w, h;84private int units;8586private static final long serialVersionUID = -1597171464050795793L;8788/**89* Value to indicate units of inches (in). It is actually the conversion90* factor by which to multiply inches to yield µm (25400).91*/92public static final int INCH = 25400;9394/**95* Value to indicate units of millimeters (mm). It is actually the96* conversion factor by which to multiply mm to yield µm (1000).97*/98public static final int MM = 1000;99100/**101* Constructs a MediaPrintableArea object from floating point values.102* @param x printable x103* @param y printable y104* @param w printable width105* @param h printable height106* @param units in which the values are expressed.107*108* @exception IllegalArgumentException109* Thrown if {@code x < 0} or {@code y < 0}110* or {@code w <= 0} or {@code h <= 0} or111* {@code units < 1}.112*/113public MediaPrintableArea(float x, float y, float w, float h, int units) {114if ((x < 0.0) || (y < 0.0) || (w <= 0.0) || (h <= 0.0) ||115(units < 1)) {116throw new IllegalArgumentException("0 or negative value argument");117}118119this.x = (int) (x * units + 0.5f);120this.y = (int) (y * units + 0.5f);121this.w = (int) (w * units + 0.5f);122this.h = (int) (h * units + 0.5f);123124}125126/**127* Constructs a MediaPrintableArea object from integer values.128* @param x printable x129* @param y printable y130* @param w printable width131* @param h printable height132* @param units in which the values are expressed.133*134* @exception IllegalArgumentException135* Thrown if {@code x < 0} or {@code y < 0}136* or {@code w <= 0} or {@code h <= 0} or137* {@code units < 1}.138*/139public MediaPrintableArea(int x, int y, int w, int h, int units) {140if ((x < 0) || (y < 0) || (w <= 0) || (h <= 0) ||141(units < 1)) {142throw new IllegalArgumentException("0 or negative value argument");143}144this.x = x * units;145this.y = y * units;146this.w = w * units;147this.h = h * units;148149}150151/**152* Get the printable area as an array of 4 values in the order153* x, y, w, h. The values returned are in the given units.154* @param units155* Unit conversion factor, e.g. {@link #INCH INCH} or156* {@link #MM MM}.157*158* @return printable area as array of x, y, w, h in the specified units.159*160* @exception IllegalArgumentException161* (unchecked exception) Thrown if {@code units < 1}.162*/163public float[] getPrintableArea(int units) {164return new float[] { getX(units), getY(units),165getWidth(units), getHeight(units) };166}167168/**169* Get the x location of the origin of the printable area in the170* specified units.171* @param units172* Unit conversion factor, e.g. {@link #INCH INCH} or173* {@link #MM MM}.174*175* @return x location of the origin of the printable area in the176* specified units.177*178* @exception IllegalArgumentException179* (unchecked exception) Thrown if {@code units < 1}.180*/181public float getX(int units) {182return convertFromMicrometers(x, units);183}184185/**186* Get the y location of the origin of the printable area in the187* specified units.188* @param units189* Unit conversion factor, e.g. {@link #INCH INCH} or190* {@link #MM MM}.191*192* @return y location of the origin of the printable area in the193* specified units.194*195* @exception IllegalArgumentException196* (unchecked exception) Thrown if {@code units < 1}.197*/198public float getY(int units) {199return convertFromMicrometers(y, units);200}201202/**203* Get the width of the printable area in the specified units.204* @param units205* Unit conversion factor, e.g. {@link #INCH INCH} or206* {@link #MM MM}.207*208* @return width of the printable area in the specified units.209*210* @exception IllegalArgumentException211* (unchecked exception) Thrown if {@code units < 1}.212*/213public float getWidth(int units) {214return convertFromMicrometers(w, units);215}216217/**218* Get the height of the printable area in the specified units.219* @param units220* Unit conversion factor, e.g. {@link #INCH INCH} or221* {@link #MM MM}.222*223* @return height of the printable area in the specified units.224*225* @exception IllegalArgumentException226* (unchecked exception) Thrown if {@code units < 1}.227*/228public float getHeight(int units) {229return convertFromMicrometers(h, units);230}231232/**233* Returns whether this media margins attribute is equivalent to the passed234* in object.235* To be equivalent, all of the following conditions must be true:236* <OL TYPE=1>237* <LI>238* <CODE>object</CODE> is not null.239* <LI>240* <CODE>object</CODE> is an instance of class MediaPrintableArea.241* <LI>242* The origin and dimensions are the same.243* </OL>244*245* @param object Object to compare to.246*247* @return True if <CODE>object</CODE> is equivalent to this media margins248* attribute, false otherwise.249*/250public boolean equals(Object object) {251boolean ret = false;252if (object instanceof MediaPrintableArea) {253MediaPrintableArea mm = (MediaPrintableArea)object;254if (x == mm.x && y == mm.y && w == mm.w && h == mm.h) {255ret = true;256}257}258return ret;259}260261/**262* Get the printing attribute class which is to be used as the "category"263* for this printing attribute value.264* <P>265* For class MediaPrintableArea, the category is266* class MediaPrintableArea itself.267*268* @return Printing attribute class (category), an instance of class269* {@link java.lang.Class java.lang.Class}.270*/271public final Class<? extends Attribute> getCategory() {272return MediaPrintableArea.class;273}274275/**276* Get the name of the category of which this attribute value is an277* instance.278* <P>279* For class MediaPrintableArea,280* the category name is <CODE>"media-printable-area"</CODE>.281* <p>This is not an IPP V1.1 attribute.282*283* @return Attribute category name.284*/285public final String getName() {286return "media-printable-area";287}288289/**290* Returns a string version of this rectangular size attribute in the291* given units.292*293* @param units294* Unit conversion factor, e.g. {@link #INCH INCH} or295* {@link #MM MM}.296* @param unitsName297* Units name string, e.g. <CODE>"in"</CODE> or <CODE>"mm"</CODE>. If298* null, no units name is appended to the result.299*300* @return String version of this two-dimensional size attribute.301*302* @exception IllegalArgumentException303* (unchecked exception) Thrown if {@code units < 1}.304*/305public String toString(int units, String unitsName) {306if (unitsName == null) {307unitsName = "";308}309float []vals = getPrintableArea(units);310String str = "("+vals[0]+","+vals[1]+")->("+vals[2]+","+vals[3]+")";311return str + unitsName;312}313314/**315* Returns a string version of this rectangular size attribute in mm.316*/317public String toString() {318return(toString(MM, "mm"));319}320321/**322* Returns a hash code value for this attribute.323*/324public int hashCode() {325return x + 37*y + 43*w + 47*h;326}327328private static float convertFromMicrometers(int x, int units) {329if (units < 1) {330throw new IllegalArgumentException("units is < 1");331}332return ((float)x) / ((float)units);333}334}335336337