Path: blob/master/test/jdk/java/awt/FontClass/FontDisposer/FontDisposeTest.java
41154 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.awt.Font;23import java.awt.Graphics2D;24import java.awt.font.FontRenderContext;25import java.awt.image.BufferedImage;26import java.io.FileInputStream;27import java.io.ByteArrayInputStream;28import java.io.InputStream;29import java.lang.reflect.Field;30import java.lang.reflect.Method;3132import sun.font.Font2DHandle;33import sun.font.Font2D;34import sun.font.FontScaler;35import sun.font.Type1Font;3637/**38* @bug 813298539* @summary Tests to verify Type1 Font scaler dispose crashes40* @modules java.desktop/sun.font41*/42public class FontDisposeTest43{44public static void main(String[] args) throws Exception45{46// The bug only happens with Type 1 fonts. The Ghostscript font files47// should be commonly available. From distro pacakge or48// ftp://ftp.gnu.org/gnu/ghostscript/gnu-gs-fonts-other-6.0.tar.gz49// Pass pfa/pfb font file as argument50String path = args[0];5152// Load53InputStream stream = new FileInputStream(path);54Font font = Font.createFont(Font.TYPE1_FONT,stream);5556// Ensure native bits have been generated57BufferedImage img = new BufferedImage(100,100,58BufferedImage.TYPE_INT_ARGB);59Graphics2D g2d = img.createGraphics();60FontRenderContext frc = g2d.getFontRenderContext();6162font.getLineMetrics("derp",frc);6364// Force disposal -65// System.gc() is not sufficient.66Field font2DHandleField = Font.class.getDeclaredField("font2DHandle");67font2DHandleField.setAccessible(true);68sun.font.Font2DHandle font2DHandle =69(sun.font.Font2DHandle)font2DHandleField.get(font);7071sun.font.Font2D font2D = font2DHandle.font2D;72sun.font.Type1Font type1Font = (sun.font.Type1Font)font2D;7374Method getScalerMethod =75sun.font.Type1Font.class.getDeclaredMethod("getScaler");76getScalerMethod.setAccessible(true);77sun.font.FontScaler scaler =78(sun.font.FontScaler)getScalerMethod.invoke(type1Font);7980// dispose should not crash due to double free81scaler.dispose();82}83}848586