Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/standard/MediaPrintableArea.java
41171 views
/*1* Copyright (c) 2000, 2021, 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*/2425package javax.print.attribute.standard;2627import java.io.Serial;2829import javax.print.DocFlavor;30import javax.print.PrintService;31import javax.print.attribute.Attribute;32import javax.print.attribute.AttributeSet;33import javax.print.attribute.DocAttribute;34import javax.print.attribute.PrintJobAttribute;35import javax.print.attribute.PrintRequestAttribute;3637/**38* Class {@code MediaPrintableArea} is a printing attribute used to distinguish39* the printable and non-printable areas of media.40* <p>41* The printable area is specified to be a rectangle, within the overall42* dimensions of a media.43* <p>44* Most printers cannot print on the entire surface of the media, due to printer45* hardware limitations. This class can be used to query the acceptable values46* for a supposed print job, and to request an area within the constraints of47* the printable area to be used in a print job.48* <p>49* To query for the printable area, a client must supply a suitable context.50* Without specifying at the very least the size of the media being used no51* meaningful value for printable area can be obtained.52* <p>53* The attribute is not described in terms of the distance from the edge of the54* paper, in part to emphasise that this attribute is not independent of a55* particular media, but must be described within the context of a choice of56* other attributes. Additionally it is usually more convenient for a client to57* use the printable area.58* <p>59* The hardware's minimum margins is not just a property of the printer, but may60* be a function of the media size, orientation, media type, and any specified61* finishings. {@code PrintService} provides the method to query the supported62* values of an attribute in a suitable context : See63* {@link PrintService#getSupportedAttributeValues(Class, DocFlavor, AttributeSet)64* PrintService.getSupportedAttributeValues()}65* <p>66* The rectangular printable area is defined thus: The (x,y) origin is67* positioned at the top-left of the paper in portrait mode regardless of the68* orientation specified in the requesting context. For example a printable area69* for A4 paper in portrait or landscape orientation will have height70* {@literal >} width.71* <p>72* A printable area attribute's values are stored internally as integers in73* units of micrometers (µm), where 1 micrometer = 10<SUP>-6</SUP> meter =74* 1/1000 millimeter = 1/25400 inch. This permits dimensions to be represented75* exactly to a precision of 1/1000 mm (= 1 µm) or 1/100 inch (= 25476* µm). If fractional inches are expressed in negative powers of two, this77* permits dimensions to be represented exactly to a precision of 1/8 inch78* (= 3175 µm) but not 1/16 inch (because 1/16 inch does not equal an79* integral number of µm).80* <p>81* <b>IPP Compatibility:</b> MediaPrintableArea is not an IPP attribute.82*/83public final class MediaPrintableArea84implements DocAttribute, PrintRequestAttribute, PrintJobAttribute {8586/**87* Printable {@code x}, {@code y}, {@code width} and {@code height}.88*/89private int x, y, w, h;9091/**92* The units in which the values are expressed.93*/94private int units;9596/**97* Use serialVersionUID from JDK 1.4 for interoperability.98*/99@Serial100private static final long serialVersionUID = -1597171464050795793L;101102/**103* Value to indicate units of inches (in). It is actually the conversion104* factor by which to multiply inches to yield µm (25400).105*/106public static final int INCH = 25400;107108/**109* Value to indicate units of millimeters (mm). It is actually the110* conversion factor by which to multiply mm to yield µm (1000).111*/112public static final int MM = 1000;113114/**115* Constructs a {@code MediaPrintableArea} object from floating point116* values.117*118* @param x printable x119* @param y printable y120* @param w printable width121* @param h printable height122* @param units in which the values are expressed123* @throws IllegalArgumentException if {@code x < 0} or {@code y < 0} or124* {@code w <= 0} or {@code h <= 0} or {@code units < 1}125*/126public MediaPrintableArea(float x, float y, float w, float h, int units) {127if ((x < 0.0) || (y < 0.0) || (w <= 0.0) || (h <= 0.0) ||128(units < 1)) {129throw new IllegalArgumentException("0 or negative value argument");130}131132this.x = (int) (x * units + 0.5f);133this.y = (int) (y * units + 0.5f);134this.w = (int) (w * units + 0.5f);135this.h = (int) (h * units + 0.5f);136137}138139/**140* Constructs a {@code MediaPrintableArea} object from integer values.141*142* @param x printable x143* @param y printable y144* @param w printable width145* @param h printable height146* @param units in which the values are expressed147* @throws IllegalArgumentException if {@code x < 0} or {@code y < 0} or148* {@code w <= 0} or {@code h <= 0} or {@code units < 1}149*/150public MediaPrintableArea(int x, int y, int w, int h, int units) {151if ((x < 0) || (y < 0) || (w <= 0) || (h <= 0) ||152(units < 1)) {153throw new IllegalArgumentException("0 or negative value argument");154}155this.x = x * units;156this.y = y * units;157this.w = w * units;158this.h = h * units;159160}161162/**163* Get the printable area as an array of 4 values in the order164* {@code x, y, w, h}. The values returned are in the given units.165*166* @param units unit conversion factor, e.g. {@link #INCH INCH} or167* {@link #MM MM}168* @return printable area as array of {@code x, y, w, h} in the specified169* units170* @throws IllegalArgumentException if {@code units < 1}171*/172public float[] getPrintableArea(int units) {173return new float[] { getX(units), getY(units),174getWidth(units), getHeight(units) };175}176177/**178* Get the {@code x} location of the origin of the printable area in the179* specified units.180*181* @param units unit conversion factor, e.g. {@link #INCH INCH} or182* {@link #MM MM}183* @return {@code x} location of the origin of the printable area in the184* specified units185* @throws IllegalArgumentException if {@code units < 1}186*/187public float getX(int units) {188return convertFromMicrometers(x, units);189}190191/**192* Get the {@code y} location of the origin of the printable area in the193* specified units.194*195* @param units unit conversion factor, e.g. {@link #INCH INCH} or196* {@link #MM MM}197* @return {@code y} location of the origin of the printable area in the198* specified units199* @throws IllegalArgumentException if {@code units < 1}200*/201public float getY(int units) {202return convertFromMicrometers(y, units);203}204205/**206* Get the {@code width} of the printable area in the specified units.207*208* @param units unit conversion factor, e.g. {@link #INCH INCH} or209* {@link #MM MM}210* @return {@code width} of the printable area in the specified units211* @throws IllegalArgumentException if {@code units < 1}212*/213public float getWidth(int units) {214return convertFromMicrometers(w, units);215}216217/**218* Get the {@code height} of the printable area in the specified units.219*220* @param units unit conversion factor, e.g. {@link #INCH INCH} or221* {@link #MM MM}222* @return {@code height} of the printable area in the specified units223* @throws IllegalArgumentException if {@code units < 1}224*/225public float getHeight(int units) {226return convertFromMicrometers(h, units);227}228229/**230* Returns whether this media margins attribute is equivalent to the passed231* in object. To be equivalent, all of the following conditions must be232* true:233* <ol type=1>234* <li>{@code object} is not {@code null}.235* <li>{@code object} is an instance of class {@code MediaPrintableArea}.236* <li>The origin and dimensions are the same.237* </ol>238*239* @param object {@code Object} to compare to240* @return {@code true} if {@code object} is equivalent to this media241* margins attribute, {@code false} otherwise242*/243public boolean equals(Object object) {244boolean ret = false;245if (object instanceof MediaPrintableArea) {246MediaPrintableArea mm = (MediaPrintableArea)object;247if (x == mm.x && y == mm.y && w == mm.w && h == mm.h) {248ret = true;249}250}251return ret;252}253254/**255* Get the printing attribute class which is to be used as the "category"256* for this printing attribute value.257* <p>258* For class {@code MediaPrintableArea}, the category is class259* {@code MediaPrintableArea} itself.260*261* @return printing attribute class (category), an instance of class262* {@link Class java.lang.Class}263*/264public final Class<? extends Attribute> getCategory() {265return MediaPrintableArea.class;266}267268/**269* Get the name of the category of which this attribute value is an270* instance.271* <p>272* For class {@code MediaPrintableArea}, the category name is273* {@code "media-printable-area"}.274* <p>275* This is not an IPP V1.1 attribute.276*277* @return attribute category name278*/279public final String getName() {280return "media-printable-area";281}282283/**284* Returns a string version of this rectangular size attribute in the given285* units.286*287* @param units unit conversion factor, e.g. {@link #INCH INCH} or288* {@link #MM MM}289* @param unitsName units name string, e.g. {@code "in"} or {@code "mm"}.290* If {@code null}, no units name is appended to the result291* @return string version of this two-dimensional size attribute292* @throws IllegalArgumentException if {@code units < 1}293*/294public String toString(int units, String unitsName) {295if (unitsName == null) {296unitsName = "";297}298float []vals = getPrintableArea(units);299String str = "("+vals[0]+","+vals[1]+")->("+vals[2]+","+vals[3]+")";300return str + unitsName;301}302303/**304* Returns a string version of this rectangular size attribute in mm.305*/306public String toString() {307return(toString(MM, "mm"));308}309310/**311* Returns a hash code value for this attribute.312*/313public int hashCode() {314return x + 37*y + 43*w + 47*h;315}316317/**318* Converts the {@code x} from micrometers to {@code units}.319*320* @param x the value321* @param units unit conversion factor, e.g. {@link #INCH INCH} or322* {@link #MM MM}323* @return the value of {@code x} in the specified units324*/325private static float convertFromMicrometers(int x, int units) {326if (units < 1) {327throw new IllegalArgumentException("units is < 1");328}329return ((float)x) / ((float)units);330}331}332333334