1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   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 
  24 /*
  25  * @test
  26  * @bug 8223558
  27  * @key headful
  28  * @summary Verifies that Myanmar script is rendered correctly:
  29  *          two characters combined into one glyph
  30  * @run main MyanmarTextTest
  31  */
  32 
  33 import java.awt.Font;
  34 import java.awt.GraphicsEnvironment;
  35 import java.util.Arrays;
  36 import javax.swing.BorderFactory;
  37 import javax.swing.BoxLayout;
  38 import javax.swing.JFrame;
  39 import javax.swing.JPanel;
  40 import javax.swing.JTextField;
  41 import javax.swing.SwingConstants;
  42 import javax.swing.SwingUtilities;
  43 import javax.swing.WindowConstants;
  44 import javax.swing.plaf.TextUI;
  45 import javax.swing.text.BadLocationException;
  46 import javax.swing.text.Position;
  47 
  48 public class MyanmarTextTest {
  49     private static final String TEXT = "\u1000\u103C";
  50 
  51     private static final String FONT_WINDOWS = "Myanmar Text";
  52     private static final String FONT_LINUX = "Padauk";
  53     private static final String FONT_MACOS = "Myanmar MN";
  54 
  55     private static final String FONT_NAME = selectFontName();
  56 
  57     private final JFrame frame;
  58     private final JTextField myanmarTF;
  59 
  60     private static volatile MyanmarTextTest mtt;
  61 
  62     public static void main(String[] args) throws Exception {
  63         if (FONT_NAME == null) {
  64             System.err.println("Unsupported OS: exiting");
  65             return;
  66         }
  67         if (!fontExists()) {
  68             System.err.println("Required font is not installed: " + FONT_NAME);
  69             return;
  70         }
  71 
  72         try {
  73             SwingUtilities.invokeAndWait(MyanmarTextTest::createUI);
  74             SwingUtilities.invokeAndWait(mtt::checkPositions);
  75         } finally {
  76             SwingUtilities.invokeAndWait(mtt::dispose);
  77         }
  78     }
  79 
  80     private static void createUI() {
  81         mtt = new MyanmarTextTest();
  82         mtt.show();
  83     }
  84 
  85     private MyanmarTextTest() {
  86         frame = new JFrame("Myanmar Text");
  87         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  88 
  89         myanmarTF = new JTextField(TEXT);
  90         myanmarTF.setFont(new Font(FONT_NAME, Font.PLAIN, 40));
  91 
  92         JPanel main = new JPanel();
  93         main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
  94         main.add(myanmarTF);
  95 
  96         main.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
  97 
  98         frame.getContentPane().add(main);
  99     }
 100 
 101     private void show() {
 102         frame.pack();
 103         frame.setLocationByPlatform(true);
 104         frame.setVisible(true);
 105     }
 106 
 107     private void dispose() {
 108         frame.dispose();
 109     }
 110 
 111     private void checkPositions() {
 112         final TextUI ui = myanmarTF.getUI();
 113         final Position.Bias[] biasRet = new Position.Bias[1];
 114         try {
 115             if (2 != ui.getNextVisualPositionFrom(myanmarTF, 0,
 116                     Position.Bias.Forward, SwingConstants.EAST, biasRet)) {
 117                 throw new RuntimeException("For 0, next position should be 2");
 118             }
 119             if (2 != ui.getNextVisualPositionFrom(myanmarTF, 1,
 120                     Position.Bias.Forward, SwingConstants.EAST, biasRet)) {
 121                 throw new RuntimeException("For 1, next position should be 2");
 122             }
 123             if (0 != ui.getNextVisualPositionFrom(myanmarTF, 2,
 124                     Position.Bias.Forward, SwingConstants.WEST, biasRet)) {
 125                 throw new RuntimeException("For 2, prev position should be 0");
 126             }
 127         } catch (BadLocationException e) {
 128             throw new RuntimeException(e);
 129         }
 130     }
 131 
 132     private static String selectFontName() {
 133         String osName = System.getProperty("os.name").toLowerCase();
 134         if (osName.contains("windows")) {
 135             return FONT_WINDOWS;
 136         } else if (osName.contains("linux")) {
 137             return FONT_LINUX;
 138         } else if (osName.contains("mac")) {
 139             return FONT_MACOS;
 140         } else {
 141             return null;
 142         }
 143     }
 144 
 145     private static boolean fontExists() {
 146         String[] fontFamilyNames = GraphicsEnvironment
 147                                    .getLocalGraphicsEnvironment()
 148                                    .getAvailableFontFamilyNames();
 149         return Arrays.asList(fontFamilyNames).contains(FONT_NAME);
 150     }
 151 }