Search in sources :

Example 6 with MediaSize

use of javax.print.attribute.standard.MediaSize in project jdk8u_jdk by JetBrains.

the class CUPSPrinter method initMedia.

/**
     * Initialize media by translating PPD info to PrintService attributes.
     */
private synchronized void initMedia() {
    if (initialized) {
        return;
    } else {
        initialized = true;
    }
    if (pageSizes == null) {
        return;
    }
    cupsMediaPrintables = new MediaPrintableArea[nPageSizes];
    cupsMediaSNames = new MediaSizeName[nPageSizes];
    cupsCustomMediaSNames = new CustomMediaSizeName[nPageSizes];
    CustomMediaSizeName msn;
    MediaPrintableArea mpa;
    float length, width, x, y, w, h;
    // initialize names and printables
    for (int i = 0; i < nPageSizes; i++) {
        // media width and length
        width = (float) (pageSizes[i * 6] / PRINTER_DPI);
        length = (float) (pageSizes[i * 6 + 1] / PRINTER_DPI);
        // media printable area
        x = (float) (pageSizes[i * 6 + 2] / PRINTER_DPI);
        h = (float) (pageSizes[i * 6 + 3] / PRINTER_DPI);
        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);
        // 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
        // for internal use of IPPPrintService
        cupsCustomMediaSNames[i] = msn;
        mpa = null;
        try {
            mpa = new MediaPrintableArea(x, y, w, h, MediaPrintableArea.INCH);
        } catch (IllegalArgumentException e) {
            if (width > 0 && length > 0) {
                mpa = new MediaPrintableArea(0, 0, width, length, MediaPrintableArea.INCH);
            }
        }
        cupsMediaPrintables[i] = mpa;
    }
    // initialize trays
    cupsMediaTrays = new MediaTray[nTrays];
    MediaTray mt;
    for (int i = 0; i < nTrays; i++) {
        mt = new CustomMediaTray(media[(nPageSizes + i) * 2], media[(nPageSizes + i) * 2 + 1]);
        cupsMediaTrays[i] = mt;
    }
}
Also used : MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) CustomMediaTray(sun.print.CustomMediaTray) MediaTray(javax.print.attribute.standard.MediaTray) CustomMediaTray(sun.print.CustomMediaTray) CustomMediaSizeName(sun.print.CustomMediaSizeName)

Example 7 with MediaSize

use of javax.print.attribute.standard.MediaSize in project jdk8u_jdk by JetBrains.

the class UnixPrintService method getAllPrintableAreas.

private MediaPrintableArea[] getAllPrintableAreas() {
    if (mpas == null) {
        Media[] media = (Media[]) getSupportedAttributeValues(Media.class, null, null);
        mpas = new MediaPrintableArea[media.length];
        for (int i = 0; i < mpas.length; i++) {
            if (media[i] instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media[i];
                MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                if (mediaSize == null) {
                    mpas[i] = (MediaPrintableArea) getDefaultAttributeValue(MediaPrintableArea.class);
                } else {
                    mpas[i] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH) - 0.5f, mediaSize.getY(MediaSize.INCH) - 0.5f, MediaSize.INCH);
                }
            }
        }
    }
    MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
    System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
    return mpasCopy;
}
Also used : MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media)

Example 8 with MediaSize

use of javax.print.attribute.standard.MediaSize in project jdk8u_jdk by JetBrains.

the class Win32MediaSize method getMediaPrintables.

/*
     * Gets a list of MediaPrintableAreas using a call to native function.
     *  msn is MediaSizeName used to get a specific printable area.  If null,
     *  it will get all the supported MediPrintableAreas.
     */
