How to identify a single font that can print all the characters in a string?

Category: 
Tags: FontSelectorfontslanguagesiText 5

I have a string containing characters from different languages, for which I'd like to pick a single font (among the registered fonts) that has glyphs for all these characters. I would like to avoid a situation where different substrings in the string are printed using different fonts, if I already have one font that can display all these glyphs.

If there's no such single font, I would still like to pick a minimal set of fonts that covers the characters in my string. I'm aware of FontSelector, but it doesn't seem to try to find a minimal set of fonts for the given text.

Posted on StackOverflow on Feb 25, 2014 by Kartick Vaddadi

I see two questions in one:

Is there a font that contains all characters for all languages?

Allow me to explain why this is impossible:

  • There are 1,114,112 code points in Unicode. Not all of these code points are used, but the possible number of different glyphs is huge.
  • A simple font only contains 256 characters (1 byte per font), a composite font uses CIDs from 0 to 65,535.

65,535 is much smaller that 1,114,112, which means that it is technically impossible to have a single font that contains all possible glyphs.

FontSelector doesn't find a minimal set of fonts!

FontSelector doesn't look for a minimal set of fonts. You have to tell FontSelector which fonts you want to use and in which order! Suppose that you have this code:

FontSelector selector = new FontSelector();
selector.addFont(font1);
selector.addFont(font2);
selector.addFont(font3);

In this case, FontSelector will first look at font1 for each specific glyph. If it's not there, it will look at font2, etc... Obviously font1, font2 and font3 will have different glyphs for the same character in common. For instance: a, a and a. Which glyph will be used depends on the order in which you added the font.

Bottom line:

Select a wide range of fonts that cover all the glyphs you need and add them to a FontSelector instance. Don't expect to find one single font that contains all the glyphs you need.