Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Proof of Concept for updating the font registery from fontconfig.
Wolthera van Hövell tot Westerflier8666e4c146e1
diff --git a/libs/flake/CMakeLists.txt b/libs/flake/CMakeLists.txtindex e87ce73921..cb56648899 100644--- a/libs/flake/CMakeLists.txt+++ b/libs/flake/CMakeLists.txt@@ -235,6 +235,7 @@ set(kritaflake_SRCStext/lager/TextTransformModel.cpptext/lager/CssFontStyleModel.cpptext/KoFFWWSConverter.cpp+ text/KoFontChangeTracker.cppresources/KoSvgSymbolCollectionResource.cppresources/KoGamutMask.cppdiff --git a/libs/flake/text/KoFontChangeTracker.cpp b/libs/flake/text/KoFontChangeTracker.cppnew file mode 100644index 0000000000..293ac1e1f8--- /dev/null+++ b/libs/flake/text/KoFontChangeTracker.cpp@@ -0,0 +1,79 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "KoFontChangeTracker.h"++#include <QFileSystemWatcher>+#include <kis_signal_compressor.h>+#include <QDebug>++struct KoFontChangeTracker::Private {++ Private(QStringList paths = QStringList(), int refresh = 500)+ : fileSystemWatcher(paths)+ , configSignalCompressor(refresh, KisSignalCompressor::POSTPONE) {++ }+ QFileSystemWatcher fileSystemWatcher;+ KisSignalCompressor configSignalCompressor;++ bool filesChanged = false;+ bool configStale = false;+};++KoFontChangeTracker::KoFontChangeTracker(QStringList paths, int configRefreshValue, QObject *parent)+ : QObject(parent)+ , d(new Private(paths, configRefreshValue))+{+ d->fileSystemWatcher;++ connect(&d->fileSystemWatcher, SIGNAL(directoryChanged(QString)), SLOT(directoriesChanged(QString)));+ connect(&d->configSignalCompressor, SIGNAL(timeout()), this, SLOT(intervalElapsed()));+}++KoFontChangeTracker::~KoFontChangeTracker()+{+}++void KoFontChangeTracker::resetChangeTracker()+{+ d->configStale = false;+ d->filesChanged = false;+ d->configSignalCompressor.start();+}++void KoFontChangeTracker::connectToRegistery()+{+ connect(this, SIGNAL(sigUpdateConfig()), this, SLOT(updateFontRegistery()), Qt::UniqueConnection);+}++#include <KoFontRegistry.h>+void KoFontChangeTracker::updateFontRegistery()+{+ KoFontRegistry::instance()->updateConfig();+}++void KoFontChangeTracker::intervalElapsed()+{+ if (!d->configStale) {+ d->configStale = true;+ testUpdate();+ }+}++void KoFontChangeTracker::directoriesChanged(QString path)+{+ if (!d->filesChanged) {+ d->filesChanged = true;+ testUpdate();+ }+}++void KoFontChangeTracker::testUpdate()+{+ if (d->filesChanged && d->configStale) {+ emit (sigUpdateConfig());+ }+}diff --git a/libs/flake/text/KoFontChangeTracker.h b/libs/flake/text/KoFontChangeTracker.hnew file mode 100644index 0000000000..5ceff97cdc--- /dev/null+++ b/libs/flake/text/KoFontChangeTracker.h@@ -0,0 +1,42 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KOFONTCHANGETRACKER_H+#define KOFONTCHANGETRACKER_H++#include <QObject>++/**+ * @brief The KoFontChangeTracker class+ * This class keeps track of the FontConfig refresh value,+ * as well as the paths FontConfig is looking at,+ * and resets the+ */+class KoFontChangeTracker : public QObject+{+ Q_OBJECT+public:+ explicit KoFontChangeTracker(QStringList paths, int configRefreshValue = 0, QObject *parent = nullptr);+ ~KoFontChangeTracker();++ /// This should be called after fontregistery initialization is done to start the signal compressor.+ void resetChangeTracker();++ /// This needs to be manually connected.+ void connectToRegistery();+public Q_SLOTS:+ void updateFontRegistery();+Q_SIGNALS:+ void sigUpdateConfig();+private Q_SLOTS:+ void intervalElapsed();+ void directoriesChanged(QString path);+ void testUpdate();+private:+ struct Private;+ QScopedPointer<Private> d;+};++#endif // KOFONTCHANGETRACKER_Hdiff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cppindex 4642802217..c1a3850633 100644--- a/libs/flake/text/KoFontRegistry.cpp+++ b/libs/flake/text/KoFontRegistry.cpp@@ -25,6 +25,8 @@#include "KoFontLibraryResourceUtils.h"#include "KoFFWWSConverter.h"+#include "KoFontChangeTracker.h"+#include <KisResourceLocator.h>static unsigned int firstCharUcs4(const QStringView qsv)@@ -52,6 +54,7 @@ class Q_DECL_HIDDEN KoFontRegistry::Privateprivate:FcConfigSP m_config;QSharedPointer<KoFFWWSConverter> fontFamilyConverter;+ QSharedPointer<KoFontChangeTracker> changeTracker;struct ThreadData {FT_LibrarySP m_library;@@ -109,6 +112,23 @@ public:#endifm_config.reset(config);+ /// Setup the change tracker.+ FcStrList *list = FcConfigGetFontDirs(m_config.data());+ FcStrListFirst(list);+ FcChar8 *dirString = FcStrListNext(list);+ QString path = QString::fromUtf8(reinterpret_cast<char *>(dirString));+ QStringList paths;+ while (!path.isEmpty()) {+ paths.append(path);+ FcStrListNext(list);+ dirString = FcStrListNext(list);+ path = QString::fromUtf8(reinterpret_cast<char *>(dirString));+ }+ FcStrListDone(list);+ int rescanInterval = FcConfigGetRescanInterval(m_config.data());+ changeTracker.reset(new KoFontChangeTracker(paths, rescanInterval * 1000));+ changeTracker->connectToRegistery();+ changeTracker->resetChangeTracker();reloadConverter();fontFamilyConverter->debugInfo();@@ -171,6 +191,18 @@ public:initialize();return m_data.localData()->m_suggestedFiles;}++public Q_SLOTS:+ void updateConfig() {+ if (!FcInitBringUptoDate()) {+ return;+ }+ if (FcConfigBuildFonts(m_config.data())) {+ reloadConverter();+ KisResourceLocator::instance()->updateFontStorage();+ changeTracker->resetChangeTracker();+ }+ }};KoFontRegistry::KoFontRegistry()@@ -602,6 +634,11 @@ KoFontFamilyWWSRepresentation KoFontRegistry::representationByFamilyName(const Qreturn d->converter()->representationByFamilyName(familyName, found);}+void KoFontRegistry::updateConfig()+{+ d->updateConfig();+}+bool KoFontRegistry::addFontFilePathToRegistery(const QString &path){const QByteArray utfData = path.toUtf8();diff --git a/libs/flake/text/KoFontRegistry.h b/libs/flake/text/KoFontRegistry.hindex f3ff1bb257..3a3f347143 100644--- a/libs/flake/text/KoFontRegistry.h+++ b/libs/flake/text/KoFontRegistry.h@@ -74,9 +74,26 @@ public:quint32 yRes,const QMap<QString, qreal> &axisSettings);+ /**+ * @brief collectRepresentations+ * @return a list of Width/Weight/Slant font family representations.+ */QList<KoFontFamilyWWSRepresentation> collectRepresentations() const;++ /**+ * @brief representationByFamilyName+ * This simplifies retrieving the representation for a given font family.+ * @param familyName - the familyName associated with the font.+ * @param found - bool to check for success.+ * @return the font family.+ */KoFontFamilyWWSRepresentation representationByFamilyName(const QString &familyName, bool *found = nullptr) const;+ /**+ * Update the config and reset the FontChangeListener.+ */+ void updateConfig();+private:class Private;diff --git a/libs/resources/KisResourceLocator.cpp b/libs/resources/KisResourceLocator.cppindex fd8377196e..8ad4aa9cc3 100644--- a/libs/resources/KisResourceLocator.cpp+++ b/libs/resources/KisResourceLocator.cpp@@ -973,6 +973,15 @@ QString KisResourceLocator::filePathForResource(KoResourceSP resource)return storage->resourceFilePath(resourceUrl);}+void KisResourceLocator::updateFontStorage()+{+ if (!KisResourceCacheDb::synchronizeStorage(fontStorage())) {+ qWarning() << i18n("Could not synchronize updated font registery with the database");+ } else {+ emit storageUpdated(fontStorage()->location());+ }+}+KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(InitializationStatus initializationStatus, const QString &installationResourcesLocation){Q_EMIT progressMessage(i18n("Krita is running for the first time. Initialization will take some time."));diff --git a/libs/resources/KisResourceLocator.h b/libs/resources/KisResourceLocator.hindex 5dbeb7ed29..163a6397cf 100644--- a/libs/resources/KisResourceLocator.h+++ b/libs/resources/KisResourceLocator.h@@ -118,6 +118,9 @@ public:* separate physical representation on the disk*/QString filePathForResource(KoResourceSP resource);+public Q_SLOTS:+ /// This updates the "fontregistery" storage. Called when the font directories change;+ void updateFontStorage();Q_SIGNALS:@@ -144,6 +147,9 @@ Q_SIGNALS:/// Emitted when a resource changes its active statevoid resourceActiveStateChanged(const QString &resourceType, int resourceId);+ /// Emitted when a storage is updated.+ void storageUpdated(const QString &location);+private:friend class KisAllTagsModel;diff --git a/libs/resources/KisResourceModel.cpp b/libs/resources/KisResourceModel.cppindex 068d1fae26..ef777f1234 100644--- a/libs/resources/KisResourceModel.cpp+++ b/libs/resources/KisResourceModel.cpp@@ -42,6 +42,7 @@ KisAllResourcesModel::KisAllResourcesModel(const QString &resourceType, QObjectconnect(KisStorageModel::instance(), SIGNAL(storageEnabled(const QString&)), this, SLOT(storageActiveStateChanged(const QString&)));connect(KisStorageModel::instance(), SIGNAL(storageDisabled(const QString&)), this, SLOT(storageActiveStateChanged(const QString&)));+ connect(KisStorageModel::instance(), SIGNAL(storageUpdated(const QString&)), this, SLOT(storageUpdated(const QString&)));connect(KisResourceLocator::instance(), SIGNAL(beginExternalResourceImport(QString, int)), this, SLOT(beginExternalResourceImport(QString, int)));connect(KisResourceLocator::instance(), SIGNAL(endExternalResourceImport(QString)), this, SLOT(endExternalResourceImport(QString)));@@ -669,6 +670,13 @@ void KisAllResourcesModel::storageActiveStateChanged(const QString &location)}}+void KisAllResourcesModel::storageUpdated(const QString &location)+{+ beginResetModel();+ resetQuery();+ endResetModel();+}+void KisAllResourcesModel::beginExternalResourceImport(const QString &resourceType, int numResources){if (resourceType != d->resourceType) return;diff --git a/libs/resources/KisResourceModel.h b/libs/resources/KisResourceModel.hindex d2c64522df..b94898b711 100644--- a/libs/resources/KisResourceModel.h+++ b/libs/resources/KisResourceModel.h@@ -271,6 +271,7 @@ public:private Q_SLOTS:void storageActiveStateChanged(const QString &location);+ void storageUpdated(const QString &location);/*** A special connection for KisResourceLocator, which can importdiff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cppindex 0e7f8a39fe..2bf5a3b2a0 100644--- a/libs/resources/KisStorageModel.cpp+++ b/libs/resources/KisStorageModel.cpp@@ -32,6 +32,7 @@ KisStorageModel::KisStorageModel(QObject *parent){connect(KisResourceLocator::instance(), SIGNAL(storageAdded(const QString&)), this, SLOT(addStorage(const QString&)));connect(KisResourceLocator::instance(), SIGNAL(storageRemoved(const QString&)), this, SLOT(removeStorage(const QString&)));+ connect(KisResourceLocator::instance(), SIGNAL(storageUpdated(const QString&)), this, SLOT(updateStorage(const QString&)));QSqlQuery query;@@ -509,4 +510,9 @@ void KisStorageModel::removeStorage(const QString &location)endRemoveRows();}+void KisStorageModel::updateStorage(const QString &location)+{+ emit storageUpdated(location);+}+diff --git a/libs/resources/KisStorageModel.h b/libs/resources/KisStorageModel.hindex fc7532ffd4..c425f5a4be 100644--- a/libs/resources/KisStorageModel.h+++ b/libs/resources/KisStorageModel.h@@ -68,6 +68,7 @@ Q_SIGNALS:void storageEnabled(const QString &storage);void storageDisabled(const QString &storage);+ void storageUpdated(const QString &storage);private Q_SLOTS:@@ -77,6 +78,9 @@ private Q_SLOTS:/// This is called when a storage really is deleted both from database and anywhere elsevoid removeStorage(const QString &location);+ /// Called when the storage is updated and everything needs to be reloaded.+ void updateStorage(const QString &location);+private:KisStorageModel(const KisStorageModel&);