1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 import java.awt.Graphics;
  26 import java.awt.GraphicsEnvironment;
  27 import java.awt.print.PageFormat;
  28 import java.awt.print.Printable;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.nio.charset.StandardCharsets;
  34 import javax.print.DocFlavor;
  35 import javax.print.DocPrintJob;
  36 import javax.print.SimpleDoc;
  37 import javax.print.StreamPrintService;
  38 import javax.print.StreamPrintServiceFactory;
  39 import javax.print.attribute.HashDocAttributeSet;
  40 import javax.print.attribute.HashPrintRequestAttributeSet;
  41 import javax.print.event.PrintJobAdapter;
  42 import javax.print.event.PrintJobEvent;
  43 
  44 /*
  45  * @test
  46  * @bug 8067364
  47  * @summary Printing to Postscript doesn't support dieresis
  48  * @build PrintSEUmlauts
  49  * @run main/othervm PrintSEUmlauts
  50  */
  51 public class PrintSEUmlauts implements Printable {
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         GraphicsEnvironment.getLocalGraphicsEnvironment();
  56 
  57         DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
  58         String mime = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
  59 
  60         StreamPrintServiceFactory[] factories =
  61                 StreamPrintServiceFactory.
  62                         lookupStreamPrintServiceFactories(flavor, mime);
  63         if (factories.length == 0) {
  64             System.out.println("No print service found.");
  65             return;
  66         }
  67 
  68         FileOutputStream output = new FileOutputStream("out.ps");
  69         StreamPrintService service = factories[0].getPrintService(output);
  70 
  71         SimpleDoc doc =
  72              new SimpleDoc(new PrintSEUmlauts(),
  73                            DocFlavor.SERVICE_FORMATTED.PRINTABLE,
  74                            new HashDocAttributeSet());
  75         DocPrintJob job = service.createPrintJob();
  76         job.addPrintJobListener(new PrintJobAdapter() {
  77             @Override
  78             public void printJobCompleted(PrintJobEvent pje) {
  79                 testPrintAndExit();
  80             }
  81         });
  82 
  83         job.print(doc, new HashPrintRequestAttributeSet());
  84     }
  85 
  86     private static final boolean DEBUG = false;
  87     private static void testPrintAndExit() {
  88         String expected = "<e4>";
  89         String content = "";
  90 
  91         File file = new File("out.ps");
  92         if (!DEBUG) {
  93             file.deleteOnExit();
  94         }
  95 
  96         try (FileInputStream stream = new FileInputStream(file)) {
  97             byte[] data = new byte[(int) file.length()];
  98             stream.read(data);
  99             content = new String(data, StandardCharsets.ISO_8859_1);
 100         } catch (IOException ex) {
 101             ex.printStackTrace();
 102         }
 103 
 104         if (!content.contains(expected)) {
 105             System.err.println("FAIL");
 106             if (DEBUG) {
 107                 System.err.println("printing content");
 108                 System.err.println(content);
 109             }
 110             throw new RuntimeException("Expected <e4> to represent 'ä' but not found!");
 111         }
 112         System.err.println("SUCCESS");
 113     }
 114 
 115     public int print(Graphics g, PageFormat pf, int pg) {
 116        if (pg > 0) return NO_SUCH_PAGE;
 117        g.drawString("ä", 100, 100);
 118        return PAGE_EXISTS;
 119    }
 120 }