< prev index next >

src/java.desktop/share/classes/sun/print/PSPrinterJob.java

Print this page




  51 import java.awt.print.PageFormat;
  52 import java.awt.print.Paper;
  53 import java.awt.print.Printable;
  54 import java.awt.print.PrinterException;
  55 import java.awt.print.PrinterIOException;
  56 import java.awt.print.PrinterJob;
  57 
  58 import javax.print.DocFlavor;
  59 import javax.print.PrintService;
  60 import javax.print.StreamPrintService;
  61 import javax.print.attribute.HashPrintRequestAttributeSet;
  62 import javax.print.attribute.PrintRequestAttributeSet;
  63 import javax.print.attribute.PrintServiceAttributeSet;
  64 import javax.print.attribute.standard.PrinterName;
  65 import javax.print.attribute.standard.Chromaticity;
  66 import javax.print.attribute.standard.Copies;
  67 import javax.print.attribute.standard.Destination;
  68 import javax.print.attribute.standard.DialogTypeSelection;
  69 import javax.print.attribute.standard.JobName;
  70 import javax.print.attribute.standard.Sides;

  71 
  72 import java.io.BufferedInputStream;
  73 import java.io.BufferedOutputStream;
  74 import java.io.BufferedReader;
  75 import java.io.CharConversionException;
  76 import java.io.File;
  77 import java.io.InputStream;
  78 import java.io.InputStreamReader;
  79 import java.io.IOException;
  80 import java.io.FileInputStream;
  81 import java.io.FileOutputStream;
  82 import java.io.OutputStream;
  83 import java.io.PrintStream;
  84 import java.io.PrintWriter;
  85 import java.io.StringWriter;
  86 
  87 import java.util.ArrayList;
  88 import java.util.Enumeration;
  89 import java.util.Locale;
  90 import java.util.Properties;


 470                     mDestination = (new File(dest.getURI())).getPath();
 471                 } catch (Exception e) {
 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();




  51 import java.awt.print.PageFormat;
  52 import java.awt.print.Paper;
  53 import java.awt.print.Printable;
  54 import java.awt.print.PrinterException;
  55 import java.awt.print.PrinterIOException;
  56 import java.awt.print.PrinterJob;
  57 
  58 import javax.print.DocFlavor;
  59 import javax.print.PrintService;
  60 import javax.print.StreamPrintService;
  61 import javax.print.attribute.HashPrintRequestAttributeSet;
  62 import javax.print.attribute.PrintRequestAttributeSet;
  63 import javax.print.attribute.PrintServiceAttributeSet;
  64 import javax.print.attribute.standard.PrinterName;
  65 import javax.print.attribute.standard.Chromaticity;
  66 import javax.print.attribute.standard.Copies;
  67 import javax.print.attribute.standard.Destination;
  68 import javax.print.attribute.standard.DialogTypeSelection;
  69 import javax.print.attribute.standard.JobName;
  70 import javax.print.attribute.standard.Sides;
  71 import javax.print.attribute.standard.MediaPrintableArea;
  72 
  73 import java.io.BufferedInputStream;
  74 import java.io.BufferedOutputStream;
  75 import java.io.BufferedReader;
  76 import java.io.CharConversionException;
  77 import java.io.File;
  78 import java.io.InputStream;
  79 import java.io.InputStreamReader;
  80 import java.io.IOException;
  81 import java.io.FileInputStream;
  82 import java.io.FileOutputStream;
  83 import java.io.OutputStream;
  84 import java.io.PrintStream;
  85 import java.io.PrintWriter;
  86 import java.io.StringWriter;
  87 
  88 import java.util.ArrayList;
  89 import java.util.Enumeration;
  90 import java.util.Locale;
  91 import java.util.Properties;


 471                     mDestination = (new File(dest.getURI())).getPath();
 472                 } catch (Exception e) {
 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     @Override
 494     protected void validatePaper(Paper origPaper, Paper newPaper) {
 495         if (origPaper == null || newPaper == null) {
 496             return;
 497         } else {
 498             double wid = origPaper.getWidth();
 499             double hgt = origPaper.getHeight();
 500             double ix = origPaper.getImageableX();
 501             double iy = origPaper.getImageableY();
 502             double iw = origPaper.getImageableWidth();
 503             double ih = origPaper.getImageableHeight();
 504             double imgX, imgY, imgWid, imgHgt;            
 505             
 506             /* 
 507              * this returns the imageable area of default media of chosen printer
 508              * from CUPS via CUPSPrinter#getPageSizes().
 509              * If it returns null (which should not be the case) we fall back
 510              * to default Paper (Letter in US, A4 otherwise) imageable area
 511              */
 512             MediaPrintableArea mpa = (MediaPrintableArea)
 513                     getPrintService().getDefaultAttributeValue(MediaPrintableArea.class);
 514             if (mpa != null) {
 515                 imgX = mpa.getX(MediaPrintableArea.INCH) * 72;
 516                 imgY = mpa.getY(MediaPrintableArea.INCH) * 72;
 517                 imgWid = mpa.getWidth(MediaPrintableArea.INCH) * 72;
 518                 imgHgt = mpa.getHeight(MediaPrintableArea.INCH) * 72;
 519             } else {
 520                 imgX = newPaper.getImageableX();
 521                 imgY = newPaper.getImageableY();
 522                 imgWid = newPaper.getImageableWidth();
 523                 imgHgt = newPaper.getImageableHeight();
 524             }
 525             
 526             wid = ((wid > 0.0) ? wid : newPaper.getWidth());
 527             hgt = ((hgt > 0.0) ? hgt : newPaper.getHeight());
 528             if ((imgX*2) + imgWid > wid) {
 529                 imgWid = wid - imgX*2;
 530             }
 531             if ((imgY*2) + imgHgt > hgt) {
 532                 imgHgt = hgt - imgY*2;
 533             }
 534             
 535             /* We try to mitigate the effects of floating point rounding errors
 536              * by only setting a value if it would differ from the value in the
 537              * target by at least 0.10 points = 1/720 inches.
 538              * eg if the values present in the target are close to the calculated
 539              * values then we accept the target.
 540              */
 541             final double epsilon = 0.10;
 542             
 543             if (ix < 0.0) {
 544                 ix = 0.0;
 545             }
 546             if (iy < 0.0) {
 547                 iy = 0.0;
 548             }
 549             if (iw < 0.0) {
 550                 iw = 0.0;
 551             }
 552             if (ih < 0.0) {
 553                 ih = 0.0;
 554             }
 555             if ((ix + epsilon) < imgX) {
 556                 ix = imgX;
 557             }
 558             if ((iy + epsilon) < imgY) {
 559                 iy = imgY;
 560             }
 561             if (iw + epsilon > imgWid) {
 562                 iw = imgWid;
 563             }
 564             if (ih + epsilon > imgHgt) {
 565                 ih = imgHgt;
 566             }
 567             if ((ix + iw + epsilon) > (imgX + imgWid)) {
 568                 ix = (imgX + imgWid) - iw;
 569             }
 570             if ((iy + ih + epsilon) > (imgY + imgHgt)) {
 571                 iy = (imgY + imgHgt) - ih;
 572             }
 573             
 574             newPaper.setSize(wid, hgt);
 575             newPaper.setImageableArea(ix, iy, iw, ih);
 576         }
 577     }
 578 
 579     /**
 580      * Invoked by the RasterPrinterJob super class
 581      * this method is called to mark the start of a
 582      * document.
 583      */
 584     protected void startDoc() throws PrinterException {
 585 
 586         // A security check has been performed in the
 587         // java.awt.print.printerJob.getPrinterJob method.
 588         // We use an inner class to execute the privilged open operations.
 589         // Note that we only open a file if it has been nominated by
 590         // the end-user in a dialog that we ouselves put up.
 591 
 592         OutputStream output = null;
 593 
 594         if (epsPrinter == null) {
 595             if (getPrintService() instanceof PSStreamPrintService) {
 596                 StreamPrintService sps = (StreamPrintService)getPrintService();


< prev index next >