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 * <P> 50 * 51 * @author Phil Race, Alan Kaminsky 52 */ 53 public class MediaSize extends Size2DSyntax implements Attribute { 54 55 private static final long serialVersionUID = -1967958664615414771L; 56 57 private MediaSizeName mediaName; 58 59 private static HashMap mediaMap = new HashMap(100, 10); 60 61 private static Vector sizeVector = new Vector(100, 10); 62 63 /** 64 * Construct a new media size attribute from the given floating-point 65 * values. 66 * 67 * @param x X dimension. 68 * @param y Y dimension. 69 * @param units 70 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 71 * <CODE>Size2DSyntax.MM</CODE>. 72 * 73 * @exception IllegalArgumentException 74 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 75 * {@code units < 1} or {@code x > y}. 76 */ 77 public MediaSize(float x, float y,int units) { 78 super (x, y, units); 79 if (x > y) { 80 throw new IllegalArgumentException("X dimension > Y dimension"); 81 } 157 } 158 159 /** 160 * Get the media name, if any, for this size. 161 * 162 * @return the name for this media size, or null if no name was 163 * associated with this size (an anonymous size). 164 */ 165 public MediaSizeName getMediaSizeName() { 166 return mediaName; 167 } 168 169 /** 170 * Get the MediaSize for the specified named media. 171 * 172 * @param media - the name of the media for which the size is sought 173 * @return size of the media, or null if this media is not associated 174 * with any size. 175 */ 176 public static MediaSize getMediaSizeForName(MediaSizeName media) { 177 return (MediaSize)mediaMap.get(media); 178 } 179 180 /** 181 * The specified dimensions are used to locate a matching MediaSize 182 * instance from amongst all the standard MediaSize instances. 183 * If there is no exact match, the closest match is used. 184 * <p> 185 * The MediaSize is in turn used to locate the MediaSizeName object. 186 * This method may return null if the closest matching MediaSize 187 * has no corresponding Media instance. 188 * <p> 189 * This method is useful for clients which have only dimensions and 190 * want to find a Media which corresponds to the dimensions. 191 * @param x - X dimension 192 * @param y - Y dimension. 193 * @param units 194 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 195 * <CODE>Size2DSyntax.MM</CODE> 196 * @return MediaSizeName matching these dimensions, or null. 197 * @exception IllegalArgumentException if {@code x <= 0}, 198 * {@code y <= 0}, or {@code units < 1}. 199 * 200 */ 201 public static MediaSizeName findMedia(float x, float y, int units) { 202 203 MediaSize match = MediaSize.ISO.A4; 204 205 if (x <= 0.0f || y <= 0.0f || units < 1) { 206 throw new IllegalArgumentException("args must be +ve values"); 207 } 208 209 double ls = x * x + y * y; 210 double tmp_ls; 211 float []dim; 212 float diffx = x; 213 float diffy = y; 214 215 for (int i=0; i < sizeVector.size() ; i++) { 216 MediaSize mediaSize = (MediaSize)sizeVector.elementAt(i); 217 dim = mediaSize.getSize(units); 218 if (x == dim[0] && y == dim[1]) { 219 match = mediaSize; 220 break; 221 } else { 222 diffx = x - dim[0]; 223 diffy = y - dim[1]; 224 tmp_ls = diffx * diffx + diffy * diffy; 225 if (tmp_ls < ls) { 226 ls = tmp_ls; 227 match = mediaSize; 228 } 229 } 230 } 231 232 return match.getMediaSizeName(); 233 } 234 235 /** 236 * Returns whether this media size attribute is equivalent to the passed | 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 * <P> 50 * 51 * @author Phil Race, Alan Kaminsky 52 */ 53 public class MediaSize extends Size2DSyntax implements Attribute { 54 55 private static final long serialVersionUID = -1967958664615414771L; 56 57 private MediaSizeName mediaName; 58 59 private static HashMap<MediaSizeName, MediaSize> mediaMap = new HashMap<>(100, 10); 60 61 private static Vector<MediaSize> sizeVector = new Vector<>(100, 10); 62 63 /** 64 * Construct a new media size attribute from the given floating-point 65 * values. 66 * 67 * @param x X dimension. 68 * @param y Y dimension. 69 * @param units 70 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 71 * <CODE>Size2DSyntax.MM</CODE>. 72 * 73 * @exception IllegalArgumentException 74 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 75 * {@code units < 1} or {@code x > y}. 76 */ 77 public MediaSize(float x, float y,int units) { 78 super (x, y, units); 79 if (x > y) { 80 throw new IllegalArgumentException("X dimension > Y dimension"); 81 } 157 } 158 159 /** 160 * Get the media name, if any, for this size. 161 * 162 * @return the name for this media size, or null if no name was 163 * associated with this size (an anonymous size). 164 */ 165 public MediaSizeName getMediaSizeName() { 166 return mediaName; 167 } 168 169 /** 170 * Get the MediaSize for the specified named media. 171 * 172 * @param media - the name of the media for which the size is sought 173 * @return size of the media, or null if this media is not associated 174 * with any size. 175 */ 176 public static MediaSize getMediaSizeForName(MediaSizeName media) { 177 return mediaMap.get(media); 178 } 179 180 /** 181 * The specified dimensions are used to locate a matching MediaSize 182 * instance from amongst all the standard MediaSize instances. 183 * If there is no exact match, the closest match is used. 184 * <p> 185 * The MediaSize is in turn used to locate the MediaSizeName object. 186 * This method may return null if the closest matching MediaSize 187 * has no corresponding Media instance. 188 * <p> 189 * This method is useful for clients which have only dimensions and 190 * want to find a Media which corresponds to the dimensions. 191 * @param x - X dimension 192 * @param y - Y dimension. 193 * @param units 194 * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or 195 * <CODE>Size2DSyntax.MM</CODE> 196 * @return MediaSizeName matching these dimensions, or null. 197 * @exception IllegalArgumentException if {@code x <= 0}, 198 * {@code y <= 0}, or {@code units < 1}. 199 * 200 */ 201 public static MediaSizeName findMedia(float x, float y, int units) { 202 203 MediaSize match = MediaSize.ISO.A4; 204 205 if (x <= 0.0f || y <= 0.0f || units < 1) { 206 throw new IllegalArgumentException("args must be +ve values"); 207 } 208 209 double ls = x * x + y * y; 210 double tmp_ls; 211 float []dim; 212 float diffx = x; 213 float diffy = y; 214 215 for (int i=0; i < sizeVector.size() ; i++) { 216 MediaSize mediaSize = sizeVector.elementAt(i); 217 dim = mediaSize.getSize(units); 218 if (x == dim[0] && y == dim[1]) { 219 match = mediaSize; 220 break; 221 } else { 222 diffx = x - dim[0]; 223 diffy = y - dim[1]; 224 tmp_ls = diffx * diffx + diffy * diffy; 225 if (tmp_ls < ls) { 226 ls = tmp_ls; 227 match = mediaSize; 228 } 229 } 230 } 231 232 return match.getMediaSizeName(); 233 } 234 235 /** 236 * Returns whether this media size attribute is equivalent to the passed |