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 text shape not handling whitespace chars properly

Dmitry Kazakoved038efcbe4f
diff --git a/plugins/tools/svgtexttool/SvgTextInsertCommand.cpp b/plugins/tools/svgtexttool/SvgTextInsertCommand.cpp
index 4e48dd6dfe..ef75c49a63 100644
--- a/plugins/tools/svgtexttool/SvgTextInsertCommand.cpp
+++ b/plugins/tools/svgtexttool/SvgTextInsertCommand.cpp
@@ -20,19 +20,24 @@ SvgTextInsertCommand::SvgTextInsertCommand(KoSvgTextShape *shape, int pos, int a
{
setText(kundo2_i18n("Insert Text"));
+ m_text = filterInputUnicodeString(text);
+}
+
+QString SvgTextInsertCommand::filterInputUnicodeString(QString text)
+{
QRegularExpression exp;
// This replaces...
// - carriage return
- // - linefeed-carriage return
// - carriage return-linefeed
// - line separator
// - paragraph separator
// - vertical tab/line tab
// with a single linefeed to avoid them from being added to the textShape.
- exp.setPattern("[\\r|\\r\\n|\\x2029|\\x2028\\x000b]");
-
+ // NOTE: \r\n comes **before** \r to ensure that full match
+ // is preferred to the partial match!
+ exp.setPattern("(\\r\\n|\\r|\\x{2029}|\\x{2028}|\\x{000b})");
text.replace(exp, "\n");
- m_text = text;
+ return text;
}
void SvgTextInsertCommand::redo()
diff --git a/plugins/tools/svgtexttool/SvgTextInsertCommand.h b/plugins/tools/svgtexttool/SvgTextInsertCommand.h
index 55aa429fce..1918ebdec3 100644
--- a/plugins/tools/svgtexttool/SvgTextInsertCommand.h
+++ b/plugins/tools/svgtexttool/SvgTextInsertCommand.h
@@ -23,6 +23,8 @@ public:
int id() const override;
bool mergeWith(const KUndo2Command *other) override;
+ static QString filterInputUnicodeString(QString text);
+
private:
KoSvgTextShape *m_shape;
diff --git a/plugins/tools/svgtexttool/tests/SvgTextCursorTest.cpp b/plugins/tools/svgtexttool/tests/SvgTextCursorTest.cpp
index adeae2d5a0..b0eaeec47b 100644
--- a/plugins/tools/svgtexttool/tests/SvgTextCursorTest.cpp
+++ b/plugins/tools/svgtexttool/tests/SvgTextCursorTest.cpp
@@ -231,6 +231,29 @@ void SvgTextCursorTest::test_ttb_lr()
QCOMPARE(cursor.getPos(), result);
}
+void SvgTextCursorTest::test_filter_control_chars_in_command_data()
+{
+ QTest::addColumn<QString>("srcText");
+ QTest::addColumn<QString>("expectedFilteredText");
+
+ QTest::addRow("cr") << "test\rtext" << "test\ntext";
+ QTest::addRow("crlf") << "test\r\ntext" << "test\ntext";
+ QTest::addRow("Unicode Paragraph Separator (U+2029)") << QString::fromUtf16(u"test\x2029text") << "test\ntext";
+ QTest::addRow("Unicode Line Separator (U+2028)") << QString::fromUtf16(u"test\x2028text") << "test\ntext";
+ QTest::addRow("Unicode Vertical Tab (U+000B)") << QString::fromUtf16(u"test\x000Btext") << "test\ntext";
+ QTest::addRow("Whitespace is preserved") << "test text" << "test text";
+ QTest::addRow("Multiwhitespace is preserved") << "test text" << "test text";
+}
+
+void SvgTextCursorTest::test_filter_control_chars_in_command()
+{
+ QFETCH(QString, srcText);
+ QFETCH(QString, expectedFilteredText);
+
+ const QString result = SvgTextInsertCommand::filterInputUnicodeString(srcText);
+ QCOMPARE(result, expectedFilteredText);
+}
+
// Test basic text insertion in a horizontal ltr wrapped text;
void SvgTextCursorTest::test_text_insert_command()
{
diff --git a/plugins/tools/svgtexttool/tests/SvgTextCursorTest.h b/plugins/tools/svgtexttool/tests/SvgTextCursorTest.h
index 68e7ad5f7a..02f7f46428 100644
--- a/plugins/tools/svgtexttool/tests/SvgTextCursorTest.h
+++ b/plugins/tools/svgtexttool/tests/SvgTextCursorTest.h
@@ -26,6 +26,9 @@ private Q_SLOTS:
void test_ttb_lr_data();
void test_ttb_lr();
+ void test_filter_control_chars_in_command_data();
+ void test_filter_control_chars_in_command();
+
void test_text_insert_command();
void test_text_remove_command();