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++
Implement FontStyleModel to handle the styles info inside a KoFontFamilyResource.
Wolthera van Hövell tot Westerflier13c236c66727
diff --git a/libs/flake/text/KoFFWWSConverter.cpp b/libs/flake/text/KoFFWWSConverter.cppindex 318d22453a..6b723f77ec 100644--- a/libs/flake/text/KoFFWWSConverter.cpp+++ b/libs/flake/text/KoFFWWSConverter.cpp@@ -656,6 +656,8 @@ KoFontFamilyWWSRepresentation createRepresentation(KisForest<FontFamilyNode>::chif (!subFamily->isVariable) {if (!subFamily->localizedWWSStyle.isEmpty()) {style.localizedLabels = subFamily->localizedWWSStyle;+ } else if (!subFamily->localizedTypographicStyle.isEmpty()) {+ style.localizedLabels = subFamily->localizedTypographicStyle;} else if (!subFamily->localizedFontStyle.isEmpty()) {style.localizedLabels = subFamily->localizedFontStyle;} else {diff --git a/plugins/dockers/textproperties/CMakeLists.txt b/plugins/dockers/textproperties/CMakeLists.txtindex 1de1062b5e..5623608927 100644--- a/plugins/dockers/textproperties/CMakeLists.txt+++ b/plugins/dockers/textproperties/CMakeLists.txt@@ -1,6 +1,7 @@set(KRITA_TEXTPROPERTIES_SOURCESTextPropertiesPlugin.cppTextPropertiesDock.cpp+ FontStyleModel.cpp)qt_add_resources(KRITA_TEXTPROPERTIES_SOURCES textproperties.qrc)diff --git a/plugins/dockers/textproperties/FontStyleModel.cpp b/plugins/dockers/textproperties/FontStyleModel.cppnew file mode 100644index 0000000000..5357b4945c--- /dev/null+++ b/plugins/dockers/textproperties/FontStyleModel.cpp@@ -0,0 +1,190 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "FontStyleModel.h"+#include <QDebug>++const QString WEIGHT_TAG = "wght";+const QString WIDTH_TAG = "wdth";+const QString SLANT_TAG = "slnt";+const QString ITALIC_TAG = "ital";+const QString OPTICAL_TAG = "opsz";++struct FontStyleModel::Private {+ QList<KoSvgText::FontFamilyStyleInfo> styles;+ QList<QLocale> locales;+};++FontStyleModel::FontStyleModel(QObject *parent)+ : QAbstractItemModel(parent)+ , d(new Private)+{++}++FontStyleModel::~FontStyleModel()+{+}+// sort the font styles in any given manner.+static bool styleLowerThan(const KoSvgText::FontFamilyStyleInfo &a, const KoSvgText::FontFamilyStyleInfo &b) {++ for (auto it = a.instanceCoords.begin(); it != a.instanceCoords.end(); it++) {+ if (it.key() == ITALIC_TAG || it.key() == SLANT_TAG) continue;+ qreal other = b.instanceCoords.value(it.key());+ if (it.value() < other) return true;+ }+ // todo: support slant and ital variation axes.+ QFont::Style aStyle = a.isItalic? a.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+ QFont::Style bStyle = b.isItalic? a.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+ return aStyle < bStyle;+}++void FontStyleModel::setStylesInfo(QList<KoSvgText::FontFamilyStyleInfo> styles)+{+ beginResetModel();+ std::sort(styles.begin(), styles.end(), styleLowerThan);+ d->styles = styles;+ endResetModel();+}++void FontStyleModel::setLocales(QList<QLocale> locales)+{+ d->locales = locales;+}++qreal FontStyleModel::weightValue(int row)+{+ return d->styles.value(row).instanceCoords.value(WEIGHT_TAG, 400.0);+}++qreal FontStyleModel::widthValue(int row)+{+ return d->styles.value(row).instanceCoords.value(WIDTH_TAG, 100.0);+}++int FontStyleModel::styleModeValue(int row)+{+ KoSvgText::FontFamilyStyleInfo style = d->styles.value(row);+ return style.isItalic? style.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+}++// TODO: consider somehow using the exact same method as in KoFFWWSConverter+QHash<int, KoSvgText::FontFamilyStyleInfo> searchAxisTag(QString tag, qreal value, QVector<qreal> values, qreal defaultVal, QHash<int, KoSvgText::FontFamilyStyleInfo> styles) {+ QHash<int, KoSvgText::FontFamilyStyleInfo> filteredStyles;+ auto valIt = std::lower_bound(values.begin(), values.end(), value);+ if (valIt == values.end()) valIt--;+ qreal selectedValue = *valIt;+ if (valIt != values.begin()) valIt--;+ qDebug() << tag << values << value;+ qDebug() << tag << *valIt << qAbs(*valIt - value) << selectedValue << qAbs(selectedValue - value);+ if (qAbs(*valIt - value) < qAbs(selectedValue - value)) {+ selectedValue = *valIt;+ }+ for (auto it = styles.begin(); it!= styles.end(); it++) {+ if (it.value().instanceCoords.value(tag, defaultVal) == selectedValue) {+ filteredStyles.insert(it.key(), it.value());+ }+ }+ return filteredStyles;+}++int FontStyleModel::rowForStyle(qreal weight, qreal width, int styleMode)+{+ QHash<int, KoSvgText::FontFamilyStyleInfo> styles;+ QVector<qreal> weights;+ QVector<qreal> widths;+ for (int i = 0; i< d->styles.size(); i++) {+ styles.insert(i, d->styles.at(i));+ weights.append(d->styles.value(i).instanceCoords.value(WEIGHT_TAG, 400.0));+ widths.append(d->styles.value(i).instanceCoords.value(WIDTH_TAG, 100.0));+ }+ std::sort(weights.begin(), weights.end());+ std::sort(widths.begin(), widths.end());++ if (styles.size() > 1) {+ styles = searchAxisTag(WEIGHT_TAG, weight, weights, 400.0, styles);+ }+ if (styles.size() > 1) {+ styles = searchAxisTag(WIDTH_TAG, width, widths, 100.0, styles);+ }+ if (styles.size() > 1) {+ QHash<int, KoSvgText::FontFamilyStyleInfo> filteredStyles;+ for (auto it = styles.begin(); it!= styles.end(); it++) {+ int fontStyle = it.value().isItalic? it.value().isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+ if (fontStyle == styleMode) {+ filteredStyles.insert(it.key(), it.value());+ }+ }+ if (!filteredStyles.isEmpty()) styles = filteredStyles;+ }+ return styles.isEmpty()? 0: styles.keys().first();+}++QModelIndex FontStyleModel::index(int row, int column, const QModelIndex &parent) const+{+ if (column != 0) return QModelIndex();+ if (row > 0 && row < d->styles.size()) return createIndex(row, column, &row);+ return QModelIndex();+}++QModelIndex FontStyleModel::parent(const QModelIndex &child) const+{+ return QModelIndex();+}++int FontStyleModel::rowCount(const QModelIndex &parent) const+{+ return d->styles.size();+}++int FontStyleModel::columnCount(const QModelIndex &parent) const+{+ return 1;+}++QVariant FontStyleModel::data(const QModelIndex &index, int role) const+{+ if (!index.isValid()) return QVariant();++ KoSvgText::FontFamilyStyleInfo style = d->styles.at(index.row());+ if (role == Qt::DisplayRole) {+ if (style.localizedLabels.isEmpty()) return QString();+ QString label = style.localizedLabels.value("en", style.localizedLabels.values().first());+ Q_FOREACH(const QLocale &locale, d->locales) {+ QString bcp = locale.bcp47Name();+ if (style.localizedLabels.keys().contains(bcp)) {+ label = style.localizedLabels.value(bcp, label);+ break;+ }+ }+ return label;+ } else if (role == Weight){+ return style.instanceCoords.value(WEIGHT_TAG, 400.0);+ } else if (role == Width){+ return style.instanceCoords.value(WIDTH_TAG, 100.0);+ } else if (role == StyleMode){+ return style.isItalic? style.isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+ } else if (role == Slant){+ return style.instanceCoords.value(SLANT_TAG, 0);+ } else if (role == AxisValues) {+ QVariantHash vals;+ for (auto it = style.instanceCoords.begin(); it != style.instanceCoords.end(); it++) {+ vals.insert(it.key(), it.value());+ }+ return vals;+ }+ return QVariant();+}++QHash<int, QByteArray> FontStyleModel::roleNames() const+{+ QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();+ roles[Weight] = "weight";+ roles[Width] = "width";+ roles[StyleMode] = "stylemode";+ roles[Slant] = "slant";+ roles[AxisValues] = "axisvalues";+ return roles;+}diff --git a/plugins/dockers/textproperties/FontStyleModel.h b/plugins/dockers/textproperties/FontStyleModel.hnew file mode 100644index 0000000000..8ef090fe23--- /dev/null+++ b/plugins/dockers/textproperties/FontStyleModel.h@@ -0,0 +1,59 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef FONTSTYLEMODEL_H+#define FONTSTYLEMODEL_H++#include <QAbstractItemModel>+#include <KoSvgText.h>+#include <QLocale>++/**+ * @brief The FontStyleModel class+ *+ * This models the styles info present in KoFontFamily Resources.+ */+class FontStyleModel: public QAbstractItemModel+{+ Q_OBJECT+ enum Roles {+ Weight = Qt::UserRole + 1, ///< qreal, represents 'wgth'+ Width, ///< qreal, represents 'wdth'+ StyleMode,///< QFont::Style+ Slant, ///< qreal, represents 'slnt'+ AxisValues ///< other axis values+ };++public:+ FontStyleModel(QObject *parent = nullptr);+ ~FontStyleModel();++ /// Set the base style info;+ void setStylesInfo(QList<KoSvgText::FontFamilyStyleInfo> styles);++ /// This sets which translated labels (if available) are returned.+ void setLocales(QList<QLocale> locales);++ Q_INVOKABLE qreal weightValue(int row);+ Q_INVOKABLE qreal widthValue(int row);+ Q_INVOKABLE int styleModeValue(int row);++ /// Find the closest style that represents the current width, weight and stylemode.+ Q_INVOKABLE int rowForStyle(qreal weight, qreal width, int styleMode);++ // QAbstractItemModel interface+public:+ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;+ QModelIndex parent(const QModelIndex &child) const;+ int rowCount(const QModelIndex &parent) const override;+ int columnCount(const QModelIndex &parent) const override;+ QVariant data(const QModelIndex &index, int role) const override;+ QHash<int, QByteArray> roleNames() const override;+private:+ struct Private;+ const QScopedPointer<Private> d;+};++#endif // FONTSTYLEMODEL_Hdiff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex c5e9d5fa1e..211b88eb4b 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -25,6 +25,7 @@#include <KoResourcePaths.h>#include <KisResourceModel.h>#include <KisResourceModelProvider.h>+#include <KisTagFilterResourceProxyModel.h>#include <KoCanvasResourcesIds.h>#include <KoSvgTextPropertyData.h>@@ -37,6 +38,8 @@#include <resources/KoFontFamily.h>#include <lager/state.hpp>+#include "FontStyleModel.h"+/// Strange place to put this, do we have a better spot?KIS_DECLARE_STATIC_INITIALIZER {qmlRegisterType<KoSvgTextPropertiesModel>("org.krita.flake.text", 1, 0, "KoSvgTextPropertiesModel");@@ -46,6 +49,8 @@ KIS_DECLARE_STATIC_INITIALIZER {qmlRegisterType<TabSizeModel>("org.krita.flake.text", 1, 0, "TabSizeModel");qmlRegisterType<TextTransformModel>("org.krita.flake.text", 1, 0, "TextTransformModel");qmlRegisterUncreatableMetaObject(KoSvgText::staticMetaObject, "org.krita.flake.text", 1, 0, "KoSvgText", "Error: Namespace with enums");++ qmlRegisterType<FontStyleModel>("org.krita.flake.text", 1, 0, "FontStyleModel");}@@ -80,8 +85,9 @@ private:struct TextPropertiesDock::Private{KoSvgTextPropertiesModel *textModel {new KoSvgTextPropertiesModel()};- QStringListModel stylesModel;+ FontStyleModel stylesModel;KisAllResourcesModel *fontModel{nullptr};+ KisTagFilterResourceProxyModel *fontTagFilterProxyModel {nullptr};KisCanvasResourceProvider *provider{nullptr};};@@ -111,10 +117,12 @@ TextPropertiesDock::TextPropertiesDock()m_quickWidget->setMinimumHeight(100);d->fontModel = KisResourceModelProvider::resourceModel(ResourceType::FontFamilies);-+ d->fontTagFilterProxyModel = new KisTagFilterResourceProxyModel(ResourceType::FontFamilies);+ d->fontTagFilterProxyModel->setSourceModel(d->fontModel);+ d->fontTagFilterProxyModel->sort(KisAbstractResourceModel::Name);m_quickWidget->rootContext()->setContextProperty("textPropertiesModel", d->textModel);- m_quickWidget->rootContext()->setContextProperty("fontFamiliesModel", QVariant::fromValue(d->fontModel));+ m_quickWidget->rootContext()->setContextProperty("fontFamiliesModel", QVariant::fromValue(d->fontTagFilterProxyModel));m_quickWidget->rootContext()->setContextProperty("fontStylesModel", QVariant::fromValue(&d->stylesModel));connect(d->textModel, SIGNAL(textPropertyChanged()),this, SLOT(slotTextPropertiesChanged()));@@ -195,22 +203,18 @@ void TextPropertiesDock::slotTextPropertiesChanged()void TextPropertiesDock::slotUpdateStylesModel(){QStringList families = d->textModel->fontFamilies();- QStringList styleList;+ QList<KoSvgText::FontFamilyStyleInfo> styles;if (!families.isEmpty() && d->fontModel) {QVector<KoResourceSP> res = d->fontModel->resourcesForFilename(families.first());if (!res.isEmpty()) {KoFontFamilySP family = res.first().staticCast<KoFontFamily>();if (family) {- QList<KoSvgText::FontFamilyStyleInfo> styles = family->styles();- for(int i=0; i<styles.size(); i++) {- KoSvgText::FontFamilyStyleInfo style = styles.at(i);- QString label = style.localizedLabels.value("en", style.localizedLabels.values().first());- styleList.append(label);- }+ styles = family->styles();+}}}- d->stylesModel.setStringList(styleList);+ d->stylesModel.setStylesInfo(styles);}#include "TextPropertiesDock.moc"diff --git a/plugins/dockers/textproperties/qml/FontStyle.qml b/plugins/dockers/textproperties/qml/FontStyle.qmlindex 7376623fa5..d091dc07e6 100644--- a/plugins/dockers/textproperties/qml/FontStyle.qml+++ b/plugins/dockers/textproperties/qml/FontStyle.qml@@ -34,6 +34,7 @@ CollapsibleGroupProperty {fontWidth = properties.fontWidth;fontOptical = properties.fontOpticalSizeLink;fontSlant = properties.fontStyle;+ styleCmb.currentIndex = fontStylesModel.rowForStyle(properties.fontWeight, properties.fontWidth, properties.fontStyle);visible = properties.fontWeightState !== KoSvgTextPropertiesModel.PropertyUnset|| properties.fontStyleState !== KoSvgTextPropertiesModel.PropertyUnset|| properties.fontWidthState !== KoSvgTextPropertiesModel.PropertyUnset@@ -67,6 +68,18 @@ CollapsibleGroupProperty {model: fontStylesModel;textRole: "display";width: parent.width;+ onActivated: {+ if (!blockSignals) {+ // Because each change to propertiesModel causes signals to fire,+ // we need to first store the vars and then apply them.+ var weight = fontStylesModel.weightValue(currentIndex);+ var width = fontStylesModel.widthValue(currentIndex);+ var styleMode = fontStylesModel.styleModeValue(currentIndex);+ properties.fontWeight = weight;+ properties.fontWidth = width;+ properties.fontStyle = styleMode;+ }+ }}contentItem: GridLayout {