private synchronized MediaPrintableArea[] getMediaPrintables(MediaSizeName msn) {
    if (msn == null) {
        if (mpaListInitialized == true) {
            return mediaPrintables;
        }
    } else {
        // get from cached mapping of MPAs
        if (mpaMap != null && (mpaMap.get(msn) != null)) {
            MediaPrintableArea[] mpaArr = new MediaPrintableArea[1];
            mpaArr[0] = (MediaPrintableArea) mpaMap.get(msn);
            return mpaArr;
        }
    }
    initMedia();
    if ((mediaSizeNames == null) || (mediaSizeNames.length == 0)) {
        return null;
    }
    MediaSizeName[] loopNames;
    if (msn != null) {
        loopNames = new MediaSizeName[1];
        loopNames[0] = msn;
    } else {
        loopNames = mediaSizeNames;
    }
    if (mpaMap == null) {
        mpaMap = new HashMap();
    }
    for (int i = 0; i < loopNames.length; i++) {
        MediaSizeName mediaName = loopNames[i];
        if (mpaMap.get(mediaName) != null) {
            continue;
        }
        if (mediaName != null) {
            int defPaper = findPaperID(mediaName);
            float[] prnArea = (defPaper != 0) ? getMediaPrintableArea(printer, defPaper) : null;
            MediaPrintableArea printableArea = null;
            if (prnArea != null) {
                try {
                    printableArea = new MediaPrintableArea(prnArea[0], prnArea[1], prnArea[2], prnArea[3], MediaPrintableArea.INCH);
                    mpaMap.put(mediaName, printableArea);
                } catch (IllegalArgumentException e) {
                }
            } else {
                // if getting  MPA failed, we use MediaSize
                MediaSize ms = MediaSize.getMediaSizeForName((MediaSizeName) mediaName);
                if (ms != null) {
                    try {
                        printableArea = new MediaPrintableArea(0, 0, ms.getX(MediaSize.INCH), ms.getY(MediaSize.INCH), MediaPrintableArea.INCH);
                        mpaMap.put(mediaName, printableArea);
                    } catch (IllegalArgumentException e) {
                    }
                }
            }
        }
    //mediaName != null
    }
    if (mpaMap.size() == 0) {
        return null;
    }
    if (msn != null) {
        if (mpaMap.get(msn) == null) {
            return null;
        }
        MediaPrintableArea[] mpaArr = new MediaPrintableArea[1];
        // by this time, we've already gotten the desired MPA
        mpaArr[0] = (MediaPrintableArea) mpaMap.get(msn);
        return mpaArr;
    } else {
        mediaPrintables = (MediaPrintableArea[]) mpaMap.values().toArray(new MediaPrintableArea[0]);
        mpaListInitialized = true;
        return mediaPrintables;
    }
}
Also used : MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) HashMap(java.util.HashMap)

Example 9 with MediaSize

use of javax.print.attribute.standard.MediaSize in project jdk8u_jdk by JetBrains.

the class Win32MediaSize method initMedia.

private synchronized void initMedia() {
    if (mediaInitialized == true) {
        return;
    }
    mediaInitialized = true;
    int[] media = getAllMediaIDs(printer, getPort());
    if (media == null) {
        return;
    }
    ArrayList msnList = new ArrayList();
    ArrayList<Win32MediaSize> trailingWmsList = new ArrayList<Win32MediaSize>();
    ArrayList printableList = new ArrayList();
    MediaSizeName mediaName;
    boolean added;
    boolean queryFailure = false;
    float[] prnArea;
    // Get all mediaSizes supported by the printer.
    // We convert media to ArrayList idList and pass this to the
    // function for getting mediaSizes.
    // This is to ensure that mediaSizes and media IDs have 1-1 correspondence.
    // We remove from ID list any invalid mediaSize.  Though this is rare,
    // it happens in HP 4050 German driver.
    idList = new ArrayList();
    for (int i = 0; i < media.length; i++) {
        idList.add(Integer.valueOf(media[i]));
    }
    ArrayList<String> dmPaperNameList = new ArrayList<String>();
    mediaSizes = getMediaSizes(idList, media, dmPaperNameList);
    for (int i = 0; i < idList.size(); i++) {
        // match Win ID with our predefined ID using table
        mediaName = findWin32Media(((Integer) idList.get(i)).intValue());
        // the driver is mis-using a standard windows paper ID.
        if (mediaName != null && idList.size() == mediaSizes.length) {
            MediaSize win32Size = MediaSize.getMediaSizeForName(mediaName);
            MediaSize driverSize = mediaSizes[i];
            // == 1/10"
            int error = 2540;
            if (Math.abs(win32Size.getX(1) - driverSize.getX(1)) > error || Math.abs(win32Size.getY(1) - driverSize.getY(1)) > error) {
                mediaName = null;
            }
        }
        boolean dmPaperIDMatched = (mediaName != null);
        // This requires 1-1 correspondence, lengths must be checked.
        if ((mediaName == null) && (idList.size() == mediaSizes.length)) {
            mediaName = mediaSizes[i].getMediaSizeName();
        }
        // Add mediaName to the msnList
        added = false;
        if (mediaName != null) {
            added = addToUniqueList(msnList, mediaName);
        }
        if ((!dmPaperIDMatched || !added) && (idList.size() == dmPaperNameList.size())) {
            /* The following block allows to add such media names to the list, whose sizes
                 * matched with media sizes predefined in JDK, while whose paper IDs did not,
                 * or whose sizes and paper IDs both did not match with any predefined in JDK.
                 */
            Win32MediaSize wms = Win32MediaSize.findMediaName(dmPaperNameList.get(i));
            if ((wms == null) && (idList.size() == mediaSizes.length)) {
                wms = new Win32MediaSize(dmPaperNameList.get(i), (Integer) idList.get(i));
                mediaSizes[i] = new MediaSize(mediaSizes[i].getX(MediaSize.MM), mediaSizes[i].getY(MediaSize.MM), MediaSize.MM, wms);
            }
            if ((wms != null) && (wms != mediaName)) {
                if (!added) {
                    added = addToUniqueList(msnList, mediaName = wms);
                } else {
                    trailingWmsList.add(wms);
                }
            }
        }
    }
    for (Win32MediaSize wms : trailingWmsList) {
        added = addToUniqueList(msnList, wms);
    }
    // init mediaSizeNames
    mediaSizeNames = new MediaSizeName[msnList.size()];
    msnList.toArray(mediaSizeNames);
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) ArrayList(java.util.ArrayList)

