Skip to content

Commit db08726

Browse files
author
Abhishek Kumar
committed
8352966: Opensource Several Font related tests - Batch 2
Reviewed-by: aivanov
1 parent 6b7b324 commit db08726

File tree

5 files changed

+623
-0
lines changed

5 files changed

+623
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 1999, 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.Dimension;
26+
import java.awt.Font;
27+
import java.awt.Graphics;
28+
import java.awt.Graphics2D;
29+
import java.awt.Rectangle;
30+
import java.awt.Shape;
31+
import java.awt.font.GlyphVector;
32+
import java.awt.geom.Point2D;
33+
import java.awt.geom.Rectangle2D;
34+
35+
import javax.swing.JPanel;
36+
37+
/*
38+
* @test
39+
* @bug 4255072
40+
* @summary Display the outline of a GlyphVector that has overlapping 'O' characters in it.
41+
* The places where the strokes of the characters cross should be filled in.
42+
* @library /java/awt/regtesthelpers
43+
* @build PassFailJFrame
44+
* @run main/manual TestOutline
45+
*/
46+
47+
public final class TestOutline extends JPanel {
48+
Shape outline;
49+
50+
public static void main(String[] args) throws Exception {
51+
final String INSTRUCTIONS = """
52+
Two overlapping 'O' characters should appear. Pass the test if
53+
the places where the strokes of the characters cross is filled in.
54+
Fail it if these places are not filled in.""";
55+
56+
PassFailJFrame.builder()
57+
.title("TestOutline Instruction")
58+
.instructions(INSTRUCTIONS)
59+
.columns(35)
60+
.splitUI(TestOutline::new)
61+
.build()
62+
.awaitAndCheck();
63+
}
64+
65+
@Override
66+
public Dimension getPreferredSize() {
67+
return new Dimension(200, 250);
68+
}
69+
70+
@Override
71+
public void paint(Graphics g) {
72+
Graphics2D g2d = (Graphics2D) g;
73+
g2d.setColor(Color.WHITE);
74+
g2d.fillRect(0, 0, getWidth(), getHeight());
75+
76+
if (outline == null) {
77+
outline = createLayout(g2d);
78+
}
79+
80+
g2d.setColor(Color.BLACK);
81+
g2d.fill(outline);
82+
}
83+
84+
private Shape createLayout(Graphics2D g2d) {
85+
Font font = new Font(Font.DIALOG, Font.PLAIN, 144);
86+
GlyphVector gv = font.createGlyphVector(g2d.getFontRenderContext(), "OO");
87+
gv.performDefaultLayout();
88+
Point2D pt = gv.getGlyphPosition(1);
89+
double delta = -pt.getX() / 2.0;
90+
pt.setLocation(pt.getX() + delta, pt.getY());
91+
gv.setGlyphPosition(1, pt);
92+
93+
pt = gv.getGlyphPosition(2);
94+
pt.setLocation(pt.getX() + delta, pt.getY());
95+
gv.setGlyphPosition(2, pt);
96+
97+
Rectangle2D bounds = gv.getLogicalBounds();
98+
Rectangle d = getBounds();
99+
float x = (float) ((d.width - bounds.getWidth()) / 2 + bounds.getX());
100+
float y = (float) ((d.height - bounds.getHeight()) / 2 - bounds.getY());
101+
System.out.println("loc: " + x + ", " + y);
102+
return gv.getOutline(x, y);
103+
}
104+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2006, 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.Dimension;
26+
import java.awt.Font;
27+
import java.awt.Graphics;
28+
import java.awt.Graphics2D;
29+
import java.awt.GraphicsEnvironment;
30+
import java.awt.RenderingHints;
31+
32+
import javax.swing.JPanel;
33+
34+
/*
35+
* @test
36+
* @bug 6320502
37+
* @summary Display laid out text which substitutes invisible glyphs correctly.
38+
* @library /java/awt/regtesthelpers /test/lib
39+
* @build PassFailJFrame jtreg.SkippedException
40+
* @run main/manual TestGASPHint
41+
*/
42+
43+
public class TestGASPHint extends JPanel {
44+
private static final String text = "\u0905\u0901\u0917\u094d\u0930\u0947\u091c\u093c\u0940";
45+
private static final Font font = getPhysicalFontForText(text, Font.PLAIN, 36);
46+
47+
public static void main(String[] args) throws Exception {
48+
if (font == null) {
49+
throw new jtreg.SkippedException("No Devanagari font found. Test Skipped");
50+
}
51+
52+
final String INSTRUCTIONS = """
53+
A short piece of Devanagari text should appear without any
54+
artifacts. In particular there should be no "empty rectangles"
55+
representing the missing glyph.
56+
57+
If the above condition is true, press Pass, else Fail.""";
58+
59+
PassFailJFrame.builder()
60+
.title("TestGASPHint Instruction")
61+
.instructions(INSTRUCTIONS)
62+
.columns(32)
63+
.splitUI(TestGASPHint::new)
64+
.build()
65+
.awaitAndCheck();
66+
}
67+
68+
@Override
69+
public Dimension getPreferredSize() {
70+
return new Dimension(200, 200);
71+
}
72+
73+
@Override
74+
public void paint(Graphics g) {
75+
Graphics2D g2d = (Graphics2D) g;
76+
77+
g2d.setColor(Color.WHITE);
78+
g2d.fillRect(0, 0, getWidth(), getHeight());
79+
80+
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
81+
RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
82+
83+
g2d.setFont(font);
84+
g2d.setColor(Color.BLACK);
85+
g2d.drawString(text, 10, 50);
86+
}
87+
88+
/*
89+
* Searches the available system fonts for a font which can display all the
90+
* glyphs in the input text correctly. Returns null, if not found.
91+
*/
92+
private static Font getPhysicalFontForText(String text, int style, int size) {
93+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
94+
String[] names = ge.getAvailableFontFamilyNames();
95+
96+
for (String n : names) {
97+
switch (n.toLowerCase()) {
98+
case "dialog":
99+
case "dialoginput":
100+
case "serif":
101+
case "sansserif":
102+
case "monospaced":
103+
break;
104+
default:
105+
Font f = new Font(n, style, size);
106+
if (f.canDisplayUpTo(text) == -1) {
107+
return f;
108+
}
109+
}
110+
}
111+
return null;
112+
}
113+
}

0 commit comments

Comments
 (0)