Search in sources :

Example 6 with Media

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

the class PSStreamPrintService method getSupportedAttributeValues.

public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
    if (category == null) {
        throw new NullPointerException("null category");
    }
    if (!Attribute.class.isAssignableFrom(category)) {
        throw new IllegalArgumentException(category + " does not implement Attribute");
    }
    if (flavor != null && !isDocFlavorSupported(flavor)) {
        throw new IllegalArgumentException(flavor + " is an unsupported flavor");
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    if (category == Chromaticity.class) {
        Chromaticity[] arr = new Chromaticity[1];
        arr[0] = Chromaticity.COLOR;
        //arr[1] = Chromaticity.MONOCHROME;
        return (arr);
    } else if (category == JobName.class) {
        return new JobName("", null);
    } else if (category == RequestingUserName.class) {
        return new RequestingUserName("", null);
    } else if (category == OrientationRequested.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            OrientationRequested[] arr = new OrientationRequested[3];
            arr[0] = OrientationRequested.PORTRAIT;
            arr[1] = OrientationRequested.LANDSCAPE;
            arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
            return arr;
        } else {
            return null;
        }
    } else if ((category == Copies.class) || (category == CopiesSupported.class)) {
        return new CopiesSupported(1, MAXCOPIES);
    } else if (category == Media.class) {
        Media[] arr = new Media[mediaSizes.length];
        System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);
        return arr;
    } else if (category == Fidelity.class) {
        Fidelity[] arr = new Fidelity[2];
        arr[0] = Fidelity.FIDELITY_FALSE;
        arr[1] = Fidelity.FIDELITY_TRUE;
        return arr;
    } else if (category == MediaPrintableArea.class) {
        if (attributes == null) {
            return null;
        }
        MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
        if (mediaSize == null) {
            Media media = (Media) attributes.get(Media.class);
            if (media != null && media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                mediaSize = MediaSize.getMediaSizeForName(msn);
            }
        }
        if (mediaSize == null) {
            return null;
        } else {
            MediaPrintableArea[] arr = new MediaPrintableArea[1];
            float w = mediaSize.getX(MediaSize.INCH);
            float h = mediaSize.getY(MediaSize.INCH);
            /* For dimensions >= 5 inches use 0.5 inch margins.
                 * For smaller dimensions, use 10% margins.
                 */
            float xmargin = 0.5f;
            float ymargin = 0.5f;
            if (w < 5f) {
                xmargin = w / 10;
            }
            if (h < 5f) {
                ymargin = h / 10;
            }
            arr[0] = new MediaPrintableArea(xmargin, ymargin, w - 2 * xmargin, h - 2 * ymargin, MediaSize.INCH);
            return arr;
        }
    } else if (category == PageRanges.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            PageRanges[] arr = new PageRanges[1];
            arr[0] = new PageRanges(1, Integer.MAX_VALUE);
            return arr;
        } else {
            return null;
        }
    } else if (category == SheetCollate.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            SheetCollate[] arr = new SheetCollate[2];
            arr[0] = SheetCollate.UNCOLLATED;
            arr[1] = SheetCollate.COLLATED;
            return arr;
        } else {
            SheetCollate[] arr = new SheetCollate[1];
            arr[0] = SheetCollate.UNCOLLATED;
            return arr;
        }
    } else if (category == Sides.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            Sides[] arr = new Sides[3];
            arr[0] = Sides.ONE_SIDED;
            arr[1] = Sides.TWO_SIDED_LONG_EDGE;
            arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
            return arr;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) PageRanges(javax.print.attribute.standard.PageRanges) MediaSize(javax.print.attribute.standard.MediaSize) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) CopiesSupported(javax.print.attribute.standard.CopiesSupported) Chromaticity(javax.print.attribute.standard.Chromaticity) Sides(javax.print.attribute.standard.Sides)

Example 7 with Media

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

the class PrintJob2D method updateAttributes.

