25 package javax.print.attribute.standard; 26 27 import java.util.HashMap; 28 import java.util.Vector; 29 30 import javax.print.attribute.Size2DSyntax; 31 import javax.print.attribute.Attribute; 32 33 /** 34 * Class MediaSize is a two-dimensional size valued printing attribute class 35 * that indicates the dimensions of the medium in a portrait orientation, with 36 * the X dimension running along the bottom edge and the Y dimension running 37 * along the left edge. Thus, the Y dimension must be greater than or equal to 38 * the X dimension. Class MediaSize declares many standard media size 39 * values, organized into nested classes for ISO, JIS, North American, 40 * engineering, and other media. 41 * <P> 42 * MediaSize is not yet used to specify media. Its current role is 43 * as a mapping for named media (see {@link MediaSizeName MediaSizeName}). 44 * Clients can use the mapping method 45 * <code>MediaSize.getMediaSizeForName(MediaSizeName)</code> 46 * to find the physical dimensions of the MediaSizeName instances 47 * enumerated in this API. This is useful for clients which need this 48 * information to format {@literal &} paginate printing. 49 * 50 * @author Phil Race, Alan Kaminsky 51 */ 52 public class MediaSize extends Size2DSyntax implements Attribute { 53 54 private static final long serialVersionUID = -1967958664615414771L; 55 56 private MediaSizeName mediaName; 57 58 private static HashMap<MediaSizeName, MediaSize> mediaMap = new HashMap<>(100, 10); 59 60 private static Vector<MediaSize> sizeVector = new Vector<>(100, 10); 61 62 /** 63 * Construct a new media size attribute from the given floating-point 64 * values. 65 * 66 * @param x X dimension. 67 * @param y Y dimension. 68 * @param units 69 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 70 * <CODE>Size2DSyntax.MM</CODE>. 71 * 72 * @exception IllegalArgumentException 73 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 74 * {@code units < 1} or {@code x > y}. 75 */ 76 public MediaSize(float x, float y,int units) { 77 super (x, y, units); 78 if (x > y) { 79 throw new IllegalArgumentException("X dimension > Y dimension"); 80 } 81 sizeVector.add(this); 82 } 83 84 /** 85 * Construct a new media size attribute from the given integer values. 86 * 87 * @param x X dimension. 88 * @param y Y dimension. 89 * @param units 90 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 91 * <CODE>Size2DSyntax.MM</CODE>. 92 * 93 * @exception IllegalArgumentException 94 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 95 * {@code units < 1} or {@code x > y}. 96 */ 97 public MediaSize(int x, int y,int units) { 98 super (x, y, units); 99 if (x > y) { 100 throw new IllegalArgumentException("X dimension > Y dimension"); 101 } 102 sizeVector.add(this); 103 } 104 105 /** 106 * Construct a new media size attribute from the given floating-point 107 * values. 108 * 109 * @param x X dimension. 110 * @param y Y dimension. 111 * @param units 112 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 113 * <CODE>Size2DSyntax.MM</CODE>. 114 * @param media a media name to associate with this MediaSize 115 * 116 * @exception IllegalArgumentException 117 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 118 * {@code units < 1} or {@code x > y}. 119 */ 120 public MediaSize(float x, float y,int units, MediaSizeName media) { 121 super (x, y, units); 122 if (x > y) { 123 throw new IllegalArgumentException("X dimension > Y dimension"); 124 } 125 if (media != null && mediaMap.get(media) == null) { 126 mediaName = media; 127 mediaMap.put(mediaName, this); 128 } 129 sizeVector.add(this); 130 } 131 132 /** 133 * Construct a new media size attribute from the given integer values. 134 * 135 * @param x X dimension. 136 * @param y Y dimension. 137 * @param units 138 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 139 * <CODE>Size2DSyntax.MM</CODE>. 140 * @param media a media name to associate with this MediaSize 141 * 142 * @exception IllegalArgumentException 143 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 144 * {@code units < 1} or {@code x > y}. 145 */ 146 public MediaSize(int x, int y,int units, MediaSizeName media) { 147 super (x, y, units); 148 if (x > y) { 149 throw new IllegalArgumentException("X dimension > Y dimension"); 150 } 151 if (media != null && mediaMap.get(media) == null) { 152 mediaName = media; 153 mediaMap.put(mediaName, this); 154 } 155 sizeVector.add(this); 156 } 157 158 /** 159 * Get the media name, if any, for this size. 173 * with any size. 174 */ 175 public static MediaSize getMediaSizeForName(MediaSizeName media) { 176 return mediaMap.get(media); 177 } 178 179 /** 180 * The specified dimensions are used to locate a matching MediaSize 181 * instance from amongst all the standard MediaSize instances. 182 * If there is no exact match, the closest match is used. 183 * <p> 184 * The MediaSize is in turn used to locate the MediaSizeName object. 185 * This method may return null if the closest matching MediaSize 186 * has no corresponding Media instance. 187 * <p> 188 * This method is useful for clients which have only dimensions and 189 * want to find a Media which corresponds to the dimensions. 190 * @param x - X dimension 191 * @param y - Y dimension. 192 * @param units 193 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 194 * <CODE>Size2DSyntax.MM</CODE> 195 * @return MediaSizeName matching these dimensions, or null. 196 * @exception IllegalArgumentException if {@code x <= 0}, 197 * {@code y <= 0}, or {@code units < 1}. 198 * 199 */ 200 public static MediaSizeName findMedia(float x, float y, int units) { 201 202 MediaSize match = MediaSize.ISO.A4; 203 204 if (x <= 0.0f || y <= 0.0f || units < 1) { 205 throw new IllegalArgumentException("args must be +ve values"); 206 } 207 208 double ls = x * x + y * y; 209 double tmp_ls; 210 float []dim; 211 float diffx = x; 212 float diffy = y; 213 214 for (int i=0; i < sizeVector.size() ; i++) { 220 } else { 221 diffx = x - dim[0]; 222 diffy = y - dim[1]; 223 tmp_ls = diffx * diffx + diffy * diffy; 224 if (tmp_ls < ls) { 225 ls = tmp_ls; 226 match = mediaSize; 227 } 228 } 229 } 230 231 return match.getMediaSizeName(); 232 } 233 234 /** 235 * Returns whether this media size attribute is equivalent to the passed 236 * in object. 237 * To be equivalent, all of the following conditions must be true: 238 * <OL TYPE=1> 239 * <LI> 240 * <CODE>object</CODE> is not null. 241 * <LI> 242 * <CODE>object</CODE> is an instance of class MediaSize. 243 * <LI> 244 * This media size attribute's X dimension is equal to 245 * <CODE>object</CODE>'s X dimension. 246 * <LI> 247 * This media size attribute's Y dimension is equal to 248 * <CODE>object</CODE>'s Y dimension. 249 * </OL> 250 * 251 * @param object Object to compare to. 252 * 253 * @return True if <CODE>object</CODE> is equivalent to this media size 254 * attribute, false otherwise. 255 */ 256 public boolean equals(Object object) { 257 return (super.equals(object) && object instanceof MediaSize); 258 } 259 260 /** 261 * Get the printing attribute class which is to be used as the "category" 262 * for this printing attribute value. 263 * <P> 264 * For class MediaSize and any vendor-defined subclasses, the category is 265 * class MediaSize itself. 266 * 267 * @return Printing attribute class (category), an instance of class 268 * {@link java.lang.Class java.lang.Class}. 269 */ 270 public final Class<? extends Attribute> getCategory() { 271 return MediaSize.class; 272 } 273 274 /** 275 * Get the name of the category of which this attribute value is an 276 * instance. 277 * <P> 278 * For class MediaSize and any vendor-defined subclasses, the category 279 * name is <CODE>"media-size"</CODE>. 280 * 281 * @return Attribute category name. 282 */ 283 public final String getName() { 284 return "media-size"; 285 } 286 287 /** 288 * Class MediaSize.ISO includes {@link MediaSize MediaSize} values for ISO 289 * media. 290 */ 291 public static final class ISO { 292 /** 293 * Specifies the ISO A0 size, 841 mm by 1189 mm. 294 */ 295 public static final MediaSize 296 A0 = new MediaSize(841, 1189, Size2DSyntax.MM, MediaSizeName.ISO_A0); 297 /** 298 * Specifies the ISO A1 size, 594 mm by 841 mm. 299 */ | 25 package javax.print.attribute.standard; 26 27 import java.util.HashMap; 28 import java.util.Vector; 29 30 import javax.print.attribute.Size2DSyntax; 31 import javax.print.attribute.Attribute; 32 33 /** 34 * Class MediaSize is a two-dimensional size valued printing attribute class 35 * that indicates the dimensions of the medium in a portrait orientation, with 36 * the X dimension running along the bottom edge and the Y dimension running 37 * along the left edge. Thus, the Y dimension must be greater than or equal to 38 * the X dimension. Class MediaSize declares many standard media size 39 * values, organized into nested classes for ISO, JIS, North American, 40 * engineering, and other media. 41 * <P> 42 * MediaSize is not yet used to specify media. Its current role is 43 * as a mapping for named media (see {@link MediaSizeName MediaSizeName}). 44 * Clients can use the mapping method 45 * {@code MediaSize.getMediaSizeForName(MediaSizeName)} 46 * to find the physical dimensions of the MediaSizeName instances 47 * enumerated in this API. This is useful for clients which need this 48 * information to format {@literal &} paginate printing. 49 * 50 * @author Phil Race, Alan Kaminsky 51 */ 52 public class MediaSize extends Size2DSyntax implements Attribute { 53 54 private static final long serialVersionUID = -1967958664615414771L; 55 56 private MediaSizeName mediaName; 57 58 private static HashMap<MediaSizeName, MediaSize> mediaMap = new HashMap<>(100, 10); 59 60 private static Vector<MediaSize> sizeVector = new Vector<>(100, 10); 61 62 /** 63 * Construct a new media size attribute from the given floating-point 64 * values. 65 * 66 * @param x X dimension. 67 * @param y Y dimension. 68 * @param units 69 * Unit conversion factor, e.g. {@code Size2DSyntax.INCH} or 70 * {@code Size2DSyntax.MM}. 71 * 72 * @exception IllegalArgumentException 73 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 74 * {@code units < 1} or {@code x > y}. 75 */ 76 public MediaSize(float x, float y,int units) { 77 super (x, y, units); 78 if (x > y) { 79 throw new IllegalArgumentException("X dimension > Y dimension"); 80 } 81 sizeVector.add(this); 82 } 83 84 /** 85 * Construct a new media size attribute from the given integer values. 86 * 87 * @param x X dimension. 88 * @param y Y dimension. 89 * @param units 90 * Unit conversion factor, e.g. {@code Size2DSyntax.INCH} or 91 * {@code Size2DSyntax.MM}. 92 * 93 * @exception IllegalArgumentException 94 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 95 * {@code units < 1} or {@code x > y}. 96 */ 97 public MediaSize(int x, int y,int units) { 98 super (x, y, units); 99 if (x > y) { 100 throw new IllegalArgumentException("X dimension > Y dimension"); 101 } 102 sizeVector.add(this); 103 } 104 105 /** 106 * Construct a new media size attribute from the given floating-point 107 * values. 108 * 109 * @param x X dimension. 110 * @param y Y dimension. 111 * @param units 112 * Unit conversion factor, e.g. {@code Size2DSyntax.INCH} or 113 * {@code Size2DSyntax.MM}. 114 * @param media a media name to associate with this MediaSize 115 * 116 * @exception IllegalArgumentException 117 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 118 * {@code units < 1} or {@code x > y}. 119 */ 120 public MediaSize(float x, float y,int units, MediaSizeName media) { 121 super (x, y, units); 122 if (x > y) { 123 throw new IllegalArgumentException("X dimension > Y dimension"); 124 } 125 if (media != null && mediaMap.get(media) == null) { 126 mediaName = media; 127 mediaMap.put(mediaName, this); 128 } 129 sizeVector.add(this); 130 } 131 132 /** 133 * Construct a new media size attribute from the given integer values. 134 * 135 * @param x X dimension. 136 * @param y Y dimension. 137 * @param units 138 * Unit conversion factor, e.g. {@code Size2DSyntax.INCH} or 139 * {@code Size2DSyntax.MM}. 140 * @param media a media name to associate with this MediaSize 141 * 142 * @exception IllegalArgumentException 143 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 144 * {@code units < 1} or {@code x > y}. 145 */ 146 public MediaSize(int x, int y,int units, MediaSizeName media) { 147 super (x, y, units); 148 if (x > y) { 149 throw new IllegalArgumentException("X dimension > Y dimension"); 150 } 151 if (media != null && mediaMap.get(media) == null) { 152 mediaName = media; 153 mediaMap.put(mediaName, this); 154 } 155 sizeVector.add(this); 156 } 157 158 /** 159 * Get the media name, if any, for this size. 173 * with any size. 174 */ 175 public static MediaSize getMediaSizeForName(MediaSizeName media) { 176 return mediaMap.get(media); 177 } 178 179 /** 180 * The specified dimensions are used to locate a matching MediaSize 181 * instance from amongst all the standard MediaSize instances. 182 * If there is no exact match, the closest match is used. 183 * <p> 184 * The MediaSize is in turn used to locate the MediaSizeName object. 185 * This method may return null if the closest matching MediaSize 186 * has no corresponding Media instance. 187 * <p> 188 * This method is useful for clients which have only dimensions and 189 * want to find a Media which corresponds to the dimensions. 190 * @param x - X dimension 191 * @param y - Y dimension. 192 * @param units 193 * Unit conversion factor, e.g. {@code Size2DSyntax.INCH} or 194 * {@code Size2DSyntax.MM} 195 * @return MediaSizeName matching these dimensions, or null. 196 * @exception IllegalArgumentException if {@code x <= 0}, 197 * {@code y <= 0}, or {@code units < 1}. 198 * 199 */ 200 public static MediaSizeName findMedia(float x, float y, int units) { 201 202 MediaSize match = MediaSize.ISO.A4; 203 204 if (x <= 0.0f || y <= 0.0f || units < 1) { 205 throw new IllegalArgumentException("args must be +ve values"); 206 } 207 208 double ls = x * x + y * y; 209 double tmp_ls; 210 float []dim; 211 float diffx = x; 212 float diffy = y; 213 214 for (int i=0; i < sizeVector.size() ; i++) { 220 } else { 221 diffx = x - dim[0]; 222 diffy = y - dim[1]; 223 tmp_ls = diffx * diffx + diffy * diffy; 224 if (tmp_ls < ls) { 225 ls = tmp_ls; 226 match = mediaSize; 227 } 228 } 229 } 230 231 return match.getMediaSizeName(); 232 } 233 234 /** 235 * Returns whether this media size attribute is equivalent to the passed 236 * in object. 237 * To be equivalent, all of the following conditions must be true: 238 * <OL TYPE=1> 239 * <LI> 240 * {@code object} is not null. 241 * <LI> 242 * {@code object} is an instance of class MediaSize. 243 * <LI> 244 * This media size attribute's X dimension is equal to 245 * {@code object}'s X dimension. 246 * <LI> 247 * This media size attribute's Y dimension is equal to 248 * {@code object}'s Y dimension. 249 * </OL> 250 * 251 * @param object Object to compare to. 252 * 253 * @return True if {@code object} is equivalent to this media size 254 * attribute, false otherwise. 255 */ 256 public boolean equals(Object object) { 257 return (super.equals(object) && object instanceof MediaSize); 258 } 259 260 /** 261 * Get the printing attribute class which is to be used as the "category" 262 * for this printing attribute value. 263 * <P> 264 * For class MediaSize and any vendor-defined subclasses, the category is 265 * class MediaSize itself. 266 * 267 * @return Printing attribute class (category), an instance of class 268 * {@link java.lang.Class java.lang.Class}. 269 */ 270 public final Class<? extends Attribute> getCategory() { 271 return MediaSize.class; 272 } 273 274 /** 275 * Get the name of the category of which this attribute value is an 276 * instance. 277 * <P> 278 * For class MediaSize and any vendor-defined subclasses, the category 279 * name is {@code "media-size"}. 280 * 281 * @return Attribute category name. 282 */ 283 public final String getName() { 284 return "media-size"; 285 } 286 287 /** 288 * Class MediaSize.ISO includes {@link MediaSize MediaSize} values for ISO 289 * media. 290 */ 291 public static final class ISO { 292 /** 293 * Specifies the ISO A0 size, 841 mm by 1189 mm. 294 */ 295 public static final MediaSize 296 A0 = new MediaSize(841, 1189, Size2DSyntax.MM, MediaSizeName.ISO_A0); 297 /** 298 * Specifies the ISO A1 size, 594 mm by 841 mm. 299 */ |