1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.awt.Font;
  24 import java.awt.Graphics2D;
  25 import java.awt.font.FontRenderContext;
  26 import java.awt.image.BufferedImage;
  27 import java.io.FileInputStream;
  28 import java.io.ByteArrayInputStream;
  29 import java.io.InputStream;
  30 import java.lang.reflect.Field;
  31 import java.lang.reflect.Method;
  32 
  33 import sun.font.Font2DHandle;
  34 import sun.font.Font2D;
  35 import sun.font.FontScaler;
  36 import sun.font.Type1Font;
  37 
  38 /**
  39  * @bug 8132985
  40  * @summary Tests to verify Type1 Font scaler dispose crashes
  41  * @modules java.desktop/sun.font
  42  */
  43 public class FontDisposeTest
  44 {
  45     public static void main(String[] args) throws Exception
  46     {
  47         // The bug only happens with Type 1 fonts. The Ghostscript font files
  48         // should be commonly available. From distro pacakge or
  49         //  ftp://ftp.gnu.org/gnu/ghostscript/gnu-gs-fonts-other-6.0.tar.gz
  50         // Pass pfa/pfb font file as argument
  51         String path = args[0];
  52 
  53         // Load
  54         InputStream stream = new FileInputStream(path);
  55         Font font = Font.createFont(Font.TYPE1_FONT,stream);
  56 
  57         // Ensure native bits have been generated
  58         BufferedImage img = new BufferedImage(100,100,
  59                                  BufferedImage.TYPE_INT_ARGB);
  60         Graphics2D g2d = img.createGraphics();
  61         FontRenderContext frc = g2d.getFontRenderContext();
  62 
  63         font.getLineMetrics("derp",frc);
  64 
  65         // Force disposal - 
  66         // System.gc() is not sufficient.
  67         Field font2DHandleField = Font.class.getDeclaredField("font2DHandle");
  68         font2DHandleField.setAccessible(true);
  69         sun.font.Font2DHandle font2DHandle =
  70                       (sun.font.Font2DHandle)font2DHandleField.get(font);
  71 
  72         sun.font.Font2D font2D = font2DHandle.font2D;
  73         sun.font.Type1Font type1Font = (sun.font.Type1Font)font2D;
  74 
  75         Method getScalerMethod =
  76         sun.font.Type1Font.class.getDeclaredMethod("getScaler");
  77         getScalerMethod.setAccessible(true);
  78         sun.font.FontScaler scaler =
  79                   (sun.font.FontScaler)getScalerMethod.invoke(type1Font);
  80 
  81         // dispose should not crash due to double free
  82         scaler.dispose();
  83     }
  84 }