PDA

View Full Version : Caching and reusing platform-level font objects



pavele
21st May 2019, 10:39
Hi. I have a limited device that only supports limited number of fonts internally. Therefore it is important that Qt does internal optimization for reusing system-level font objects. For example:


QFont f1("Arial", 18);
QFont f2("Arial", 18);


shall result in a same system-level font (handle) internally. Does Qt implement this reliably or I need to roll my own parameter-based font caching?

Before migrating to Qt I had to implemented such caching which was the only way to handle system limitations.

anda_skoa
21st May 2019, 14:12
That's a good question.

I would assume it does do such caching, but you can probably check for yourself.
The code for Qt is easily browsable over at woboq.org https://code.woboq.org/qt5/qtbase/src/gui/text/qfont.h.html

Cheers,
_

pavele
27th May 2019, 11:43
Tanks anda_skoa,
I was hoping to save time and avoid debugging Qt, but...
Here is what I found:
QFont holds an internal shared data object of QFontPrivate following Qt's shared data object scheme (pattern). QFontPrivate has members called:


QFontDef request;
mutable QFontEngineData *engineData;

Which seem to be related to the internal font support involving classes like QFontCache, QFontEngineData, QFontData, QFontDef, etc. Those suppose caching of objects through object maps - which is mentioned in the documentation as well:


Loading fonts can be expensive, especially on X11. QFont contains extensive optimizations to make the copying of QFont objects fast, and to cache the results of the slow window system functions it depends upon.
But the above are all internals.
Therefore the only optimization one can do is saving unnecessary QFontPrivate object creation. This is to save creating two identical instances - one for each line in case of following constructor:


QFont f1("Arial", 18);
QFont f2("Arial", 18);

One shall decide how to proceed and if this kind of light optimization deserves the efforts.