Solved

Loading TYPE1 fonts (with JRE 1.5.0)

Posted on 2004-05-02
10
237 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 46
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 32
Coding for the first time 9 100
Outlook for mac 2011 21 54
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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