'Qt: Font agnostic text bounding rect

I'm drawing messages in a message feed of a messenger.

By design first I need to understand how much space each message is going to take, and only then, may be only those that are visible are actually going to be displayed.

I use QFontMetrics::boundingRect method to get text size now.

Everything works perfectly while the text contains symbols that present in current font. As soon as someone introduces some missing in the current font glyphs, lets say from Chinese or Japanese char sets the calculations become wrong and I can understand why - my fontMetrics basically has no idea of those glyphs and probably measure them as some placeholder or that quare sign.

My question: what is the correct way to measure the text in font agnostic way? Meaning that the calculations of the text would take to account all of the algorithms the system is going to apply rendering the same text?

This is a performance critical piece, I would like to avoid solutions like creating a QLabel and requesting its size.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source