89 import java.util.Locale;
90 import java.util.Properties;
91
92 import sun.awt.CharsetString;
93 import sun.awt.FontConfiguration;
94 import sun.awt.FontDescriptor;
95 import sun.awt.PlatformFont;
96 import sun.awt.SunToolkit;
97 import sun.font.FontAccess;
98 import sun.font.FontManagerFactory;
99 import sun.font.FontUtilities;
100
101 import java.nio.charset.*;
102 import java.nio.CharBuffer;
103 import java.nio.ByteBuffer;
104 import java.nio.file.Files;
105
106 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
107 import java.lang.reflect.Method;
108 import javax.print.attribute.standard.JobSheets;
109
110 /**
111 * A class which initiates and executes a PostScript printer job.
112 *
113 * @author Richard Blanchard
114 */
115 public class PSPrinterJob extends RasterPrinterJob {
116
117 /* Class Constants */
118
119 /**
120 * Passed to the {@code setFillMode}
121 * method this value forces fills to be
122 * done using the even-odd fill rule.
123 */
124 protected static final int FILL_EVEN_ODD = 1;
125
126 /**
127 * Passed to the {@code setFillMode}
128 * method this value forces fills to be
472 mDestination = "out.ps";
473 }
474 } else {
475 mDestType = RasterPrinterJob.PRINTER;
476 PrintService pServ = getPrintService();
477 if (pServ != null) {
478 mDestination = pServ.getName();
479 if (isMac) {
480 PrintServiceAttributeSet psaSet = pServ.getAttributes() ;
481 if (psaSet != null) {
482 mDestination = psaSet.get(PrinterName.class).toString();
483 }
484 }
485 }
486 }
487 }
488
489 return doPrint;
490 }
491
492 /**
493 * Invoked by the RasterPrinterJob super class
494 * this method is called to mark the start of a
495 * document.
496 */
497 protected void startDoc() throws PrinterException {
498
499 // A security check has been performed in the
500 // java.awt.print.printerJob.getPrinterJob method.
501 // We use an inner class to execute the privilged open operations.
502 // Note that we only open a file if it has been nominated by
503 // the end-user in a dialog that we ouselves put up.
504
505 OutputStream output = null;
506
507 if (epsPrinter == null) {
508 if (getPrintService() instanceof PSStreamPrintService) {
509 StreamPrintService sps = (StreamPrintService)getPrintService();
510 mDestType = RasterPrinterJob.STREAM;
511 if (sps.isDisposed()) {
|
89 import java.util.Locale;
90 import java.util.Properties;
91
92 import sun.awt.CharsetString;
93 import sun.awt.FontConfiguration;
94 import sun.awt.FontDescriptor;
95 import sun.awt.PlatformFont;
96 import sun.awt.SunToolkit;
97 import sun.font.FontAccess;
98 import sun.font.FontManagerFactory;
99 import sun.font.FontUtilities;
100
101 import java.nio.charset.*;
102 import java.nio.CharBuffer;
103 import java.nio.ByteBuffer;
104 import java.nio.file.Files;
105
106 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
107 import java.lang.reflect.Method;
108 import javax.print.attribute.standard.JobSheets;
109 import javax.print.attribute.standard.MediaPrintableArea;
110
111 /**
112 * A class which initiates and executes a PostScript printer job.
113 *
114 * @author Richard Blanchard
115 */
116 public class PSPrinterJob extends RasterPrinterJob {
117
118 /* Class Constants */
119
120 /**
121 * Passed to the {@code setFillMode}
122 * method this value forces fills to be
123 * done using the even-odd fill rule.
124 */
125 protected static final int FILL_EVEN_ODD = 1;
126
127 /**
128 * Passed to the {@code setFillMode}
129 * method this value forces fills to be
473 mDestination = "out.ps";
474 }
475 } else {
476 mDestType = RasterPrinterJob.PRINTER;
477 PrintService pServ = getPrintService();
478 if (pServ != null) {
479 mDestination = pServ.getName();
480 if (isMac) {
481 PrintServiceAttributeSet psaSet = pServ.getAttributes() ;
482 if (psaSet != null) {
483 mDestination = psaSet.get(PrinterName.class).toString();
484 }
485 }
486 }
487 }
488 }
489
490 return doPrint;
491 }
492
493 private boolean isSupportedMediaPrintableArea(MediaPrintableArea mpa) {
494 int units = MediaPrintableArea.INCH;
495 MediaPrintableArea[] mediaPrintables =
496 new CUPSPrinter(getPrintService().getName()).getMediaPrintableArea();
497 if (mediaPrintables != null) {
498 for (int i=0; i<mediaPrintables.length; i++) {
499 if ((mpa.getX(units) >= mediaPrintables[i].getX(units)) &&
500 (mpa.getY(units) >= mediaPrintables[i].getY(units)) &&
501 (mpa.getX(units) + mpa.getWidth(units) <=
502 mediaPrintables[i].getX(units) +
503 mediaPrintables[i].getWidth(units)) &&
504 (mpa.getY(units) + mpa.getHeight(units) <=
505 mediaPrintables[i].getY(units) +
506 mediaPrintables[i].getHeight(units))) {
507 return true;
508 }
509 }
510 }
511 return false;
512 }
513
514 @Override
515 protected void validatePaper(Paper origPaper, Paper newPaper) {
516 if (origPaper == null || newPaper == null) {
517 return;
518 } else {
519 double wid = origPaper.getWidth();
520 double hgt = origPaper.getHeight();
521 double ix = origPaper.getImageableX();
522 double iy = origPaper.getImageableY();
523 double iw = origPaper.getImageableWidth();
524 double ih = origPaper.getImageableHeight();
525 double imgX, imgY, imgWid, imgHgt;
526
527 MediaPrintableArea mpa = null;
528 MediaPrintableArea origmpa = new MediaPrintableArea((float)ix/72.0f,
529 (float)iy/72.0f, (float)iw/72.0f, (float)ih/72.0f, MediaPrintableArea.INCH);
530
531 if (isSupportedMediaPrintableArea(origmpa)) {
532 mpa = origmpa;
533 } else {
534 /*
535 * this returns the imageable area of default media of chosen printer
536 * from CUPS via CUPSPrinter#getPageSizes().
537 */
538 mpa = (MediaPrintableArea)
539 getPrintService().getDefaultAttributeValue(MediaPrintableArea.class);
540 }
541 if (mpa != null) {
542 imgX = mpa.getX(MediaPrintableArea.INCH) * 72;
543 imgY = mpa.getY(MediaPrintableArea.INCH) * 72;
544 imgWid = mpa.getWidth(MediaPrintableArea.INCH) * 72;
545 imgHgt = mpa.getHeight(MediaPrintableArea.INCH) * 72;
546 } else {
547 imgX = newPaper.getImageableX();
548 imgY = newPaper.getImageableY();
549 imgWid = newPaper.getImageableWidth();
550 imgHgt = newPaper.getImageableHeight();
551 }
552
553 wid = ((wid > 0.0) ? wid : newPaper.getWidth());
554 hgt = ((hgt > 0.0) ? hgt : newPaper.getHeight());
555 if ((imgX*2) + imgWid > wid) {
556 imgWid = wid - imgX*2;
557 }
558 if ((imgY*2) + imgHgt > hgt) {
559 imgHgt = hgt - imgY*2;
560 }
561
562 /* We try to mitigate the effects of floating point rounding errors
563 * by only setting a value if it would differ from the value in the
564 * target by at least 0.10 points = 1/720 inches.
565 * eg if the values present in the target are close to the calculated
566 * values then we accept the target.
567 */
568 final double epsilon = 0.10;
569
570 if (ix < 0.0) {
571 ix = 0.0;
572 }
573 if (iy < 0.0) {
574 iy = 0.0;
575 }
576 if (iw < 0.0) {
577 iw = 0.0;
578 }
579 if (ih < 0.0) {
580 ih = 0.0;
581 }
582 if ((ix + epsilon) < imgX) {
583 ix = imgX;
584 }
585 if ((iy + epsilon) < imgY) {
586 iy = imgY;
587 }
588 if (iw + epsilon > imgWid) {
589 iw = imgWid;
590 }
591 if (ih + epsilon > imgHgt) {
592 ih = imgHgt;
593 }
594 if ((ix + iw + epsilon) > (imgX + imgWid)) {
595 ix = (imgX + imgWid) - iw;
596 }
597 if ((iy + ih + epsilon) > (imgY + imgHgt)) {
598 iy = (imgY + imgHgt) - ih;
599 }
600
601 newPaper.setSize(wid, hgt);
602 newPaper.setImageableArea(ix, iy, iw, ih);
603 }
604 }
605
606
607 /**
608 * Invoked by the RasterPrinterJob super class
609 * this method is called to mark the start of a
610 * document.
611 */
612 protected void startDoc() throws PrinterException {
613
614 // A security check has been performed in the
615 // java.awt.print.printerJob.getPrinterJob method.
616 // We use an inner class to execute the privilged open operations.
617 // Note that we only open a file if it has been nominated by
618 // the end-user in a dialog that we ouselves put up.
619
620 OutputStream output = null;
621
622 if (epsPrinter == null) {
623 if (getPrintService() instanceof PSStreamPrintService) {
624 StreamPrintService sps = (StreamPrintService)getPrintService();
625 mDestType = RasterPrinterJob.STREAM;
626 if (sps.isDisposed()) {
|