3 Replies Latest reply on May 4, 2004 12:01 PM by 807587

    Loading TYPE1 fonts with JRE 1.5.0

    807587
      Supposedly 1.5.0 has added support for Adobe TYPE1 fonts ..

      But it doesn't seem to work when I try to load any.

      I've created this font loader which loads a font from the net and uses it in a JTextPane. It works fine with TrueType fonts, but when I modified it to support TYPE1 it doesn't work.

      It doesn't display and error message or anything, it's just that all fonts I load seem to look just like the default Swing font ...

      Does anyone know if it's possible to load TYPE1 fonts using the createFont() method yet?
        • 1. Re: Loading TYPE1 fonts with JRE 1.5.0
          807587
          Here's an example of a program that will load a font from an url and use it in a JTextPane.

          This code works with TrueType font if you change the createFont() parameters to Font.TRUETYPE_FONT.

          The code does not work with createFont() and the specified .pfa file. However, if I download the font and put it in $JAVA_HOME/jre/lib/fonts/ before i start the java application, I'm able to use the font using new Font().

          So, this means that there is nothing wrong with the font file, but it cannot be used with createFont()

          Should I submit a bug to Sun?
          import javax.swing.*;
          import java.awt.*;
          import javax.swing.text.*;
          import java.net.*;
          import java.io.*;
          
          public class FontTest extends JFrame {
          
               private String fontURL = null;
          
               public FontTest(String fontURL) {
                    setSize(400,400);
                    this.fontURL = fontURL;
                    System.out.println("Trying to use " + fontURL +" ...");
               
          
          /*
                    // this works if I put the font in $JAVA_HOME/jre/lib/fonts/
                    Font font = new Font("Times New Roman PS", Font.BOLD, 32);
          */
          
          
                    Font font = getFont(fontURL);
          
                    StyleContext sc = new StyleContext();
                    Style style = sc.addStyle("Paragraph Style", null);
                    StyleConstants.setFontFamily(style, font.getFamily());
                    StyleConstants.setBold(style, true);
                    StyleConstants.setItalic(style, false);
          
                    System.out.println(font.getFamily());
                    System.out.println(font.getName());
                    System.out.println(font.getPSName());
                    
          //          System.out.println("isBold="+font.isBold());
          //          System.out.println("isItalic="+font.isItalic());
          
                    StyleConstants.setFontSize(style, 22);
                    
                    DefaultStyledDocument doc = new DefaultStyledDocument(sc);
                    doc.setLogicalStyle(0, style);
                    JTextPane tp = new JTextPane(doc);
                    Container c = getContentPane();
                    c.add(tp, BorderLayout.CENTER);
                    
               }
          
          
          
             /**
               * Method that will load a font file from an URL and return a Font object
               * @return the Font object
               */
              public Font getFont( String urlToFont ) {
                  Font testFont = null;
                  try
                  {
                          InputStream inps = (new URL(urlToFont)).openStream();
                          testFont = Font.createFont(Font.TYPE1_FONT, inps);
                  }
                  catch (FontFormatException ffe)
                  {
                          ffe.printStackTrace();
                  }
                  catch (IOException ioe)
                  {
                          JOptionPane.showMessageDialog(null,
                                  "Could not load font - " + urlToFont,
                                  "Unable to load font",
                                  JOptionPane.WARNING_MESSAGE);
                  }
                  catch (Exception e) {
                          e.printStackTrace();
                  }
          
                  return testFont;
              }
          
          
               public static void main( String args[] ) {
                    FontTest f = new FontTest("http://www.cs.umb.edu/~bill/java/tools/frame-6.0/fminit/fontdir/TimesNewRoman-Bold.pfa");
                    f.show();
               }
          }
          • 2. Re: Loading TYPE1 fonts with JRE 1.5.0
            807587
            Generally fonts are created by getFont() method of StyledDocument interface. The method gets Fontname, size etc. from text element's attributes and creates a new font. Your font can't be created automatically. So to solve the problem just override getFont() method of your document and check if the font is Adobe TYPE1 create the font via your own algorithm. In other cases call super.getFont().

            regards,
            Stas
            • 3. Re: Loading TYPE1 fonts with JRE 1.5.0
              807587
              ah!..

              thanks Stas, that fixed my problems :)

              Here's a working example of loading a TYPE1 font from an URL and using it in a JTextPane for those interested:
              import javax.swing.*;
              import java.awt.*;
              import javax.swing.text.*;
              import java.net.*;
              import java.io.*;
              import java.util.*;
              
              public class FontTest extends JFrame {
              
                   private String fontURL = null;
                   private TreeMap fontMap = new TreeMap();     
              
                   public FontTest(String fontURL) {
                        setSize(400,400);
                        this.fontURL = fontURL;
                        System.out.println("Trying to use " + fontURL +" ...");
                   
                        Font font = getFont(fontURL);
                        fontMap.put(font.getFamily(), font);
              
                        StyleContext sc = new StyleContext();
                        Style style = sc.addStyle("Paragraph Style", null);
                        StyleConstants.setFontFamily(style, font.getFamily());
                        StyleConstants.setFontSize(style, 22);
                        
                        FontDocument doc = new FontDocument(sc);
                        doc.setLogicalStyle(0, style);
                        JTextPane tp = new JTextPane(doc);
                        Container c = getContentPane();
                        c.add(tp, BorderLayout.CENTER);
                        
                   }     
              
              
              
                 /**
                   * Method that will load a font file from an URL and return a Font object
                   * @return the Font object
                   */
                  public Font getFont( String urlToFont ) {
                      Font testFont = null;
                      try
                      {
                              InputStream inps = (new URL(urlToFont)).openStream();
                              testFont = Font.createFont(Font.TYPE1_FONT, inps);
                      }
                      catch (FontFormatException ffe)
                      {
                              ffe.printStackTrace();
                      }
                      catch (IOException ioe)
                      {
                              JOptionPane.showMessageDialog(null,
                                      "Could not load font - " + urlToFont,
                                      "Unable to load font",
                                      JOptionPane.WARNING_MESSAGE);
                      }
                      catch (Exception e) {
                              e.printStackTrace();
                      }
              
                      return testFont;
                  }
              
              
                   class FontDocument extends DefaultStyledDocument {
                        public FontDocument(StyleContext sc) {
                             super(sc);
                        } 
                        public FontDocument() {
                             super();
                        }
              
                        public Font getFont(AttributeSet attr) {
                             String fontFamily = StyleConstants.getFontFamily(attr);
                             if(fontMap.containsKey( fontFamily )) {
                                  float size = StyleConstants.getFontSize(attr); 
                                  Font f = ((Font)fontMap.get( fontFamily )).deriveFont(size);
                                  return f;
                             }
                             else return super.getFont(attr);
                        }
                   }
              
                   public static void main( String args[] ) {
                        FontTest f = new FontTest("http://www.cs.umb.edu/~bill/java/tools/frame-6.0/fminit/fontdir/TimesNewRoman-Bold.pfa");
                        f.show();
                   }
              }