|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2025, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Font; |
| 26 | +import java.awt.Frame; |
| 27 | +import java.awt.Graphics; |
| 28 | +import java.awt.Graphics2D; |
| 29 | +import java.awt.Panel; |
| 30 | +import java.awt.font.FontRenderContext; |
| 31 | +import java.awt.font.LineBreakMeasurer; |
| 32 | +import java.awt.font.NumericShaper; |
| 33 | +import java.awt.font.TextAttribute; |
| 34 | +import java.awt.font.TextLayout; |
| 35 | +import java.text.AttributedCharacterIterator; |
| 36 | +import java.text.AttributedString; |
| 37 | +import java.util.HashMap; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 4210199 |
| 42 | + * @summary Draw a string with mixed ASCII digits and different scripts, applying |
| 43 | + * different kinds of numeric shapers. Verify that the proper digits are affected. |
| 44 | + * @library /java/awt/regtesthelpers |
| 45 | + * @build PassFailJFrame |
| 46 | + * @run main/manual ShaperTest |
| 47 | + */ |
| 48 | + |
| 49 | +public class ShaperTest extends Panel { |
| 50 | + private static final String fontName = Font.DIALOG; |
| 51 | + private final TextLayout[][] layouts; |
| 52 | + private final String[] titles; |
| 53 | + private static final String text = |
| 54 | + "-123 (English) 456.00 (Arabic) \u0641\u0642\u0643 -789 (Thai) \u0e01\u0e33 01.23"; |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + final String INSTRUCTIONS = """ |
| 58 | + A line of text containing mixed numeric and other text is drawn four times |
| 59 | + (Depending on the font/platform, some of the other text may not be visible). |
| 60 | +
|
| 61 | + There are four runs of digits, '-123' at the front of the text, '456.00' after |
| 62 | + English text, '-789' after Arabic text, and '01.23' after Thai text. |
| 63 | +
|
| 64 | + In the first line, all four runs of digits should be present as ASCII digits. |
| 65 | +
|
| 66 | + In the second line, all four runs of digits should be Arabic digits |
| 67 | + (they may not be visible if the font does not support Arabic). |
| 68 | +
|
| 69 | + In the third line, the initial run of digits (-123) and the one following the |
| 70 | + Arabic text (-789) should be Arabic, while the others should be ASCII. |
| 71 | +
|
| 72 | + In the fourth line, only the digits following the Arabic text (-789) should be Arabic, |
| 73 | + and the others should be ASCII. |
| 74 | +
|
| 75 | + Pass the test if this is true."""; |
| 76 | + |
| 77 | + PassFailJFrame.builder() |
| 78 | + .title("ShaperTest Instruction") |
| 79 | + .instructions(INSTRUCTIONS) |
| 80 | + .columns(45) |
| 81 | + .testUI(ShaperTest::createUI) |
| 82 | + .logArea(8) |
| 83 | + .build() |
| 84 | + .awaitAndCheck(); |
| 85 | + } |
| 86 | + |
| 87 | + private static Frame createUI() { |
| 88 | + Frame frame = new Frame("ShaperTest Test UI"); |
| 89 | + frame.add(new ShaperTest()); |
| 90 | + frame.setSize(600, 400); |
| 91 | + return frame; |
| 92 | + } |
| 93 | + |
| 94 | + void dumpChars(char[] chars) { |
| 95 | + for (int i = 0; i < chars.length; ++i) { |
| 96 | + char c = chars[i]; |
| 97 | + if (c < 0x7f) { |
| 98 | + System.out.print(c); |
| 99 | + } else { |
| 100 | + String n = Integer.toHexString(c); |
| 101 | + n = "0000".substring(n.length()) + n; |
| 102 | + System.out.print("0x" + n); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private ShaperTest() { |
| 108 | + setBackground(Color.WHITE); |
| 109 | + setForeground(Color.BLACK); |
| 110 | + |
| 111 | + Font textfont = new Font(fontName, Font.PLAIN, 12); |
| 112 | + PassFailJFrame.log("asked for: " + fontName + " and got: " + textfont.getFontName()); |
| 113 | + setFont(textfont); |
| 114 | + |
| 115 | + Font font = new Font(fontName, Font.PLAIN, 18); |
| 116 | + PassFailJFrame.log("asked for: " + fontName + " and got: " + font.getFontName()); |
| 117 | + |
| 118 | + FontRenderContext frc = new FontRenderContext(null, false, false); |
| 119 | + |
| 120 | + layouts = new TextLayout[5][2]; |
| 121 | + |
| 122 | + HashMap<AttributedCharacterIterator.Attribute, Object> map = new HashMap<>(); |
| 123 | + map.put(TextAttribute.FONT, font); |
| 124 | + layouts[0][0] = new TextLayout(text, map, frc); |
| 125 | + AttributedCharacterIterator iter = new AttributedString(text, map).getIterator(); |
| 126 | + layouts[0][1] = new LineBreakMeasurer(iter, frc).nextLayout(Float.MAX_VALUE); |
| 127 | + |
| 128 | + NumericShaper arabic = NumericShaper.getShaper(NumericShaper.ARABIC); |
| 129 | + map.put(TextAttribute.NUMERIC_SHAPING, arabic); |
| 130 | + layouts[1][0] = new TextLayout(text, map, frc); |
| 131 | + iter = new AttributedString(text, map).getIterator(); |
| 132 | + layouts[1][1] = new LineBreakMeasurer(iter, frc).nextLayout(Float.MAX_VALUE); |
| 133 | + |
| 134 | + NumericShaper contextualArabic = NumericShaper.getContextualShaper(NumericShaper.ARABIC, NumericShaper.ARABIC); |
| 135 | + map.put(TextAttribute.NUMERIC_SHAPING, contextualArabic); |
| 136 | + layouts[2][0] = new TextLayout(text, map, frc); |
| 137 | + iter = new AttributedString(text, map).getIterator(); |
| 138 | + layouts[2][1] = new LineBreakMeasurer(iter, frc).nextLayout(Float.MAX_VALUE); |
| 139 | + |
| 140 | + NumericShaper contextualArabicASCII = NumericShaper.getContextualShaper(NumericShaper.ARABIC); |
| 141 | + map.put(TextAttribute.NUMERIC_SHAPING, contextualArabicASCII); |
| 142 | + layouts[3][0] = new TextLayout(text, map, frc); |
| 143 | + iter = new AttributedString(text, map).getIterator(); |
| 144 | + layouts[3][1] = new LineBreakMeasurer(iter, frc).nextLayout(Float.MAX_VALUE); |
| 145 | + |
| 146 | + NumericShaper contextualAll = NumericShaper.getContextualShaper(NumericShaper.ALL_RANGES); |
| 147 | + map.put(TextAttribute.NUMERIC_SHAPING, contextualAll); |
| 148 | + layouts[4][0] = new TextLayout(text, map, frc); |
| 149 | + iter = new AttributedString(text, map).getIterator(); |
| 150 | + layouts[4][1] = new LineBreakMeasurer(iter, frc).nextLayout(Float.MAX_VALUE); |
| 151 | + |
| 152 | + titles = new String[]{ |
| 153 | + "plain -- all digits ASCII", |
| 154 | + "Arabic -- all digits Arabic", |
| 155 | + "contextual Arabic default Arabic -- only leading digits and digits following Arabic text are Arabic", |
| 156 | + "contextual Arabic default ASCII -- only digits following Arabic text are Arabic", |
| 157 | + "contextual all default ASCII -- leading digits english, others correspond to context" |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void paint(Graphics g) { |
| 163 | + Graphics2D g2d = (Graphics2D) g; |
| 164 | + |
| 165 | + float x = 5; |
| 166 | + float y = 5; |
| 167 | + |
| 168 | + for (int i = 0; i < layouts.length; ++i) { |
| 169 | + y += 18; |
| 170 | + g2d.drawString(titles[i], x, y); |
| 171 | + y += 4; |
| 172 | + |
| 173 | + for (int j = 0; j < 2; ++j) { |
| 174 | + y += layouts[i][j].getAscent(); |
| 175 | + layouts[i][j].draw(g2d, x, y); |
| 176 | + y += layouts[i][j].getDescent() + layouts[i][j].getLeading(); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments