Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/FontClass/SurrogateTest/SuppCharTest.java
38828 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @bug 801555626* @summary Surrogate pairs do not render properly on MacOS X.27*/2829import java.util.Locale;30import java.awt.*;31import java.awt.font.*;32import javax.swing.*;3334public class SuppCharTest {3536static String str = "ABC\uD840\uDC01\uD840\uDC00AB";37static String EXTB_FONT = "MingLiU-ExtB";3839public static void main(String args[]) throws Exception {4041final Font font = new Font(EXTB_FONT, Font.PLAIN, 36);42if (!EXTB_FONT.equalsIgnoreCase(font.getFamily(Locale.ENGLISH))) {43return;44}4546SwingUtilities.invokeLater(new Runnable(){47@Override48public void run(){49JFrame f = new JFrame("Test Supplementary Char Support");50Component c = new SuppCharComp(font, str);51f.add("Center", c);52JButton b = new JButton(str);53b.setFont(font);54f.add("South", b);55f.pack();56f.setVisible(true);57}58});5960/* If a supplementary character was found, 'invisible glyphs'61* with value 65535 will be inserted in the place of the 2nd (low)62* char index. So we are looking here to make sure such substitutions63* took place.64*/65FontRenderContext frc = new FontRenderContext(null, false, false);66GlyphVector gv = font.createGlyphVector(frc, str);67int numGlyphs = gv.getNumGlyphs();68int[] codes = gv.getGlyphCodes(0, numGlyphs, null);69boolean foundInvisibleGlyph = false;70for (int i=0; i<numGlyphs;i++) {71if (codes[i] == 65535) {72foundInvisibleGlyph = true;73break;74}75}7677if (!foundInvisibleGlyph) {78throw new RuntimeException("No invisible glyphs");79}8081if (font.canDisplayUpTo(str) != -1) {82throw new RuntimeException("Font can't display all chars");83}8485}86}8788class SuppCharComp extends Component {8990static final int w=400, h=250;91public Dimension preferredSize() {92return new Dimension(w,h);93}9495String str = null;96Font font = null;97public SuppCharComp(Font font, String str) {98this.font = font;99this.str = str;100}101public void paint(Graphics g) {102Graphics2D g2d = (Graphics2D)g.create();103g2d.setColor(Color.white);104g2d.fillRect(0,0,w,h);105g2d.setColor(Color.black);106int y = 0;107g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,108RenderingHints.VALUE_FRACTIONALMETRICS_ON);109g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,110RenderingHints.VALUE_TEXT_ANTIALIAS_ON);111112g2d.setFont(font);113g2d.drawString(str, 10, 50);114115FontRenderContext frc = g2d.getFontRenderContext();116GlyphVector gv = font.createGlyphVector(frc, str);117g2d.drawGlyphVector(gv, 10, 100);118TextLayout tl = new TextLayout(str, font, frc);119tl.draw(g2d, 10, 150);120char[] ca = str.toCharArray();121g2d.drawChars(ca, 0, ca.length, 10, 200);122123}124125}126127128129