diff --git a/src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java b/src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java index 34035e5c1a3..b8d3ec723b8 100644 --- a/src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java +++ b/src/java.desktop/share/classes/sun/print/CustomMediaSizeName.java @@ -27,15 +27,20 @@ package sun.print; import java.io.Serial; import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; +import java.util.Objects; import javax.print.attribute.EnumSyntax; import javax.print.attribute.standard.Media; import javax.print.attribute.standard.MediaSize; import javax.print.attribute.standard.MediaSizeName; +import javax.print.attribute.Size2DSyntax; class CustomMediaSizeName extends MediaSizeName { private static ArrayList customStringTable = new ArrayList<>(); private static ArrayList customEnumTable = new ArrayList<>(); + private static Map customMap = new HashMap<>(); private String choiceName; private MediaSizeName mediaName; @@ -191,4 +196,55 @@ class CustomMediaSizeName extends MediaSizeName { return customEnumTable.toArray(enumTable); } + public static CustomMediaSizeName create(String name, String choice, + float width, float length) { + SizeNameChoiceItem key = new SizeNameChoiceItem(name, choice, width, length); + CustomMediaSizeName value = customMap.get(key); + if (value == null) { + value = new CustomMediaSizeName(name, choice, width, length); + customMap.put(key, value); + + // add this new custom media size name to MediaSize array + if ((width > 0.0) && (length > 0.0)) { + try { + new MediaSize(width, length, Size2DSyntax.INCH, value); + } catch (IllegalArgumentException e) { + /* PDF printer in Linux for Ledger paper causes + "IllegalArgumentException: X dimension > Y dimension". + We rotate based on IPP spec. */ + new MediaSize(length, width, Size2DSyntax.INCH, value); + } + } + } + return value; + } + + private static class SizeNameChoiceItem { + + private final String name; + private final String choice; + private final float width; + private final float length; + + public SizeNameChoiceItem(String name, String choice, float width, float length) { + this.name = name; + this.choice = choice; + this.width = width; + this.length = length; + } + + public boolean equals(Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + SizeNameChoiceItem that = (SizeNameChoiceItem) object; + return Objects.equals(this.name, that.name) + && Objects.equals(this.choice, that.choice) && + Float.compare(this.width, that.width) == 0 && + Float.compare(this.length, that.length) == 0; + } + + public int hashCode() { + return Objects.hash(name, choice, width, length); + } + } } diff --git a/src/java.desktop/share/classes/sun/print/CustomMediaTray.java b/src/java.desktop/share/classes/sun/print/CustomMediaTray.java index 324059a56d6..e1ce64106b1 100644 --- a/src/java.desktop/share/classes/sun/print/CustomMediaTray.java +++ b/src/java.desktop/share/classes/sun/print/CustomMediaTray.java @@ -27,6 +27,9 @@ package sun.print; import java.io.Serial; import java.util.ArrayList; +import java.util.Objects; +import java.util.Map; +import java.util.HashMap; import javax.print.attribute.EnumSyntax; import javax.print.attribute.standard.Media; @@ -35,6 +38,7 @@ import javax.print.attribute.standard.MediaTray; public class CustomMediaTray extends MediaTray { private static ArrayList customStringTable = new ArrayList<>(); private static ArrayList customEnumTable = new ArrayList<>(); + private static Map customMap = new HashMap<>(); private String choiceName; private CustomMediaTray(int x) { @@ -93,4 +97,36 @@ public class CustomMediaTray extends MediaTray { return customEnumTable.toArray(enumTable); } + public static CustomMediaTray create(String name, String choice) { + NameChoiceItem key = new NameChoiceItem(name, choice); + CustomMediaTray value = customMap.get(key); + if (value == null) { + value = new CustomMediaTray(name, choice); + customMap.put(key, value); + } + return value; + } + + private static class NameChoiceItem { + + private final String name; + private final String choice; + + public NameChoiceItem(String name, String choice) { + this.name = name; + this.choice = choice; + } + + public boolean equals(Object object) { + if (this == object) return true; + if (object == null || getClass() != object.getClass()) return false; + NameChoiceItem that = (NameChoiceItem) object; + return Objects.equals(this.name, that.name) + && Objects.equals(this.choice, that.choice); + } + + public int hashCode() { + return Objects.hash(name, choice); + } + } } diff --git a/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java b/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java index e2a69c79c3e..6c75949731d 100644 --- a/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java +++ b/src/java.desktop/unix/classes/sun/print/CUPSPrinter.java @@ -225,26 +225,13 @@ public class CUPSPrinter { w = (float)(pageSizes[i*6+4]/PRINTER_DPI); y = (float)(pageSizes[i*6+5]/PRINTER_DPI); - msn = new CustomMediaSizeName(media[i*2], media[i*2+1], - width, length); + msn = CustomMediaSizeName.create(media[i*2], media[i*2+1], + width, length); // add to list of standard MediaSizeNames if ((cupsMediaSNames[i] = msn.getStandardMedia()) == null) { // add custom if no matching standard media cupsMediaSNames[i] = msn; - - // add this new custom msn to MediaSize array - if ((width > 0.0) && (length > 0.0)) { - try { - new MediaSize(width, length, - Size2DSyntax.INCH, msn); - } catch (IllegalArgumentException e) { - /* PDF printer in Linux for Ledger paper causes - "IllegalArgumentException: X dimension > Y dimension". - We rotate based on IPP spec. */ - new MediaSize(length, width, Size2DSyntax.INCH, msn); - } - } } // add to list of custom MediaSizeName @@ -269,8 +256,8 @@ public class CUPSPrinter { MediaTray mt; for (int i=0; i System.currentTimeMillis()); + } +}