Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loading TYPE1 fonts (with JRE 1.5.0)

Posted on 2004-05-02
10
Medium Priority
?
243 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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