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

Ugly fonts for HTML text in Linux

XMLWordPrintable

      Run the following program on Ubuntu 8.04 using JDK 6:

      ---%<---
      public class UglyFonts extends javax.swing.JFrame {
          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new UglyFonts().setVisible(true);
                  }
              });
          }
          public UglyFonts() {
              initComponents();
          }
          @SuppressWarnings("unchecked")
          // <editor-fold defaultstate="collapsed" desc="Generated Code">
          private void initComponents() {

              label = new javax.swing.JLabel();
              plainTextEditor = new javax.swing.JEditorPane();
              htmlEditor = new javax.swing.JEditorPane();
              htmlEditorWithCSS = new javax.swing.JEditorPane();
              serifLabel = new javax.swing.JLabel();
              htmlLabel = new javax.swing.JLabel();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              setTitle("Ugly Fonts");

              label.setText("The quick brown fox jumps over the lazy dog.");

              plainTextEditor.setText("The quick brown fox jumps over the lazy dog.");

              htmlEditor.setContentType("text/html");
              htmlEditor.setText("<html><body>The quick brown fox jumps over the lazy dog.</body></html>");

              htmlEditorWithCSS.setContentType("text/html");
              htmlEditorWithCSS.setText("<html><body style=\"font-family: dialog\">The quick brown fox jumps over the lazy dog.</body></html>");

              serifLabel.setFont(new java.awt.Font("Serif", 0, 12)); // NOI18N
              serifLabel.setText("The quick brown fox jumps over the lazy dog.");

              htmlLabel.setText("<html><body>The quick brown fox jumps over the lazy dog.</body></html>");

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addComponent(htmlEditor, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                          .addComponent(plainTextEditor)
                          .addComponent(label, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                          .addComponent(htmlEditorWithCSS, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                          .addComponent(serifLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                          .addComponent(htmlLabel))
                      .addContainerGap())
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(label)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(plainTextEditor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(htmlEditor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(htmlEditorWithCSS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(serifLabel)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addComponent(htmlLabel)
                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              pack();
          }// </editor-fold>
          // Variables declaration - do not modify
          private javax.swing.JEditorPane htmlEditor;
          private javax.swing.JEditorPane htmlEditorWithCSS;
          private javax.swing.JLabel htmlLabel;
          private javax.swing.JLabel label;
          private javax.swing.JEditorPane plainTextEditor;
          private javax.swing.JLabel serifLabel;
          // End of variables declaration
      }
      ---%<---

      A screenshot from my computer is attached.

      Note the quite visible degradation in appearance of the JEditorPane (3rd row) once it is set to use HTML content, as opposed to plain text (2nd row): it switches to a non-anti-aliased serif font with badly broken metrics, giving it the "ransom note" appearance, and is also too large.

      The JLabel using HTML (6th row) looks normal. The JLabel just using the JRE's standard serif font (5th row) is ugly - the font is not very good, and the anti-aliasing does not seem to be working - but at least the lowercase letters obey a consistent midline, and the size is right.

            Unassigned Unassigned
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: