|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | + |
| 24 | +import java.awt.Font; |
| 25 | +import java.awt.FontMetrics; |
| 26 | +import java.awt.Graphics2D; |
| 27 | +import java.awt.Rectangle; |
| 28 | +import java.awt.font.FontRenderContext; |
| 29 | +import java.awt.font.GlyphVector; |
| 30 | +import java.awt.geom.AffineTransform; |
| 31 | +import java.awt.geom.Rectangle2D; |
| 32 | +import java.awt.image.BufferedImage; |
| 33 | + |
| 34 | +/* |
| 35 | + * @test |
| 36 | + * @bug 8328896 |
| 37 | + * @summary test that using very large font sizes used don't break later uses |
| 38 | + */ |
| 39 | + |
| 40 | +public class ExtremeFontSizeTest { |
| 41 | + |
| 42 | + static BufferedImage bi = new BufferedImage(1,1,1); |
| 43 | + static Graphics2D g2d = bi.createGraphics(); |
| 44 | + static String testString = "M"; |
| 45 | + static Font font = new Font("SansSerif", Font.PLAIN, 12); |
| 46 | + static int fontSize = 0; |
| 47 | + static boolean failed = false; |
| 48 | + static int[] fontSizes = { 10, 12, 1000, 2000, 20000, 100000, 8 }; |
| 49 | + static double[] scales = { 1.0, 900.0}; |
| 50 | + static boolean[] fms = { false, true }; |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + |
| 54 | + /* run tests validating bounds etc are non-zero |
| 55 | + * then run with extreme scales for which zero is allowed - but not required |
| 56 | + * then run the first tests again to be sure they are still reasonable. |
| 57 | + */ |
| 58 | + runTests(); |
| 59 | + test(5_000_000, 10_000, false, testString, false); |
| 60 | + test(5_000_000, 10_000, true, testString, false); |
| 61 | + test(0, 0.00000001, false, testString, false); |
| 62 | + runTests(); |
| 63 | + |
| 64 | + if (failed) { |
| 65 | + throw new RuntimeException("Test failed. Check stdout log."); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + static void runTests() { |
| 70 | + for (int fontSize : fontSizes) { |
| 71 | + for (double scale : scales) { |
| 72 | + for (boolean fm : fms) { |
| 73 | + test(fontSize, scale, fm, testString, true); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static void test(int size, double scale, boolean fm, String str, boolean checkAll) { |
| 80 | + |
| 81 | + AffineTransform at = AffineTransform.getScaleInstance(scale, scale); |
| 82 | + FontRenderContext frc = new FontRenderContext(at, false, fm); |
| 83 | + font = font.deriveFont((float)size); |
| 84 | + g2d.setTransform(at); |
| 85 | + g2d.setFont(font); |
| 86 | + FontMetrics metrics = g2d.getFontMetrics(); |
| 87 | + int height = metrics.getHeight(); |
| 88 | + double width = font.getStringBounds(str, frc).getWidth(); |
| 89 | + |
| 90 | + GlyphVector gv = font.createGlyphVector(frc, str.toCharArray()); |
| 91 | + Rectangle pixelBounds = gv.getPixelBounds(frc, 0, 0); |
| 92 | + Rectangle2D visualBounds = gv.getVisualBounds(); |
| 93 | + |
| 94 | + System.out.println("Test parameters: size="+size+" scale="+scale+" fm="+fm+" str="+str); |
| 95 | + System.out.println("font height="+metrics.getHeight()); |
| 96 | + System.out.println("string bounds width="+width); |
| 97 | + System.out.println("GlyphVector Pixel Bounds="+ pixelBounds); |
| 98 | + System.out.println("GlyphVector Visual Bounds="+ visualBounds); |
| 99 | + |
| 100 | + |
| 101 | + if (height < 0 || width < 0 || pixelBounds.getWidth() < 0 || visualBounds.getWidth() < 0) { |
| 102 | + failed = true; |
| 103 | + System.out.println(" *** Unexpected negative size reported *** "); |
| 104 | + } |
| 105 | + if (!checkAll) { |
| 106 | + System.out.println(); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (height == 0 || width == 0 || (pixelBounds.isEmpty()) || visualBounds.isEmpty() ) { |
| 111 | + failed = true; |
| 112 | + System.out.println("Pixel bounds empty="+pixelBounds.isEmpty()); |
| 113 | + System.out.println("Visual bounds empty="+visualBounds.isEmpty()); |
| 114 | + System.out.println(" *** RESULTS NOT AS EXPECTED *** "); |
| 115 | + } |
| 116 | + System.out.println(); |
| 117 | + } |
| 118 | +} |
0 commit comments