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++
Generate SVG per font and draw said SVG in the font family selector.
Wolthera van Hövell tot Westerflierda66668f0ed2
diff --git a/libs/flake/resources/KoFontFamily.cpp b/libs/flake/resources/KoFontFamily.cppindex de375b53d5..31d0c6ae6c 100644--- a/libs/flake/resources/KoFontFamily.cpp+++ b/libs/flake/resources/KoFontFamily.cpp@@ -7,7 +7,9 @@#include <KoMD5Generator.h>#include <KoSvgTextShape.h>#include <KoColorBackground.h>+#include <SvgWriter.h>#include <QPainter>+#include <QBuffer>const QString TYPOGRAPHIC_NAME = "typographic_name";const QString LOCALIZED_TYPOGRAPHIC_NAME = "localized_typographic_name";@@ -21,6 +23,7 @@ const QString COLOR_CLRV0 = "color_clrv0";const QString COLOR_CLRV1 = "color_clrv1";const QString COLOR_SVG = "color_SVG";const QString SAMPLE_STRING = "sample_string";+const QString SAMPLE_SVG = "sample_svg";struct KoFontFamily::Private {};@@ -57,6 +60,24 @@ QImage generateImage(QString sample, QString fontFamily, bool isColor) {return img;}+QString generateSVG(QString sample, QString fontFamily) {+ QSharedPointer<KoSvgTextShape> shape(new KoSvgTextShape);+ shape->setResolution(300, 300);+ shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(Qt::black)));+ KoSvgTextProperties props = shape->textProperties();+ props.setProperty(KoSvgTextProperties::FontFamiliesId, {fontFamily});+ shape->setPropertiesAtPos(-1, props);+ shape->insertText(0, sample);++ SvgWriter writer({shape->textOutline()});+ QBuffer buffer;+ buffer.open(QIODevice::WriteOnly);+ writer.save(buffer, shape->boundingRect().size());+ buffer.close();++ return QString::fromUtf8(buffer.data());+}+QHash<QString, QVariant> localeHashtoVariantHash(QHash<QLocale, QString> names) {QHash<QString, QVariant> newNames;Q_FOREACH(const QLocale key, names.keys()) {@@ -152,6 +173,7 @@ void KoFontFamily::updateThumbnail()QString sample = samples.isEmpty()? QString("AaBbGg"):samples.value(QString::number(QLocale::LatinScript), samples.values().first()).toString();bool isColor = (metadata().value(COLOR_BITMAP).toBool() || metadata().value(COLOR_CLRV0).toBool());+ addMetaData(SAMPLE_SVG, generateSVG(sample, filename()));setImage(generateImage(sample, filename(), isColor));}diff --git a/libs/resources/KisResourceModel.cpp b/libs/resources/KisResourceModel.cppindex 96a5970264..068d1fae26 100644--- a/libs/resources/KisResourceModel.cpp+++ b/libs/resources/KisResourceModel.cpp@@ -194,6 +194,7 @@ QHash<int, QByteArray> KisAllResourcesModel::roleNames() constroles[Qt::UserRole + Tags] = "tags";roles[Qt::UserRole + LargeThumbnail] = "largethumbnail";roles[Qt::UserRole + Dirty] = "dirty";+ roles[Qt::UserRole + MetaData] = "metadata";roles[Qt::UserRole + ResourceActive] = "resourceactive";roles[Qt::UserRole + StorageActive] = "storageactive";diff --git a/plugins/dockers/textproperties/CMakeLists.txt b/plugins/dockers/textproperties/CMakeLists.txtindex 45a18c8d39..1061cd1aa4 100644--- a/plugins/dockers/textproperties/CMakeLists.txt+++ b/plugins/dockers/textproperties/CMakeLists.txt@@ -3,6 +3,7 @@ set(KRITA_TEXTPROPERTIES_SOURCESTextPropertiesDock.cppFontStyleModel.cppFontAxesModel.cpp+ KoShapeQtQuickLabel.cpp)qt_add_resources(KRITA_TEXTPROPERTIES_SOURCES textproperties.qrc)diff --git a/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cppnew file mode 100644index 0000000000..dd9aa50c64--- /dev/null+++ b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp@@ -0,0 +1,149 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "KoShapeQtQuickLabel.h"+#include <KoShape.h>+#include <SvgParser.h>+#include <KoColorBackground.h>+#include <KoDocumentResourceManager.h>+#include <QQuickWindow>+#include <KoShapeGroupCommand.h>++struct KoShapeQtQuickLabel::Private {+ KoShapeGroup *shape = nullptr;+ QString svgData;+ int imagePadding = 5;+ qreal imageScale = 1;+};++KoShapeQtQuickLabel::KoShapeQtQuickLabel(QQuickItem *parent)+ : QQuickPaintedItem(parent)+ , d(new Private)+{++}++KoShapeQtQuickLabel::~KoShapeQtQuickLabel()+{+}++void KoShapeQtQuickLabel::paint(QPainter *painter)+{+ if (!painter->isActive()) return;+ painter->save();+ painter->setRenderHint(QPainter::Antialiasing, true);+ painter->fillRect(0, 0, width(), height(), fillColor());+ if (!d->shape) {+ painter->restore();+ return;+ }++ QRectF bbox = d->shape->boundingRect();+ qreal scale = (height()- d->imagePadding*2) / bbox.height();++ painter->translate(boundingRect().center());+ painter->scale(scale, scale);+ painter->translate(-bbox.center());++ qreal pixelRatio = this->window()->effectiveDevicePixelRatio();+ QRectF scaledBounds = QRectF(0, 0, this->boundingRect().width()*pixelRatio, this->boundingRect().height()*pixelRatio);++ painter->setClipRect(painter->transform().inverted().mapRect(scaledBounds));+ painter->setPen(Qt::transparent);++ Q_FOREACH(const KoShape *shape, d->shape->shapes()) {+ shape->paint(*painter);+ }++ painter->restore();++}++QString KoShapeQtQuickLabel::svgData() const+{+ return d->svgData;+}++void KoShapeQtQuickLabel::setSvgData(const QString &newSvgData)+{+ if (d->svgData == newSvgData)+ return;+ d->svgData = newSvgData;++ QDomDocument doc = SvgParser::createDocumentFromSvg(d->svgData);+ KoDocumentResourceManager resourceManager;+ SvgParser p(&resourceManager);+ qreal max = qMax(60.0, qMax(width()-(d->imagePadding*2), height()-(d->imagePadding*2)));+ QRectF bb = QRectF( 0, 0, max, max);+ QSizeF sz = bb.size();+ p.setResolution(bb, 72);++ QList<KoShape*> shapes = p.parseSvg(doc.documentElement(), &sz);+ if (shapes.isEmpty()) return;+ d->shape = new KoShapeGroup();+ KoShapeGroupCommand cmd(d->shape, shapes, false);+ cmd.redo();++ d->shape->setAbsolutePosition(QPointF(), KoFlake::TopLeft);++ qreal scale = bb.height() / sz.height();++ setImplicitHeight(bb.height());++ emit svgDataChanged();+}++QColor KoShapeQtQuickLabel::foregroundColor() const+{+ QColor fg = Qt::black;+ if (d->shape && d->shape->background()) {+ KoColorBackground *bg = dynamic_cast<KoColorBackground*>(d->shape->background().data());+ if (bg) {+ return bg->color();+ }++ }+ return fg;+}++void KoShapeQtQuickLabel::setForegroundColor(const QColor &newForegroundColor)+{+ if (!d->shape) return;+ KoColorBackground *bg = dynamic_cast<KoColorBackground*>(d->shape->background().data());+ if (bg) {+ if (bg->color() == newForegroundColor)+ return;+ }++ d->shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(newForegroundColor)));++ emit foregroundColorChanged();+}++int KoShapeQtQuickLabel::imagePadding() const+{+ return d->imagePadding;+}++void KoShapeQtQuickLabel::setImagePadding(int newPadding)+{+ if (d->imagePadding == newPadding)+ return;+ d->imagePadding = newPadding;+ emit imagePaddingChanged();+}++qreal KoShapeQtQuickLabel::imageScale() const+{+ return d->imageScale;+}++void KoShapeQtQuickLabel::setImageScale(qreal newImageScale)+{+ if (qFuzzyCompare(d->imageScale, newImageScale))+ return;+ d->imageScale = newImageScale;+ emit imageScaleChanged();+}diff --git a/plugins/dockers/textproperties/KoShapeQtQuickLabel.h b/plugins/dockers/textproperties/KoShapeQtQuickLabel.hnew file mode 100644index 0000000000..1d5dcdafa7--- /dev/null+++ b/plugins/dockers/textproperties/KoShapeQtQuickLabel.h@@ -0,0 +1,62 @@+/*+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KOSHAPEQTQUICKLABEL_H+#define KOSHAPEQTQUICKLABEL_H++#include <QQuickPaintedItem>++/**+ * @brief The KoShapeQtQuickLabel class+ *+ * A QQuickPaintedItem that will load SVG as a KoShape+ * and paint it.+ */+class KoShapeQtQuickLabel: public QQuickPaintedItem+{+ Q_OBJECT+ Q_PROPERTY(QString svgData READ svgData WRITE setSvgData NOTIFY svgDataChanged)+ Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor NOTIFY foregroundColorChanged)+ Q_PROPERTY(int imagePadding READ imagePadding WRITE setImagePadding NOTIFY imagePaddingChanged)+ Q_PROPERTY(qreal imageScale READ imageScale WRITE setImageScale NOTIFY imageScaleChanged)+public:+ KoShapeQtQuickLabel(QQuickItem *parent = nullptr);+ ~KoShapeQtQuickLabel();++ // QQuickPaintedItem interface+public:+ void paint(QPainter *painter) override;+ QString svgData() const;+ void setSvgData(const QString &newSvgData);++ /// Foreground color.+ QColor foregroundColor() const;+ void setForegroundColor(const QColor &newForegroundColor);++ /// Padding of the image to the borders.+ int imagePadding() const;+ void setImagePadding(int newPadding);++ /// extra scaling applied to the image.+ qreal imageScale() const;+ void setImageScale(qreal newImageScale);++Q_SIGNALS:+ void svgDataChanged();++ void fillColorChanged();++ void foregroundColorChanged();++ void imagePaddingChanged();++ void imageScaleChanged();++private:+ struct Private;+ const QScopedPointer<Private> d;+};++#endif // KOSHAPEQTQUICKLABEL_Hdiff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex 42fcaf53d1..30236f6b9c 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -41,6 +41,7 @@#include "FontStyleModel.h"#include "FontAxesModel.h"+#include "KoShapeQtQuickLabel.h"/// Strange place to put this, do we have a better spot?KIS_DECLARE_STATIC_INITIALIZER {@@ -54,6 +55,7 @@ KIS_DECLARE_STATIC_INITIALIZER {qmlRegisterType<FontStyleModel>("org.krita.flake.text", 1, 0, "FontStyleModel");qmlRegisterType<FontAxesModel>("org.krita.flake.text", 1, 0, "FontAxesModel");+ qmlRegisterType<KoShapeQtQuickLabel>("org.krita.flake.text", 1, 0, "KoShapeQtQuickLabel");}diff --git a/plugins/dockers/textproperties/qml/FontFamily.qml b/plugins/dockers/textproperties/qml/FontFamily.qmlindex baaf4d1f62..5e73cbcccf 100644--- a/plugins/dockers/textproperties/qml/FontFamily.qml+++ b/plugins/dockers/textproperties/qml/FontFamily.qml@@ -49,14 +49,27 @@ CollapsibleGroupProperty {textRole: "name";delegate: ItemDelegate {+ id: fontDelegaterequired property var model;- contentItem: Label {- text: model.name;++ highlighted: mainFamilyCmb.highlightedIndex === model.index;+ contentItem: KoShapeQtQuickLabel{Layout.fillWidth: true;- height:implicitHeight;+ height: fontResourceView.listWidth/8;+ svgData: model.metadata["sample_svg"]++ imageScale: 3;+ imagePadding: nameLabel.height;+ foregroundColor: sysPalette.text;++ Label {+ id: nameLabel;+ text: model.name;+ anchors.top: parent.top;+ anchors.left: parent.left;+ }}width: fontResourceView.listWidth;- highlighted: mainFamilyCmb.highlightedIndex === model.index;background: Rectangle { color: highlighted? parent.palette.highlight:"transparent"; }}popup: Popup {