Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loading TYPE1 fonts (with JRE 1.5.0)

Posted on 2004-05-02
10
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

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