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

javax.swing.plaf.basic.BasicHTML does not support fonts loaded at runtime

XMLWordPrintable

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

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]
      Probably all OSes

      A DESCRIPTION OF THE PROBLEM :
      javax.swing.text.html.StyleSheet used in javax.swing.plaf.basic.BasicHTML uses new Font(String, int, int) constructor unless a default style with a font has been defined. BasicHTML does not install this default style correctly.

      The new Font(String, int, int) constructor does not seem to find fonts which are not installed in the OS, but have been loaded previously. (I should perhaps file this as another report)

      Because of the two previously mentioned issues, any font loaded at runtime, which is not installed in the OS will not work on views created with BasicHTML (f.ex. JLabels).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the source code replacing the font file with a truetype font not currently installed in the OS.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The two strings displayed should use the font loaded
      ACTUAL -
      Since the new Font(String, int, int) returns the font Dialog, unless the font requested is installed in the OS, the second string displayed is shown in the font Dialog.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Font;
      import java.io.File;
      import javax.swing.JFrame;
      import javax.swing.JLabel;

      public class Main extends JFrame {

      public Main() {
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      Font f = null;
      try {
      File fontFile = new File("uninstalled-fontfile.ttf");
      f = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(12.0f);
      } catch (Exception e) {
      e.printStackTrace();
      }
      JLabel ok = new JLabel("This is a font loaded at runtime");
      JLabel bugged = new JLabel("<html>This should be the same font");
      ok.setFont(f);
      bugged.setFont(f);
      add(ok, BorderLayout.NORTH);
      add(bugged, BorderLayout.SOUTH);
      pack();
      }

      public static void main(String[] args) {
      new Main().setVisible(true);
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Re-implement BasicHTML (and BasicLabelUI) so that the method setFontAndColor in BasicHTML.BasicDocument is as follows:
      (This installs the default style with a font attribute, which will subsequently be used in javax.swing.text.StyleContext in the method getFont(String, int, int))

      private void setFontAndColor(Font font, Color fg) {
      StyleSheet ss = getStyleSheet();
      ss.addRule(sun.swing.SwingUtilities2.displayPropertiesToCSS(font,fg));
      Style defaultStyle = ss.getStyle(StyleContext.DEFAULT_STYLE);
      if (defaultStyle == null) {
      ss.addStyle(StyleContext.DEFAULT_STYLE, null);
      defaultStyle = ss.getStyle(StyleContext.DEFAULT_STYLE);
      }
      final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
      defaultStyle.addAttribute(FONT_ATTRIBUTE_KEY, font);
      }

      Optionally you could override BasicHTML.BasicEditorKit, javax.swing.text.html.StyleSheet and java.awt.Font so that they would use an implementation of Font, which caches fonts loaded at runtime.

      Overall a very tedious workaround, which is not guaranteed to work in later versions of swing.

            peterz Peter Zhelezniakov
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: