From 5335cc4a5a73bbd6d717989b4257660b92000fb6 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 15 Oct 2021 15:11:41 +0200 Subject: Fix cursor positioning on bidi boundaries When the cursor is positioned between to script items that have different writing directions, prioritise the script item that has the same direction as the paragraph (i.e. the QTextEngine) when deciding where and how to display the cursor. If visual cursor movement is enabled, the behavior is unchanged. As a drive-by, clean up coding style and avoid shadowing of function- local variables. Task-number: QTBUG-88529 Pick-to: 6.2 Change-Id: I15227b10b1469d9caf1235b00e4d6f9f64a8b510 Reviewed-by: Lars Knoll Reviewed-by: Qt CI Bot --- .../auto/gui/text/qtextlayout/tst_qtextlayout.cpp | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'tests') diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp index a4c848c1ec..32a0374fcd 100644 --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp @@ -74,6 +74,8 @@ private slots: void cursorToXForTrailingSpaces_data(); void cursorToXForTrailingSpaces(); void cursorToXInvalidInput(); + void cursorToXForBidiBoundaries_data(); + void cursorToXForBidiBoundaries(); void horizontalAlignment_data(); void horizontalAlignment(); @@ -738,6 +740,58 @@ void tst_QTextLayout::cursorToXInvalidInput() QCOMPARE(cursorPos, 3); } +void tst_QTextLayout::cursorToXForBidiBoundaries_data() +{ + QTest::addColumn("textDirection"); + QTest::addColumn("text"); + QTest::addColumn("cursorPosition"); + QTest::addColumn("expectedX"); + + QTest::addRow("LTR, abcشزذabc, 0") << Qt::LeftToRight << "abcشزذabc" + << 0 << 0; + QTest::addRow("RTL, abcشزذabc, 9") << Qt::RightToLeft << "abcشزذabc" + << 9 << TESTFONT_SIZE * 3; + QTest::addRow("LTR, abcشزذabc, 3") << Qt::LeftToRight << "abcشزذabc" + << 0 << 0; + QTest::addRow("RTL, abcشزذabc, 6") << Qt::RightToLeft << "abcشزذabc" + << 9 << TESTFONT_SIZE * 3; + + QTest::addRow("LTR, شزذabcشزذ, 0") << Qt::LeftToRight << "شزذabcشزذ" + << 0 << TESTFONT_SIZE * 2; + QTest::addRow("RTL, شزذabcشزذ, 9") << Qt::RightToLeft << "شزذabcشزذ" + << 9 << 0; + QTest::addRow("LTR, شزذabcشزذ, 3") << Qt::LeftToRight << "شزذabcشزذ" + << 3 << TESTFONT_SIZE * 2; + QTest::addRow("RTL, شزذabcشزذ, 3") << Qt::RightToLeft << "شزذabcشزذ" + << 3 << TESTFONT_SIZE * 5; + QTest::addRow("LTR, شزذabcشزذ, 6") << Qt::LeftToRight << "شزذabcشزذ" + << 6 << TESTFONT_SIZE * 5; + QTest::addRow("RTL, شزذabcشزذ, 6") << Qt::RightToLeft << "شزذabcشزذ" + << 6 << TESTFONT_SIZE * 2; +} + +void tst_QTextLayout::cursorToXForBidiBoundaries() +{ + QFETCH(Qt::LayoutDirection, textDirection); + QFETCH(QString, text); + QFETCH(int, cursorPosition); + QFETCH(int, expectedX); + + QTextOption option; + option.setTextDirection(textDirection); + + QTextLayout layout(text, testFont); + layout.setTextOption(option); + layout.beginLayout(); + + QTextLine line = layout.createLine(); + line.setLineWidth(0x10000); + + QCOMPARE(line.cursorToX(cursorPosition), expectedX); + + layout.endLayout(); +} + void tst_QTextLayout::horizontalAlignment_data() { qreal width = TESTFONT_SIZE * 4; @@ -1146,6 +1200,10 @@ void tst_QTextLayout::xToCursorForBidiEnds_data() << 0 << 6; QTest::addRow("RTL, شزذabc") << Qt::RightToLeft << "شزذabc" << 6 << 0; + QTest::addRow("LTR, شزذ123") << Qt::LeftToRight << "شزذ123" + << 0 << 6; + QTest::addRow("RTL, شزذ123") << Qt::RightToLeft << "شزذ123" + << 6 << 0; QTest::addRow("LTR, abcشزذabc") << Qt::LeftToRight << "abcشزذabc" << 0 << 9; -- cgit v1.2.3