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

Invalid font family returned by getFontFamily()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      build 1.6.0-b105 (for Windows XP)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP & Mac OS X 10.4

      A DESCRIPTION OF THE PROBLEM :
      In some circumstances, the font family returned by StyleConstants.FontConstants.getFontFamily() is invalid. It is invalid because there is no font in the JDK with that family. As shown in the included example code, we find two "Tahoma" fonts in the JDK. Both have the family "Tahoma," and their names are "Tahoma" and "Tahoma Bold" (or "Tahoma-Bold" on Mac). But when we set the JTextPane's font to the one with the name "Tahoma Bold" and then try to get the family of the text in the JTextPane using StyleConstants.FontConstants.getFontFamily(), it returns "Tahoma Bold," but this is the font's name, not the family.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Was expecting to see the line:
      font family of first character = Tahoma
      at the end of the output.
      ACTUAL -
      Actually saw:
      font family of first character = Tahoma Bold

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.text.AttributeSet;
      import javax.swing.text.StyleConstants;
      import java.awt.*;

      public class FontBug extends JFrame {
          public FontBug() {
              Font jdkFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

              System.out.println("Found these Tahoma fonts in the JDK:");
              System.out.println("Family\tName");
              for(Font font : jdkFonts) {
                  String family = font.getFamily();
                  String name = font.getName();
                  if(family.indexOf("Tahoma") != -1) {
                      System.out.println(family + "\t" + name);
                  }
              }
              System.out.println("-----");

              addComponents();

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }

          private void addComponents() {
              setTitle("FontBug");
              JPanel mainPanel = new JPanel();
              mainPanel.setLayout(new BorderLayout());
              JTextPane textPane = new JTextPane();

              Font jdkFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
              for(Font font : jdkFonts) {
                  if (font.getName().equals("Tahoma Bold") || //Windows
                          font.getName().equals("Tahoma-Bold")) { //Mac

                      System.out.println("Setting to a font, with");
                      System.out.println(" family = " + font.getFamily());
                      System.out.println(" name = " + font.getName());
                      textPane.setFont(font);
                      break;
                  }
              }
              System.out.println("-----");

              AttributeSet attrs = textPane.getStyledDocument().getCharacterElement(0).getAttributes();
              System.out.println("font family of first character = " + StyleConstants.FontConstants.getFontFamily(attrs));
              mainPanel.add(textPane,BorderLayout.CENTER);
              getContentPane().add(mainPanel);
              pack();
          }

          public static void main(String args[]) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      FontBug s = new FontBug();
                      s.setVisible(true);
                  }
              });
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      When using StyleConstants.FontConstants.getFontFamily(), realize that you might get the font family, but you might get the font name.

            peterz Peter Zhelezniakov
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: