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.
IndexDiscussionExhibition › fontFinder
Page Index Toggle Pages: 1
fontFinder (Read 1084 times)
fontFinder
Sep 1st, 2009, 9:27pm
 
I made this little sketch that reads all fonts in your system and prints them to a simple plain text file for later reference when working with createFont() and such functions...

Code:
/*
* fontFinder by 0p0.
* This small and quick utility app
* will track all the fonts in your
* system and will print them in a
* plain text file so you can use the
* valid names in your apps
*
* It is posible you never see the
* fontFinder window when it executes.
*/

// PrintWriter element
PrintWriter allYourFonts;

// An array of strings to store all the font names
String[] fontList = PFont.list();

// Initializes the destination file.
allYourFonts = createWriter("all_fonts_list.txt");

// Writes the file header
allYourFonts.println("Use this font names as they are printed in this file:");
allYourFonts.println();
allYourFonts.println();

// Writes every font, one by one, into the text file.
for (int i = 0; i < fontList.length; i ++) {
 allYourFonts.println(fontList[i]);
}

// Closes the miniApp window
exit();


The target of this app is to make the complete list available to you when working in text displaying apps on wich is annoying to look for the font in the console, or when you want to reserve the console for more interesting, pertinent stuff, like debugging and not worrying abot a huge list of fonts comin up in the console and getting in the way, but you rather have a handy list in a simple text file, to look at. My experience is working with an app that loads variables from an editable xml config file.

I'll make a better version later.
fontFinder 2 a much better version!
Reply #1 - Sep 1st, 2009, 11:35pm
 
a way much better version Cheesy

This one will print the font name with the TypeFace it references in a PDF file, youll have all font names in one place plus a preview of the TypeFace. Check this out! Cheesy

Quote:
/*
 * fontFinder 2 by 0p0.
 * This small and quick utility sketch
 * will track all the fonts in your 
 * system and will print them in a 
 * plain text file so you can use the 
 * valid names in you own apps and sketches
 *
 * This sketch must be run from Processing
 * environment to have a good console to 
 * see what fonts are problematic.
 */

import processing.pdf.*;

int lineMargin = 35;
int position = 50;
PFont currentFont;
String[] fontList;
PGraphicsPDF pdf;

void setup() {
  size(600,800); // vertical pages
  pdf = (PGraphicsPDF)beginRecord(PDF, "allYourFonts.pdf");
  // An array of strings to store all the font names
  fontList = PFont.list();
  beginRecord(pdf);
}


void draw() {
  background(255);
  // Writes every font, one by one, into the PDF file
  // The dummy line prints the name between quotes
  // with the TypeFace applied to it Cheesy
  for (int i = 0; i < fontList.length; i ++) {
    
    // this section is problematic
    // it freezes the sketch if Processing finds
    // that the font has copyright retrictions. (how cool is that? Cheesy)
    
    // I just don't know how to catch
    // a createFont exception, If you solve the riddle
    // please post the solution Smiley
    currentFont = createFont(fontList[i],20,true);
    
    // uses the current-loop-cycle font in "currentFont" for the current line
    textFont(currentFont,20);
    fill(0); // black text
    // Prints the font name with some dummy text.
    text("\"" + fontList[i] + "\"" + ": 123, test print!",20,position);
    // shifts to the next line position
    position += lineMargin;
    // if the line position is too close to the window bottom...
    if (position > height-50) {
      // reset the line position to the top of the page...
      position = 50;
      // create a new page in the document to write to...
      pdf.nextPage();
      // and paint a white background to scrible on
      fill(255);
      rect(0,0,width,height);
    }
  }
  // When all fonts have been printed
  endRecord(); // close the PDF file...
  exit(); // and exit the program.
}



I got to tell ya, the result is just beautiful!

It turned out that Processing detects if a font has copyright restrictions, which is just great to avoid copiright infringements... BUT! There is, however, a little problem in the createFont command copyright detection. It throws an exception I don't know how to catch.

When the exception is thrown the sketch freezes... :S

If you know the answer to the riddle, please post it Smiley

enjoy!
Re: fontFinder
Reply #2 - Sep 1st, 2009, 11:50pm
 
Looks great!  Will try it out.

General method for unknown / sundry exceptions:

PFont currentfont; // just to emphasize that you can't actually create a variable inside a try{} to be used beyond that local scope
boolean fontCopyrighted;

fontCopyrighted=false;
try{
   currentFont = createFont(fontList[i],20,true);
}
catch(Exception e){
 e.printStackTrace();  // or do nothing
 exit(); // if you want to exit the program at that point
 // actually I'd do:
 fontCopyrighted = true; // (this was reset to false at the start of the loop)
}
if (!fontCopyrighted){
 // do all the rest of the stuff in your loop
}

edit: oh, this may not work.  I'm trying it now and it seems that it's not really a createFont exception at all, but a pdf embedding exception.  The exception actually belongs to Adobe, in a way.  Must be why even catching and printing it hangs the program.  I don't know if you can set p5 to use local fonts rather than embed them in your pdf, which would make it non-transferable to other computers, but fine for personal use.  I'm just making it into a bunch of saveFrame()'s, for my part.  Looks great as a .png and includes the copyrighted fonts.

-- Ben
Re: fontFinder
Reply #3 - Sep 2nd, 2009, 12:41am
 
Here's that "Bunch of .pngs" rewrite, condensed a bit:

Quote:
void setup() {
  size(800,750);
  background(255);
 fill(0);
  String[] fontList = PFont.list();
  PFont currentFont;
  int lineMargin = 35, position = 50, pageNumber=1;
  for (int i = 0; i < fontList.length; i ++) {
    println ("Reading font: " + i + " of " + fontList.length);
    currentFont = createFont(fontList[i],20,true);
    if (currentFont != null){
      textFont(currentFont,20);
      text(i + ". " + fontList[i]  + ":  \"Hello World!\"  (1234567890)", 20, position);
      position += lineMargin;
      if (position > height-50) {
        position = 50;
        println("Printing page " + pageNumber);
        saveFrame("AllYourFonts-" + pageNumber + ".png");
        background(255);
        pageNumber++;
      }
    }
  }
  println ("Finished!");
}
Re: fontFinder
Reply #4 - Sep 2nd, 2009, 1:37am
 
Awesome!

I just came around and saw your edit of the first reply, hehe, and yes, it didn't work quite Tongue but Thanks anyway Smiley

In the end, I'm not havin' any more copyrighted fonts in my system, I smashed them off! lol.

Now the sketch runs from top to bootom and doesn't freeze at all Smiley
Page Index Toggle Pages: 1