Unable to load fonts using createFont()

I tried to load a few different fonts I have inside my data folder but to no success. This is my code:

PGraphics pg;

void setup() {
  PFont font = createFont("ZapfDingbats.ttf", 100);
  size(800,800, P2D);
  pg = createGraphics(1080,1080, P2D);
}

I’ve used the following snippet to access all available fonts:

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

The things I tried so far:

  • Write PFont font; outside setup() and only then call font = createFont(); inside setup();
  • Using .ttc, .otf and .ttf font formats;
  • Calling PFont font = createFont("ZapfDingbats.ttf", 600); outside setup() - but the code won’t compile;
  • Tried only using fonts from the available list;
  • Tried both with and without the extension: createFont("ZapfDingbats", 100); and createFont("ZapfDingbats.ttf");
  • Other fonts like font = createFont("Georgia", 600);

Any ideas of what could be happening here?

1 Like

Hi @Lucianoinfanti

I don’t know where you are accessing the font you are using, but try this code. It works for me.
Also, make sure that the name of the font is the exact same. It is case sensitive.

PGraphics pg;
PFont font;

void setup() {
 font = createFont("ZapfDingbats.ttf", 100);
 size(800,800, P2D);
 pg = createGraphics(1080,1080, P2D);
}

void draw(){
background(0);
textFont(font);
textAlign(CENTER,CENTER);
text("Here it is",width/2,height/2); 
}

Result:

1 Like

Hey @MiguelSanches, thanks for replying!

Unfortunately whenever I try this code I get this error message:

A null PFont was passed to textFont().

But I did manage to make it work. I guess the problem was related to the variable’s scope. Here is the final code:


PFont font; 
PGraphics pg;

void setup() {
  font = createFont("Soulcraft.ttf", 1000);
  size(800,800, P2D);
  pg = createGraphics(1080,1080, P2D);
  
  // Printing the Font List to understand what fonts ara avaliable for use
  //String[] fontList = PFont.list();
  // printArray(fontList);
}

void draw (){
  background(0);
  
  pg.beginDraw();
  pg.background(0);
  pg.textFont(font);
  pg.fill(250,250,250, 1000);
  pg.textSize(400);
  pg.pushMatrix();
  pg.translate(width/2, height/2-90);
  pg.textAlign(CENTER,CENTER);
  pg.text("01", 0,0);
  pg.popMatrix();
  pg.endDraw();

  image(pg,0,0);
  
  int tilesX = 8;
  int tilesY = 16;

  int tileW = int(width/tilesX);
  int tileH = int(height/tilesY);
  
  for (int y = 0; y < tilesY; y++){
    for (int x = 0; x < tilesX; x++){
      
      // WARP
      int movex = int(sin(frameCount * 0.08 + (x * y) * 0.04) * 100);
      int movey = int(cos(frameCount * 0.05 + (x * y) * 0.08) * 100);
      
      //SOURCE
      int sx = x*tileW + movex;
      int sy = y*tileH + movey;
      int sw = tileW + (movey / 2);
      int sh = tileH ;
      
      //DESTINATION
      int dx = x * tileW;
      int dy = y * tileH;
      int dw = tileW;
      int dh = tileH ;
      
      copy (pg, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

Thanks for your help!

1 Like

Great @Lucianoinfanti

I believe that the null PFont error either happens because the name that you are passing when creating a font does not match the exact same that you have in the folder, including the type .ttf or . TTF or because you didn’t the variable scoped as global, which was your case I believe.

2 Likes