Path: blob/master/test/jdk/java/awt/FontClass/SurrogateTest/SuppCharDrawTest.java
41154 views
/*1* Copyright 2017 JetBrains s.r.o.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 817474426* @summary Wrong rendering of string containing surrogate pairs on macOS27*/2829import java.awt.Color;30import java.awt.Font;31import java.awt.FontMetrics;32import java.awt.Graphics;33import java.awt.image.BufferedImage;3435public class SuppCharDrawTest {36private static final Font FONT = new Font(Font.MONOSPACED, Font.PLAIN, 12);37private static final int IMAGE_WIDTH = 20;38private static final int IMAGE_HEIGHT = 20;394041public static void main(String[] args) {42BufferedImage base = renderFirstChar("A");43BufferedImage basePlusSurrogate = renderFirstChar("A \uD835\uDC00");4445if (!imagesAreEqual(base, basePlusSurrogate)) {46throw new RuntimeException("Unexpected rendering change");47}48}4950private static BufferedImage renderFirstChar(String s) {51BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);52Graphics g = image.createGraphics();53g.setColor(Color.white);54g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);55g.setColor(Color.black);56g.setFont(FONT);57FontMetrics metrics = g.getFontMetrics();58g.clipRect(0, 0, metrics.charWidth(s.charAt(0)), IMAGE_HEIGHT);59g.drawString(s, 0, metrics.getAscent());60g.dispose();61return image;62}6364private static boolean imagesAreEqual(BufferedImage i1, BufferedImage i2) {65if (i1.getWidth() != i2.getWidth() || i1.getHeight() != i2.getHeight()) return false;66for (int i = 0; i < i1.getWidth(); i++) {67for (int j = 0; j < i1.getHeight(); j++) {68if (i1.getRGB(i, j) != i2.getRGB(i, j)) {69return false;70}71}72}73return true;74}75}767778