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

Fix TestSvgTextShape::testSetTextOnShape()

Dmitry Kazakov3d844ebed99d
diff --git a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
index ecd6527274..c9881d9e27 100644
--- a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
+++ b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
@@ -4,6 +4,9 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "KoSvgTextAddRemoveShapeCommands.h"
+#include "kis_assert.h"
+#include <optional>
+#include "kis_debug.h"
#include <KoSvgTextShape.h>
#include <KoShapeContainer.h>
@@ -20,7 +23,7 @@ struct KoSvgTextAddRemoveShapeCommandImpl::Private {
~Private() {}
KoSvgTextShape *textShape = nullptr;
KoShape* shape = nullptr;
- KoShapeContainer *originalShapeParent = nullptr;
+ std::optional<KoShapeContainer*> originalShapeParent;
KoSvgTextShapeMementoSP memento;
KoSvgTextAddRemoveShapeCommandImpl::ContourType type;
bool removeCommand;
@@ -30,7 +33,20 @@ KoSvgTextAddRemoveShapeCommandImpl::KoSvgTextAddRemoveShapeCommandImpl(KoSvgText
: KisCommandUtils::FlipFlopCommand(state, parent)
, d(new Private(textShape, shape))
{
+ /**
+ * 1) The shapes should belong to the same parent or have no parent at all.
+ * 2) The parent of the text shape is always preserved (null or non-null)
+ * 3) The parent of the contour shape is dropped, since it is put into
+ * a virtual group created by the text shape
+ */
+ KIS_SAFE_ASSERT_RECOVER_NOOP(!textShape->parent() || !shape->parent() || textShape->parent() == shape->parent());
+
d->removeCommand = state == FINALIZING;
+
+ if (!d->removeCommand) {
+ d->originalShapeParent = shape->parent();
+ }
+
if (type == Unknown) {
if (d->textShape->shapesInside().contains(shape)) {
d->type = Inside;
@@ -57,15 +73,20 @@ void KoSvgTextAddRemoveShapeCommandImpl::partB()
QRectF updateRectText = d->textShape->boundingRect();
QRectF updateRectShape = d->shape->boundingRect();
- // Because "applyAbsoluteTransformation" applies ontop of the local transform, we want to get the minimal transfor difference.
- QTransform removeFromAbsolute = d->originalShapeParent? d->originalShapeParent->absoluteTransformation() * d->shape->transformation(): d->shape->transformation();
- QTransform absoluteTf = (d->shape->absoluteTransformation()*removeFromAbsolute.inverted());
+ KoShapeContainer *newParent = d->originalShapeParent.has_value() ? d->originalShapeParent.value() : d->textShape->parent();
+ const QTransform newParentTransform = newParent ? newParent->absoluteTransformation() : QTransform();
+ const QTransform absoluteTf = newParentTransform.inverted() * d->textShape->absoluteTransformation();
d->textShape->removeShapesFromContours({d->shape}, true);
+ // by this time d->shape->parent() is set to nullptr
+
+ if (newParent) {
+ // set new parent if exists
+ d->shape->setParent(newParent);
+ }
if (!d->removeCommand) {
d->textShape->setMemento(d->memento);
- d->shape->setParent(d->originalShapeParent);
} else {
d->textShape->relayout();
}
@@ -81,10 +102,10 @@ void KoSvgTextAddRemoveShapeCommandImpl::partA()
{
QRectF updateRect = d->textShape->boundingRect();
updateRect |= d->shape->boundingRect();
- if (!d->removeCommand) {
- d->originalShapeParent = d->shape->parent();
- }
- QTransform absoluteTf = (d->shape->absoluteTransformation()*d->shape->transformation().inverted()) * d->textShape->absoluteTransformation().inverted();
+
+ KoShapeContainer *oldParent = d->shape->parent();
+ const QTransform oldParentTransform = oldParent ? oldParent->absoluteTransformation() : QTransform();
+ const QTransform absoluteTf = d->textShape->absoluteTransformation().inverted() * oldParentTransform;
if (d->type == Inside) {
d->textShape->addShapeContours({d->shape}, true);
@@ -118,7 +139,7 @@ KoSvgTextAddShapeCommand::~KoSvgTextAddShapeCommand()
KoSvgTextRemoveShapeCommand::KoSvgTextRemoveShapeCommand(KoSvgTextShape *textShape, KoShape *shape, KUndo2Command *parentCommand)
: KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, Unknown, FINALIZING, parentCommand)
{
-
+ // the \p shape will be ungrouped into the parent of \p textShape
}
KoSvgTextRemoveShapeCommand::~KoSvgTextRemoveShapeCommand()
diff --git a/libs/flake/tests/TestSvgTextShape.cpp b/libs/flake/tests/TestSvgTextShape.cpp
index 504e1366d6..03ef3fdde2 100644
--- a/libs/flake/tests/TestSvgTextShape.cpp
+++ b/libs/flake/tests/TestSvgTextShape.cpp
@@ -6,6 +6,11 @@
#include "TestSvgTextShape.h"
+#include <kis_algebra_2d.h>
+#include <kis_debug.h>
+
+#include <QPainter>
+
#include <kistest.h>
#include <KoSvgTextShape.h>
#include <KoShape.h>
@@ -14,6 +19,8 @@
#include <KoShapeManager.h>
#include <KoShapeRegistry.h>
+#include "KoShapePainter.h"
+#include <KoShapeStroke.h>
#include <commands/KoSvgChangeTextContoursCommand.h>
#include <commands/KoSvgTextAddRemoveShapeCommands.h>
@@ -21,6 +28,7 @@
#include <commands/KoSvgTextReorderShapeInsideCommand.h>
#include <commands/KoShapeSizeCommand.h>
+
// Quick function to get a rectangle.
KoShape *createRectangles() {
KoShapeFactoryBase *base = KoShapeRegistry::instance()->get("RectangleShape");
@@ -35,105 +43,264 @@ KoPathShape *createPolygon() {
p->lineTo(QPointF(100, 0));
p->lineTo(QPointF(0,0));
p->close();
+ KoShapeStrokeModelSP blackStroke(new KoShapeStroke(1.0, QColor(Qt::black)));
+ p->setStroke(blackStroke);
return p;
}
+namespace detail {
+Q_NAMESPACE
+
+enum TransformComponent
+{
+ None = 0x0,
+ Translate = 0x1,
+ Scale = 0x2,
+ Rotate = 0x4,
+ Shear = 0x8,
+ Project = 0x10
+};
+Q_ENUM_NS(TransformComponent)
+
+}
+
+using detail::TransformComponent;
+
+Q_DECLARE_FLAGS(TransformComponents, TransformComponent)
+Q_DECLARE_OPERATORS_FOR_FLAGS(TransformComponents)
+Q_DECLARE_METATYPE(TransformComponents)
+
+TransformComponents componentsForTransform(const QTransform &t) {
+ TransformComponents result = TransformComponent::None;
+
+ KisAlgebra2D::DecomposedMatrix m(t);
+
+ result.setFlag(TransformComponent::Translate,
+ !qFuzzyIsNull(m.dx) || !qFuzzyIsNull(m.dy));
+
+ result.setFlag(TransformComponent::Scale,
+ !qFuzzyCompare(m.scaleX, 1.0) || !qFuzzyCompare(m.scaleY, 1.0));
+
+ result.setFlag(TransformComponent::Shear,
+ !qFuzzyIsNull(m.shearXY));
+
+ result.setFlag(TransformComponent::Rotate,
+ !qFuzzyIsNull(m.angle));
+
+ result.setFlag(TransformComponent::Project,
+ !qFuzzyIsNull(m.proj[0]) || !qFuzzyIsNull(m.proj[1]) || !qFuzzyCompare(m.proj[2], 1.0));
+
+ return result;
+}
+
+
void TestSvgTextShape::testSetTextOnShape_data()
{
+ auto rotateAroundPoint = [] (qreal deg, const QPointF center) {
+ return QTransform::fromTranslate(-center.x(), -center.y()) * QTransform().rotate(deg) * QTransform::fromTranslate(center.x(), center.y());
+ };
+
QTest::addColumn<QTransform>("textTransform");
- QTest::addColumn<bool>("textHasParent");
QTest::addColumn<int>("contourCount");
QTest::addColumn<QTransform>("contourTransform");
+ QTest::addColumn<bool>("textHasParent");
QTest::addColumn<bool>("contourHasParent");
+ QTest::addColumn<TransformComponents>("expectedTextTransformComponents");
+
+ // TODO: test origin of the text shape to change to the contour shapes
+
+ for (bool textHasParent : {false, true}) {
+ for (bool contourHasParent : {false, true}) {
+ const char *textParentPrefix =
+ textHasParent ? "[text:parent]" : "[text:no-parent]";
+ const char *contourParentPrefix =
+ contourHasParent ? "[contour:parent]" : "[contour:no-parent]";
+
+ QTest::addRow("%s%s text and 1 contour", textParentPrefix, contourParentPrefix)
+ << QTransform::fromTranslate(10, 20) << 1
+ << QTransform::fromTranslate(5, 5)
+ << textHasParent
+ << contourHasParent
+ << TransformComponents(TransformComponent::Translate);
+
+ QTest::addRow("%s%s text and 1 contour (scaled)", textParentPrefix, contourParentPrefix)
+ << QTransform::fromTranslate(10, 20) << 1
+ << QTransform::fromScale(1.0, 1.5) * QTransform::fromTranslate(5, 5)
+ << textHasParent
+ << contourHasParent
+ << TransformComponents(TransformComponent::Translate);
+
+ QTest::addRow("%s%s text and 1 contour (rotated)", textParentPrefix, contourParentPrefix)
+ << QTransform::fromTranslate(10, 20) << 1
+ << rotateAroundPoint(30, QPointF(50, 50)) * QTransform::fromTranslate(20, 25)
+ << textHasParent
+ << contourHasParent
+ << TransformComponents(TransformComponent::Translate);
+
+ QTest::addRow("%s%s text (scaled) and 1 contour", textParentPrefix, contourParentPrefix)
+ << QTransform::fromScale(1.0, 1.5) * QTransform::fromTranslate(10, 20) << 1
+ << QTransform::fromTranslate(5, 5)
+ << textHasParent
+ << contourHasParent
+ << TransformComponents(TransformComponent::Translate | TransformComponent::Scale);
+
+ QTest::addRow("%s%s text (rotated) and 1 contour", textParentPrefix, contourParentPrefix)
+ << rotateAroundPoint(30, QPointF(100, -10)) * QTransform::fromTranslate(10, 80) << 1
+ << QTransform::fromTranslate(5, 5)
+ << textHasParent
+ << contourHasParent
+ << TransformComponents(TransformComponent::Translate | TransformComponent::Rotate);
+ }
+ }
- QTest::addRow("text and 1 contour") << QTransform() << false << 1 << QTransform() << false;
- QTest::addRow("text and 2 contours") << QTransform() << false << 2 << QTransform() << false;
- QTest::addRow("text and 1 contour + local transform") << QTransform() << false << 1 << QTransform::fromTranslate(20, 20) << false;
- QTest::addRow("text + local transform and 1 contour") << QTransform::fromTranslate(20, 20) << false << 1 << QTransform() << false;
- QTest::addRow("text + absolute transform and 1 contour") << QTransform() << true << 1 << QTransform() << false;
- QTest::addRow("text and 1 contour + absolute transform") << QTransform() << false << 1 << QTransform() << true;
+ // QTest::addRow("text and 2 contours") << QTransform() << false << 2 << QTransform() << false;
+ // QTest::addRow("text and 1 contour + local transform") << QTransform() << false << 1 << QTransform::fromTranslate(20, 20) << false;
+ // QTest::addRow("text + local transform and 1 contour") << QTransform::fromTranslate(20, 20) << false << 1 << QTransform() << false;
+ // QTest::addRow("text + absolute transform and 1 contour") << QTransform() << true << 1 << QTransform() << false;
+ // QTest::addRow("text and 1 contour + absolute transform") << QTransform() << false << 1 << QTransform() << true;
}
void TestSvgTextShape::testSetTextOnShape()
{
+ auto stripTestName = [] (QString name) {
+ name.replace(':', '_');
+ name.replace('[', '_');
+ name.replace(']', '_');
+ name.replace(' ', '_');
+ name.replace('-', '_');
+ name.remove('(');
+ name.remove(')');
+ return name;
+ };
+
+ auto paintShapes = [stripTestName] (KoShape *textShape, QList<KoShape*> contourShapes, const QString &testName) {
+ QImage image(QSize(300, 200), QImage::Format_ARGB32);
+ image.fill(0);
+ QPainter gc(&image);
+ gc.setClipRect(image.rect());
+
+ QList<KoShape*> paintedShapes;
+ if (textShape) {
+ paintedShapes << textShape;
+ }
+ paintedShapes << contourShapes;
+
+ KoShapePainter p;
+ p.setShapes(paintedShapes);
+ p.paint(gc, image.rect(), image.rect());
+ image.save(stripTestName(testName));
+ };
+
+ auto debugShapes = [] (KoShape *textShape, QList<KoShape*> contourShapes) {
+ qDebug() << ppVar(textShape->boundingRect());
+ qDebug() << ppVar(textShape->transformation());
+ for (int i = 0; i < contourShapes.size(); i++) {
+ KoShape *shape = contourShapes[i];
+ qDebug() << "contour" << i << ppVar(shape->boundingRect());
+ qDebug() << "contour" << i << ppVar(shape->absoluteOutlineRect());
+ qDebug() << "contour" << i << ppVar(shape->transformation());
+ }
+ };
+
+
+
KoSvgTextShape *textShape = new KoSvgTextShape();
textShape->insertText(0, "The quick brown fox jumps over the lazy dog.");
QList<KoShape*> shapes;
QFETCH(QTransform, textTransform);
- QFETCH(bool, textHasParent);
QFETCH(int, contourCount);
QFETCH(QTransform, contourTransform);
+ QFETCH(bool, textHasParent);
QFETCH(bool, contourHasParent);
+ QFETCH(TransformComponents, expectedTextTransformComponents);
- KoShapeGroup *contourParentGroup = nullptr;
- if (contourHasParent) {
- contourParentGroup = new KoShapeGroup();
- contourParentGroup->setTransformation(QTransform::fromTranslate(10, 10));
+ KoShapeGroup *commonParent = nullptr;
+ if (textHasParent || contourHasParent) {
+ commonParent = new KoShapeGroup();
+ commonParent->setTransformation(QTransform::fromTranslate(17, 23));
}
textShape->setTransformation(textTransform);
+ if (textHasParent) {
+ textShape->setParent(commonParent);
+ }
for (int i = 0; i < contourCount; i++) {
KoPathShape *p = createPolygon();
p->setTransformation(contourTransform);
- if (contourParentGroup) {
- p->setParent(contourParentGroup);
+ if (contourHasParent) {
+ p->setParent(commonParent);
}
shapes.append(p);
}
bool inside = true;
- if (textHasParent) {
- KoShapeGroup *parentGroup = new KoShapeGroup();
- parentGroup->setTransformation(QTransform::fromTranslate(10, 10));
- textShape->setParent(parentGroup);
- }
-
- KUndo2Command *parentCommand = new KUndo2Command();
+ QScopedPointer<KUndo2Command> parentCommand(new KUndo2Command());
- QRectF textOutlineRect = textShape->outlineRect();
- QRectF textBoundingRect = textShape->boundingRect();
+ const QRectF originalTextOutlineRect = textShape->outlineRect();
+ const QRectF originalTextBoundingRect = textShape->boundingRect();
+ const QRectF originalContourShapeBoundingRect = KoShape::boundingRect(shapes);
- QRectF shapeBoundingRect;
QList<KoShape*> clonedShapes;
Q_FOREACH(KoShape *shape, shapes) {
- shapeBoundingRect |= shape->boundingRect();
KoShape *clone = shape->cloneShape();
- if (contourParentGroup) {
- clone->setParent(contourParentGroup);
+ if (contourHasParent) {
+ clone->setParent(commonParent);
}
clonedShapes.append(clone);
- new KoSvgTextAddShapeCommand(textShape, clone, inside, parentCommand);
+ new KoSvgTextAddShapeCommand(textShape, clone, inside, parentCommand.data());
}
+ debugShapes(textShape, clonedShapes);
+ paintShapes(textShape, clonedShapes, QString("ddd_%1_00_initial.png").arg(QTest::currentDataTag()));
+
parentCommand->redo();
- QVERIFY(textShape->boundingRect() == shapeBoundingRect);
- QVERIFY(textShape->boundingRect() != textBoundingRect);
- QVERIFY(textShape->outlineRect() != textOutlineRect);
+ debugShapes(textShape, clonedShapes);
+ paintShapes(textShape, {}, QString("ddd_%1_10_redo.png").arg(QTest::currentDataTag()));
+
+ QCOMPARE(componentsForTransform(textShape->absoluteTransformation()), expectedTextTransformComponents);
+
+ QVERIFY(textShape->boundingRect() == originalContourShapeBoundingRect);
+ QVERIFY(textShape->boundingRect() != originalTextBoundingRect);
+ QVERIFY(textShape->outlineRect() != originalTextOutlineRect);
for (int i = 0; i< clonedShapes.size(); i++) {
KoShape *contour = clonedShapes.at(i);
KoShape *original = shapes.at(i);
- QVERIFY(contour->absoluteTransformation() == original->absoluteTransformation());
+ QVERIFY(KisAlgebra2D::fuzzyMatrixCompare(contour->absoluteTransformation(), original->absoluteTransformation(), 1e-3));
}
parentCommand->undo();
- QVERIFY(textShape->boundingRect() != shapeBoundingRect);
- QVERIFY(textShape->boundingRect() == textBoundingRect);
- QVERIFY(textShape->outlineRect() == textOutlineRect);
+
+ debugShapes(textShape, clonedShapes);
+ paintShapes(textShape, clonedShapes, QString("ddd_%1_20_undo.png").arg(QTest::currentDataTag()));
+
+ QVERIFY(textShape->boundingRect() != originalContourShapeBoundingRect);
+ QVERIFY(textShape->boundingRect() == originalTextBoundingRect);
+ QVERIFY(textShape->outlineRect() == originalTextOutlineRect);
for (int i = 0; i< clonedShapes.size(); i++) {
KoShape *contour = clonedShapes.at(i);
KoShape *original = shapes.at(i);
QVERIFY(contour->parent() == original->parent());
- QVERIFY(contour->transformation() == original->transformation());
- QVERIFY(contour->absoluteTransformation() == original->absoluteTransformation());
+ QVERIFY(KisAlgebra2D::fuzzyMatrixCompare(contour->transformation(), original->transformation(), 1e-3));
+ QVERIFY(KisAlgebra2D::fuzzyMatrixCompare(contour->absoluteTransformation(), original->absoluteTransformation(), 1e-3));
+ }
+
+ if (!textHasParent) {
+ delete textShape;
+ }
+ if (!contourHasParent) {
+ qDeleteAll(clonedShapes);
+ qDeleteAll(shapes);
}
- // Don't understand how to test textAfter transform with contour transform???
+ if (commonParent) {
+ delete commonParent;
+ }
}
void TestSvgTextShape::testRemoveShapeFromText()
@@ -314,3 +481,5 @@ void TestSvgTextShape::testReorderShapesInside()
}
KISTEST_MAIN(TestSvgTextShape)
+
+#include "TestSvgTextShape.moc"