Path: blob/master/src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java
41153 views
/*1* Copyright (c) 2003, 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 sun.print;2627import java.io.Serial;28import java.util.ArrayList;2930import javax.print.attribute.EnumSyntax;31import javax.print.attribute.standard.Media;32import javax.print.attribute.standard.MediaSize;33import javax.print.attribute.standard.MediaSizeName;3435class CustomMediaSizeName extends MediaSizeName {36private static ArrayList<String> customStringTable = new ArrayList<>();37private static ArrayList<MediaSizeName> customEnumTable = new ArrayList<>();38private String choiceName;39private MediaSizeName mediaName;4041private CustomMediaSizeName(int x) {42super(x);4344}4546private static synchronized int nextValue(String name) {47customStringTable.add(name);4849return (customStringTable.size()-1);50}5152public CustomMediaSizeName(String name) {53super(nextValue(name));54customEnumTable.add(this);55choiceName = null;56mediaName = null;57}5859public CustomMediaSizeName(String name, String choice,60float width, float length) {61super(nextValue(name));62choiceName = choice;63customEnumTable.add(this);64mediaName = null;65try {66mediaName = MediaSize.findMedia(width, length,67MediaSize.INCH);68} catch (IllegalArgumentException iae) {69}70// The public API method finds a closest match even if it not71// all that close. Here we want to be sure its *really* close.72if (mediaName != null) {73MediaSize sz = MediaSize.getMediaSizeForName(mediaName);74if (sz == null) {75mediaName = null;76} else {77float w = sz.getX(MediaSize.INCH);78float h = sz.getY(MediaSize.INCH);79float dw = Math.abs(w - width);80float dh = Math.abs(h - length);81if (dw > 0.1 || dh > 0.1) {82mediaName = null;83}84}85}86}8788/**89* Use serialVersionUID from JDK 1.5 for interoperability.90*/91@Serial92private static final long serialVersionUID = 7412807582228043717L;9394/**95* Returns the command string for this media.96*/97public String getChoiceName() {98return choiceName;99}100101102/**103* Returns matching standard MediaSizeName.104*/105public MediaSizeName getStandardMedia() {106return mediaName;107}108109110// moved from RasterPrinterJob111/**112* Returns closest matching MediaSizeName among given array of Media113*/114public static MediaSizeName findMedia(Media[] media, float x, float y,115int units) {116117118if (x <= 0.0f || y <= 0.0f || units < 1) {119throw new IllegalArgumentException("args must be +ve values");120}121122if (media == null || media.length == 0) {123throw new IllegalArgumentException("args must have valid array of media");124}125126int size =0;127MediaSizeName[] msn = new MediaSizeName[media.length];128for (int i=0; i<media.length; i++) {129if (media[i] instanceof MediaSizeName) {130msn[size++] = (MediaSizeName)media[i];131}132}133134if (size == 0) {135return null;136}137138int match = 0;139140double ls = x * x + y * y;141double tmp_ls;142float []dim;143float diffx = x;144float diffy = y;145146for (int i=0; i < size ; i++) {147MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);148if (mediaSize == null) {149continue;150}151dim = mediaSize.getSize(units);152if (x == dim[0] && y == dim[1]) {153match = i;154break;155} else {156diffx = x - dim[0];157diffy = y - dim[1];158tmp_ls = diffx * diffx + diffy * diffy;159if (tmp_ls < ls) {160ls = tmp_ls;161match = i;162}163}164}165166return msn[match];167}168169/**170* Returns the string table for super class MediaSizeName.171*/172public Media[] getSuperEnumTable() {173return (Media[])super.getEnumValueTable();174}175176177/**178* Returns the string table for class CustomMediaSizeName.179*/180protected String[] getStringTable() {181String[] nameTable = new String[customStringTable.size()];182return customStringTable.toArray(nameTable);183}184185/**186* Returns the enumeration value table for class CustomMediaSizeName.187*/188protected EnumSyntax[] getEnumValueTable() {189MediaSizeName[] enumTable = new MediaSizeName[customEnumTable.size()];190return customEnumTable.toArray(enumTable);191}192193}194195196