# Can't normalize fonts according to x-height

**URL:** <https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324>\
**Category:** Coding Questions\
**Created:** [April 30, 2020, 8:18pm UTC](https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324 "2020-04-30T20:18:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dehyde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dehyde/32/5153_2.png) [@dehyde](https://discourse.processing.org/u/dehyde)\
**Post date:** [April 30, 2020, 8:18pm UTC](https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324/1 "2020-04-30T20:18:41Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/419ec1b9c56aad106eccec1b4d403ee1b4c48ae5.png)

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

```auto
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:

```auto
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)

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [April 30, 2020, 10:17pm UTC](https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324/2 "2020-04-30T22:17:34Z")

</div>

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 …?

---

<div class="post-metadata">

**Author:** ![dehyde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dehyde/32/5153_2.png) [@dehyde](https://discourse.processing.org/u/dehyde)\
**Post date:** [May 1, 2020, 6:30am UTC](https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324/3 "2020-05-01T06:30:00Z")

</div>

Hi

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

---

<div class="post-metadata">

**Author:** ![dehyde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dehyde/32/5153_2.png) [@dehyde](https://discourse.processing.org/u/dehyde)\
**Post date:** [May 1, 2020, 6:34am UTC](https://discourse.processing.org/t/cant-normalize-fonts-according-to-x-height/20324/4 "2020-05-01T06:34:23Z")

</div>

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.
