How can i set the font to random(), by using the PFont mode

edited May 2014 in How To...

Hello! At the moment i am loading fonts into my work by using the PFont mode. This is the code i am using:

    PFont font;
        PFont.list();
        String[] fontList = PFont.list();
        println(fontList);
        font = createFont("Akkurat-Mono.ttf", 45);
        textFont(font);
        fill(0);

I was curious if i could load more than one font, for example five. And than display a text which uses this randomfont function, to display the text in a variety of fonts. I don't know exactly how to do this, has anyone an idea?

Merci beaucoup!

Sander

Tagged:

Answers

  • here

    PFont font;
    PFont.list();
    size(700, 300);
    String[] fontList = PFont.list();
    println(fontList);
    int randomIndex = int(random(fontList.length));
    println ();
    println (fontList[randomIndex]);
    font = createFont(fontList[randomIndex], 45);
    textFont(font);
    fill(0);
    text ("lksdjfslkdgjsdfgh", 120, 120) ;
    
  • edited May 2014

    This loads a random font on each frame and uses it for display. You could do the same thing and load a PFont array in setup()...

    String[] fontList;
    
    void setup(){
      size(500, 500);
      fontList = PFont.list();
    }
    
    void draw(){
      background(255);
      fill(0);
      int choice= (int)random(0, fontList.length);
      PFont f= createFont(fontList[choice], 50, true);
      textFont(f);
      text("Hello!", 200, 225);
    }
    

    Hope that helps.

    edit: too slow :(

  • I am sorry ;-)

  • The random font seems to work! So i'm one step closer, thanks :) However, it still displays the entire text in the same font. I tried something with the charAt() function, but then i still got the same. And i thought, in order to use just 5 fonts instead of my entire collection, could i loop trought the fonts i have in the data folder instead of my whole computer? Thanks for your helpful feedback, it really means a lot to me!

  • Answer ✓

    well... this is totally different from what you wanted first. Why did you ask the wrong thing first?

    here is a version that changes the font for each letter

    PFont font;
    PFont.list();
    size(700, 300);
    String[] fontList = PFont.list();
    println(fontList);
    int randomIndex = int(random(fontList.length));
    println ();
    println (fontList[randomIndex]);
    font = createFont(fontList[randomIndex], 45);
    textFont(font);
    fill(0);
    String textMy="LKHJDFLKHFDEU";
    for (int i = 0; i < textMy.length(); i++) {
      randomIndex = int(random(fontList.length));
      font = createFont(fontList[randomIndex], 45);
      textFont(font);
      text (textMy.charAt(i), 120+i*32, 120) ;
    }
    

    the other things should be easy too but I don't know it (or have time)

  • Im sorry i didn't formulated it correct, thanks!

  • Answer ✓

    it just occurred to me...

    String textMy="LKHJDFLKHFDEU";
    PFont[] fonts; 
    PFont font; 
    int randomIndex; 
    
    // ---------------------------------------
    
    void setup() {
    
      size (900, 400);
    
      String myDataPath1;
    
      myDataPath1 = dataPath("");
      println (myDataPath1);
      File folderDataPath1 = new File (myDataPath1);
      println (folderDataPath1);
    
      String[] listFiles = folderDataPath1.list();
      StringList inventory;
      inventory = new StringList();
    
      for (int i = 0; i < listFiles.length; i++ ) {
        println (listFiles[i]);
        if (listFiles[i].indexOf(".TTF")>0) 
        {
          inventory.append(listFiles[i]);
        }
      } // for 
    
      println(inventory);
    
      fonts = new PFont [inventory.size()];
    
      for (int i = 0; i < inventory.size(); i++ ) {
    
        String item = inventory.get(i);
        fonts [i] = createFont(item, 22);
      }
      background (0);
      noLoop();
    } // func ---------------------------------
    
    void draw () {
      // background (0);
      for (int i = 0; i < textMy.length(); i++) {
        randomIndex = int(random(fonts.length));
        font = fonts[randomIndex];
        textFont(font);
        text (textMy.charAt(i), 120+i*32, 120) ;
      } // for
    } // func 
    //
    
  • Hi, I'm really interested to get this to work but I just started learning Processing/Java a week ago. The examples with PFont.list works just fine but I can't seem to get the other to function. I receive a "ArrayIndexOutOfBoundsException: 0" on the line "font = fonts[randomIndex];". I have placed some .ttf fonts in my /data folder and I can see that thous are printed in the console.

    Would really appreciate some help! Torbjörn

  • you are referring to that?

    PFont font;
    PFont.list();
    size(700, 300);
    String[] fontList = PFont.list();
    println(fontList);
    int randomIndex = int(random(fontList.length));
    println ();
    println (fontList[randomIndex]);
    font = createFont(fontList[randomIndex], 45);
    textFont(font);
    fill(0);
    text ("lksdjfslkdgjsdfgh", 120, 120) ;
    
  • pls show your entire code

    randomIndex must be defined correctly --- int randomIndex = int(random(fontList.length));

  • String textMy="LKHJDFLKHFDEU";
    PFont[] fonts;
    PFont font;
    int randomIndex;
     
    // ---------------------------------------
     
    void setup() {
     
      size (900, 400);
     
      String myDataPath1;
     
      myDataPath1 = dataPath("");
      println (myDataPath1);
      File folderDataPath1 = new File (myDataPath1);
      println (folderDataPath1);
     
      String[] listFiles = folderDataPath1.list();
      StringList inventory;
      inventory = new StringList();
     
      for (int i = 0; i < listFiles.length; i++ ) {
        println (listFiles[i]);
        if (listFiles[i].indexOf(".TTF")>0)
        {
          inventory.append(listFiles[i]);
        }
      } // for
     
      println(inventory);
     
      fonts = new PFont [inventory.size()];
     
      for (int i = 0; i < inventory.size(); i++ ) {
     
        String item = inventory.get(i);
        fonts [i] = createFont(item, 22);
      }
      background (0);
      noLoop();
    } // func ---------------------------------
     
    void draw () {
      // background (0);
      for (int i = 0; i < textMy.length(); i++) {
        randomIndex = int(random(fonts.length));
        font = fonts[randomIndex];
        textFont(font);
        text (textMy.charAt(i), 120+i*32, 120) ;
      } // for
    } // func
    //
    

    This was the code I was referring to. I get an error saying StringList size=0 [ ] and the println(inventory); doesn't print to the console.

    Also. Can't seem to figure out how to formate the code-block correctly with every line starting with a number?

  • Weird... now the code looks right!

  • edited March 2015

    several issues here

    1.

    the sketch checks only the fonts in the data directory of the sketch

    you need to have fonts therein

    use in the IDE menu tools|create font

    2.

    the created fonts are vlw but the sketch accepted only ttf - I changed that now

    ;-)

    String textMy="LKHJDFLKHFDEU";
    PFont[] fonts;
    PFont font;
    int randomIndex;
    
    // ---------------------------------------
    
    void setup() {
    
      size (900, 400);
    
      String myDataPath1;
    
      myDataPath1 = dataPath("");
      println (myDataPath1);
      File folderDataPath1 = new File (myDataPath1);
      println (folderDataPath1);
    
      String[] listFiles = folderDataPath1.list();
      StringList inventory;
      inventory = new StringList();
    
      println ("---------------");
    
      for (int i = 0; i < listFiles.length; i++ ) {
        println (listFiles[i]);
        if (listFiles[i].indexOf(".TTF")>0||listFiles[i].indexOf(".vlw")>0)
        {
          inventory.append(listFiles[i]);
        }
      } // for
    
      println ("---------------");
    
      println(inventory);
    
      fonts = new PFont [inventory.size()];
    
      println ("---------------");
    
      for (int i = 0; i < inventory.size(); i++ ) {
    
        String item = inventory.get(i);
        println (item);
        fonts [i] = createFont(item, 22);
      }
      background (0);
      noLoop();
    } // func ---------------------------------
    
    void draw () {
      // background (0);
      for (int i = 0; i < textMy.length(); i++) {
        randomIndex = int(random(fonts.length));
        font = fonts[randomIndex];
        textFont(font);
        text (textMy.charAt(i), 120+i*32, 120) ;
      } // for
    } // func
    //
    
  • quote

    Weird... now the code looks right!

    probably you need 2 empty lines before and after the code

  • Thank you for the fast reply, really appreciate your help! I have .ttf fonts in my /data folder but those doesn't seem to be loaded into StringList as it returns size = 0. When I added some .vlw font they where added to StringList but they don't display when I run the code...

  • edited March 2015

    yeah...

    it's this:

    listFiles[i].indexOf(".TTF")>0

    maybe your fonts are named ttf instead of TTF

    make it

    if (listFiles[i].indexOf(".TTF")>0||
    listFiles[i].indexOf(".ttf")>0||
    listFiles[i].indexOf(".vlw")>0)
    
  • AH! That was it! Thank you very much :D

  • edited March 2015

    btw.

    just a remark

    quote

    I get an error saying StringList size=0 [ ] and the println(inventory); doesn't print to the console.

    it's not an error though.

    StringList size=0 [ ] is just what println(inventory); gives out since inventory was empty (so println(inventory); did print to the console but not what you've expected)

    ;-)

Sign In or Register to comment.