22
22
*/
23
23
import java .awt .Font ;
24
24
import java .awt .Graphics ;
25
+ import java .awt .Graphics2D ;
26
+ import java .awt .RenderingHints ;
27
+ import static java .awt .RenderingHints .*;
28
+ import java .awt .Toolkit ;
29
+ import java .util .Map ;
25
30
import java .util .concurrent .CountDownLatch ;
26
31
import java .util .concurrent .TimeUnit ;
27
32
import javax .swing .JFrame ;
31
36
/**
32
37
* @test
33
38
* @key headful
34
- * @bug 8142966
39
+ * @bug 8142966 8199529
35
40
* @summary Wrong cursor position in text components on HiDPI display
36
41
* @run main/othervm -Dsun.java2d.uiScale=2 SwingFontMetricsTest
37
42
*/
@@ -42,8 +47,15 @@ public class SwingFontMetricsTest {
42
47
private static final String TEXT = LOWER_CASE_TEXT + UPPER_CASE_TEXT ;
43
48
private static boolean passed = false ;
44
49
private static CountDownLatch latch = new CountDownLatch (1 );
50
+ private static Object aaHint = null ;
45
51
46
52
public static void main (String [] args ) throws Exception {
53
+ Map map = (Map )Toolkit .getDefaultToolkit ().getDesktopProperty ("awt.font.desktophints" );
54
+ aaHint = map .get (RenderingHints .KEY_TEXT_ANTIALIASING );
55
+ if (aaHint == null ) {
56
+ aaHint = VALUE_TEXT_ANTIALIAS_DEFAULT ;
57
+ }
58
+
47
59
SwingUtilities .invokeAndWait (SwingFontMetricsTest ::createAndShowGUI );
48
60
latch .await (5 , TimeUnit .SECONDS );
49
61
@@ -61,7 +73,10 @@ private static void createAndShowGUI() {
61
73
public void paint (Graphics g ) {
62
74
super .paint (g );
63
75
Font font = getFont ();
76
+ Graphics2D g2d = (Graphics2D )g ;
64
77
int width1 = getFontMetrics (font ).stringWidth (TEXT );
78
+ // Set the same AA hint that the built-in Swing L&Fs set.
79
+ g2d .setRenderingHint (KEY_TEXT_ANTIALIASING , aaHint );
65
80
int width2 = g .getFontMetrics (font ).stringWidth (TEXT );
66
81
passed = (width1 == width2 );
67
82
latch .countDown ();
0 commit comments