Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JComponent/4337267/bug4337267.java
41153 views
1
/*
2
* Copyright (c) 2009, 2017, 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
* @key headful
27
* @bug 4337267
28
* @summary test that numeric shaping works in Swing components
29
* @author Sergey Groznyh
30
* @run main bug4337267
31
*/
32
33
import java.awt.Component;
34
import java.awt.Dimension;
35
import java.awt.Graphics;
36
import java.awt.font.NumericShaper;
37
import java.awt.font.TextAttribute;
38
import java.awt.image.BufferedImage;
39
import javax.swing.BoxLayout;
40
import javax.swing.JComponent;
41
import javax.swing.JFrame;
42
import javax.swing.JLabel;
43
import javax.swing.JPanel;
44
import javax.swing.JTextArea;
45
import javax.swing.SwingUtilities;
46
47
public class bug4337267 {
48
TestJPanel p1, p2;
49
TestBufferedImage i1, i2;
50
JComponent[] printq;
51
static JFrame window;
52
static boolean testFailed = false;
53
54
String shaped =
55
"000 (E) 111 (A) \u0641\u0642\u0643 \u0662\u0662\u0662 (E) 333";
56
String text = "000 (E) 111 (A) \u0641\u0642\u0643 222 (E) 333";
57
58
void run() {
59
initUI();
60
testTextComponent();
61
testNonTextComponentHTML();
62
testNonTextComponentPlain();
63
64
}
65
66
void initUI() {
67
window = new JFrame("bug4337267");
68
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
69
window.setSize(800, 600);
70
Component content = createContentPane();
71
window.add(content);
72
window.setVisible(true);
73
}
74
75
void fail(String message) {
76
testFailed = true;
77
throw new RuntimeException(message);
78
}
79
80
void assertEquals(Object o1, Object o2) {
81
if ((o1 == null) && (o2 != null)) {
82
fail("Expected null, got " + o2);
83
} else if ((o1 != null) && (o2 == null)) {
84
fail("Expected " + o1 + ", got null");
85
} else if (!o1.equals(o2)) {
86
fail("Expected " + o1 + ", got " + o2);
87
}
88
}
89
90
void testTextComponent() {
91
System.out.println("testTextComponent:");
92
JTextArea area1 = new JTextArea();
93
injectComponent(p1, area1, false);
94
area1.setText(shaped);
95
JTextArea area2 = new JTextArea();
96
injectComponent(p2, area2, true);
97
area2.setText(text);
98
window.repaint();
99
printq = new JComponent[] { area1, area2 };
100
printComponent(printq[0], i1);
101
printComponent(printq[1], i2);
102
assertEquals(p1.image, p2.image);
103
assertEquals(i1, i2);
104
}
105
106
void testNonTextComponentHTML() {
107
System.out.println("testNonTextComponentHTML:");
108
JLabel label1 = new JLabel();
109
injectComponent(p1, label1, false);
110
label1.setText("<html>" + shaped);
111
JLabel label2 = new JLabel();
112
injectComponent(p2, label2, true);
113
label2.setText("<html>" + text);
114
window.repaint();
115
printq = new JComponent[] { label1, label2 };
116
printComponent(printq[0], i1);
117
printComponent(printq[1], i2);
118
assertEquals(p1.image, p2.image);
119
assertEquals(i1, i2);
120
}
121
122
void testNonTextComponentPlain() {
123
System.out.println("testNonTextComponentPlain:");
124
JLabel label1 = new JLabel();
125
injectComponent(p1, label1, false);
126
label1.setText(shaped);
127
JLabel label2 = new JLabel();
128
injectComponent(p2, label2, true);
129
label2.setText(text);
130
window.repaint();
131
printq = new JComponent[] { label1, label2 };
132
printComponent(printq[0], i1);
133
printComponent(printq[1], i2);
134
assertEquals(p1.image, p2.image);
135
assertEquals(i1, i2);
136
}
137
138
void setShaping(JComponent c) {
139
c.putClientProperty(TextAttribute.NUMERIC_SHAPING,
140
NumericShaper.getContextualShaper(NumericShaper.ARABIC));
141
}
142
143
void injectComponent(JComponent p, JComponent c, boolean shape) {
144
if (shape) {
145
setShaping(c);
146
}
147
p.removeAll();
148
p.add(c);
149
}
150
151
void printComponent(JComponent c, TestBufferedImage i) {
152
Graphics g = i.getGraphics();
153
g.setColor(c.getBackground());
154
g.fillRect(0, 0, i.getWidth(), i.getHeight());
155
c.print(g);
156
}
157
158
Component createContentPane() {
159
Dimension size = new Dimension(500, 100);
160
i1 = new TestBufferedImage(size.width, size.height,
161
BufferedImage.TYPE_INT_ARGB);
162
i2 = new TestBufferedImage(size.width, size.height,
163
BufferedImage.TYPE_INT_ARGB);
164
p1 = new TestJPanel();
165
p1.setPreferredSize(size);
166
p2 = new TestJPanel();
167
p2.setPreferredSize(size);
168
JPanel panel = new JPanel();
169
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
170
panel.add(p1);
171
panel.add(p2);
172
173
return panel;
174
}
175
176
static class TestBufferedImage extends BufferedImage {
177
int MAX_GLITCHES = 0;
178
179
TestBufferedImage(int width, int height, int imageType) {
180
super(width, height, imageType);
181
}
182
183
@Override
184
public boolean equals(Object other) {
185
if (! (other instanceof TestBufferedImage)) {
186
return false;
187
}
188
TestBufferedImage image2 = (TestBufferedImage) other;
189
int width = getWidth();
190
int height = getHeight();
191
if ((image2.getWidth() != width) || (image2.getHeight() != height)) {
192
return false;
193
}
194
int glitches = 0;
195
for (int x = 0; x < width; x++) {
196
for (int y = 0; y < height; y++) {
197
int rgb1 = getRGB(x, y);
198
int rgb2 = image2.getRGB(x, y);
199
if (rgb1 != rgb2) {
200
//System.out.println(x+" "+y+" "+rgb1+" "+rgb2);
201
glitches++;
202
}
203
}
204
}
205
return glitches <= MAX_GLITCHES;
206
}
207
}
208
209
static class TestJPanel extends JPanel {
210
TestBufferedImage image = createImage(new Dimension(1, 1));
211
212
TestBufferedImage createImage(Dimension d) {
213
return new TestBufferedImage(d.width, d.height,
214
BufferedImage.TYPE_INT_ARGB);
215
}
216
217
public void setPreferredSize(Dimension size) {
218
super.setPreferredSize(size);
219
image = createImage(size);
220
}
221
222
public void paint(Graphics g) {
223
Graphics g0 = image.getGraphics();
224
super.paint(g0);
225
g.drawImage(image, 0, 0, this);
226
} }
227
228
229
230
public static void main(String[] args) throws Exception {
231
try {
232
final bug4337267 test = new bug4337267();
233
SwingUtilities.invokeAndWait(new Runnable() {
234
public void run() {
235
test.run();
236
}
237
});
238
239
if (testFailed) {
240
throw new RuntimeException("FAIL");
241
}
242
243
System.out.println("OK");
244
} finally {
245
if (window != null) SwingUtilities.invokeAndWait(() -> window.dispose());
246
}
247
}
248
}
249
250