Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/FontMetrics/MaxAdvanceIsMax.java
38812 views
/*1* Copyright (c) 2019, Red Hat, Inc.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*/2223/*24* @test25* @bug 821885426* @requires jdk.version.major >= 827* @run main/othervm MaxAdvanceIsMax28*/2930import java.awt.Font;31import java.awt.FontMetrics;32import java.awt.Graphics2D;33import java.awt.GraphicsEnvironment;34import java.awt.RenderingHints;35import java.awt.image.BufferedImage;3637public class MaxAdvanceIsMax {3839private static boolean debug = true;4041private static final class AntialiasHint {42private Object aaHint;43private String asString = "";4445AntialiasHint(Object aaHint) {46if (aaHint.equals(47RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) {48asString += "FT_LOAD_TARGET_MONO";49} else if (aaHint.equals(50RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) {51asString += "FT_LOAD_TARGET_NORMAL";52} else if (aaHint.equals(53RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)) {54asString += "FT_LOAD_TARGET_LCD";55} else if (aaHint.equals(56RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB)) {57asString += "FT_LOAD_TARGET_LCD_V";58}59this.aaHint = aaHint;60}6162public Object getHint() {63return aaHint;64}6566public String toString() {67return asString;68}69}7071private static final AntialiasHint[] antialiasHints = {72new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF),73new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_ON),74new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB),75new AntialiasHint(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB)76};7778private static final class StyleAndSize {79int style;80float size;81public StyleAndSize(int style, float size) {82this.style = style;83this.size = size;84}85};8687private static final StyleAndSize[] stylesAndSizes = new StyleAndSize[] {88new StyleAndSize(Font.BOLD | Font.ITALIC, 10)89};9091public static void main(String[] args) throws Exception {92GraphicsEnvironment e =93GraphicsEnvironment.getLocalGraphicsEnvironment();94Font[] fonts = e.getAllFonts();95BufferedImage bi = new BufferedImage(500, 500,96BufferedImage.TYPE_INT_RGB);97for (AntialiasHint antialiasHint : antialiasHints) {98for (Font f : fonts) {99for (StyleAndSize styleAndSize : stylesAndSizes) {100f = f.deriveFont(styleAndSize.style, styleAndSize.size);101Graphics2D g2d = bi.createGraphics();102g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,103antialiasHint.getHint());104FontMetrics fm = g2d.getFontMetrics(f);105int[] width;106int maxWidth = -1;107int maxAdvance = fm.getMaxAdvance();108if (debug) {109System.out.println("Testing " + f + " in " +110antialiasHint);111System.out.println("getMaxAdvance: " + maxAdvance);112}113if (maxAdvance != -1) {114String failureMessage = null;115width = fm.getWidths();116for (int j = 0; j < width.length; j++) {117if (width[j] > maxWidth) {118maxWidth = width[j];119}120if (width[j] > maxAdvance) {121failureMessage = "FAILED: getMaxAdvance is " +122"not max for font: " +123f.toString() +124" getMaxAdvance(): " +125maxAdvance +126" getWidths()[" + j + "]: " +127width[j];128throw new Exception(failureMessage);129}130}131}132if (debug) {133System.out.println("Max char width: " + maxWidth);134System.out.println("PASSED");135System.out.println(".........................");136}137}138}139}140System.out.println("TEST PASS - OK");141}142}143144145