Path: blob/master/test/jdk/java/awt/FontClass/SurrogateTest/SuppCharTest.java
41154 views
/*1* Copyright (c) 2013, 2017, 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* @key headful26* @bug 801555627* @summary Surrogate pairs do not render properly on MacOS X.28*/2930import java.util.Locale;31import java.awt.*;32import java.awt.font.*;33import javax.swing.*;3435public class SuppCharTest {3637static String str = "ABC\uD840\uDC01\uD840\uDC00AB";38static String EXTB_FONT = "MingLiU-ExtB";3940public static void main(String args[]) throws Exception {4142final Font font = new Font(EXTB_FONT, Font.PLAIN, 36);43if (!EXTB_FONT.equalsIgnoreCase(font.getFamily(Locale.ENGLISH))) {44return;45}4647SwingUtilities.invokeLater(new Runnable(){48@Override49public void run(){50JFrame f = new JFrame("Test Supplementary Char Support");51Component c = new SuppCharComp(font, str);52f.add("Center", c);53JButton b = new JButton(str);54b.setFont(font);55f.add("South", b);56f.pack();57f.setVisible(true);58}59});6061/* If a supplementary character was found, 'invisible glyphs'62* with value 65535 will be inserted in the place of the 2nd (low)63* char index. So we are looking here to make sure such substitutions64* took place.65*/66FontRenderContext frc = new FontRenderContext(null, false, false);67GlyphVector gv = font.createGlyphVector(frc, str);68int numGlyphs = gv.getNumGlyphs();69int[] codes = gv.getGlyphCodes(0, numGlyphs, null);70boolean foundInvisibleGlyph = false;71for (int i=0; i<numGlyphs;i++) {72if (codes[i] == 65535) {73foundInvisibleGlyph = true;74break;75}76}7778if (!foundInvisibleGlyph) {79throw new RuntimeException("No invisible glyphs");80}8182if (font.canDisplayUpTo(str) != -1) {83throw new RuntimeException("Font can't display all chars");84}8586}87}8889class SuppCharComp extends Component {9091static final int w=400, h=250;92public Dimension preferredSize() {93return new Dimension(w,h);94}9596String str = null;97Font font = null;98public SuppCharComp(Font font, String str) {99this.font = font;100this.str = str;101}102public void paint(Graphics g) {103Graphics2D g2d = (Graphics2D)g.create();104g2d.setColor(Color.white);105g2d.fillRect(0,0,w,h);106g2d.setColor(Color.black);107int y = 0;108g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,109RenderingHints.VALUE_FRACTIONALMETRICS_ON);110g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,111RenderingHints.VALUE_TEXT_ANTIALIAS_ON);112113g2d.setFont(font);114g2d.drawString(str, 10, 50);115116FontRenderContext frc = g2d.getFontRenderContext();117GlyphVector gv = font.createGlyphVector(frc, str);118g2d.drawGlyphVector(gv, 10, 100);119TextLayout tl = new TextLayout(str, font, frc);120tl.draw(g2d, 10, 150);121char[] ca = str.toCharArray();122g2d.drawChars(ca, 0, ca.length, 10, 200);123124}125126}127128129130