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

SPAN elements in JTextPane decrease letter spacing of monospace characters

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      java version "11.0.3" 2019-04-16 LTS
      Java(TM) SE Runtime Environment 18.9 (build 11.0.3+12-LTS)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.3+12-LTS, mixed mode)

      System/OS:
      OS Name Microsoft Windows 10 Pro
      Version 10.0.17134 Build 17134

      Desktop resolution: 3840 x 2160
      Scaling: ("Change the size of text, apps, and other items"): 250%

      A DESCRIPTION OF THE PROBLEM :
      When displaying HTML text in a in JTextPane using a monospace font, SPAN elements decrease the width of the text within. Lines of text with more SPAN elements appear shorter than lines with the same number of characters and fewer SPAN elements.

      REGRESSION : Last worked in version 8u202

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See attached minimal example.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All lines of text should appear to be the same width.
      ACTUAL -
      The first 5 lines contain 5 'A' characters with a single SPAN around 1 or more of the characters. The final line contains 5 SPAN elements (each 'A' in its own SPAN). On Windows 10 with a High DPI display, the final line is noticeably shorter than the preceding lines.

      This is not reproducible with Java 8, or on Mac OS or on Windows 10 without High DPI. Changing the scaling factor to 100% corrects the problem, but is not usable (I believe 250% is the default in Windows 10 with a 4k display).

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.border.EmptyBorder;
      import java.awt.*;

      public class SpanBug extends JFrame {

          public SpanBug() {
              setTitle("Monospace HTML viewer");

              JPanel mainPanel = new JPanel(new BorderLayout());
              mainPanel.setOpaque(false);
              mainPanel.add(new TextPanel(), BorderLayout.CENTER);
              mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
              getContentPane().add(mainPanel, BorderLayout.CENTER);

              setLocationRelativeTo(null);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
          }


          private class TextPanel extends JPanel {
              TextPanel() {
                  JTextPane textArea = new JTextPane();
                  textArea.setContentType("text/html");
                  textArea.setPreferredSize(new Dimension(100, 100));

                  textArea.setText(getCustomHtml());

                  textArea.setEditable(false);
                  setLayout(new BorderLayout());
                  add(textArea, BorderLayout.CENTER);
              }
          }

          private String getCustomHtml() {
              String fontInfo = "font-family: monospace; font-size: 14";
              return "<html>\n" +
                      "<head><style>\n" +
                      ".usefont {" + fontInfo + "}\n" +
                      ".hlt {color:#ffffff; background-color:#808080;" + fontInfo + "}</style></head>\n" +
                      "<body>\n" +
                      "<span class=\"usefont\">" +
                      "<span class=\"hlt\">AAAAA</span>" +
                      "<br>" +
                      "AAAA<span class=\"hlt\">A</span>" +
                      "<br>" +
                      "AAA<span class=\"hlt\">AA</span>" +
                      "<br>" +
                      "AA<span class=\"hlt\">AAA</span>" +
                      "<br>" +
                      "A<span class=\"hlt\">AAAA</span>" +
                      "<br>" +
                      "<span class=\"hlt\">A</span><span class=\"hlt\">A</span><span class=\"hlt\">A</span><span class=\"hlt\">A</span><span class=\"hlt\">A</span>" +
                      "<br>" +
                      "</span>" +
                      "</body>\n" +
                      "</html>";
          }

          public static void main(String[] args) {

              EventQueue.invokeLater(() -> {
                  SpanBug frame = new SpanBug();
                  frame.pack();
                  frame.setVisible(true);
              });

          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None found.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psadhukhan Prasanta Sadhukhan
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: