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++
Introduce KisFontFunctions and move out some cpp code into QML.
Wolthera van Hövell tot Westerflier57121616e6aa
diff --git a/libs/flake/resources/KoFontFamily.cpp b/libs/flake/resources/KoFontFamily.cppindex 48dfdfe842..d99a7c8648 100644--- a/libs/flake/resources/KoFontFamily.cpp+++ b/libs/flake/resources/KoFontFamily.cpp@@ -184,8 +184,8 @@ QPair<QString, QString> KoFontFamily::resourceType() constvoid KoFontFamily::updateThumbnail(){QHash<QString, QVariant> samples = metadata().value(SAMPLE_STRING).toHash();- QHash<QString, QVariant> sampleSVG;- QHash<QString, QVariant> sampleSVGBbox;+ QMap<QString, QVariant> sampleSVG;+ QMap<QString, QVariant> sampleSVGBbox;Q_FOREACH (const QString key, samples.keys()) {QString sample = samples.value(key).toString();diff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp b/plugins/dockers/textproperties/CssStylePresetEditDialog.cppindex c3290fc765..44183af712 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp@@ -138,15 +138,6 @@ QColor CssStylePresetEditDialog::modalColorDialog(QColor oldColor)return c.isValid()? c: oldColor;}-QString CssStylePresetEditDialog::wwsFontFamilyName(QString familyName)-{- std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(familyName);- if (!name) {- return familyName;- }- return name.value();-}-void CssStylePresetEditDialog::slotUpdateDirty() {bool isDirty = m_currentResource->isDirty();const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();diff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.h b/plugins/dockers/textproperties/CssStylePresetEditDialog.hindex c1ce49dd69..3440836e17 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.h+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.h@@ -27,7 +27,6 @@ public:public Q_SLOTS:void slotUpdateTextProperties();QColor modalColorDialog(QColor oldColor);- QString wwsFontFamilyName(QString familyName);void slotUpdateDirty();void slotUpdateStoreDPI();diff --git a/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.cpp b/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.cppindex 70d7f423c4..14d74fae2f 100644--- a/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.cpp+++ b/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.cpp@@ -202,24 +202,6 @@ void TagFilterProxyModelQmlWrapper::untagResource(const int &tagIndex, const int}}-#include <KoWritingSystemUtils.h>-QString TagFilterProxyModelQmlWrapper::localizedSampleFromMetadata(const QMap<QString, QVariant> &metadata, const QStringList &locales, const QString &fallBack)-{- QString sample = fallBack;- QVariantMap samples = metadata.value("sample_svg").toMap();- if (!samples.isEmpty()) sample = samples.value("s_Latn", samples.values().first()).toString();- if (!locales.isEmpty()) {- Q_FOREACH(const QString locale, locales) {- const QString tag = KoWritingSystemUtils::sampleTagForQLocale(QLocale (locale));- if (samples.keys().contains(tag) ) {- sample = samples.value(tag).toString();- break;- }- }- }- return sample;-}-QVariantList TagFilterProxyModelQmlWrapper::taggedResourceModel (const int &resourceIndex) const{QVariantList taggedResourceModel;diff --git a/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.h b/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.hindex 4450eeee4a..1ab0d8a834 100644--- a/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.h+++ b/plugins/dockers/textproperties/TagFilterProxyModelQmlWrapper.h@@ -24,7 +24,7 @@ class TagFilterProxyModelQmlWrapper : public QObjectQ_PROPERTY(QString resourceType READ resourceType WRITE setResourceType NOTIFY resourceTypeChanged)Q_PROPERTY(QString resourceTypeName READ resourceTypeName NOTIFY resourceTypeChanged)- Q_PROPERTY(bool importEnabled READ importEnabled NOTIFY resourceTypeChanged);+ Q_PROPERTY(bool importEnabled READ importEnabled NOTIFY resourceTypeChanged)Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)Q_PROPERTY(int currentTag READ currentTag WRITE tagActivated NOTIFY activeTagChanged)@@ -85,9 +85,6 @@ public:/// Untag the resourceQ_INVOKABLE void untagResource(const int &tagIndex, const int &resourceIndex);- /// Get localized sample string based on scripts of locale.- Q_INVOKABLE static QString localizedSampleFromMetadata(const QMap<QString, QVariant> &metadata, const QStringList &locales, const QString &fallBack = "");-/// Get a variantlist with the tags for the given resource index.Q_INVOKABLE QVariantList taggedResourceModel (const int &resourceIndex) const;diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex 45b20536fe..a81522a43b 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -84,13 +84,3 @@ QColor TextPropertiesDock::modalColorDialog(QColor oldColor)QColor c = QColorDialog::getColor(oldColor);return c.isValid()? c: oldColor;}--#include <KoFontRegistry.h>-QString TextPropertiesDock::wwsFontFamilyName(QString familyName, bool returnEmptyWhenMissing)-{- std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(familyName);- if (!name) {- return returnEmptyWhenMissing? QString(): familyName;- }- return name.value();-}diff --git a/plugins/dockers/textproperties/TextPropertiesDock.h b/plugins/dockers/textproperties/TextPropertiesDock.hindex 28b13162bc..43c703ac5a 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.h+++ b/plugins/dockers/textproperties/TextPropertiesDock.h@@ -30,8 +30,6 @@ public:void unsetCanvas() override;public Q_SLOTS:- QString wwsFontFamilyName(QString familyName, bool returnEmptyWhenMissing = false);-QColor modalColorDialog(QColor oldColor);diff --git a/plugins/dockers/textproperties/qml/FontFamily.qml b/plugins/dockers/textproperties/qml/FontFamily.qmlindex eb3501d847..576b05c0b0 100644--- a/plugins/dockers/textproperties/qml/FontFamily.qml+++ b/plugins/dockers/textproperties/qml/FontFamily.qml@@ -61,6 +61,10 @@ CollapsibleGroupProperty {}}+ Kis.FontFunctions {+ id: fontFunctions;+ }+titleItem: RowLayout {width: parent.width;height: childrenRect.height;@@ -84,7 +88,7 @@ CollapsibleGroupProperty {}}function updateCurrentIndex() {- var name = mainWindow.wwsFontFamilyName(fontFamilies[0]);+ var name = fontFunctions.wwsFontFamilyName(fontFamilies[0]);if (name !== modelWrapper.resourceFilename) {modelWrapper.currentTag = 0;modelWrapper.currentIndex = -1;@@ -131,10 +135,10 @@ CollapsibleGroupProperty {Component.onCompleted: {if (fontListDelegate.dIndex === 0) {modelWrapper = mainFamilyCmb.modelWrapper;- fontFileName = mainWindow.wwsFontFamilyName(fontFamilies[fontListDelegate.dIndex]);+ fontFileName = fontFunctions.wwsFontFamilyName(fontFamilies[fontListDelegate.dIndex]);}}- fontFileName: mainWindow.wwsFontFamilyName(fontFamilies[fontListDelegate.dIndex]);+ fontFileName: fontFunctions.wwsFontFamilyName(fontFamilies[fontListDelegate.dIndex]);}ToolButton {id: removeFont;diff --git a/plugins/dockers/textproperties/qml/FontResourceDropdown.qml b/plugins/dockers/textproperties/qml/FontResourceDropdown.qmlindex 1fc5c906f5..416512e898 100644--- a/plugins/dockers/textproperties/qml/FontResourceDropdown.qml+++ b/plugins/dockers/textproperties/qml/FontResourceDropdown.qml@@ -37,6 +37,9 @@ Button {id: palControl;}palette: palControl.palette;+ Kis.FontFunctions {+ id: fontFunctions;+ }/// Ideally we'd have the max popup height be Window-height - y-pos-of-item-to-window./// But that's pretty hard to calculate (map to global is screen relative, but there's@@ -79,9 +82,9 @@ Button {warningTimeOut: activeFocus? 1000: 0;function testInput() {- let test = mainWindow.wwsFontFamilyName(text, true);+ let test = fontFunctions.wwsFontFamilyNameVariant(text);- if (test === "") {+ if (typeof test === 'undefined') {if (!activeFocus) {startWarning();}@@ -147,16 +150,39 @@ Button {property string sample: "";onMetadataChanged: {- updateNameAndSample();+ updateSample();}Component.onCompleted: {- updateNameAndSample();+ updateSample();}- function updateNameAndSample() {- if (typeof metadata == "undefined") return;- sample = modelWrapper.localizedSampleFromMetadata(metadata, familyCmb.locales, "");+ function updateSample() {+ if (typeof metadata === "undefined") return;+ let samples = fontFunctions.getMapFromQVariant(metadata.sample_svg);+ let sampleKeys = Object.keys(samples);+ sample = "";+ const latn = samples["s_Latn"];+ if (sampleKeys.length > 0) {+ if (latn) {+ sample = latn;+ } else {+ let tag = sampleKeys[0];+ sample = samples[tag];+ }+ }+ if (familyCmb.locales.length > 0) {+ for (const locale of familyCmb.locales) {+ let tag = fontFunctions.sampleTagForQLocale(Qt.locale(locale));+ let svg = samples[tag];+ if (svg) {+ sample = svg;+ break;+ }+++ }+ }}/// When updating the model wrapper, the "model" doesn't always update on the delegate, so we need to manually load@@ -207,6 +233,7 @@ Button {fullColor: colorBitmap || colorCLRV0 || colorCLRV1 || colorSVG;padding: nameLabel.height;+Row {id: featureRow;spacing: columnSpacing;@@ -311,7 +338,7 @@ Button {preventStealing: true;onClicked: {if (mouse.button === Qt.RightButton) {- resourceView.openContextMenu(mouse.x, mouse.y, parent.model.name, parent.model.index);+ resourceView.openContextMenu(mouse.x, mouse.y, parent.model.name, parent.index);} else {resourceView.applyHighlightedIndex();familyCmb.activated();diff --git a/plugins/dockers/textproperties/qml/ResourceView.qml b/plugins/dockers/textproperties/qml/ResourceView.qmlindex d74a17ca9e..a99aa37a65 100644--- a/plugins/dockers/textproperties/qml/ResourceView.qml+++ b/plugins/dockers/textproperties/qml/ResourceView.qml@@ -102,7 +102,7 @@ Control {palette: control.palette;function hideShowMenu() {if (!tagActionsContextMenu.visible) {- tagActionsContextMenu.resourceIndex = control.highlightedIndex;+ tagActionsContextMenu.resourceIndex = modelWrapper.currentIndex;tagActionsContextMenu.popup(tagMenuButton,tagActionsContextMenu.width - tagMenuButton.width,tagMenuButton.height - 1);diff --git a/plugins/dockers/textproperties/qml/TextProperties.qml b/plugins/dockers/textproperties/qml/TextProperties.qmlindex 9974d493eb..fcf878cad4 100644--- a/plugins/dockers/textproperties/qml/TextProperties.qml+++ b/plugins/dockers/textproperties/qml/TextProperties.qml@@ -27,6 +27,9 @@ Control {id: focusChecker;onInFocusChanged: canvasObserver.hasFocus = inFocus;}+ Kis.FontFunctions {+ id: fontFunctions;+ }property TextPropertyConfigModel configModel : canvasObserver.textPropertyConfig;property double canvasDPI: canvasObserver.dpi;@@ -156,7 +159,7 @@ Control {id: missingFamily;visible: typeof presetDelegate.meta.primary_font_family !== 'undefined'?presetDelegate.meta.primary_font_family !== ""- && mainWindow.wwsFontFamilyName(presetDelegate.meta.primary_font_family, true) === "": false;+ && typeof fontFunctions.wwsFontFamilyNameVariant(presetDelegate.meta.primary_font_family) === 'undefined': false;icon.source: palette.window.hslLightness < 0.5? "qrc:///16_light_warning.svg": "qrc:///16_dark_warning.svg";icon.height: 8;icon.width: 8;diff --git a/qmlmodules/components/CMakeLists.txt b/qmlmodules/components/CMakeLists.txtindex b5055f057e..fb844f101e 100644--- a/qmlmodules/components/CMakeLists.txt+++ b/qmlmodules/components/CMakeLists.txt@@ -19,6 +19,8 @@ if (QT_MAJOR_VERSION STREQUAL "6")KisCubicCurveQMLWrapper.hKisTheme.cppKisTheme.h+ KisFontFunctions.cpp+ KisFontFunctions.h)else ()@@ -40,6 +42,8 @@ else ()KisCubicCurveQMLWrapper.hKisTheme.cppKisTheme.h+ KisFontFunctions.cpp+ KisFontFunctions.h)endif ()diff --git a/qmlmodules/components/KisFontFunctions.cpp b/qmlmodules/components/KisFontFunctions.cppnew file mode 100644index 0000000000..a7f0efb119--- /dev/null+++ b/qmlmodules/components/KisFontFunctions.cpp@@ -0,0 +1,36 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "KisFontFunctions.h"+#include <KoWritingSystemUtils.h>+#include <KoFontRegistry.h>++QString KisFontFunctions::wwsFontFamilyName(QString familyName)+{+ std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(familyName);+ if (!name) {+ return familyName;+ }+ return name.value();+}++QVariant KisFontFunctions::wwsFontFamilyNameVariant(QString familyName)+{+ std::optional<QString> name = KoFontRegistry::instance()->wwsNameByFamilyName(familyName);+ if (!name) {+ return QVariant();+ }+ return name.value();+}++QString KisFontFunctions::sampleTagForQLocale(const QLocale &locale)+{+ return KoWritingSystemUtils::sampleTagForQLocale(locale);+}++QVariantMap KisFontFunctions::getMapFromQVariant(QVariant var)+{+ return var.toMap();+}diff --git a/qmlmodules/components/KisFontFunctions.h b/qmlmodules/components/KisFontFunctions.hnew file mode 100644index 0000000000..59053d1e7e--- /dev/null+++ b/qmlmodules/components/KisFontFunctions.h@@ -0,0 +1,40 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KISFONTFUNCTIONS_H+#define KISFONTFUNCTIONS_H++#include <QObject>+#include <QQmlEngine>+class QQmlEngine;+class QJSEngine;+/**+ * Number of functions related to handling FontFamilies inside QML.+ * For some reason, if you have a collection of static functions,+ * they can only be accessed via QML via an Object.+ */+class KisFontFunctions: public QObject {++ Q_OBJECT+ QML_NAMED_ELEMENT(FontFunctions)++public:++ /// Returns the wwsname when present, otherwise returns the family name;+ Q_INVOKABLE static QString wwsFontFamilyName(QString familyName);++ /// Returns the wws name as a variant which can be invalid when not found.+ Q_INVOKABLE static QVariant wwsFontFamilyNameVariant(QString familyName);++ /// Returns the sample tag for a given locale, calls KoWritingSystemUtils+ Q_INVOKABLE static QString sampleTagForQLocale(const QLocale &locale);++ /// QML can only really use QVariantMaps, so if you have a potential+ /// QVariantHash, you can use this function to convert it to a QVariantMap.+ Q_INVOKABLE static QVariantMap getMapFromQVariant(QVariant var);++};++#endif // KISFONTFUNCTIONS_Hdiff --git a/qmlmodules/components/KritaQmlComponentsPlugin.cpp b/qmlmodules/components/KritaQmlComponentsPlugin.cppindex de9e4b191f..40448903d8 100644--- a/qmlmodules/components/KritaQmlComponentsPlugin.cpp+++ b/qmlmodules/components/KritaQmlComponentsPlugin.cpp@@ -13,6 +13,7 @@#include <KisNumParser.h>#include <KisCubicCurveQMLWrapper.h>#include <KisTheme.h>+#include <KisFontFunctions.h>#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)#error "This plugin implementation is to be used with Qt5 only!\n"@@ -41,6 +42,7 @@ void KritaQmlComponentsPlugin::registerTypes(const char *uri)qmlRegisterType<KisCubicCurveQml>(uri, 1, 0, "KisCubicCurve");qmlRegisterType<KisTheme>(uri, 1, 0, "Theme");qmlRegisterUncreatableType<KisThemeColorGroup>(uri, 1, 0, "ThemeColorGroup", "Use Theme instead");+ qmlRegisterType<KisFontFunctions>(uri, 1, 0, "FontFunctions");}#include "KritaQmlComponentsPlugin.moc"