Example 10 with MediaSize

use of javax.print.attribute.standard.MediaSize in project jdk8u_jdk by JetBrains.

the class Win32MediaSize method getMediaSizes.

private MediaSize[] getMediaSizes(ArrayList idList, int[] media, ArrayList<String> dmPaperNameList) {
    if (dmPaperNameList == null) {
        dmPaperNameList = new ArrayList<String>();
    }
    String prnPort = getPort();
    int[] mediaSz = getAllMediaSizes(printer, prnPort);
    String[] winMediaNames = getAllMediaNames(printer, prnPort);
    MediaSizeName msn = null;
    MediaSize ms = null;
    float wid, ht;
    if ((mediaSz == null) || (winMediaNames == null)) {
        return null;
    }
    int nMedia = mediaSz.length / 2;
    ArrayList msList = new ArrayList();
    for (int i = 0; i < nMedia; i++, ms = null) {
        wid = mediaSz[i * 2] / 10f;
        ht = mediaSz[i * 2 + 1] / 10f;
        // returned is not constant.
        if ((wid <= 0) || (ht <= 0)) {
            //Remove corresponding ID from list
            if (nMedia == media.length) {
                Integer remObj = Integer.valueOf(media[i]);
                idList.remove(idList.indexOf(remObj));
            }
            continue;
        }
        // Find matching media using dimensions.
        // This call matches only with our own predefined sizes.
        msn = findMatchingMediaSizeNameMM(wid, ht);
        if (msn != null) {
            ms = MediaSize.getMediaSizeForName(msn);
        }
        if (ms != null) {
            msList.add(ms);
            dmPaperNameList.add(winMediaNames[i]);
        } else {
            Win32MediaSize wms = Win32MediaSize.findMediaName(winMediaNames[i]);
            if (wms == null) {
                wms = new Win32MediaSize(winMediaNames[i], media[i]);
            }
            try {
                ms = new MediaSize(wid, ht, MediaSize.MM, wms);
                msList.add(ms);
                dmPaperNameList.add(winMediaNames[i]);
            } catch (IllegalArgumentException e) {
                if (nMedia == media.length) {
                    Integer remObj = Integer.valueOf(media[i]);
                    idList.remove(idList.indexOf(remObj));
                }
            }
        }
    }
    MediaSize[] arr2 = new MediaSize[msList.size()];
    msList.toArray(arr2);
    return arr2;
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) ArrayList(java.util.ArrayList)

Aggregations

MediaSize (javax.print.attribute.standard.MediaSize)21 MediaSizeName (javax.print.attribute.standard.MediaSizeName)16 Media (javax.print.attribute.standard.Media)10 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)10 OrientationRequested (javax.print.attribute.standard.OrientationRequested)8 PageFormat (java.awt.print.PageFormat)6 Paper (java.awt.print.Paper)6 PrintService (javax.print.PrintService)5 JobName (javax.print.attribute.standard.JobName)4 Attribute (javax.print.attribute.Attribute)3 Fidelity (javax.print.attribute.standard.Fidelity)3 RequestingUserName (javax.print.attribute.standard.RequestingUserName)3 SheetCollate (javax.print.attribute.standard.SheetCollate)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 StreamPrintService (javax.print.StreamPrintService)2 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)2 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)2 Chromaticity (javax.print.attribute.standard.Chromaticity)2 CopiesSupported (javax.print.attribute.standard.CopiesSupported)2