"Create font" not find new font

Downloaded new and install new ttf font, but processing dont see he!
Haw I do using .ttf font in processing?
and:
image|690x475
and:

1 Like

Hello,

Welcome to the Processing community.

I tried adding a TTF font (to W10 and data folder) also and came up with the same message.

A bit of exploration prompted me to write this:

// Show Font Avaiability
// v1.0.0
// GLV 2020-02-22

PFont font;
String[] fontList;

void setup() 
	{
  size(200, 200);
  fontList = PFont.list();
  //printArray(fontList);
  
  for (int i= 0; i< fontList.length; i++)
    {
    font = createFont(fontList[i], 16);
    println(i, fontList[i]);
    }
  }

void draw() 
	{
  background(0);
	}

It seems that some of the installed fonts are “not available, so another font will be used.”

References:
https://processing.org/tutorials/typography/
https://processing.org/reference/createFont_.html

:)

1 Like

I think all fonts must end in .ttf (true type font). So for example: font.ttf. This is similar to how images must end in .img.

Try:

PFont font = createFont("fontName.ttf", 14);

And if that doesn’t work, then but that line outside of the setup() function.

Do I understand correctly that there are currently no available means of using third-party fonts? It cannot be that I alone come across this problem. It’s generally strange that developers use font conversion tools to convert to .vlw format and not all, but only specific system fonts.

1 Like

If you want to share sketches with other users the YourFont.ttf needs to be in the data folder, and called:-

createFont("YourFont.ttf", 24);

If you want to use some funky font installed on your system (I know this for linux but expect it’s true elsewhere) then it needs to be called by either the system font name, or full path to ttf or otf file eg:-

createFont("/usr/share/fonts/adobe-source-code-pro/SourceCodePro-Medium.otf", 24);
4 Likes

Hi.
Today I hit the same obstacle.
I was planning to make bit jpgs out of an installed handwritten font to make a handwriting practice game.
After trying to use an installed font (“Mestra3(pauta doble)”) using its font name, I ended up writing a routine to choose fonts with similar name:

PFont myFont;
PImage myImage;
int letraIndex=0;
String letras="abcdefghijklmnopqrstuvwxyzABCDEFEGHIJKLMNOPQRSTUVWXYZáéíóúüÁÉÍÓÚñÑ";

void setup()
{
  size(800,800);
  String[] fontArray={"Mestra","Masallera","Memima"};
  chooseFont(myFont,100,  fontArray);
}

/* 
 * chooseFont 
 * Tries to choose a font with similar name to a list
 * mFont: PFont input/output objetct 
 * Size: font Size
 * fontArray: Font names to choose from 
*/  
void chooseFont(PFont theFont, int Size, String[] fontArray)
{
  int i,j;
  int fontchosen, fontNameLength;
  String[] lista=theFont.list();
 printArray(fontArray);
 printArray(lista);
  
/* fontChosen:                                                     *
 * A big value, bigger than fontArray, so we can use it *
 * to make a hierarchy of fonts.                        *
 */
  int fontChosen=fontArray.length+999;
/* fontChosenSystemName= stores the system font in fontArray for the chosen font*/
  int fontChosenSystemName=0; 

  for (i=0;i<lista.length;i++){
    for (j=0;j<fontArray.length;j++)
   {
      /* Manage precedence of font names */
      if (j>fontChosen) {
       // println("Test font "+str(j)+"has lower priority than "+str(fontChosen));
        break;
      }
     
      fontNameLength=fontArray[j].length();
      if (fontNameLength<=lista[i].length()){
        if (fontArray[j].equals(lista[i].substring(0,fontNameLength))){
          println("chosen: >"+fontArray[j]+"<");
          fontChosen=j;
          fontChosenSystemName=i;
        } else {
          //println("Test font name >"+lista[i].substring(0,fontNameLength)+"< is not >"+fontArray[j]+"<");
        }
    } else {
        println("Test font >"+lista[i]+"< is shorter than >"+fontArray[j]+"<");
      }
    }  
  }
     theFont=createFont(lista[fontChosenSystemName],Size);
     textSize(200);
    textAlign(CENTER);

 }


void draw(){
  text(letras.substring(letraIndex,1), height/2,width/2);
}

The output of the program was:


[347] "Mestra3(PautaDoble)"

Test font >AR CENA< is shorter than >Masallera<

Test font >Marlett< is shorter than >Masallera<
chosen: >Mestra<
Test font >Onyx< is shorter than >Mestra<

"Mestra3(PautaDoble)" is not available, so another font will be used. Use PFont.list() to show available fonts.

Yours,

José

1 Like

Fun! Thank you for sharing this.

You might also use the Levenshtein distance – or the distance on each token and weighing them – first token is heavy, 3rd or 4th token is light.

Times
Times New Roman
RomanD

1 Like