Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/print/PSStreamPrintService.java
38829 views
/*1* Copyright (c) 2000, 2005, 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.OutputStream;28import java.util.Iterator;29import java.util.Locale;3031import javax.print.DocFlavor;32import javax.print.DocPrintJob;33import javax.print.StreamPrintService;34import javax.print.StreamPrintServiceFactory;35import javax.print.ServiceUIFactory;36import javax.print.attribute.Attribute;37import javax.print.attribute.AttributeSet;38import javax.print.attribute.AttributeSetUtilities;39import javax.print.attribute.HashAttributeSet;40import javax.print.attribute.HashPrintServiceAttributeSet;41import javax.print.attribute.PrintServiceAttribute;42import javax.print.attribute.PrintServiceAttributeSet;43import javax.print.attribute.Size2DSyntax;44import javax.print.event.PrintServiceAttributeListener;45import javax.print.attribute.standard.JobName;46import javax.print.attribute.standard.RequestingUserName;47import javax.print.attribute.standard.Chromaticity;48import javax.print.attribute.standard.ColorSupported;49import javax.print.attribute.standard.Copies;50import javax.print.attribute.standard.CopiesSupported;51import javax.print.attribute.standard.Fidelity;52import javax.print.attribute.standard.Media;53import javax.print.attribute.standard.MediaPrintableArea;54import javax.print.attribute.standard.MediaSize;55import javax.print.attribute.standard.MediaSizeName;56import javax.print.attribute.standard.OrientationRequested;57import javax.print.attribute.standard.PageRanges;58import javax.print.attribute.standard.SheetCollate;59import javax.print.attribute.standard.Sides;6061public class PSStreamPrintService extends StreamPrintService62implements SunPrinterJobService {6364private static final Class[] suppAttrCats = {65Chromaticity.class,66Copies.class,67Fidelity.class,68JobName.class,69Media.class,70MediaPrintableArea.class,71OrientationRequested.class,72PageRanges.class,73RequestingUserName.class,74SheetCollate.class,75Sides.class,76};7778private static int MAXCOPIES = 1000;7980private static final MediaSizeName mediaSizes[] = {81MediaSizeName.NA_LETTER,82MediaSizeName.TABLOID,83MediaSizeName.LEDGER,84MediaSizeName.NA_LEGAL,85MediaSizeName.EXECUTIVE,86MediaSizeName.ISO_A3,87MediaSizeName.ISO_A4,88MediaSizeName.ISO_A5,89MediaSizeName.ISO_B4,90MediaSizeName.ISO_B5,91};9293public PSStreamPrintService(OutputStream out) {94super(out);95}9697public String getOutputFormat() {98return PSStreamPrinterFactory.psMimeType;99}100101102public DocFlavor[] getSupportedDocFlavors() {103return PSStreamPrinterFactory.getFlavors();104}105106public DocPrintJob createPrintJob() {107return new PSStreamPrintJob(this);108}109110public boolean usesClass(Class c) {111return (c == sun.print.PSPrinterJob.class);112}113114public String getName() {115return "Postscript output";116}117118public void addPrintServiceAttributeListener(119PrintServiceAttributeListener listener) {120return;121}122123public void removePrintServiceAttributeListener(124PrintServiceAttributeListener listener) {125return;126}127128129public <T extends PrintServiceAttribute>130T getAttribute(Class<T> category)131{132if (category == null) {133throw new NullPointerException("category");134}135if (!(PrintServiceAttribute.class.isAssignableFrom(category))) {136throw new IllegalArgumentException("Not a PrintServiceAttribute");137}138if (category == ColorSupported.class) {139return (T)ColorSupported.SUPPORTED;140} else {141return null;142}143}144public PrintServiceAttributeSet getAttributes() {145PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();146attrs.add(ColorSupported.SUPPORTED);147148return AttributeSetUtilities.unmodifiableView(attrs);149}150151public boolean isDocFlavorSupported(DocFlavor flavor) {152DocFlavor [] flavors = getSupportedDocFlavors();153for (int f=0; f<flavors.length; f++) {154if (flavor.equals(flavors[f])) {155return true;156}157}158return false;159}160161162public Class<?>[] getSupportedAttributeCategories() {163Class []cats = new Class[suppAttrCats.length];164System.arraycopy(suppAttrCats, 0, cats, 0, cats.length);165return cats;166}167168public boolean169isAttributeCategorySupported(Class<? extends Attribute> category)170{171if (category == null) {172throw new NullPointerException("null category");173}174if (!(Attribute.class.isAssignableFrom(category))) {175throw new IllegalArgumentException(category +176" is not an Attribute");177}178179for (int i=0;i<suppAttrCats.length;i++) {180if (category == suppAttrCats[i]) {181return true;182}183}184return false;185}186187188public Object189getDefaultAttributeValue(Class<? extends Attribute> category)190{191if (category == null) {192throw new NullPointerException("null category");193}194if (!Attribute.class.isAssignableFrom(category)) {195throw new IllegalArgumentException(category +196" is not an Attribute");197}198199if (!isAttributeCategorySupported(category)) {200return null;201}202203if (category == Copies.class) {204return new Copies(1);205} else if (category == Chromaticity.class) {206return Chromaticity.COLOR;207} else if (category == Fidelity.class) {208return Fidelity.FIDELITY_FALSE;209} else if (category == Media.class) {210String defaultCountry = Locale.getDefault().getCountry();211if (defaultCountry != null &&212(defaultCountry.equals("") ||213defaultCountry.equals(Locale.US.getCountry()) ||214defaultCountry.equals(Locale.CANADA.getCountry()))) {215return MediaSizeName.NA_LETTER;216} else {217return MediaSizeName.ISO_A4;218}219} else if (category == MediaPrintableArea.class) {220String defaultCountry = Locale.getDefault().getCountry();221float iw, ih;222float margin = 0.5f; // both these papers > 5" in all dimensions223if (defaultCountry != null &&224(defaultCountry.equals("") ||225defaultCountry.equals(Locale.US.getCountry()) ||226defaultCountry.equals(Locale.CANADA.getCountry()))) {227iw = MediaSize.NA.LETTER.getX(Size2DSyntax.INCH) - 2*margin;228ih = MediaSize.NA.LETTER.getY(Size2DSyntax.INCH) - 2*margin;229} else {230iw = MediaSize.ISO.A4.getX(Size2DSyntax.INCH) - 2*margin;231ih = MediaSize.ISO.A4.getY(Size2DSyntax.INCH) - 2*margin;232}233return new MediaPrintableArea(margin, margin, iw, ih,234MediaPrintableArea.INCH);235} else if (category == OrientationRequested.class) {236return OrientationRequested.PORTRAIT;237} else if (category == PageRanges.class) {238return new PageRanges(1, Integer.MAX_VALUE);239} else if (category == SheetCollate.class) {240return SheetCollate.UNCOLLATED;241} else if (category == Sides.class) {242return Sides.ONE_SIDED;243244} else245return null;246}247248249public Object250getSupportedAttributeValues(Class<? extends Attribute> category,251DocFlavor flavor,252AttributeSet attributes)253{254255if (category == null) {256throw new NullPointerException("null category");257}258if (!Attribute.class.isAssignableFrom(category)) {259throw new IllegalArgumentException(category +260" does not implement Attribute");261}262if (flavor != null && !isDocFlavorSupported(flavor)) {263throw new IllegalArgumentException(flavor +264" is an unsupported flavor");265}266267if (!isAttributeCategorySupported(category)) {268return null;269}270271if (category == Chromaticity.class) {272Chromaticity[]arr = new Chromaticity[1];273arr[0] = Chromaticity.COLOR;274//arr[1] = Chromaticity.MONOCHROME;275return (arr);276} else if (category == JobName.class) {277return new JobName("", null);278} else if (category == RequestingUserName.class) {279return new RequestingUserName("", null);280} else if (category == OrientationRequested.class) {281if (flavor == null ||282flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||283flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {284OrientationRequested []arr = new OrientationRequested[3];285arr[0] = OrientationRequested.PORTRAIT;286arr[1] = OrientationRequested.LANDSCAPE;287arr[2] = OrientationRequested.REVERSE_LANDSCAPE;288return arr;289} else {290return null;291}292} else if ((category == Copies.class) ||293(category == CopiesSupported.class)) {294return new CopiesSupported(1, MAXCOPIES);295} else if (category == Media.class) {296Media []arr = new Media[mediaSizes.length];297System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);298return arr;299} else if (category == Fidelity.class) {300Fidelity []arr = new Fidelity[2];301arr[0] = Fidelity.FIDELITY_FALSE;302arr[1] = Fidelity.FIDELITY_TRUE;303return arr;304} else if (category == MediaPrintableArea.class) {305if (attributes == null) {306return null;307}308MediaSize mediaSize = (MediaSize)attributes.get(MediaSize.class);309if (mediaSize == null) {310Media media = (Media)attributes.get(Media.class);311if (media != null && media instanceof MediaSizeName) {312MediaSizeName msn = (MediaSizeName)media;313mediaSize = MediaSize.getMediaSizeForName(msn);314}315}316if (mediaSize == null) {317return null;318} else {319MediaPrintableArea []arr = new MediaPrintableArea[1];320float w = mediaSize.getX(MediaSize.INCH);321float h = mediaSize.getY(MediaSize.INCH);322/* For dimensions >= 5 inches use 0.5 inch margins.323* For smaller dimensions, use 10% margins.324*/325float xmargin = 0.5f;326float ymargin = 0.5f;327if (w < 5f) {328xmargin = w/10;329}330if (h < 5f) {331ymargin = h/10;332}333arr[0] = new MediaPrintableArea(xmargin, ymargin,334w - 2*xmargin,335h - 2*ymargin,336MediaSize.INCH);337return arr;338}339} else if (category == PageRanges.class) {340if (flavor == null ||341flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||342flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {343PageRanges []arr = new PageRanges[1];344arr[0] = new PageRanges(1, Integer.MAX_VALUE);345return arr;346} else {347return null;348}349} else if (category == SheetCollate.class) {350if (flavor == null ||351flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||352flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {353SheetCollate []arr = new SheetCollate[2];354arr[0] = SheetCollate.UNCOLLATED;355arr[1] = SheetCollate.COLLATED;356return arr;357} else {358SheetCollate []arr = new SheetCollate[1];359arr[0] = SheetCollate.UNCOLLATED;360return arr;361}362} else if (category == Sides.class) {363if (flavor == null ||364flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||365flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {366Sides []arr = new Sides[3];367arr[0] = Sides.ONE_SIDED;368arr[1] = Sides.TWO_SIDED_LONG_EDGE;369arr[2] = Sides.TWO_SIDED_SHORT_EDGE;370return arr;371} else {372return null;373}374} else {375return null;376}377}378379private boolean isSupportedCopies(Copies copies) {380int numCopies = copies.getValue();381return (numCopies > 0 && numCopies < MAXCOPIES);382}383384private boolean isSupportedMedia(MediaSizeName msn) {385for (int i=0; i<mediaSizes.length; i++) {386if (msn.equals(mediaSizes[i])) {387return true;388}389}390return false;391}392393public boolean isAttributeValueSupported(Attribute attr,394DocFlavor flavor,395AttributeSet attributes) {396if (attr == null) {397throw new NullPointerException("null attribute");398}399if (flavor != null && !isDocFlavorSupported(flavor)) {400throw new IllegalArgumentException(flavor +401" is an unsupported flavor");402}403Class category = attr.getCategory();404if (!isAttributeCategorySupported(category)) {405return false;406}407else if (attr.getCategory() == Chromaticity.class) {408return attr == Chromaticity.COLOR;409}410else if (attr.getCategory() == Copies.class) {411return isSupportedCopies((Copies)attr);412} else if (attr.getCategory() == Media.class &&413attr instanceof MediaSizeName) {414return isSupportedMedia((MediaSizeName)attr);415} else if (attr.getCategory() == OrientationRequested.class) {416if (attr == OrientationRequested.REVERSE_PORTRAIT ||417(flavor != null) &&418!(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||419flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {420return false;421}422} else if (attr.getCategory() == PageRanges.class) {423if (flavor != null &&424!(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||425flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {426return false;427}428} else if (attr.getCategory() == SheetCollate.class) {429if (flavor != null &&430!(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||431flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {432return false;433}434} else if (attr.getCategory() == Sides.class) {435if (flavor != null &&436!(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||437flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {438return false;439}440}441return true;442}443444public AttributeSet getUnsupportedAttributes(DocFlavor flavor,445AttributeSet attributes) {446447if (flavor != null && !isDocFlavorSupported(flavor)) {448throw new IllegalArgumentException("flavor " + flavor +449"is not supported");450}451452if (attributes == null) {453return null;454}455456Attribute attr;457AttributeSet unsupp = new HashAttributeSet();458Attribute[] attrs = attributes.toArray();459for (int i=0; i<attrs.length; i++) {460try {461attr = attrs[i];462if (!isAttributeCategorySupported(attr.getCategory())) {463unsupp.add(attr);464} else if (!isAttributeValueSupported(attr, flavor,465attributes)) {466unsupp.add(attr);467}468} catch (ClassCastException e) {469}470}471if (unsupp.isEmpty()) {472return null;473} else {474return unsupp;475}476}477478public ServiceUIFactory getServiceUIFactory() {479return null;480}481482public String toString() {483return "PSStreamPrintService: " + getName();484}485486/* Stream services have an output stream which cannot be shared,487* so two services are equal only if they are the same object.488*/489public boolean equals(Object obj) {490return (obj == this ||491(obj instanceof PSStreamPrintService &&492((PSStreamPrintService)obj).getName().equals(getName())));493}494495public int hashCode() {496return this.getClass().hashCode()+getName().hashCode();497}498499}500501502