jdk/test/java/awt/font/Fallback/SurrogatesFallbackTest.java
changeset 42713 593783862f40
equal deleted inserted replaced
42712:679c4f3300b1 42713:593783862f40
       
     1 /*
       
     2  * Copyright 2016 JetBrains s.r.o.
       
     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 /* @test
       
    24  * @bug 8169202
       
    25  * @summary verify font fallback for surrogate pairs on macOS
       
    26  * @requires os.family == "mac"
       
    27  */
       
    28 
       
    29 import java.awt.Color;
       
    30 import java.awt.Font;
       
    31 import java.awt.Graphics2D;
       
    32 import java.awt.font.GlyphVector;
       
    33 import java.awt.image.BufferedImage;
       
    34 import java.util.function.Consumer;
       
    35 
       
    36 public class SurrogatesFallbackTest {
       
    37     private static final int CHARACTER = 0x1d400; // MATHEMATICAL BOLD CAPITAL A
       
    38     private static final Font FONT = new Font("Menlo", // expected to fallback to STIXGeneral for the character above
       
    39                                               Font.PLAIN,
       
    40                                               12);
       
    41     private static final int IMAGE_WIDTH = 20;
       
    42     private static final int IMAGE_HEIGHT = 20;
       
    43     private static final int GLYPH_X = 5;
       
    44     private static final int GLYPH_Y = 15;
       
    45 
       
    46     public static void main(String[] args) {
       
    47         BufferedImage noGlyph = createImage(g -> {});
       
    48         BufferedImage missingGlyph = createImage(g -> {
       
    49             GlyphVector gv = FONT.createGlyphVector(g.getFontRenderContext(), new int[]{FONT.getMissingGlyphCode()});
       
    50             g.drawGlyphVector(gv, GLYPH_X, GLYPH_Y);
       
    51         });
       
    52         BufferedImage surrogateCharGlyph = createImage(g -> {
       
    53             g.setFont(FONT);
       
    54             g.drawString(new String(Character.toChars(CHARACTER)), GLYPH_X, GLYPH_Y);
       
    55         });
       
    56 
       
    57         if (imagesAreEqual(surrogateCharGlyph, noGlyph)) {
       
    58             throw new RuntimeException("Character was not rendered");
       
    59         }
       
    60         if (imagesAreEqual(surrogateCharGlyph, missingGlyph)) {
       
    61             throw new RuntimeException("Character is rendered as missing");
       
    62         }
       
    63     }
       
    64 
       
    65     private static BufferedImage createImage(Consumer<Graphics2D> drawing) {
       
    66         BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
       
    67         Graphics2D g = image.createGraphics();
       
    68         g.setColor(Color.white);
       
    69         g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
       
    70         g.setColor(Color.black);
       
    71         drawing.accept(g);
       
    72         g.dispose();
       
    73         return image;
       
    74     }
       
    75 
       
    76     private static boolean imagesAreEqual(BufferedImage i1, BufferedImage i2) {
       
    77         if (i1.getWidth() != i2.getWidth() || i1.getHeight() != i2.getHeight()) return false;
       
    78         for (int i = 0; i < i1.getWidth(); i++) {
       
    79             for (int j = 0; j < i1.getHeight(); j++) {
       
    80                 if (i1.getRGB(i, j) != i2.getRGB(i, j)) {
       
    81                     return false;
       
    82                 }
       
    83             }
       
    84         }
       
    85         return true;
       
    86     }
       
    87 }
       
    88