private void updateAttributes() {
    Copies c = (Copies) attributes.get(Copies.class);
    jobAttributes.setCopies(c.getValue());
    SunPageSelection sel = (SunPageSelection) attributes.get(SunPageSelection.class);
    if (sel == SunPageSelection.RANGE) {
        jobAttributes.setDefaultSelection(DefaultSelectionType.RANGE);
    } else if (sel == SunPageSelection.SELECTION) {
        jobAttributes.setDefaultSelection(DefaultSelectionType.SELECTION);
    } else {
        jobAttributes.setDefaultSelection(DefaultSelectionType.ALL);
    }
    Destination dest = (Destination) attributes.get(Destination.class);
    if (dest != null) {
        jobAttributes.setDestination(DestinationType.FILE);
        jobAttributes.setFileName(dest.getURI().getPath());
    } else {
        jobAttributes.setDestination(DestinationType.PRINTER);
    }
    PrintService serv = printerJob.getPrintService();
    if (serv != null) {
        jobAttributes.setPrinter(serv.getName());
    }
    PageRanges range = (PageRanges) attributes.get(PageRanges.class);
    int[][] members = range.getMembers();
    jobAttributes.setPageRanges(members);
    SheetCollate collation = (SheetCollate) attributes.get(SheetCollate.class);
    if (collation == SheetCollate.COLLATED) {
        jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES);
    } else {
        jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
    }
    Sides sides = (Sides) attributes.get(Sides.class);
    if (sides == Sides.TWO_SIDED_LONG_EDGE) {
        jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);
    } else if (sides == Sides.TWO_SIDED_SHORT_EDGE) {
        jobAttributes.setSides(SidesType.TWO_SIDED_SHORT_EDGE);
    } else {
        jobAttributes.setSides(SidesType.ONE_SIDED);
    }
    // PageAttributes
    Chromaticity color = (Chromaticity) attributes.get(Chromaticity.class);
    if (color == Chromaticity.COLOR) {
        pageAttributes.setColor(ColorType.COLOR);
    } else {
        pageAttributes.setColor(ColorType.MONOCHROME);
    }
    OrientationRequested orient = (OrientationRequested) attributes.get(OrientationRequested.class);
    if (orient == OrientationRequested.LANDSCAPE) {
        pageAttributes.setOrientationRequested(OrientationRequestedType.LANDSCAPE);
    } else {
        pageAttributes.setOrientationRequested(OrientationRequestedType.PORTRAIT);
    }
    PrintQuality qual = (PrintQuality) attributes.get(PrintQuality.class);
    if (qual == PrintQuality.DRAFT) {
        pageAttributes.setPrintQuality(PrintQualityType.DRAFT);
    } else if (qual == PrintQuality.HIGH) {
        pageAttributes.setPrintQuality(PrintQualityType.HIGH);
    } else {
        // NORMAL
        pageAttributes.setPrintQuality(PrintQualityType.NORMAL);
    }
    Media msn = (Media) attributes.get(Media.class);
    if (msn != null && msn instanceof MediaSizeName) {
        MediaType mType = unMapMedia((MediaSizeName) msn);
        if (mType != null) {
            pageAttributes.setMedia(mType);
        }
    }
    debugPrintAttributes(false, false);
}
Also used : Destination(javax.print.attribute.standard.Destination) PageRanges(javax.print.attribute.standard.PageRanges) MediaSizeName(javax.print.attribute.standard.MediaSizeName) PrintQuality(javax.print.attribute.standard.PrintQuality) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService) SheetCollate(javax.print.attribute.standard.SheetCollate) Copies(javax.print.attribute.standard.Copies) SunPageSelection(sun.print.SunPageSelection) Chromaticity(javax.print.attribute.standard.Chromaticity) Sides(javax.print.attribute.standard.Sides)

Example 8 with Media

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

the class RasterPrinterJob method attributeToPageFormat.

private PageFormat attributeToPageFormat(PrintService service, PrintRequestAttributeSet attSet) {
    PageFormat page = defaultPage();
    if (service == null) {
        return page;
    }
    OrientationRequested orient = (OrientationRequested) attSet.get(OrientationRequested.class);
    if (orient == null) {
        orient = (OrientationRequested) service.getDefaultAttributeValue(OrientationRequested.class);
    }
    if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
        page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
    } else if (orient == OrientationRequested.LANDSCAPE) {
        page.setOrientation(PageFormat.LANDSCAPE);
    } else {
        page.setOrientation(PageFormat.PORTRAIT);
    }
    Media media = (Media) attSet.get(Media.class);
    MediaSize size = getMediaSize(media, service, page);
    Paper paper = new Paper();
    //units == 1 to avoid FP error
    float[] dim = size.getSize(1);
    double w = Math.rint((dim[0] * 72.0) / Size2DSyntax.INCH);
    double h = Math.rint((dim[1] * 72.0) / Size2DSyntax.INCH);
    paper.setSize(w, h);
    MediaPrintableArea area = (MediaPrintableArea) attSet.get(MediaPrintableArea.class);
    if (area == null) {
        area = getDefaultPrintableArea(page, w, h);
    }
    double ix, iw, iy, ih;
    // Should pass in same unit as updatePageAttributes
    // to avoid rounding off errors.
    ix = Math.rint(area.getX(MediaPrintableArea.INCH) * DPI);
    iy = Math.rint(area.getY(MediaPrintableArea.INCH) * DPI);
    iw = Math.rint(area.getWidth(MediaPrintableArea.INCH) * DPI);
    ih = Math.rint(area.getHeight(MediaPrintableArea.INCH) * DPI);
    paper.setImageableArea(ix, iy, iw, ih);
    page.setPaper(paper);
    return page;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) OrientationRequested(javax.print.attribute.standard.OrientationRequested)

