Solved

Loading TYPE1 fonts (with JRE 1.5.0)

Posted on 2004-05-02
10
236 Views
Last Modified: 2013-12-03
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?
0
Comment
Question by:borbjo
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971323
put ur font in $JAVA_HOME\j2sdk1.4.2_02\jre\lib\fonts
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971324
put ur font in $JAVA_HOME\j2sdk1.4.2_02\jre\lib\fonts and check out the name of the font
0
 

Author Comment

by:borbjo
ID: 10972680
createFont() is used to load font over a URL ..

This is one of my requirements - to be able to load a font and use it directory, without installing it in the OS. It works fine for truetype fonts, but not TYPE1 fonts.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 10974032
> Does anyone know if it's possible to load TYPE1 fonts using the createFont() method yet?


it is. http://java.sun.com/j2se/1.5.0/docs/guide/2d/new_features.html
0
 

Author Comment

by:borbjo
ID: 10974966
Yes, I know it is according to the documentation.

But I haven't been able to do it.

Here's my code that loads a font from a server, and uses it in a JTextPane. If you have a PFB file you can test it (it works with TrueType if you change the Font.TYPE1 to Font.TRUETYPE btw) .. you specify the font to load in args[0] ...


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 +" ...");
            Font font = getFont(fontURL);
            // Font font = getFont("http://dev17.xait.no/dod/fonts/adobe.otf");                        // OpenType font test

            StyleContext sc = new StyleContext();
            Style style = sc.addStyle("Paragraph Style", null);
            StyleConstants.setFontFamily(style, font.getFamily());
            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(args[0]);
            f.show();
      }
}



Since this code works with TrueType fonts I try it on (when changing the parameters used in createFont()) - I come to the conclusion that

1. The font files I've tested it on have been erronous
2. It doesn't work at all

I've tested it on multiple PFB files, and when I've tested it on other files (non font files) I've gotten an error (invalid font data). No error in the pfb files though.
0
 
LVL 92

Expert Comment

by:objects
ID: 10975126
no idea sorry, perhaps raise a bug with sun.
0
 

Author Comment

by:borbjo
ID: 10975807
Ok, thanks ..

FYI I've also tested PFA files now (PFB=Binary, PFA=Ascii) ..

E.g. this file:
http://www.cs.umb.edu/~bill/java/tools/frame-6.0/fminit/fontdir/TimesNewRoman-Bold.pfa

I'm able to get it to work using new Font() after I've copied the file into $JAVA_HOME/jre/lib/fonts/ .. however, the createFont() method is still not working.
0
 

Author Comment

by:borbjo
ID: 11267793
I discovered the problem here; I needed to override the getFont() method of DefaultStyledDocument to search for the font, as getFont() will normally only find installed fonts.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11742697
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question