1 /*
   2  * Copyright 2002-2004 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package build.tools.fontchecker;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.io.*;
  31 
  32 /**
  33  * FontCheckDummy (not unlike Crash Test Dummy).
  34  *
  35  * <PRE>
  36  * FontCheckDummy is the "child" process. Its task is to verify
  37  * integrity of system fonts. Since unexpected crashes are known
  38  * to occur when certain fonts are manipulated, the process is
  39  * "monitored" by the parent process, which might have to restart
  40  * the "child" if it crashes.
  41  * </PRE>
  42  *
  43  * @author Ilya Bagrak
  44  */
  45 public class FontCheckDummy implements FontCheckerConstants {
  46 
  47     /**
  48      * Input stream from parent process.
  49      */
  50     private BufferedReader is;
  51 
  52     /**
  53      * Output stream to parent process.
  54      */
  55     private BufferedOutputStream os;
  56 
  57     /**
  58      * Image on which font characters will be drawn.
  59      */
  60     private BufferedImage bi;
  61 
  62     /**
  63      * graphics object on which characters will be drawn.
  64      */
  65     private Graphics graphics;
  66 
  67     /**
  68      * This constructor wraps the process's standard output and input streams
  69      * to enable easier communication with parent process. It also initializes
  70      * the graphics object used for drawing font characters.
  71      * <BR><BR>
  72      * @see FontCheckerConstants
  73      */
  74     public FontCheckDummy() {
  75         is = new BufferedReader(new InputStreamReader(System.in));
  76         os = new BufferedOutputStream(System.out);
  77         /* make suffficient space for 12 point font */
  78         bi = new BufferedImage(40, 40, BufferedImage.TYPE_INT_RGB);
  79         graphics = bi.getGraphics();
  80         try {
  81             os.write(CHILD_STARTED_OK);
  82             os.flush();
  83         } catch (IOException e) {
  84             System.exit(-1);
  85         }
  86     }
  87 
  88     /**
  89      * Initializes an instance of Font from given font path.
  90      * <BR>
  91      * This methods attempts to create an instance of font from
  92      * a string that represents path to the font file.
  93      * <BR><BR>
  94      * @param fontPath string representing path to font file
  95      * @param flag indicating whether or not checking of non-TrueType fonts
  96      * is necessary
  97      */
  98     private void testFont(String fontPath, boolean checkNonTTF) {
  99 
 100         FontFileFilter fff = new FontFileFilter(checkNonTTF);
 101         File fontFile = new File(fontPath);
 102         if (!fontFile.canRead()) {
 103             try {
 104                 os.write(ERR_FONT_NOT_FOUND);
 105                 os.flush();
 106             } catch (IOException e) {
 107                 System.exit(-1);
 108             }
 109         }
 110         Font font = null;
 111         try {
 112             File file = new File(fontPath);
 113             font = Font.createFont(fff.getFontType(fontPath), file);
 114         } catch (FontFormatException e1) {
 115         } catch (IOException e2) {
 116         }
 117         if (font == null) {
 118              return;
 119         }
 120         font = font.deriveFont(Font.PLAIN, 12);
 121         String name = font.getFontName();
 122         String family = font.getFamily();
 123 
 124         char[] testChars = { '0' };
 125         if (font.canDisplay(testChars[0])) {
 126             graphics.setFont(font);
 127             graphics.drawChars(testChars, 0, 1, 20, 20);
 128         }
 129         try {
 130             os.write(ERR_FONT_OK);
 131             os.flush();
 132         } catch (IOException e) {
 133             System.exit(-1);
 134         }
 135     }
 136 
 137     /**
 138      * Begins synchronous communication betweeen parent and child processes.
 139      * <BR>
 140      * This method begins communication between parent and child processes.
 141      * FontCheckDummy reads a line of text from input stream (@see #is).
 142      */
 143     public void run() {
 144         String command = null;
 145         while (true) {
 146             try {
 147                 command = is.readLine();
 148             } catch (IOException e) {
 149                 System.exit(-1);
 150             }
 151             if (command != null && command.length() >= 1) {
 152                 int cmd = Integer.parseInt(command.substring(0,1));
 153                 if (cmd == EXITCOMMAND) {
 154                     return;
 155                 }
 156                 boolean checkNonTTF = ((cmd == 1) ? true : false);
 157                 String fontPath = command.substring(1);
 158                 testFont(fontPath, checkNonTTF);
 159             } else {
 160                 return;
 161             }
 162         }
 163     }
 164 
 165     public static void main(String[] args) {
 166         try {
 167             /* Background app. */
 168             System.setProperty("java.awt.headless", "true");
 169             System.setProperty("sun.java2d.noddraw", "true");
 170             new FontCheckDummy().run();
 171         } catch (Throwable t) {
 172         }
 173         System.exit(0);
 174     }
 175 }