Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Make it so we can paint KoSvgTextShapes inside the GlyphSelector.

Wolthera van Hövell tot Westerflier93c4b9c2cf8f
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index 386b9b3e38..356b2bef96 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -1053,7 +1053,6 @@ KoSvgTextProperties KoSvgTextShape::textProperties() const
QSharedPointer<KoShapeBackground> KoSvgTextShape::background() const
{
- QSharedPointer<KoShapeBackground> bg(new KoColorBackground(Qt::black));
KoSvgTextProperties props = KisForestDetail::size(d->textData)? d->textData.childBegin()->properties: KoSvgTextProperties();
if (props.hasProperty(KoSvgTextProperties::FillId)) {
return props.property(KoSvgTextProperties::FillId).value<KoSvgText::BackgroundProperty>().property;
diff --git a/plugins/tools/svgtexttool/CMakeLists.txt b/plugins/tools/svgtexttool/CMakeLists.txt
index b2dc5eeafa..e9d23c6463 100644
--- a/plugins/tools/svgtexttool/CMakeLists.txt
+++ b/plugins/tools/svgtexttool/CMakeLists.txt
@@ -25,6 +25,7 @@ set (SvgTextTool_SRCS
SvgTextShapeManagerBlocker.cpp
glyphpalette/GlyphPaletteDialog.cpp
+ glyphpalette/SvgTextLabel.cpp
)
ki18n_wrap_ui(SvgTextTool_SRCS
diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml
index a060085e46..a43e312242 100644
--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml
+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml
@@ -4,8 +4,8 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/
import QtQuick 2.0
-import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
+import org.krita.tools.text 1.0
Rectangle {
id: root;
@@ -13,6 +13,10 @@ Rectangle {
property string titleText;
property QtObject model;
property alias currentIndex: charMap.currentIndex;
+ property var fontFamilies: [];
+ property double fontSize :10.0;
+ property int fontWeight : 400;
+ property int fontStyle;
SystemPalette {
id: palette;
colorGroup: SystemPalette.Active
@@ -20,9 +24,10 @@ Rectangle {
color: palette.window
Text {
- id: title
- text: titleText
- color: palette.windowText
+ id: title;
+ text: titleText;
+ color: palette.text;
+
anchors.left: parent.left;
anchors.right: parent.right;
anchors.top: parent.top;
@@ -33,29 +38,36 @@ Rectangle {
anchors.right: parent.right;
anchors.top: title.bottom;
anchors.bottom: parent.bottom;
+
+ property int scrollWidth: ScrollBar.implicitWidth;
GridView {
id: charMap;
model: root.model;
focus: true;
anchors.fill: parent;
- cellWidth: 25;
- cellHeight: 25;
+ cellWidth: (root.width - parent.scrollWidth)/8;
+ cellHeight: cellWidth;
+
+ delegate:SvgTextLabel {
+ textColor: palette.windowText;
+ fillColor: palette.base;
+ fontFamilies: root.fontFamilies;
+ fontSize: root.fontSize;
+ fontStyle: root.fontStyle;
+ fontWeight: root.fontWeight;
+ text: model.display;
+ padding: height/8;
- delegate: Rectangle {
width: charMap.cellWidth;
height: charMap.cellHeight;
- color: palette.base
- border.color: GridView.isCurrentItem? palette.highlight: palette.alternateBase;
-
- Text {
- clip: true;
- text: model.display
- color: palette.windowText
- anchors.centerIn: parent;
+ property bool currentItem: GridView.isCurrentItem;
+ Rectangle {
+ anchors.fill: parent;
+ color: "transparent";
+ border.color: parent.currentItem? palette.highlight: palette.alternateBase;
}
-
MouseArea {
anchors.fill: parent;
onClicked: {
@@ -67,7 +79,8 @@ Rectangle {
ToolTip.visible: containsMouse;
}
}
+
+
}
}
-
}
diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp
index 1eb3b5f5a2..463fbd2576 100644
--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp
+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp
@@ -13,6 +13,13 @@
#include <KoFontRegistry.h>
#include <KoSvgText.h>
+#include <KisStaticInitializer.h>
+#include "glyphpalette/SvgTextLabel.h"
+KIS_DECLARE_STATIC_INITIALIZER {
+ qmlRegisterType<SvgTextLabel>("org.krita.tools.text", 1, 0, "SvgTextLabel");
+
+}
+
GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)
{
setMinimumSize(500, 300);
@@ -33,6 +40,9 @@ GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)
if (m_quickWidget->rootObject()) {
m_quickWidget->rootObject()->setProperty("titleText", "Glyph palette");
}
+ if (!m_quickWidget->errors().empty()) {
+ qWarning() << "Errors in " << windowTitle() << ":" << m_quickWidget->errors();
+ }
}
void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties properties, QString text)
@@ -56,6 +66,9 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propert
if (properties.hasProperty(KoSvgTextProperties::KraTextVersionId)) {
fontSizeAdjust.isAuto = (properties.property(KoSvgTextProperties::KraTextVersionId).toInt() < 3);
}
+ QStringList families = properties.property(KoSvgTextProperties::FontFamiliesId).toStringList();
+ qreal size = properties.propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal();
+ int weight = properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt();
const std::vector<FT_FaceSP> faces = KoFontRegistry::instance()->facesForCSSValues(
properties.property(KoSvgTextProperties::FontFamiliesId).toStringList(),
lengths,
@@ -63,7 +76,7 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propert
text,
static_cast<quint32>(res),
static_cast<quint32>(res),
- properties.propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal(),
+ size,
fontSizeAdjust.isAuto ? 1.0 : fontSizeAdjust.customValue,
properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),
properties.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),
@@ -74,6 +87,10 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propert
QString name = faces.front().data()->family_name;
m_quickWidget->rootObject()->setProperty("titleText", QString("Glyph palette: "+name));
m_quickWidget->rootObject()->setProperty("model", QVariant::fromValue(model));
+ m_quickWidget->rootObject()->setProperty("fontFamilies", QVariant::fromValue(families));
+ m_quickWidget->rootObject()->setProperty("fontSize", QVariant::fromValue(size));
+ m_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(weight));
+ m_quickWidget->rootObject()->setProperty("fontStyle", QVariant::fromValue(style));
if (idx.isValid()) {
m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));
}
diff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp
new file mode 100644
index 0000000000..8daa01455d
--- /dev/null
+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp
@@ -0,0 +1,196 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "SvgTextLabel.h"
+#include <KoSvgTextShape.h>
+#include <KoSvgTextProperties.h>
+#include <KoColorBackground.h>
+#include <QPainter>
+#include <QImage>
+
+struct Q_DECL_HIDDEN SvgTextLabel::Private {
+ QStringList openTypeFeatures;
+ KoSvgTextShape* shape{nullptr};
+ int padding = 2;
+};
+
+SvgTextLabel::SvgTextLabel(QQuickItem *parent)
+ : QQuickPaintedItem(parent)
+ , d(new Private)
+{
+ d->shape = new KoSvgTextShape();
+ d->shape->setResolution(72, 72);
+ setImplicitSize(10, 10);
+ setOpaquePainting(true);
+ setAntialiasing(true);
+}
+
+SvgTextLabel::~SvgTextLabel()
+{
+}
+
+void SvgTextLabel::paint(QPainter *painter)
+{
+ painter->save();
+ painter->fillRect(0, 0, width(), height(), fillColor());
+ qreal scale = (height()- d->padding*2) / fontSize();
+
+ painter->translate(boundingRect().center());
+ painter->scale(scale, scale);
+ painter->translate(-d->shape->boundingRect().center());
+
+
+ painter->setClipRect(painter->transform().inverted().mapRect(this->boundingRect()));
+ painter->setPen(Qt::transparent);
+ d->shape->paint(*painter);
+
+ painter->restore();
+}
+
+QStringList SvgTextLabel::fontFamilies() const
+{
+ return d->shape->textProperties().property(KoSvgTextProperties::FontFamiliesId).value<QStringList>();
+}
+
+int SvgTextLabel::fontWeight() const
+{
+ return d->shape->textProperties().property(KoSvgTextProperties::FontWeightId).toInt();
+}
+
+QFont::Style SvgTextLabel::fontStyle() const
+{
+ return d->shape->textProperties().property(KoSvgTextProperties::FontStyleId).value<QFont::Style>();
+}
+
+qreal SvgTextLabel::fontSize() const
+{
+ return d->shape->textProperties().property(KoSvgTextProperties::FontSizeId).toReal();
+}
+
+QColor SvgTextLabel::textColor() const
+{
+ KoColorBackground *bg = dynamic_cast<KoColorBackground*>(d->shape->background().data());
+ if (bg) {
+ return bg->color();
+ }
+ return Qt::black;
+}
+
+QStringList SvgTextLabel::openTypeFeatures() const
+{
+ return d->openTypeFeatures;
+}
+
+QString SvgTextLabel::text() const
+{
+ return d->shape->plainText();
+}
+
+int SvgTextLabel::padding() const
+{
+ return d->padding;
+}
+
+void SvgTextLabel::setFontFamilies(QStringList fontFamilies)
+{
+ KoSvgTextProperties props = d->shape->textProperties();
+ QStringList current = props.property(KoSvgTextProperties::FontFamiliesId).value<QStringList>();
+ if (current == fontFamilies)
+ return;
+
+ props.setProperty(KoSvgTextProperties::FontFamiliesId, fontFamilies);
+ d->shape->setPropertiesAtPos(-1, props);
+ emit fontFamiliesChanged(fontFamilies);
+}
+
+void SvgTextLabel::setFontWeight(int fontWeight)
+{
+ KoSvgTextProperties props = d->shape->textProperties();
+ int current = props.property(KoSvgTextProperties::FontWeightId).toInt();
+ if (current == fontWeight)
+ return;
+
+ props.setProperty(KoSvgTextProperties::FontWeightId, fontWeight);
+ d->shape->setPropertiesAtPos(-1, props);
+ emit fontWeightChanged(fontWeight);
+}
+
+void SvgTextLabel::setFontStyle(QFont::Style fontStyle)
+{
+ KoSvgTextProperties props = d->shape->textProperties();
+ QFont::Style current = props.property(KoSvgTextProperties::FontStyleId).value<QFont::Style>();
+ if (current == fontStyle)
+ return;
+
+ props.setProperty(KoSvgTextProperties::FontStyleId, fontStyle);
+ d->shape->setPropertiesAtPos(-1, props);
+ updateShape();
+ emit fontStyleChanged(fontStyle);
+}
+
+void SvgTextLabel::setFontSize(qreal fontSize)
+{
+ KoSvgTextProperties props = d->shape->textProperties();
+ qreal current = props.property(KoSvgTextProperties::FontSizeId).toReal();
+ if (qFuzzyCompare(current, fontSize))
+ return;
+
+ props.setProperty(KoSvgTextProperties::FontSizeId, fontSize);
+ d->shape->setPropertiesAtPos(-1, props);
+ emit fontSizeChanged(fontSize);
+}
+
+void SvgTextLabel::setTextColor(QColor textColor)
+{
+ KoSvgTextProperties props = d->shape->textProperties();
+ if (props.hasProperty(KoSvgTextProperties::FillId)) {
+ KoColorBackground *bg = dynamic_cast<KoColorBackground*>(d->shape->background().data());
+ if (bg) {
+ if (bg->color() == textColor)
+ return;
+ }
+ }
+
+ d->shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(textColor)));
+ emit textColorChanged(textColor);
+}
+
+void SvgTextLabel::setOpenTypeFeatures(QStringList openTypeFeatures)
+{
+ if (d->openTypeFeatures == openTypeFeatures)
+ return;
+
+ d->openTypeFeatures = openTypeFeatures;
+ emit openTypeFeaturesChanged(d->openTypeFeatures);
+}
+
+void SvgTextLabel::setText(QString text)
+{
+ if (d->shape->plainText() == text)
+ return;
+
+ int length = d->shape->plainText().size();
+ int start = 0;
+ d->shape->removeText(start, length);
+ d->shape->insertText(0, text);
+ updateShape();
+ emit textChanged(d->shape->plainText());
+}
+
+void SvgTextLabel::setPadding(int padding)
+{
+ if (d->padding == padding)
+ return;
+
+ d->padding = padding;
+ emit paddingChanged(d->padding);
+}
+
+void SvgTextLabel::updateShape()
+{
+ d->shape->relayout();
+ setImplicitSize(d->shape->boundingRect().width(), d->shape->boundingRect().height());
+ update();
+}
diff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h
new file mode 100644
index 0000000000..fb22b461f8
--- /dev/null
+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h
@@ -0,0 +1,97 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef SVGTEXTLABEL_H
+#define SVGTEXTLABEL_H
+
+#include <QQuickPaintedItem>
+#include <QFont>
+#include <QScopedPointer>
+
+/**
+ * SvgTextLabel makes it possible to paint a KoSvgTextShape as a QML item.
+ * It will position and scale the text centered in the bounds of the items.
+ *
+ * This is nowhere near as fast as a regular Text item, as Qt has a glyph
+ * cache, while Krita's system paints paths directly.
+ */
+
+class SvgTextLabel : public QQuickPaintedItem
+{
+ Q_OBJECT
+ Q_PROPERTY(QStringList fontFamilies READ fontFamilies WRITE setFontFamilies NOTIFY fontFamiliesChanged)
+ Q_PROPERTY(int fontWeight READ fontWeight WRITE setFontWeight NOTIFY fontWeightChanged)
+ Q_PROPERTY(QFont::Style fontStyle READ fontStyle WRITE setFontStyle NOTIFY fontStyleChanged)
+ Q_PROPERTY(qreal fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
+ Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
+ Q_PROPERTY(QStringList openTypeFeatures READ openTypeFeatures WRITE setOpenTypeFeatures NOTIFY openTypeFeaturesChanged)
+ Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
+ Q_PROPERTY(int padding READ padding WRITE setPadding NOTIFY paddingChanged)
+
+public:
+ SvgTextLabel(QQuickItem *parent = nullptr);
+ ~SvgTextLabel();
+
+ void paint(QPainter *painter) override;
+
+ QStringList fontFamilies() const;
+
+ int fontWeight() const;
+
+ QFont::Style fontStyle() const;
+
+ qreal fontSize() const;
+
+ QColor textColor() const;
+
+ QStringList openTypeFeatures() const;
+
+ QString text() const;
+
+ int padding() const;
+
+public Q_SLOTS:
+ void setFontFamilies(QStringList fontFamilies);
+
+ void setFontWeight(int fontWeight);
+
+ void setFontStyle(QFont::Style fontStyle);
+
+ void setFontSize(qreal fontSize);
+
+ void setTextColor(QColor textColor);
+
+ void setOpenTypeFeatures(QStringList openTypeFeatures);
+
+ void setText(QString text);
+
+ void setPadding(int padding);
+
+Q_SIGNALS:
+ void fontFamiliesChanged(QStringList);
+
+ void fontWeightChanged(int fontWeight);
+
+ void fontStyleChanged(QFont::Style fontStyle);
+
+ void fontSizeChanged(qreal fontSize);
+
+ void textColorChanged(QColor textColor);
+
+ void openTypeFeaturesChanged(QStringList openTypeFeatures);
+
+ void textChanged(QString text);
+
+ void paddingChanged(int padding);
+
+private:
+
+ void updateShape();
+
+ struct Private;
+ const QScopedPointer<Private> d;
+};
+
+#endif // SVGTEXTLABEL_H