-
Bug
-
Resolution: Fixed
-
P3
-
8, 11, 12, 13, 14
-
b09
-
x86_64
-
windows_10
ADDITIONAL SYSTEM INFORMATION :
Windows 10 version 1903, 64 bit
A DESCRIPTION OF THE PROBLEM :
This issue appears on Windows 10 with high DPI monitors when using display scaling above 100%, e.g. 150% or 200%.
Therefore, this is related toJDK-8187367 : Numerous swing display problems with scaled displays on Windows.
When using a JEditorPane to render HTML views with W3C_LENGTH_UNITS enabled, font-sizes set using CSS are much larger than the same font size outside the HTML.
The difference in size appears to be proportional to the display scaling factor.
If the factor is 100%, the font sizes are roughly equal.
The more the scaling factor is increased, the more the font size of HTML and non HTML texts differs.
This makes it impossible to use W3C_LENGTH_UNITS when combining HTML and non HTML views.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the provided source code on a Windows machine.
Compare the HTML Paragraphs with the text in the JLabel.
Repeat the process with different display scaling factors.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
All texts should have equal size, or at least roughly equal.
In particular, the display scaling should be applied equally to HTML and non-HTML texts.
ACTUAL -
The ratio between the font sizes of HTML and non-HTML texts should remain constant for different display scaling factors.
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HtmlFontSize extends JFrame {
public HtmlFontSize() {
JLabel label = new JLabel("This is 16 pt.");
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
JEditorPane htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
HTMLEditorKit kit = new HTMLEditorKit();
htmlPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body { font-family: SansSerif; font-size: 16pt; }");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<p>This should be 16 pt.</p>\n"
+ "<p style=\"font-size:16pt\">And this as well.</p>\n"
+ "</body>\n"
+ "</html>";
Document doc = kit.createDefaultDocument();
htmlPane.setDocument(doc);
htmlPane.setText(htmlString);
setLayout(new BorderLayout());
add(label, BorderLayout.NORTH);
add(htmlPane, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
HtmlFontSize frame = new HtmlFontSize();
frame.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
One option is to disable W3C_LENGTH_UNITS, but that leads to other sizing issues.
Another option is to hard-code a smaller font-size style, but this requires knowledge of the actual scaling setting.
FREQUENCY : always
Windows 10 version 1903, 64 bit
A DESCRIPTION OF THE PROBLEM :
This issue appears on Windows 10 with high DPI monitors when using display scaling above 100%, e.g. 150% or 200%.
Therefore, this is related to
When using a JEditorPane to render HTML views with W3C_LENGTH_UNITS enabled, font-sizes set using CSS are much larger than the same font size outside the HTML.
The difference in size appears to be proportional to the display scaling factor.
If the factor is 100%, the font sizes are roughly equal.
The more the scaling factor is increased, the more the font size of HTML and non HTML texts differs.
This makes it impossible to use W3C_LENGTH_UNITS when combining HTML and non HTML views.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the provided source code on a Windows machine.
Compare the HTML Paragraphs with the text in the JLabel.
Repeat the process with different display scaling factors.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
All texts should have equal size, or at least roughly equal.
In particular, the display scaling should be applied equally to HTML and non-HTML texts.
ACTUAL -
The ratio between the font sizes of HTML and non-HTML texts should remain constant for different display scaling factors.
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HtmlFontSize extends JFrame {
public HtmlFontSize() {
JLabel label = new JLabel("This is 16 pt.");
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
JEditorPane htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
HTMLEditorKit kit = new HTMLEditorKit();
htmlPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body { font-family: SansSerif; font-size: 16pt; }");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<p>This should be 16 pt.</p>\n"
+ "<p style=\"font-size:16pt\">And this as well.</p>\n"
+ "</body>\n"
+ "</html>";
Document doc = kit.createDefaultDocument();
htmlPane.setDocument(doc);
htmlPane.setText(htmlString);
setLayout(new BorderLayout());
add(label, BorderLayout.NORTH);
add(htmlPane, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
HtmlFontSize frame = new HtmlFontSize();
frame.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
One option is to disable W3C_LENGTH_UNITS, but that leads to other sizing issues.
Another option is to hard-code a smaller font-size style, but this requires knowledge of the actual scaling setting.
FREQUENCY : always
- relates to
-
JDK-8260687 Inherited font size is smaller than expected when using StyleSheet to add styles
- Resolved
-
JDK-8261094 Open javax/swing/text/html/CSS/4765271/bug4765271.java
- Resolved
-
JDK-4765271 Mapping of the CSS length units is incorrect
- Resolved
- links to
-
Commit openjdk/jdk/48c932e1
-
Review openjdk/jdk/1628
-
Review openjdk/jdk/2223
-
Review openjdk/jdk/2256
(2 links to)