Trying to check if a font can display certain glyphs

Regarding the code below, I’m having difficulties using font.canDisplayUpTo() – I think it’s an issue with the URL being passed to Font font =… ? The fonts are all fine, so it’s something to do with how Font is being fed the file locations?

Thanks in advance if you have any ideas,

Dan

int sizeWidth = 800;
int sizeHeight = 800;
int frameRate = 22;

File fontDir;
File [] fontFiles;
String fontFolder = "/Volumes/macOS/Users/****USERNAME****/Desktop/Fonts/";
String theWord = "fonte";
String [] fontArray;
PFont pfont;
Font font;
ArrayList<File> fontName = new ArrayList<File>();

import java.awt.Font;

void settings() {
    size(sizeWidth, sizeHeight);
}

void setup() {
    frameRate(frameRate);

    char [] arr = theWord.toCharArray();
    println(arr);

    fontDir = new File(fontFolder);
    fontFiles = fontDir.listFiles();
    for (int i = 0; i < fontFiles.length; i++) {
        if (fontFiles[i].getName().startsWith("._")) {
            fontFiles[i].delete();
        }
        if (fontFiles[i].toString().matches(".*(ttf|otf)$")) {
            // ERROR: Unhandled exception type FontFormatException... 
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontFiles[i]);
            if (font.canDisplayUpTo(arr, 0, arr.length) == -1) {
                fontName.add(fontFiles[i]);
            }
        }
    }    
}

Check the reference on this please

EDIT: I was barking up the wrong tree.

Never managed to get Java’s Font createFont() & the InputStream rigmarole to work as I needed … so I resolved my issue by ignoring the Java approach and instead used PFont getGlyph() to check if a font was able to render a specific character.

The example shown at this URL (How to detect no glyph-char - Processing 2.x and 3.x Forum) should help any future readers:

PFont font = createFont("DejaVu Sans", 32, true);
char ch = '\u171A';

if (font.getGlyph(ch) == null)  println("Char ", ch, "is unavailable!");
else                            println("Char ", ch, "is displayable!");

exit();

Have a great day,

Dan

1 Like

Glad to hear that!

see Reference / Processing.org

1 Like