Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Custom Fonts [java.io.IOException: Error reading font data.]

Tags:

java

text

fonts

The title doesn't allow me to say Problem, so the actual error message was -

java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Unknown Source)
at AddFont.createFont(AddFont.java:11)
at MainFrame$1.run(MainFrame.java:105)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The code is -

     public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
 public void run() {
    try {
        AddFont addFont = new AddFont();
        addFont.createFont();
    } catch (Exception e) {
        e.printStackTrace();
    }
    createGUI();

 } //public void run() Closing
});
}

and the file that I used to get the AddFont addFont-

import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;


public class AddFont extends MainFrame{
public void createFont(){
Font ttfBase = null;
    Font telegraficoFont = null;{
try {
    InputStream myStream = new BufferedInputStream(new FileInputStream(FONT_PATH_TELEGRAFICO));
    ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
    telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
    ex.printStackTrace();
    System.err.println("Font not loaded.");
}
}
}
}

I was instructed to make a new thread because this is a separate problem from my other one.

Why am I getting this problem, and how can I fix it? I have my TELEGRAFICO.TTF font in my imageFolder, which is really just my resources folder. I use

   public static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";

to call in my path.

What am I doing wrong?

EDIT - I no longer get that error message, and I don't get "Font not loaded". How can I use the font in other class files other than the one I made that method in?

(I want to use that font on buttons in multiple class files. I tried using it here -

regButton = new JButton();
regButton.setText("Foo");
regButton.setAlignmentX(Component.CENTER_ALIGNMENT);
regButton.setFont(telegraficoFont);

But it said telegraficoFont cannot be resolved to a variable. (Because it was in a different class file.)

How can I fix this? Thanks again for the help.

like image 352
Hathor Avatar asked May 18 '13 07:05

Hathor


3 Answers

In some cases the cause is the running instance not being able to write to the Java temp directory (java.io.tmpdir).

If your are running it on tomcat maybe you deleted the temp directory of the tomcat installation, or the folder have wrong permissions.

(tomcat folder)/temp

like image 103
Steven Lizarazo Avatar answered Sep 19 '22 20:09

Steven Lizarazo


As you have a problem with possible font file locating and font stream creation,

Try this >> Issue loading custom font AND http://forums.devshed.com/showpost.php?p=2268351&postcount=2

To answer your question "how to make this function easy to use everywhere", do as this:

    public class AddFont extends MainFrame {

    private static Font ttfBase = null;
    private static Font telegraficoFont = null;
    private static InputStream myStream = null;
    private static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";

    public Font createFont() {


            try {
                myStream = new BufferedInputStream(
                        new FileInputStream(FONT_PATH_TELEGRAFICO));
                ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
                telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);               
            } catch (Exception ex) {
                ex.printStackTrace();
                System.err.println("Font not loaded.");
            }
            return telegraficoFont;
    }
}

And then in your calling class:

    public class Test {

    public static Font font = null;

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    if (font == null) {
                        font = AddFont.createFont();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                createGUI();

            } // public void run() Closing
        });
    }
}
like image 32
Ravi Trivedi Avatar answered Sep 19 '22 20:09

Ravi Trivedi


In some cases, maybe the Fontconfig is lack in your running environment. After installing, everything is OK.

For example,

yum install fontconfig
like image 36
liushuaikobe Avatar answered Sep 21 '22 20:09

liushuaikobe