Example 9 with Media

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

the class RasterPrinterJob method updateAttributesWithPageFormat.

protected void updateAttributesWithPageFormat(PrintService service, PageFormat page, PrintRequestAttributeSet pageAttributes) {
    if (service == null || page == null || pageAttributes == null) {
        return;
    }
    float x = (float) Math.rint((page.getPaper().getWidth() * Size2DSyntax.INCH) / (72.0)) / (float) Size2DSyntax.INCH;
    float y = (float) Math.rint((page.getPaper().getHeight() * Size2DSyntax.INCH) / (72.0)) / (float) Size2DSyntax.INCH;
    // We should limit the list where we search the matching
    // media, this will prevent mapping to wrong media ex. Ledger
    // can be mapped to B.  Especially useful when creating
    // custom MediaSize.
    Media[] mediaList = (Media[]) service.getSupportedAttributeValues(Media.class, null, null);
    Media media = null;
    try {
        media = CustomMediaSizeName.findMedia(mediaList, x, y, Size2DSyntax.INCH);
    } catch (IllegalArgumentException iae) {
    }
    if ((media == null) || !(service.isAttributeValueSupported(media, null, null))) {
        media = (Media) service.getDefaultAttributeValue(Media.class);
    }
    OrientationRequested orient;
    switch(page.getOrientation()) {
        case PageFormat.LANDSCAPE:
            orient = OrientationRequested.LANDSCAPE;
            break;
        case PageFormat.REVERSE_LANDSCAPE:
            orient = OrientationRequested.REVERSE_LANDSCAPE;
            break;
        default:
            orient = OrientationRequested.PORTRAIT;
    }
    if (media != null) {
        pageAttributes.add(media);
    }
    pageAttributes.add(orient);
    float ix = (float) (page.getPaper().getImageableX() / DPI);
    float iw = (float) (page.getPaper().getImageableWidth() / DPI);
    float iy = (float) (page.getPaper().getImageableY() / DPI);
    float ih = (float) (page.getPaper().getImageableHeight() / DPI);
    if (ix < 0)
        ix = 0f;
    if (iy < 0)
        iy = 0f;
    try {
        pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih, MediaPrintableArea.INCH));
    } catch (IllegalArgumentException iae) {
    }
}
Also used : MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested)

Example 10 with Media

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

the class RasterPrinterJob method defaultPage.

/**
     * The passed in PageFormat will be copied and altered to describe
     * the default page size and orientation of the PrinterJob's
     * current printer.
     * Platform subclasses which can access the actual default paper size
     * for a printer may override this method.
     */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat) page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;
    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media = (Media) service.getDefaultAttributeValue(Media.class);
        if (media instanceof MediaSizeName && ((size = MediaSize.getMediaSizeForName((MediaSizeName) media)) != null)) {
            w = size.getX(MediaSize.INCH) * ptsPerInch;
            h = size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch, w - 2.0 * ptsPerInch, h - 2.0 * ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;
        }
    }
    /* Default to A4 paper outside North America.
         */
    String defaultCountry = Locale.getDefault().getCountry();
    if (// ie "C"
    !Locale.getDefault().equals(Locale.ENGLISH) && defaultCountry != null && !defaultCountry.equals(Locale.US.getCountry()) && !defaultCountry.equals(Locale.CANADA.getCountry())) {
        double mmPerInch = 25.4;
        w = Math.rint((210.0 * ptsPerInch) / mmPerInch);
        h = Math.rint((297.0 * ptsPerInch) / mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch, w - 2.0 * ptsPerInch, h - 2.0 * ptsPerInch);
    }
    newPage.setPaper(newPaper);
    return newPage;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Aggregations

Media (javax.print.attribute.standard.Media)17 MediaSizeName (javax.print.attribute.standard.MediaSizeName)14 OrientationRequested (javax.print.attribute.standard.OrientationRequested)12 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)10 MediaSize (javax.print.attribute.standard.MediaSize)10 Chromaticity (javax.print.attribute.standard.Chromaticity)7 SheetCollate (javax.print.attribute.standard.SheetCollate)7 PageFormat (java.awt.print.PageFormat)5 Paper (java.awt.print.Paper)5 PrintService (javax.print.PrintService)5 Destination (javax.print.attribute.standard.Destination)5 JobName (javax.print.attribute.standard.JobName)5 PageRanges (javax.print.attribute.standard.PageRanges)5 Sides (javax.print.attribute.standard.Sides)5 Attribute (javax.print.attribute.Attribute)4 Fidelity (javax.print.attribute.standard.Fidelity)4 MediaTray (javax.print.attribute.standard.MediaTray)4 PrintQuality (javax.print.attribute.standard.PrintQuality)4 RequestingUserName (javax.print.attribute.standard.RequestingUserName)4 File (java.io.File)3