void QFontDatabase::load(const QFontPrivate *d, int script) { // sanity checks if (!qApp) qWarning("QFontDatabase::load: Must construct QApplication first"); Q_ASSERT(script >= 0 && script < QUnicodeTables::ScriptCount); // normalize the request to get better caching QFontDef req = d->request; if (req.pixelSize <= 0) req.pixelSize = floor((100.0 * req.pointSize * d->dpi) / 72. + 0.5) / 100; if (req.pixelSize < 1) req.pixelSize = 1; if (req.weight == 0) req.weight = QFont::Normal; if (req.stretch == 0) req.stretch = 100; QFontCache::Key key(req, d->rawMode ? QUnicodeTables::Common : script, d->screen); if (!d->engineData) getEngineData(d, key); // the cached engineData could have already loaded the engine we want if (d->engineData->engines[script]) return; QFontEngine *fe = QFontCache::instance()->findEngine(key); // set it to the actual pointsize, so QFontInfo will do the right thing if (req.pointSize < 0) req.pointSize = req.pixelSize*72./d->dpi; if (!fe) { if (qt_enable_test_font && req.family == QLatin1String("__Qt__Box__Engine__")) { fe = new QTestFontEngine(req.pixelSize); fe->fontDef = req; } else { QMutexLocker locker(fontDatabaseMutex()); if (!privateDb()->count) initializeDb(); fe = loadWin(d, script, req); } if (!fe) { fe = new QFontEngineBox(req.pixelSize); fe->fontDef = QFontDef(); } } d->engineData->engines[script] = fe; fe->ref.ref(); QFontCache::instance()->insertEngine(key, fe); }
void tst_QFontCache::clear() { #ifdef QT_BUILD_INTERNAL QFontEngine_startCollectingEngines(); #else // must not crash, at very least ;) #endif QFontEngine *fontEngine = 0; { // we're never caching the box (and the "test") font engines // let's ensure we're not leaking them as well as the cached ones qt_setQtEnableTestFont(true); QFont f; f.setFamily("__Qt__Box__Engine__"); f.exactMatch(); // loads engine } { QFontDatabase db; QFont f; f.setStyleHint(QFont::Serif); const QString familyForHint(f.defaultFamily()); // it should at least return a family that is available QVERIFY(db.hasFamily(familyForHint)); f.exactMatch(); // loads engine fontEngine = QFontPrivate::get(f)->engineForScript(QChar::Script_Common); QVERIFY(fontEngine); QVERIFY(QFontCache::instance()->engineCacheCount.value(fontEngine) > 0); // ensure it is cached // acquire the engine to use it somewhere else: // (e.g. like the we do in QFontSubset() or like QRawFont does in fromFont()) fontEngine->ref.ref(); // cache the engine once again; there is a special case when the engine is cached more than once QFontCache::instance()->insertEngine(QFontCache::Key(QFontDef(), 0, 0), fontEngine); } // use it: // e.g. fontEngine->stringToCMap(..); // and whilst it is alive, don't hesitate to add/remove the app-local fonts: // (QFontDatabase::{add,remove}ApplicationFont() clears the cache) QFontCache::instance()->clear(); // release the acquired engine: if (fontEngine) { if (!fontEngine->ref.deref()) delete fontEngine; fontEngine = 0; } // we may even exit the application now: QFontCache::instance()->cleanup(); #ifdef QT_BUILD_INTERNAL QList<QFontEngine *> leakedEngines = QFontEngine_stopCollectingEngines(); for (int i = 0; i < leakedEngines.size(); ++i) qWarning() << i << leakedEngines.at(i) << leakedEngines.at(i)->ref.load(); // and we are not leaking! QCOMPARE(leakedEngines.size(), 0); #endif }