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

Print this page




  29 import java.awt.Component;
  30 import java.awt.Font;
  31 import java.awt.FontMetrics;
  32 import java.awt.GraphicsEnvironment;
  33 import java.awt.Graphics;
  34 import java.awt.Graphics2D;
  35 import java.awt.HeadlessException;
  36 import java.awt.Rectangle;
  37 import java.awt.Shape;
  38 
  39 import java.awt.image.BufferedImage;
  40 
  41 import java.awt.font.FontRenderContext;
  42 
  43 import java.awt.geom.AffineTransform;
  44 import java.awt.geom.PathIterator;
  45 import java.awt.geom.Rectangle2D;
  46 
  47 import java.awt.image.BufferedImage;
  48 

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


  76 import java.io.InputStream;
  77 import java.io.InputStreamReader;
  78 import java.io.IOException;
  79 import java.io.FileInputStream;
  80 import java.io.FileOutputStream;
  81 import java.io.OutputStream;
  82 import java.io.PrintStream;
  83 import java.io.PrintWriter;
  84 import java.io.StringWriter;
  85 
  86 import java.util.ArrayList;
  87 import java.util.Enumeration;
  88 import java.util.Locale;
  89 import java.util.Properties;
  90 
  91 import sun.awt.CharsetString;
  92 import sun.awt.FontConfiguration;
  93 import sun.awt.FontDescriptor;
  94 import sun.awt.PlatformFont;
  95 import sun.awt.SunToolkit;

  96 import sun.font.FontManagerFactory;
  97 import sun.font.FontUtilities;
  98 
  99 import java.nio.charset.*;
 100 import java.nio.CharBuffer;
 101 import java.nio.ByteBuffer;
 102 import java.nio.file.Files;
 103 
 104 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
 105 import java.lang.reflect.Method;
 106 
 107 /**
 108  * A class which initiates and executes a PostScript printer job.
 109  *
 110  * @author Richard Blanchard
 111  */
 112 public class PSPrinterJob extends RasterPrinterJob {
 113 
 114  /* Class Constants */
 115 


1197                 pos++;
1198             }
1199             char []inArr = str.toCharArray();
1200             char []outArr = new char[inArr.length+count];
1201             pos = 0;
1202             for (int i=0;i<inArr.length;i++) {
1203                 if (inArr[i] == '(' || inArr[i] == ')') {
1204                     outArr[pos++] = '\\';
1205                 }
1206                 outArr[pos++] = inArr[i];
1207             }
1208             return new String(outArr);
1209 
1210         }
1211     }
1212 
1213     /* return of 0 means unsupported. Other return indicates the number
1214      * of distinct PS fonts needed to draw this text. This saves us
1215      * doing this processing one extra time.
1216      */
1217     @SuppressWarnings("deprecation")
1218     protected int platformFontCount(Font font, String str) {
1219         if (mFontProps == null) {
1220             return 0;
1221         }
1222         CharsetString[] acs =
1223             ((PlatformFont)(font.getPeer())).makeMultiCharsetString(str,false);

1224         if (acs == null) {
1225             /* AWT can't convert all chars so use 2D path */
1226             return 0;
1227         }
1228         int[] psFonts = getPSFontIndexArray(font, acs);
1229         return (psFonts == null) ? 0 : psFonts.length;
1230     }
1231 
1232     @SuppressWarnings("deprecation")
1233      protected boolean textOut(Graphics g, String str, float x, float y,
1234                                Font mLastFont, FontRenderContext frc,
1235                                float width) {
1236         boolean didText = true;
1237 
1238         if (mFontProps == null) {
1239             return false;
1240         } else {
1241             prepDrawing();
1242 
1243             /* On-screen drawString renders most control chars as the missing
1244              * glyph and have the non-zero advance of that glyph.
1245              * Exceptions are \t, \n and \r which are considered zero-width.
1246              * Postscript handles control chars mostly as a missing glyph.
1247              * But we use 'ashow' specifying a width for the string which
1248              * assumes zero-width for those three exceptions, and Postscript
1249              * tries to squeeze the extra char in, with the result that the
1250              * glyphs look compressed or even overlap.
1251              * So exclude those control chars from the string sent to PS.
1252              */
1253             str = removeControlChars(str);
1254             if (str.length() == 0) {
1255                 return true;
1256             }
1257             CharsetString[] acs =
1258                 ((PlatformFont)
1259                  (mLastFont.getPeer())).makeMultiCharsetString(str, false);
1260             if (acs == null) {
1261                 /* AWT can't convert all chars so use 2D path */
1262                 return false;
1263             }
1264             /* Get an array of indices into our PostScript name
1265              * table. If all of the runs can not be converted
1266              * to PostScript fonts then null is returned and
1267              * we'll want to fall back to printing the text
1268              * as shapes.
1269              */
1270             int[] psFonts = getPSFontIndexArray(mLastFont, acs);
1271             if (psFonts != null) {
1272 
1273                 for (int i = 0; i < acs.length; i++){
1274                     CharsetString cs = acs[i];
1275                     CharsetEncoder fontCS = cs.fontDescriptor.encoder;
1276 
1277                     StringBuilder nativeStr = new StringBuilder();
1278                     byte[] strSeg = new byte[cs.length * 2];
1279                     int len = 0;




  29 import java.awt.Component;
  30 import java.awt.Font;
  31 import java.awt.FontMetrics;
  32 import java.awt.GraphicsEnvironment;
  33 import java.awt.Graphics;
  34 import java.awt.Graphics2D;
  35 import java.awt.HeadlessException;
  36 import java.awt.Rectangle;
  37 import java.awt.Shape;
  38 
  39 import java.awt.image.BufferedImage;
  40 
  41 import java.awt.font.FontRenderContext;
  42 
  43 import java.awt.geom.AffineTransform;
  44 import java.awt.geom.PathIterator;
  45 import java.awt.geom.Rectangle2D;
  46 
  47 import java.awt.image.BufferedImage;
  48 
  49 import java.awt.peer.FontPeer;
  50 import java.awt.print.Pageable;
  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;


  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;
  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 
 109 /**
 110  * A class which initiates and executes a PostScript printer job.
 111  *
 112  * @author Richard Blanchard
 113  */
 114 public class PSPrinterJob extends RasterPrinterJob {
 115 
 116  /* Class Constants */
 117 


1199                 pos++;
1200             }
1201             char []inArr = str.toCharArray();
1202             char []outArr = new char[inArr.length+count];
1203             pos = 0;
1204             for (int i=0;i<inArr.length;i++) {
1205                 if (inArr[i] == '(' || inArr[i] == ')') {
1206                     outArr[pos++] = '\\';
1207                 }
1208                 outArr[pos++] = inArr[i];
1209             }
1210             return new String(outArr);
1211 
1212         }
1213     }
1214 
1215     /* return of 0 means unsupported. Other return indicates the number
1216      * of distinct PS fonts needed to draw this text. This saves us
1217      * doing this processing one extra time.
1218      */

1219     protected int platformFontCount(Font font, String str) {
1220         if (mFontProps == null) {
1221             return 0;
1222         }
1223         PlatformFont peer = (PlatformFont) FontAccess.getFontAccess()
1224                                                      .getFontPeer(font);
1225         CharsetString[] acs = peer.makeMultiCharsetString(str, false);
1226         if (acs == null) {
1227             /* AWT can't convert all chars so use 2D path */
1228             return 0;
1229         }
1230         int[] psFonts = getPSFontIndexArray(font, acs);
1231         return (psFonts == null) ? 0 : psFonts.length;
1232     }
1233 

1234      protected boolean textOut(Graphics g, String str, float x, float y,
1235                                Font mLastFont, FontRenderContext frc,
1236                                float width) {
1237         boolean didText = true;
1238 
1239         if (mFontProps == null) {
1240             return false;
1241         } else {
1242             prepDrawing();
1243 
1244             /* On-screen drawString renders most control chars as the missing
1245              * glyph and have the non-zero advance of that glyph.
1246              * Exceptions are \t, \n and \r which are considered zero-width.
1247              * Postscript handles control chars mostly as a missing glyph.
1248              * But we use 'ashow' specifying a width for the string which
1249              * assumes zero-width for those three exceptions, and Postscript
1250              * tries to squeeze the extra char in, with the result that the
1251              * glyphs look compressed or even overlap.
1252              * So exclude those control chars from the string sent to PS.
1253              */
1254             str = removeControlChars(str);
1255             if (str.length() == 0) {
1256                 return true;
1257             }
1258             PlatformFont peer = (PlatformFont) FontAccess.getFontAccess()
1259                                                          .getFontPeer(mLastFont);
1260             CharsetString[] acs = peer.makeMultiCharsetString(str, false);
1261             if (acs == null) {
1262                 /* AWT can't convert all chars so use 2D path */
1263                 return false;
1264             }
1265             /* Get an array of indices into our PostScript name
1266              * table. If all of the runs can not be converted
1267              * to PostScript fonts then null is returned and
1268              * we'll want to fall back to printing the text
1269              * as shapes.
1270              */
1271             int[] psFonts = getPSFontIndexArray(mLastFont, acs);
1272             if (psFonts != null) {
1273 
1274                 for (int i = 0; i < acs.length; i++){
1275                     CharsetString cs = acs[i];
1276                     CharsetEncoder fontCS = cs.fontDescriptor.encoder;
1277 
1278                     StringBuilder nativeStr = new StringBuilder();
1279                     byte[] strSeg = new byte[cs.length * 2];
1280                     int len = 0;