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

Add SVGTextLabel to OpenTypeFeature dropdown.

Wolthera van Hövell tot Westerflier5c528f86b5ab
diff --git a/plugins/dockers/textproperties/qml/OpenTypeFeatureDelegate.qml b/plugins/dockers/textproperties/qml/OpenTypeFeatureDelegate.qml
index de6f878131..861d88db7d 100644
--- a/plugins/dockers/textproperties/qml/OpenTypeFeatureDelegate.qml
+++ b/plugins/dockers/textproperties/qml/OpenTypeFeatureDelegate.qml
@@ -7,23 +7,35 @@ import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import org.krita.flake.text 1.0
+import org.krita.components 1.0
Control {
id: root;
height: visible? implicitHeight: 0;
implicitHeight: layout.height + padding*2;
+ clip: true;
property string display;
property string tag;
property string sample;
property string toolTip;
+ property int featureValue: 0;
+
+ property var fontFamilies: [];
+ property double fontSize: 12.0;
+ property double fontWeight: 400;
+ property double fontWidth: 100;
+ property int fontStyle: 0;
+ property double fontSlant: 0.0;
+ property var fontAxesValues: ({});
+ property string language: "";
property bool enableMouseEvents: true;
property alias containsMouse: mouseArea.containsMouse;
background: Rectangle {
- color: containsMouse? sysPalette.highlight: "transparent";
+ color: containsMouse? palette.highlight: "transparent";
}
signal featureClicked (QtObject mouse);
@@ -54,6 +66,7 @@ Control {
Layout.rightMargin: layout.spacing;
Layout.bottomMargin: layout.spacing;
Label {
+ id: displayLabel;
text: root.display;
elide: Text.ElideRight;
Layout.fillWidth: true;
@@ -61,12 +74,28 @@ Control {
color: root.containsMouse? palette.highlightedText: palette.text;
}
- Label {
+ SvgTextLabel {
text: root.sample;
- elide: Text.ElideRight;
- Layout.fillWidth: true;
- Layout.preferredHeight: implicitHeight
- color: root.containsMouse? palette.highlightedText: palette.text;
+ id: svgTextLabel;
+ Layout.preferredHeight: displayLabel.height * 2;
+ Layout.preferredWidth: (displayLabel.height * 2) * (implicitWidth/implicitHeight);
+ Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter;
+ textColor: root.containsMouse? root.palette.highlightedText: root.palette.text;
+ fillColor: "transparent";
+ fontFamilies: root.fontFamilies;
+ fontSize: root.fontSize;
+ fontStyle: root.fontStyle;
+ fontWeight: root.fontWeight;
+ fontWidth: root.fontWidth;
+ fontSlant: root.fontSlant;
+ fontAxesValues: root.fontAxesValues;
+ language: root.language;
+
+ Component.onCompleted: {
+ var newFeatures = {};
+ newFeatures[root.tag] = root.featureValue;
+ openTypeFeatures = newFeatures;
+ }
}
}
}
diff --git a/plugins/dockers/textproperties/qml/OpenTypeFeatureSettings.qml b/plugins/dockers/textproperties/qml/OpenTypeFeatureSettings.qml
index b22b78b11f..1648fe3762 100644
--- a/plugins/dockers/textproperties/qml/OpenTypeFeatureSettings.qml
+++ b/plugins/dockers/textproperties/qml/OpenTypeFeatureSettings.qml
@@ -102,6 +102,13 @@ TextPropertyBase {
toolTip: activeFeatureDelegate.toolTip;
sample: activeFeatureDelegate.sample;
tag: activeFeatureDelegate.tag;
+ fontFamilies: properties.fontFamilies;
+ fontWeight: properties.fontWeight;
+ fontStyle: properties.fontStyle.style;
+ fontSlant: properties.fontStyle.value;
+ fontWidth: properties.fontWidth;
+ fontAxesValues: properties.axisValues;
+ featureValue: activeFeatureControl.currentIndex;
enableMouseEvents: false;
}
@@ -115,6 +122,13 @@ TextPropertyBase {
toolTip: display;
sample: activeFeatureDelegate.sample;
tag: activeFeatureDelegate.tag;
+ fontFamilies: properties.fontFamilies;
+ fontWeight: properties.fontWeight;
+ fontStyle: properties.fontStyle.style;
+ fontSlant: properties.fontStyle.value;
+ fontWidth: properties.fontWidth;
+ fontAxesValues: properties.axisValues;
+ featureValue: modelData.value;
onFeatureClicked: {
activeFeatureControl.currentIndex = index;
activeFeatureControl.popup.close();
@@ -168,6 +182,13 @@ TextPropertyBase {
tag: model.tag;
width: cmbAvailableFeatures.width;
visible: model.available;
+ fontFamilies: properties.fontFamilies;
+ fontWeight: properties.fontWeight;
+ fontStyle: properties.fontStyle.style;
+ fontSlant: properties.fontStyle.value;
+ fontWidth: properties.fontWidth;
+ fontAxesValues: properties.axisValues;
+ featureValue: 0;
onFeatureClicked: (mouse) => {
cmbAvailableFeatures.currentIndex = index;
fontFeatureModel.addFeature(tag);
diff --git a/qmlmodules/components/KritaQmlComponentsPlugin.cpp b/qmlmodules/components/KritaQmlComponentsPlugin.cpp
index 5b690dad3e..4de53c30c1 100644
--- a/qmlmodules/components/KritaQmlComponentsPlugin.cpp
+++ b/qmlmodules/components/KritaQmlComponentsPlugin.cpp
@@ -8,6 +8,7 @@
#include <QObject>
#include <KisStaticInitializer.h>
+#include <SvgTextLabel.h>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#error "This plugin implementation is to be used with Qt5 only!\n"
@@ -31,7 +32,7 @@ void KritaQmlComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *
void KritaQmlComponentsPlugin::registerTypes(const char *uri)
{
- Q_UNUSED(uri);
+ qmlRegisterType<SvgTextLabel>(uri, 1, 0, "SvgTextLabel");
}
#include "KritaQmlComponentsPlugin.moc"
diff --git a/qmlmodules/components/SvgTextLabel.cpp b/qmlmodules/components/SvgTextLabel.cpp
index 043098e091..c674ffccfd 100644
--- a/qmlmodules/components/SvgTextLabel.cpp
+++ b/qmlmodules/components/SvgTextLabel.cpp
@@ -32,8 +32,11 @@ struct Q_DECL_HIDDEN SvgTextLabel::Private {
QScopedPointer<KoShapePainter> shapePainter;
KoSvgTextProperties props;
- bool textHasChanged = false;
- int padding = 2;
+
+ int topPadding = 2;
+ int bottomPadding = 2;
+ int leftPadding = 2;
+ int rightPadding = 2;
};
SvgTextLabel::SvgTextLabel(QQuickItem *parent)
@@ -41,8 +44,8 @@ SvgTextLabel::SvgTextLabel(QQuickItem *parent)
, d(new Private())
{
setImplicitSize(10, 10);
- setOpaquePainting(true);
setAntialiasing(true);
+ setOpaquePainting(true);
}
SvgTextLabel::~SvgTextLabel()
@@ -54,12 +57,12 @@ void SvgTextLabel::paint(QPainter *painter)
// the QML item must be finalized before paint() is called
KIS_SAFE_ASSERT_RECOVER_RETURN(d->shapePainter);
+ if (width() == 0 || height() == 0) return;
if (d->shape->textProperties() != d->props) {
updateShape();
}
if (!painter->isActive()) return;
painter->save();
- painter->fillRect(0, 0, width(), height(), fillColor());
if (d->shape->boundingRect().isEmpty()) {
painter->restore();
return;
@@ -68,9 +71,17 @@ void SvgTextLabel::paint(QPainter *painter)
QRectF bbox = d->shape->selectionBoxes(0, d->shape->posForIndex(d->shape->plainText().size())).boundingRect();
bbox = bbox.isEmpty()? d->shape->boundingRect(): bbox;
+ // Scale padding so that it is relative to the width and height.
+ const double newWidth = width()-(d->leftPadding+d->rightPadding);
+ const double newLeftPadding = (d->leftPadding / newWidth) * bbox.width();
+ const double newRightPadding = (d->rightPadding / newWidth) * bbox.width();
+ const double newHeight = height()-(d->topPadding+d->bottomPadding);
+ const double newTopPadding = (d->topPadding / newHeight) * bbox.height();
+ const double newBottomPadding = (d->bottomPadding / newHeight) * bbox.height();
+
d->shapePainter->paint(*painter,
- QRectF(d->padding, d->padding, width()-(d->padding*2), height()-(d->padding*2)).toAlignedRect(),
- bbox);
+ QRectF(0, 0, width(), height()).toAlignedRect(),
+ bbox.adjusted(-newLeftPadding, -newTopPadding, newRightPadding, newBottomPadding));
painter->restore();
}
@@ -131,7 +142,7 @@ QString SvgTextLabel::text() const
int SvgTextLabel::padding() const
{
- return d->padding;
+ return (d->leftPadding+d->rightPadding+d->topPadding+d->bottomPadding)/4;
}
QString SvgTextLabel::language() const
@@ -227,11 +238,10 @@ void SvgTextLabel::setTextColor(QColor textColor)
d->shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(textColor)));
d->props.setProperty(KoSvgTextProperties::FillId, d->shape->textProperties().property(KoSvgTextProperties::FillId));
- if (this->window()) {
- const qreal pixelRatio = this->window()->effectiveDevicePixelRatio();
- d->shape->setResolution(pixelRatio*72, pixelRatio*72);
- }
emit textColorChanged(textColor);
+ if (!opaquePainting() && isComponentComplete()) {
+ update(boundingRect().toAlignedRect());
+ }
}
void SvgTextLabel::setOpenTypeFeatures(QVariantMap openTypeFeatures)
@@ -258,11 +268,17 @@ void SvgTextLabel::setText(QString text)
void SvgTextLabel::setPadding(int padding)
{
- if (d->padding == padding)
+ const int currentPadding = (d->leftPadding+d->rightPadding+d->topPadding+d->bottomPadding)/4;
+ if (currentPadding == padding)
return;
- d->padding = padding;
- emit paddingChanged(d->padding);
+ d->leftPadding = padding;
+ d->rightPadding = padding;
+ d->topPadding = padding;
+ d->bottomPadding = padding;
+ setImplicitSize(d->shape->boundingRect().width() + d->leftPadding + d->rightPadding,
+ d->shape->boundingRect().height() + d->topPadding + d->bottomPadding);
+ emit paddingChanged(padding);
}
void SvgTextLabel::setLanguage(QString language)
@@ -279,17 +295,25 @@ void SvgTextLabel::componentComplete()
{
QQuickPaintedItem::componentComplete();
+ if (this->window()) {
+ const qreal pixelRatio = this->window()->effectiveDevicePixelRatio();
+ d->shape->setResolution(pixelRatio*72, pixelRatio*72);
+ }
+ setOpaquePainting(fillColor().alpha() == 255);
updateShape();
if (!d->shapePainter) {
d->shapePainter.reset(new KoShapePainter());
- d->shapePainter->setShapes({d->shape.data()});
+ if (!d->shape->boundingRect().isEmpty()) {
+ d->shapePainter->setShapes({d->shape.data()});
+ }
}
-
}
void SvgTextLabel::updateShape()
{
d->shape->setPropertiesAtPos(-1, d->props);
d->shape->relayout();
- setImplicitSize(d->shape->boundingRect().width(), d->shape->boundingRect().height());
+ const QRectF bbox = d->shape->selectionBoxes(0, d->shape->posForIndex(d->shape->plainText().size())).boundingRect();
+ setImplicitSize(bbox.width() + d->leftPadding + d->rightPadding,
+ bbox.height() + d->topPadding + d->bottomPadding);
}