Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8231286

HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS

XMLWordPrintable

    • 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 to JDK-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


        1. Capture.JPG
          26 kB
          Pardeep Sharma
        2. HtmlFontSize.java
          2 kB
          Pardeep Sharma
        3. Test.java
          4 kB
          Prasanta Sadhukhan

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: