1 /* 2 * Copyright (c) 2019, Red Hat, Inc. 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 /* 25 * @test 26 * @bug 8218854 27 * @requires (jdk.version.major >= 8) & (os.family != "solaris") 28 * @run main/othervm MaxAdvanceIsMax 29 */ 30 31 import java.awt.Font; 32 import java.awt.FontMetrics; 33 import java.awt.Graphics2D; 34 import java.awt.GraphicsEnvironment; 35 import java.awt.RenderingHints; 36 import java.awt.image.BufferedImage; 37 38 public class MaxAdvanceIsMax { 39 40 private static boolean debug = true; 41 42 private static final class AntialiasHint { 43 private Object aaHint; 44 private String asString = ""; 45 46 AntialiasHint(Object aaHint) { 47 if (aaHint.equals( 48 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) { 49 asString += "FT_LOAD_TARGET_MONO"; 50 } else if (aaHint.equals( 51 RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) { 52 asString += "FT_LOAD_TARGET_NORMAL"; 53 } else if (aaHint.equals( 54 RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)) { 55 asString += "FT_LOAD_TARGET_LCD"; 56 } else if (aaHint.equals( 57 RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB)) { 58 asString += "FT_LOAD_TARGET_LCD_V"; 59 } 60 this.aaHint = aaHint; 61 } 62 63 public Object getHint() { 64 return aaHint; 65 } 66 67 public String toString() { 68 return asString; 69 } 70 } 71 72 private static final AntialiasHint[] antialiasHints = { 73 new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF), 74 new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_ON), 75 new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB), 76 new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB) 77 }; 78 79 private static final class StyleAndSize { 80 int style; 81 float size; 82 public StyleAndSize(int style, float size) { 83 this.style = style; 84 this.size = size; 85 } 86 }; 87 88 private static final StyleAndSize[] stylesAndSizes = new StyleAndSize[] { 89 new StyleAndSize(Font.BOLD | Font.ITALIC, 1) 90 }; 91 92 public static void main(String[] args) throws Exception { 93 GraphicsEnvironment e = 94 GraphicsEnvironment.getLocalGraphicsEnvironment(); 95 Font[] fonts = e.getAllFonts(); 96 if (fonts == null) { 97 return; 98 } 99 BufferedImage bi = new BufferedImage(500, 500, 100 BufferedImage.TYPE_INT_RGB); 101 for (AntialiasHint antialiasHint : antialiasHints) { 102 for (Font f : fonts) { 103 for (StyleAndSize styleAndSize : stylesAndSizes) { 104 f = f.deriveFont(styleAndSize.style, styleAndSize.size); 105 Graphics2D g2d = bi.createGraphics(); 106 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 107 antialiasHint.getHint()); 108 FontMetrics fm = g2d.getFontMetrics(f); 109 int[] width; 110 int maxWidth = -1; 111 int maxAdvance = fm.getMaxAdvance(); 112 if (debug) { 113 System.out.println("Testing " + f + " in " + 114 antialiasHint); 115 System.out.println("getMaxAdvance: " + maxAdvance); 116 } 117 if (maxAdvance != -1) { 118 String failureMessage = null; 119 width = fm.getWidths(); 120 for (int j = 0; j < width.length; j++) { 121 if (width[j] > maxWidth) { 122 maxWidth = width[j]; 123 } 124 if (width[j] > maxAdvance) { 125 failureMessage = "FAILED: getMaxAdvance is " + 126 "not max for font: " + 127 f.toString() + 128 " getMaxAdvance(): " + 129 maxAdvance + 130 " getWidths()[" + j + "]: " + 131 width[j]; 132 throw new Exception(failureMessage); 133 } 134 } 135 } 136 if (debug) { 137 System.out.println("Max char width: " + maxWidth); 138 System.out.println("PASSED"); 139 System.out.println("........................."); 140 } 141 } 142 } 143 } 144 System.out.println("TEST PASS - OK"); 145 } 146 }