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/ResolutionSyntax.java
41159 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;
27
28
import java.io.Serial;
29
import java.io.Serializable;
30
31
/**
32
* Class {@code ResolutionSyntax} is an abstract base class providing the common
33
* implementation of all attributes denoting a printer resolution.
34
* <p>
35
* A resolution attribute's value consists of two items, the cross feed
36
* direction resolution and the feed direction resolution. A resolution
37
* attribute may be constructed by supplying the two values and indicating the
38
* units in which the values are measured. Methods are provided to return a
39
* resolution attribute's values, indicating the units in which the values are
40
* to be returned. The two most common resolution units are dots per inch (dpi)
41
* and dots per centimeter (dpcm), and exported constants {@link #DPI DPI} and
42
* {@link #DPCM DPCM} are provided for indicating those units.
43
* <p>
44
* Once constructed, a resolution attribute's value is immutable.
45
* <p>
46
* <b>Design</b>
47
* <p>
48
* A resolution attribute's cross feed direction resolution and feed direction
49
* resolution values are stored internally using units of dots per 100 inches
50
* (dphi). Storing the values in dphi rather than, say, metric units allows
51
* precise integer arithmetic conversions between dpi and dphi and between dpcm
52
* and dphi: 1 dpi = 100 dphi, 1 dpcm = 254 dphi. Thus, the values can be stored
53
* into and retrieved back from a resolution attribute in either units with no
54
* loss of precision. This would not be guaranteed if a floating point
55
* representation were used. However, roundoff error will in general occur if a
56
* resolution attribute's values are created in one units and retrieved in
57
* different units; for example, 600 dpi will be rounded to 236 dpcm, whereas
58
* the true value (to five figures) is 236.22 dpcm.
59
* <p>
60
* Storing the values internally in common units of dphi lets two resolution
61
* attributes be compared without regard to the units in which they were
62
* created; for example, 300 dpcm will compare equal to 762 dpi, as they both
63
* are stored as 76200 dphi. In particular, a lookup service can match
64
* resolution attributes based on equality of their serialized representations
65
* regardless of the units in which they were created. Again, using integers for
66
* internal storage allows precise equality comparisons to be done, which would
67
* not be guaranteed if a floating point representation were used.
68
* <p>
69
* The exported constant {@link #DPI DPI} is actually the conversion factor by
70
* which to multiply a value in dpi to get the value in dphi. Likewise, the
71
* exported constant {@link #DPCM DPCM} is the conversion factor by which to
72
* multiply a value in dpcm to get the value in dphi. A client can specify a
73
* resolution value in units other than dpi or dpcm by supplying its own
74
* conversion factor. However, since the internal units of dphi was chosen with
75
* supporting only the external units of dpi and dpcm in mind, there is no
76
* guarantee that the conversion factor for the client's units will be an exact
77
* integer. If the conversion factor isn't an exact integer, resolution values
78
* in the client's units won't be stored precisely.
79
*
80
* @author David Mendenhall
81
* @author Alan Kaminsky
82
*/
83
public abstract class ResolutionSyntax implements Serializable, Cloneable {
84
85
/**
86
* Use serialVersionUID from JDK 1.4 for interoperability.
87
*/
88
@Serial
89
private static final long serialVersionUID = 2706743076526672017L;
90
91
/**
92
* Cross feed direction resolution in units of dots per 100 inches (dphi).
93
*
94
* @serial
95
*/
96
private int crossFeedResolution;
97
98
/**
99
* Feed direction resolution in units of dots per 100 inches (dphi).
100
*
101
* @serial
102
*/
103
private int feedResolution;
104
105
/**
106
* Value to indicate units of dots per inch (dpi). It is actually the
107
* conversion factor by which to multiply dpi to yield dphi (100).
108
*/
109
public static final int DPI = 100;
110
111
/**
112
* Value to indicate units of dots per centimeter (dpcm). It is actually the
113
* conversion factor by which to multiply dpcm to yield dphi (254).
114
*/
115
public static final int DPCM = 254;
116
117
/**
118
* Construct a new resolution attribute from the given items.
119
*
120
* @param crossFeedResolution cross feed direction resolution
121
* @param feedResolution feed direction resolution
122
* @param units unit conversion factor, e.g. {@link #DPI DPI} or
123
* {@link #DPCM DPCM}
124
* @throws IllegalArgumentException if {@code crossFeedResolution < 1} or
125
* {@code feedResolution < 1} or {@code units < 1}
126
*/
127
public ResolutionSyntax(int crossFeedResolution, int feedResolution,
128
int units) {
129
130
if (crossFeedResolution < 1) {
131
throw new IllegalArgumentException("crossFeedResolution is < 1");
132
}
133
if (feedResolution < 1) {
134
throw new IllegalArgumentException("feedResolution is < 1");
135
}
136
if (units < 1) {
137
throw new IllegalArgumentException("units is < 1");
138
}
139
140
this.crossFeedResolution = crossFeedResolution * units;
141
this.feedResolution = feedResolution * units;
142
}
143
144
/**
145
* Convert a value from dphi to some other units. The result is rounded to
146
* the nearest integer.
147
*
148
* @param dphi value (dphi) to convert
149
* @param units unit conversion factor, e.g. {@link #DPI DPI} or
150
* {@link #DPCM DPCM}
151
* @return the value of {@code dphi} converted to the desired units
152
* @throws IllegalArgumentException if {@code units < 1}
153
*/
154
private static int convertFromDphi(int dphi, int units) {
155
if (units < 1) {
156
throw new IllegalArgumentException(": units is < 1");
157
}
158
int round = units / 2;
159
return (dphi + round) / units;
160
}
161
162
/**
163
* Get this resolution attribute's resolution values in the given units. The
164
* values are rounded to the nearest integer.
165
*
166
* @param units unit conversion factor, e.g. {@link #DPI DPI} or
167
* {@link #DPCM DPCM}
168
* @return a two-element array with the cross feed direction resolution at
169
* index 0 and the feed direction resolution at index 1
170
* @throws IllegalArgumentException if {@code units < 1}
171
*/
172
public int[] getResolution(int units) {
173
return new int[] { getCrossFeedResolution(units),
174
getFeedResolution(units)
175
};
176
}
177
178
/**
179
* Returns this resolution attribute's cross feed direction resolution in
180
* the given units. The value is rounded to the nearest integer.
181
*
182
* @param units unit conversion factor, e.g. {@link #DPI DPI} or
183
* {@link #DPCM DPCM}
184
* @return cross feed direction resolution
185
* @throws IllegalArgumentException if {@code units < 1}
186
*/
187
public int getCrossFeedResolution(int units) {
188
return convertFromDphi (crossFeedResolution, units);
189
}
190
191
/**
192
* Returns this resolution attribute's feed direction resolution in the
193
* given units. The value is rounded to the nearest integer.
194
*
195
* @param units unit conversion factor, e.g. {@link #DPI DPI} or
196
* {@link #DPCM DPCM}
197
* @return feed direction resolution
198
* @throws IllegalArgumentException if {@code units < 1}
199
*/
200
public int getFeedResolution(int units) {
201
return convertFromDphi (feedResolution, units);
202
}
203
204
/**
205
* Returns a string version of this resolution attribute in the given units.
206
* The string takes the form <code>"<i>C</i>x<i>F</i> <i>U</i>"</code>,
207
* where <i>C</i> is the cross feed direction resolution, <i>F</i> is the
208
* feed direction resolution, and <i>U</i> is the units name. The values are
209
* rounded to the nearest integer.
210
*
211
* @param units unit conversion factor, e.g. {@link #DPI CODE>DPI} or
212
* {@link #DPCM DPCM}
213
* @param unitsName units name string, e.g. {@code "dpi"} or
214
* {@code "dpcm"}. If {@code null}, no units name is appended to the
215
* result.
216
* @return string version of this resolution attribute
217
* @throws IllegalArgumentException if {@code units < 1}
218
*/
219
public String toString(int units, String unitsName) {
220
StringBuilder result = new StringBuilder();
221
result.append(getCrossFeedResolution (units));
222
result.append('x');
223
result.append(getFeedResolution (units));
224
if (unitsName != null) {
225
result.append (' ');
226
result.append (unitsName);
227
}
228
return result.toString();
229
}
230
231
/**
232
* Determine whether this resolution attribute's value is less than or equal
233
* to the given resolution attribute's value. This is true if all of the
234
* following conditions are true:
235
* <ul>
236
* <li>This attribute's cross feed direction resolution is less than or
237
* equal to the {@code other} attribute's cross feed direction resolution.
238
* <li>This attribute's feed direction resolution is less than or equal to
239
* the {@code other} attribute's feed direction resolution.
240
* </ul>
241
*
242
* @param other resolution attribute to compare with
243
* @return {@code true} if this resolution attribute is less than or equal
244
* to the {@code other} resolution attribute, {@code false}
245
* otherwise
246
* @throws NullPointerException if {@code other} is {@code null}
247
*/
248
public boolean lessThanOrEquals(ResolutionSyntax other) {
249
return (this.crossFeedResolution <= other.crossFeedResolution &&
250
this.feedResolution <= other.feedResolution);
251
}
252
253
/**
254
* Returns whether this resolution attribute is equivalent to the passed in
255
* object. To be equivalent, all of the following conditions must be true:
256
* <ol type=1>
257
* <li>{@code object} is not {@code null}.
258
* <li>{@code object} is an instance of class {@code ResolutionSyntax}.
259
* <li>This attribute's cross feed direction resolution is equal to
260
* {@code object}'s cross feed direction resolution.
261
* <li>This attribute's feed direction resolution is equal to
262
* {@code object}'s feed direction resolution.
263
* </ol>
264
*
265
* @param object {@code Object} to compare to
266
* @return {@code true} if {@code object} is equivalent to this resolution
267
* attribute, {@code false} otherwise
268
*/
269
public boolean equals(Object object) {
270
271
return(object != null &&
272
object instanceof ResolutionSyntax &&
273
this.crossFeedResolution ==
274
((ResolutionSyntax) object).crossFeedResolution &&
275
this.feedResolution ==
276
((ResolutionSyntax) object).feedResolution);
277
}
278
279
/**
280
* Returns a hash code value for this resolution attribute.
281
*/
282
public int hashCode() {
283
return(((crossFeedResolution & 0x0000FFFF)) |
284
((feedResolution & 0x0000FFFF) << 16));
285
}
286
287
/**
288
* Returns a string version of this resolution attribute. The string takes
289
* the form <code>"<i>C</i>x<i>F</i> dphi"</code>, where <i>C</i> is the
290
* cross feed direction resolution and <i>F</i> is the feed direction
291
* resolution. The values are reported in the internal units of dphi.
292
*/
293
public String toString() {
294
StringBuilder result = new StringBuilder();
295
result.append(crossFeedResolution);
296
result.append('x');
297
result.append(feedResolution);
298
result.append(" dphi");
299
return result.toString();
300
}
301
302
/**
303
* Returns this resolution attribute's cross feed direction resolution in
304
* units of dphi. (For use in a subclass.)
305
*
306
* @return cross feed direction resolution
307
*/
308
protected int getCrossFeedResolutionDphi() {
309
return crossFeedResolution;
310
}
311
312
/**
313
* Returns this resolution attribute's feed direction resolution in units of
314
* dphi. (For use in a subclass.)
315
*
316
* @return feed direction resolution
317
*/
318
protected int getFeedResolutionDphi() {
319
return feedResolution;
320
}
321
}
322
323