Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/standard/MediaPrintableArea.java
41171 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.print.attribute.standard;
27
28
import java.io.Serial;
29
30
import javax.print.DocFlavor;
31
import javax.print.PrintService;
32
import javax.print.attribute.Attribute;
33
import javax.print.attribute.AttributeSet;
34
import javax.print.attribute.DocAttribute;
35
import javax.print.attribute.PrintJobAttribute;
36
import javax.print.attribute.PrintRequestAttribute;
37
38
/**
39
* Class {@code MediaPrintableArea} is a printing attribute used to distinguish
40
* the printable and non-printable areas of media.
41
* <p>
42
* The printable area is specified to be a rectangle, within the overall
43
* dimensions of a media.
44
* <p>
45
* Most printers cannot print on the entire surface of the media, due to printer
46
* hardware limitations. This class can be used to query the acceptable values
47
* for a supposed print job, and to request an area within the constraints of
48
* the printable area to be used in a print job.
49
* <p>
50
* To query for the printable area, a client must supply a suitable context.
51
* Without specifying at the very least the size of the media being used no
52
* meaningful value for printable area can be obtained.
53
* <p>
54
* The attribute is not described in terms of the distance from the edge of the
55
* paper, in part to emphasise that this attribute is not independent of a
56
* particular media, but must be described within the context of a choice of
57
* other attributes. Additionally it is usually more convenient for a client to
58
* use the printable area.
59
* <p>
60
* The hardware's minimum margins is not just a property of the printer, but may
61
* be a function of the media size, orientation, media type, and any specified
62
* finishings. {@code PrintService} provides the method to query the supported
63
* values of an attribute in a suitable context : See
64
* {@link PrintService#getSupportedAttributeValues(Class, DocFlavor, AttributeSet)
65
* PrintService.getSupportedAttributeValues()}
66
* <p>
67
* The rectangular printable area is defined thus: The (x,y) origin is
68
* positioned at the top-left of the paper in portrait mode regardless of the
69
* orientation specified in the requesting context. For example a printable area
70
* for A4 paper in portrait or landscape orientation will have height
71
* {@literal >} width.
72
* <p>
73
* A printable area attribute's values are stored internally as integers in
74
* units of micrometers (&#181;m), where 1 micrometer = 10<SUP>-6</SUP> meter =
75
* 1/1000 millimeter = 1/25400 inch. This permits dimensions to be represented
76
* exactly to a precision of 1/1000 mm (= 1 &#181;m) or 1/100 inch (= 254
77
* &#181;m). If fractional inches are expressed in negative powers of two, this
78
* permits dimensions to be represented exactly to a precision of 1/8 inch
79
* (= 3175 &#181;m) but not 1/16 inch (because 1/16 inch does not equal an
80
* integral number of &#181;m).
81
* <p>
82
* <b>IPP Compatibility:</b> MediaPrintableArea is not an IPP attribute.
83
*/
84
public final class MediaPrintableArea
85
implements DocAttribute, PrintRequestAttribute, PrintJobAttribute {
86
87
/**
88
* Printable {@code x}, {@code y}, {@code width} and {@code height}.
89
*/
90
private int x, y, w, h;
91
92
/**
93
* The units in which the values are expressed.
94
*/
95
private int units;
96
97
/**
98
* Use serialVersionUID from JDK 1.4 for interoperability.
99
*/
100
@Serial
101
private static final long serialVersionUID = -1597171464050795793L;
102
103
/**
104
* Value to indicate units of inches (in). It is actually the conversion
105
* factor by which to multiply inches to yield &#181;m (25400).
106
*/
107
public static final int INCH = 25400;
108
109
/**
110
* Value to indicate units of millimeters (mm). It is actually the
111
* conversion factor by which to multiply mm to yield &#181;m (1000).
112
*/
113
public static final int MM = 1000;
114
115
/**
116
* Constructs a {@code MediaPrintableArea} object from floating point
117
* values.
118
*
119
* @param x printable x
120
* @param y printable y
121
* @param w printable width
122
* @param h printable height
123
* @param units in which the values are expressed
124
* @throws IllegalArgumentException if {@code x < 0} or {@code y < 0} or
125
* {@code w <= 0} or {@code h <= 0} or {@code units < 1}
126
*/
127
public MediaPrintableArea(float x, float y, float w, float h, int units) {
128
if ((x < 0.0) || (y < 0.0) || (w <= 0.0) || (h <= 0.0) ||
129
(units < 1)) {
130
throw new IllegalArgumentException("0 or negative value argument");
131
}
132
133
this.x = (int) (x * units + 0.5f);
134
this.y = (int) (y * units + 0.5f);
135
this.w = (int) (w * units + 0.5f);
136
this.h = (int) (h * units + 0.5f);
137
138
}
139
140
/**
141
* Constructs a {@code MediaPrintableArea} object from integer values.
142
*
143
* @param x printable x
144
* @param y printable y
145
* @param w printable width
146
* @param h printable height
147
* @param units in which the values are expressed
148
* @throws IllegalArgumentException if {@code x < 0} or {@code y < 0} or
149
* {@code w <= 0} or {@code h <= 0} or {@code units < 1}
150
*/
151
public MediaPrintableArea(int x, int y, int w, int h, int units) {
152
if ((x < 0) || (y < 0) || (w <= 0) || (h <= 0) ||
153
(units < 1)) {
154
throw new IllegalArgumentException("0 or negative value argument");
155
}
156
this.x = x * units;
157
this.y = y * units;
158
this.w = w * units;
159
this.h = h * units;
160
161
}
162
163
/**
164
* Get the printable area as an array of 4 values in the order
165
* {@code x, y, w, h}. The values returned are in the given units.
166
*
167
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
168
* {@link #MM MM}
169
* @return printable area as array of {@code x, y, w, h} in the specified
170
* units
171
* @throws IllegalArgumentException if {@code units < 1}
172
*/
173
public float[] getPrintableArea(int units) {
174
return new float[] { getX(units), getY(units),
175
getWidth(units), getHeight(units) };
176
}
177
178
/**
179
* Get the {@code x} location of the origin of the printable area in the
180
* specified units.
181
*
182
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
183
* {@link #MM MM}
184
* @return {@code x} location of the origin of the printable area in the
185
* specified units
186
* @throws IllegalArgumentException if {@code units < 1}
187
*/
188
public float getX(int units) {
189
return convertFromMicrometers(x, units);
190
}
191
192
/**
193
* Get the {@code y} location of the origin of the printable area in the
194
* specified units.
195
*
196
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
197
* {@link #MM MM}
198
* @return {@code y} location of the origin of the printable area in the
199
* specified units
200
* @throws IllegalArgumentException if {@code units < 1}
201
*/
202
public float getY(int units) {
203
return convertFromMicrometers(y, units);
204
}
205
206
/**
207
* Get the {@code width} of the printable area in the specified units.
208
*
209
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
210
* {@link #MM MM}
211
* @return {@code width} of the printable area in the specified units
212
* @throws IllegalArgumentException if {@code units < 1}
213
*/
214
public float getWidth(int units) {
215
return convertFromMicrometers(w, units);
216
}
217
218
/**
219
* Get the {@code height} of the printable area in the specified units.
220
*
221
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
222
* {@link #MM MM}
223
* @return {@code height} of the printable area in the specified units
224
* @throws IllegalArgumentException if {@code units < 1}
225
*/
226
public float getHeight(int units) {
227
return convertFromMicrometers(h, units);
228
}
229
230
/**
231
* Returns whether this media margins attribute is equivalent to the passed
232
* in object. To be equivalent, all of the following conditions must be
233
* true:
234
* <ol type=1>
235
* <li>{@code object} is not {@code null}.
236
* <li>{@code object} is an instance of class {@code MediaPrintableArea}.
237
* <li>The origin and dimensions are the same.
238
* </ol>
239
*
240
* @param object {@code Object} to compare to
241
* @return {@code true} if {@code object} is equivalent to this media
242
* margins attribute, {@code false} otherwise
243
*/
244
public boolean equals(Object object) {
245
boolean ret = false;
246
if (object instanceof MediaPrintableArea) {
247
MediaPrintableArea mm = (MediaPrintableArea)object;
248
if (x == mm.x && y == mm.y && w == mm.w && h == mm.h) {
249
ret = true;
250
}
251
}
252
return ret;
253
}
254
255
/**
256
* Get the printing attribute class which is to be used as the "category"
257
* for this printing attribute value.
258
* <p>
259
* For class {@code MediaPrintableArea}, the category is class
260
* {@code MediaPrintableArea} itself.
261
*
262
* @return printing attribute class (category), an instance of class
263
* {@link Class java.lang.Class}
264
*/
265
public final Class<? extends Attribute> getCategory() {
266
return MediaPrintableArea.class;
267
}
268
269
/**
270
* Get the name of the category of which this attribute value is an
271
* instance.
272
* <p>
273
* For class {@code MediaPrintableArea}, the category name is
274
* {@code "media-printable-area"}.
275
* <p>
276
* This is not an IPP V1.1 attribute.
277
*
278
* @return attribute category name
279
*/
280
public final String getName() {
281
return "media-printable-area";
282
}
283
284
/**
285
* Returns a string version of this rectangular size attribute in the given
286
* units.
287
*
288
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
289
* {@link #MM MM}
290
* @param unitsName units name string, e.g. {@code "in"} or {@code "mm"}.
291
* If {@code null}, no units name is appended to the result
292
* @return string version of this two-dimensional size attribute
293
* @throws IllegalArgumentException if {@code units < 1}
294
*/
295
public String toString(int units, String unitsName) {
296
if (unitsName == null) {
297
unitsName = "";
298
}
299
float []vals = getPrintableArea(units);
300
String str = "("+vals[0]+","+vals[1]+")->("+vals[2]+","+vals[3]+")";
301
return str + unitsName;
302
}
303
304
/**
305
* Returns a string version of this rectangular size attribute in mm.
306
*/
307
public String toString() {
308
return(toString(MM, "mm"));
309
}
310
311
/**
312
* Returns a hash code value for this attribute.
313
*/
314
public int hashCode() {
315
return x + 37*y + 43*w + 47*h;
316
}
317
318
/**
319
* Converts the {@code x} from micrometers to {@code units}.
320
*
321
* @param x the value
322
* @param units unit conversion factor, e.g. {@link #INCH INCH} or
323
* {@link #MM MM}
324
* @return the value of {@code x} in the specified units
325
*/
326
private static float convertFromMicrometers(int x, int units) {
327
if (units < 1) {
328
throw new IllegalArgumentException("units is < 1");
329
}
330
return ((float)x) / ((float)units);
331
}
332
}
333
334