1 /*
   2  * Copyright (c) 2016, 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 /* @test
  25  * @key headful
  26  * @summary verify Arab Diacritic Positioning
  27  * @bug 8168759
  28  */
  29 
  30 import java.awt.Font;
  31 import java.awt.GridLayout;
  32 import java.awt.Rectangle;
  33 import java.awt.font.FontRenderContext;
  34 import java.awt.font.TextLayout;
  35 import java.util.Locale;
  36 import javax.swing.JFrame;
  37 import javax.swing.JLabel;
  38 import javax.swing.SwingUtilities;
  39 import javax.swing.WindowConstants;
  40 
  41 public class ArabicDiacriticTest {
  42 
  43     static final String SAMPLE =
  44      "\u0627\u0644\u0639\u064e\u0631\u064e\u0628\u0650\u064a\u064e\u0651\u0629";
  45 
  46     static final String STR1 = "\u0644\u0639\u064e\u0629";
  47     static final String STR2 = "\u0644\u0639\u0629";
  48 
  49     static JFrame frame;
  50     static final String FONT = "DejaVu Sans";
  51 
  52     public static void main(String args[]) throws Exception {
  53         showText(); // for a human
  54         measureText(); // for the test harness
  55         Thread.sleep(5000);
  56         frame.dispose();
  57     }
  58 
  59     static void showText() {
  60         SwingUtilities.invokeLater(() -> {
  61             frame = new JFrame();
  62             JLabel label = new JLabel(SAMPLE);
  63             Font font = new Font(FONT, Font.PLAIN, 36);
  64             label.setFont(font);
  65             frame.setLayout(new GridLayout(3,1));
  66             frame.add(label);
  67             label = new JLabel(STR1);
  68             label.setFont(font);
  69             frame.add(label);
  70             label = new JLabel(STR2);
  71             label.setFont(font);
  72             frame.add(label);
  73             frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  74             frame.pack();
  75             frame.setLocationRelativeTo(null);
  76             frame.setVisible(true);
  77         });
  78     }
  79 
  80     static void measureText() {
  81         Font font = new Font(FONT, Font.PLAIN, 36);
  82         if (!font.getFamily(Locale.ENGLISH).equals(FONT)) {
  83             return;
  84         }
  85         FontRenderContext frc = new FontRenderContext(null, false, false);
  86         TextLayout tl1 = new TextLayout(STR1, font, frc);
  87         TextLayout tl2 = new TextLayout(STR2, font, frc);
  88         Rectangle r1 = tl1.getPixelBounds(frc, 0f, 0f);
  89         Rectangle r2 = tl2.getPixelBounds(frc, 0f, 0f);
  90         if (r1.height > r2.height) {
  91             System.out.println(font);
  92             System.out.println(r1);
  93             System.out.println(r2);
  94             throw new RuntimeException("BAD BOUNDS");
  95         }
  96     }
  97 }