We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › loadFont problems in setup try/catch
Page Index Toggle Pages: 1
loadFont problems in setup try/catch (Read 1515 times)
loadFont problems in setup try/catch
Jun 30th, 2009, 9:22pm
 
I'm having trouble with font loading.  Fonts are specified in a separate file
(I simplified the program since getting the list of fonts works fine, please
excuse the poor string manipulation especially) and I'm attempting to load
them as follows:

1. Load from the data directory
2. Load from the system fonts
3. Use the first system fonts

But java seems to freeze at the end of setup when it gets the
NullPointerException since "Futura Book" is not in the data directory, however
that should have been caught by the try/catch block.

I'm using Processing 1.0.1 on a Windows XP machine.

Quote:
************************* Console output ***************************************
# of sysFonts 445
Attempting to load 'Futura Book' from data directory
Font 'Futura Book' not loaded from data directory
java.lang.NullPointerException
     at java.io.DataInputStream.readInt(DataInputStream.java:370)
     at processing.core.PFont.<init>(PFont.java:128)
     at processing.core.PApplet.loadFont(PApplet.java:3500)
     at net.jpcutler.markertags.FontFailureProblem.setup(FontFailureProblem.java:30)
     at processing.core.PApplet.handleDraw(PApplet.java:1383)
     at processing.core.PApplet.run(PApplet.java:1311)
     at java.lang.Thread.run(Thread.java:619)
Attempting to load 'LucidaSans-16.vlw' from data directory

Code:
import java.util.HashMap;

import processing.core.PApplet;
import processing.core.PFont;

public class FontFailureProblem extends PApplet{

HashMap pfonts;
String[] fontNames = {"Futura Book","LucidaSans-16.vlw"};
String[] sysFonts;
int j = 0;

public void setup() {
size(100,100);
strokeWeight(2);

sysFonts = PFont.list();
pfonts = new HashMap();
println("# of sysFonts " + sysFonts.length);
// load the required fonts.
for (int i=0; i<fontNames.length; i++) {
PFont tmpPFont = null;
boolean fontLoaded = false;
// Also load the required pfonts
try {
println("Attempting to load '" + fontNames[i] + "' from data directory");
// Try to get the font from the data directory
tmpPFont = loadFont(fontNames[i]);
fontLoaded = true;
} catch (Exception e) {
println("Font '" + fontNames[i] + "' not loaded from data directory");
}
if (!fontLoaded) {
// Try to get the font from the system fonts
try {
tmpPFont = createFont(fontNames[i],12);
fontLoaded = true;
} catch (Exception e) {
println("Font not found in system fonts");
}
}
if (!fontLoaded) {
println("Unable to load font '"+fontNames[i]+"' from the system or the bin/data directory using '"+sysFonts[0]+"'");
tmpPFont = createFont(sysFonts[1],12);
}
pfonts.put(fontNames[i], tmpPFont);
}
println("end of setup");

}

public void draw() {
println("begin draw");
background(255);
stroke(0);
line(0,j,this.width,j);
if (j > this.height) { j=0; } else {j++;}
}
}

Re: loadFont problems in setup try/catch
Reply #1 - Jul 1st, 2009, 3:28am
 
hmmm difficult question. But as far as I can see, your code is correct. Looking at the source of PApplet, I think loadFont will print the exception's stacktrace, and try to kill the applet if it can't find the fonts. I'm afraid this is not going to work, but you better ask fry.
Re: loadFont problems in setup try/catch
Reply #2 - Jul 1st, 2009, 6:59am
 
I created a new function to use a java.io.File to make sure the font file exists before trying to load it which resolves the issue.

Code:
	public boolean fileExists(String fileName) {
File file=new File(fileName);
   boolean exists = file.exists();
   if (!exists) {
    return false;
   }else{
    return true;
   }
}


Of course, it would be nice if processing handled this gracefully since try/catch blocks should work, but this is a decent work around and probably a better way to do it.

Thanks for the quick response!
Page Index Toggle Pages: 1