1

This is wrt iText 2.1.6.

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. Correct? How do I do this?

1 Answer 1

3

iText 2.1.6 is obsolete. Please stop using it: http://itextpdf.com/salesfaq

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.

6
  • Thanks very much, Bruno. A different way to phrase my question would be: Is there a FontSelector-like class that tries to find a minimal set of fonts? I'm actually aware of the FontSelector behavior. Feb 25, 2014 at 10:50
  • Define 'minimal set of fonts' ;-) The FontFactory will spider possible font directories (the usual suspects) if you use registerDirectories(). However: software can't decide in your place which font you prefer. For instance: how would a font know whether you have a preference for Arial instead of Times Roman (or vice versa)? Feb 25, 2014 at 12:08
  • By minimal set of fonts, I mean, "If one font is enough to render a given string, don't use two or more. If the input string uses English and Bengali, and we have a font in the registered directory that has glyphs for these characters, use that font, rather than using one font for English and one for Bengali" It's about greedy vs optimal. Feb 26, 2014 at 6:00
  • As for what font I prefer, I prefer using the fewest number of fonts to render a given string. For consistency, and to reduce the PDF file size due to font embedding. BTW, thanks for pointing me to your licensing page. I am aware of that and working on it. Feb 26, 2014 at 6:01
  • The fewest number of fonts can currently only be achieved by choosing the order of the fonts added to the FontSelector wisely. Choosing the "fewest" number of fonts would require a different algorithm: some kind of SmartFontSelector that tries out all combinations. The CPU-time required to achieve this would increase exponentially the more possible fonts you add. Feb 26, 2014 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.