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 › Can I specify a folder path for PFont.list()
Page Index Toggle Pages: 1
Can I specify a folder path for PFont.list()? (Read 1702 times)
Can I specify a folder path for PFont.list()?
Mar 23rd, 2009, 4:42pm
 
Greetings,

I am using PFont.list() to generate an array of fonts:

String[] fontList = PFont.list();
println(fontList);


But this list() function pulls all fonts installed on my system.

Is there a way to specify a folder path so I can control which fonts get pulled into the array? I can use selectFolder() to have the user choose a font folder, but I don't know how to target PFont.list() to that path.

Thanks!

Re: Can I specify a folder path for PFont.list()?
Reply #1 - Mar 23rd, 2009, 5:25pm
 
You can use File.list() (or the version with filter to limit to .ttf extension) to get a list of files.

PFont.list() lists only system installed fonts, as provided by Java.
Re: Can I specify a folder path for PFont.list()?
Reply #2 - Mar 23rd, 2009, 6:04pm
 
Thanks. Perhaps you can point me to an example, or help with a little more detail? I've pulled my code from http://processing.org/learning/topics/directorylist.html. But I'm still not establishing a clean font listing.

Can I limit the return string of filenames to ttf or functioning fonts as Plist does? And how to set up the array of fonts to recursively pass through all sub-folders in the directory?


I've got this in my setup():

 // Open file chooser to select font folder
 String folderPath = selectFolder();  
 if (folderPath == null) {
   // If a folder was not selected
   println("No folder was selected...");
 }
 else {
   // If a folder was selected, print path to folder
   println(folderPath);
 }

 // determine Path
 String path = folderPath;
 println("Listing all filenames in a directory: ");
 String[] filenames = listFileNames(path);
 println(filenames);

 // fontList =  PFont.list();
 fontList =  filenames;    //FONT SELECTION CODE

 println(fontList);



And this function below:

// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
 File file = new File(dir);
 if (file.isDirectory()) {
   String names[] = file.list();
   return names;
 }
 else {
   // If it's not a directory
   return null;
 }
}
Re: Can I specify a folder path for PFont.list()?
Reply #3 - Mar 23rd, 2009, 6:51pm
 
PhiLho  wrote on Mar 23rd, 2009, 5:25pm:
or the version with filter to limit to .ttf extension

That's File.list(FilenameFilter)
If you can't make it work, I can give an example (gotta run just now).
Re: Can I specify a folder path for PFont.list()?
Reply #4 - Mar 24th, 2009, 12:25pm
 
Thanks PhiLho, an example would be great.

I'm trying to:
- select a folder (using selectFolder, which is working)
- filter back a string array from within the selected folder and its subfolders to font files as recognized by PFont.list (MAC OSX fonts including otf, ttf and all font suitcases)

Thanks!
Re: Can I specify a folder path for PFont.list()?
Reply #5 - Mar 24th, 2009, 1:49pm
 
Code:
// This function returns all the font files in a directory as an array of Strings  
String[] listFontFileNames(String dir) {
 File file = new File(dir);
 if (file.isDirectory()) {
   String names[] = file.list(new FilenameFilter() {
     boolean accept(File dir, String name) {
       return name.endsWith(".ttf") || name.endsWith(".odf") ||
name.endsWith(".sit"); // Etc.
     }
   });
   return names;
 } else {
   // If it's not a directory
   return null;
 }
}
Re: Can I specify a folder path for PFont.list()?
Reply #6 - Mar 24th, 2009, 2:03pm
 
Thanks PhiLho! How can that dig recursively into subfolders?

Do you know how PFont.list identifies usable fonts? Because not all font files have extensions (.ttf, .otf etc.), so I'm not sure exactly which parameters need to be set up for the filter.
Re: Can I specify a folder path for PFont.list()?
Reply #7 - Mar 24th, 2009, 2:40pm
 
PFont.list() gives all fonts which have been installed on the system. These could be in many places depending on the operating system in question.

If you have fonts that are in local directories but not "installed" onto the system, then you should be able to loadFont() them but they'll not appear in the PFont.list() ever since it's designed to only tell you the ones "installed".
Re: Can I specify a folder path for PFont.list()?
Reply #8 - Mar 24th, 2009, 3:38pm
 
Thanks JohnG.

I wanted to dynamically createFont from a user-specified folder...

But it seems easier to pre-create chosen vlw fonts into the data folder and then dynamically loadFont from there.
Re: Can I specify a folder path for PFont.list()?
Reply #9 - Mar 24th, 2009, 3:50pm
 
As JohnG wrote, Java doesn't check font files, it justs ask the system for the fonts it knows.
At best you can check the binary signature of the file (read the first bytes, verify it conforms to standard).
It is a bit of overkill, I haven't seen much exotic font extensions...

Walking recursively a dir isn't hard, you have to check if a given file is a dir or not, then list this dir, etc.
But since you seem to have dropped this way (a bit too convoluted, indeed, at worst you just ask the users to have the fonts they want in the same place in a flat way), I won't expand on it.
Page Index Toggle Pages: 1