Solved

Loading TYPE1 fonts (with JRE 1.5.0)

Posted on 2004-05-02
10
231 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
wordcount challenge 11 75
maven project error 5 21
how do i compare an object based on two fields 6 31
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now