Skip to content

Commit 48c932e

Browse files
mperktoldaivanov-jdk
authored andcommittedFeb 8, 2021
8231286: HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS
Reviewed-by: aivanov, psadhukhan
1 parent dbc35f6 commit 48c932e

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed
 

‎src/java.desktop/share/classes/javax/swing/JEditorPane.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ private void writeObject(ObjectOutputStream s) throws IOException {
16211621

16221622
/**
16231623
* Key for a client property used to indicate whether
1624-
* <a href="http://www.w3.org/TR/CSS21/syndata.html#length-units">
1624+
* <a href="https://www.w3.org/TR/CSS22/syndata.html#length-units">
16251625
* w3c compliant</a> length units are used for html rendering.
16261626
* <p>
16271627
* By default this is not enabled; to enable

‎src/java.desktop/share/classes/javax/swing/text/html/CSS.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.awt.Color;
2929
import java.awt.Font;
30-
import java.awt.HeadlessException;
3130
import java.awt.Image;
3231
import java.awt.Toolkit;
3332
import java.io.IOException;
@@ -2881,18 +2880,14 @@ static class LengthUnit implements Serializable {
28812880
lengthMapping.put("cm", 28.3464f);
28822881
lengthMapping.put("pc", 12f);
28832882
lengthMapping.put("in", 72f);
2884-
int res = 72;
2885-
try {
2886-
res = Toolkit.getDefaultToolkit().getScreenResolution();
2887-
} catch (HeadlessException e) {
2888-
}
2889-
// mapping according to the CSS2 spec
2890-
w3cLengthMapping.put("pt", res / 72f);
2891-
w3cLengthMapping.put("px", 1f);
2892-
w3cLengthMapping.put("mm", res / 25.4f);
2893-
w3cLengthMapping.put("cm", res / 2.54f);
2894-
w3cLengthMapping.put("pc", res / 6f);
2895-
w3cLengthMapping.put("in", (float) res);
2883+
// Mapping according to the CSS2.2 spec
2884+
// https://www.w3.org/TR/CSS22/syndata.html#x39
2885+
w3cLengthMapping.put("pt", 96f / 72f); // 1/72 of 1in
2886+
w3cLengthMapping.put("px", 1f); // 1/96 of 1in
2887+
w3cLengthMapping.put("mm", 96f / 2.54f / 10f); // 1/10 of 1cm
2888+
w3cLengthMapping.put("cm", 96f / 2.54f); // 96px/2.54
2889+
w3cLengthMapping.put("pc", 96f / 6f); // 1/6 of 1in
2890+
w3cLengthMapping.put("in", 96f); // 96px
28962891
}
28972892

28982893
LengthUnit(String value, short defaultType, float defaultValue) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2021, 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+
* @bug 8231286
26+
* @summary Verifies if HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS
27+
* @run main/othervm -Dsun.java2d.uiScale=1.0 HtmlFontSizeTest
28+
*/
29+
30+
import java.awt.Dimension;
31+
import java.util.Locale;
32+
33+
import javax.swing.JEditorPane;
34+
import javax.swing.SwingUtilities;
35+
import javax.swing.text.Document;
36+
import javax.swing.text.html.HTMLEditorKit;
37+
38+
public class HtmlFontSizeTest {
39+
static volatile Dimension w3cFrameSize;
40+
static volatile Dimension stdFrameSize;
41+
42+
private static Dimension test(boolean w3ccheck) {
43+
JEditorPane htmlPane = new JEditorPane();
44+
htmlPane.setEditable(false);
45+
46+
if (w3ccheck) {
47+
htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
48+
}
49+
50+
HTMLEditorKit kit = new HTMLEditorKit();
51+
htmlPane.setEditorKit(kit);
52+
53+
String htmlString = "<html>\n"
54+
+ "<body style=\"font-family:SansSerif; font-size:16pt\">\n"
55+
+ "<p>This should be 16 pt.</p>\n"
56+
+ "</body>\n"
57+
+ "</html>";
58+
59+
Document doc = kit.createDefaultDocument();
60+
htmlPane.setDocument(doc);
61+
htmlPane.setText(htmlString);
62+
63+
return htmlPane.getPreferredSize();
64+
}
65+
66+
public static void main(String[] args) throws Exception {
67+
SwingUtilities.invokeAndWait(() -> {
68+
w3cFrameSize = test(true);
69+
stdFrameSize = test(false);
70+
});
71+
System.out.println("size with W3C:" + w3cFrameSize);
72+
System.out.println("size without W3C:" + stdFrameSize);
73+
74+
float ratio = (float)w3cFrameSize.width / (float)stdFrameSize.width;
75+
System.out.println("w3cFrameSize.width/stdFrameSize.width " + ratio);
76+
77+
if (!"1.3".equals(String.format(Locale.ENGLISH, "%.1f", ratio))) {
78+
throw new RuntimeException("HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS");
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)
Please sign in to comment.