Can't normalize fonts according to x-height

Hi,

I’m trying to create a normalized version of the same letter in multiple fonts.
It doesn’t work as you can see in the image, the height is different for each font (same letter)
image

I’m looping through all the fonts and creating this:

myFont = createFont(filenames[i], (getX(font)/getX(myFont))+400);
textFont(myFont);
text(printString, width/2, height/2+40/2);

This is the code for the getX function:

int getX(PFont font_for_X) {
  textFont(font_for_X);
  for (char c : s.toCharArray())
  {
    PFont.Glyph g = font_for_X.getGlyph(c);   //the magic
    if (g!=null) {
      //  println(g.height);
      xHeight = g.height;
    } else
    {
      xHeight = 0;
    }
  }
  return xHeight;
}

Also every few times I run it processing gets stuck with a huge java error, could be due to how it iterates through the files.

I’m adding the entire code here just in case anything here may be useful:
(and thanks in advance for any insight or help)

int xHeight;
int baseline =600;
String s = "ם";                   // The letter mem-sofit (same as x-height)
PFont font;
int fontSizeIs = 100;
String printString = "ם";         // The printed string
String[] filenames;
String path;
String[] fontList;
int i;
PFont myFont;
char ch = '\u05DD';

import java.util.Date;

void setup() {
  i=0;
  size(1900, 1128);
  font = createFont("Arial", fontSizeIs);

  path = sketchPath()+"/data";
  println("Listing all filenames in a directory: ");
  filenames = listFileNames(path);

  printArray(filenames);

}

void draw() {
  println(i);
  if (i==0) { 
    background(255);
  }
  if (i==filenames.length-2) {
    stop();
  }
  fill(255, 0, 0);
  textAlign(CENTER, BASELINE);
  rectMode(CENTER);
  textSize(fontSizeIs);
  println(textWidth(s), "is the text width");
  fill(0, 2);

  myFont = createFont(filenames[i], getX(font));
  if (myFont.getGlyph(ch)!=null && font.getGlyph(ch)!=null && getX(myFont)!=0)
  {

    println("drawing " + myFont);
    myFont = createFont(filenames[i], (getX(font)/getX(myFont))+400);

    println("drawing "+myFont);
    textFont(myFont);
    text(printString, width/2, height/2+40/2);

  }
  i++;

  stroke(0, 0, 255);

}

int getX(PFont font_for_X) {


  textFont(font_for_X);
  for (char c : s.toCharArray())
  {
    PFont.Glyph g = font_for_X.getGlyph(c);   //the magic
    if (g!=null) {
      //  println(g.height);
      xHeight = g.height;
    } else
    {
      xHeight = 0;
    }
  }
  return xHeight;
}

/////////////////////////    file directory stuff    ////////////////////////////


// 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;
  }
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    File[] files = file.listFiles();
    return files;
  } else {
    // If it's not a directory
    return null;
  }
}

// Function to get a list of all files in a directory and all subdirectories
ArrayList<File> listFilesRecursive(String dir) {
  ArrayList<File> fileList = new ArrayList<File>(); 
  recurseDir(fileList, dir);
  return fileList;
}

// Recursive function to traverse subdirectories
void recurseDir(ArrayList<File> a, String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    // If you want to include directories in the list
    a.add(file);  
    File[] subfiles = file.listFiles();
    for (int i = 0; i < subfiles.length; i++) {
      // Call this function on all files in this directory
      recurseDir(a, subfiles[i].getAbsolutePath());
    }
  } else {
    a.add(file);
  }
}
1 Like

Hi @dehyde, I don’t know enough about fonts to answer you, but I would like to try your example. As it is, it exceptions straight away. Can you tell me what it needs? Probably 'copy all the fonts from somewhere to ’ ? Use the font tool to create …?

1 Like

Hi

Yes I’ve moved all fonts to “data” folder

Basically in typography “x-height” is some sort of indicator for preceived font size. Unfortunately I think it does not exist neither in the font’s data or in processing, so I’ve tried to create a way to to figure it out using getX.

In hebrew the closes equivilent to x height is the letter “ם”, which I’ve tried using here.