qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,42 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qcomplextext Test:
#####################################################################
# Collect test data
list(APPEND test_data "data")
qt_internal_add_test(tst_qcomplextext
SOURCES
tst_qcomplextext.cpp
LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::GuiPrivate
TESTDATA ${test_data}
)
## Scopes:
#####################################################################
if(ANDROID)
# Resources:
set(android_testdata_resource_files
"data/BidiCharacterTest.txt"
"data/BidiTest.txt"
)
qt_internal_add_resource(tst_qcomplextext "android_testdata"
PREFIX
"/android_testdata"
FILES
${android_testdata_resource_files}
)
endif()
qt_internal_extend_target(tst_qcomplextext CONDITION builtin_testdata
DEFINES
BUILTIN_TESTDATA
)

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/android_testdata">
<file>data/BidiCharacterTest.txt</file>
<file>data/BidiTest.txt</file>
</qresource>
</RCC>

View File

@ -0,0 +1,142 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
struct LV {
const char *name;
const char *logical;
const char *visual;
int basicDir;
};
const LV logical_visual[] = {
{ "data0", "Hello", "Hello", QChar::DirL },
{ "data1", "\327\251\327\234\327\225\327\235", "\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data2", "Hello \327\251\327\234\327\225\327\235", "Hello \327\235\327\225\327\234\327\251", QChar::DirL },
{ "data3", "car is \327\236\327\233\327\225\327\240\327\231\327\252 \327\220\327\225\327\230\327\225 in hebrew", "car is \327\225\327\230\327\225\327\220 \327\252\327\231\327\240\327\225\327\233\327\236 in hebrew", QChar::DirL },
{ "data4", "\327\236\327\233\327\225\327\240\327\231\327\252 \327\224\327\231\327\220 the car \327\221\327\251\327\244\327\224 \327\224\327\220\327\240\327\222\327\234\327\231\327\252", "\327\252\327\231\327\234\327\222\327\240\327\220\327\224 \327\224\327\244\327\251\327\221 the car \327\220\327\231\327\224 \327\252\327\231\327\240\327\225\327\233\327\236", QChar::DirR },
{ "data5", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 123, 456, \327\221\327\241\327\223\327\250\" ", "he said \"\327\250\327\223\327\241\327\221 ,456 ,123 \327\224\327\225\327\225\327\251 \327\224\327\226\" ", QChar::DirL },
{ "data6", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 (123, 456), \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 ,(456 ,123) \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data7", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 123,456, \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 ,123,456 \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data8", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 ,(123,456) \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 (123,456), \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data9", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is 123, 456, ok\"", "\"it is 123, 456, ok\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data10", "<\327\233123>shalom</\327\233123>", "<123\327\233/>shalom<123\327\233>", QChar::DirR },
{ "data11", "<h123>\327\251\327\234\327\225\327\235</h123>", "<h123>\327\235\327\225\327\234\327\251</h123>", QChar::DirL },
{ "data12", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is a car!\" \327\225\327\220\327\226 \327\250\327\245", "\327\245\327\250 \327\226\327\220\327\225 \"!it is a car\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data13", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is a car!x\" \327\225\327\220\327\226 \327\250\327\245", "\327\245\327\250 \327\226\327\220\327\225 \"it is a car!x\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data14", "-2 \327\236\327\242\327\234\327\225\327\252 \327\226\327\224 \327\247\327\250", "\327\250\327\247 \327\224\327\226 \327\252\327\225\327\234\327\242\327\236 2-", QChar::DirR },
{ "data15", "-10% \327\251\327\231\327\240\327\225\327\231", "\327\231\327\225\327\240\327\231\327\251 10%-", QChar::DirR },
{ "data16", "\327\224\327\230\327\225\327\225\327\227 \327\224\327\231\327\240\327\225 2.5..5", "5..2.5 \327\225\327\240\327\231\327\224 \327\227\327\225\327\225\327\230\327\224", QChar::DirR },
{ "data17", "he said \"\327\226\327\225 \327\236\327\233\327\225\327\240\327\231\327\252!\"", "he said \"\327\252\327\231\327\240\327\225\327\233\327\236 \327\225\327\226!\"", QChar::DirL },
{ "data18", "he said \"\327\226\327\225 \327\236\327\233\327\225\327\240\327\231\327\252!\327\251\"", "he said \"\327\251!\327\252\327\231\327\240\327\225\327\233\327\236 \327\225\327\226\"", QChar::DirL },
{ "data19", "(\327\240\327\231\327\241\327\225\327\231) abc", "abc (\327\231\327\225\327\241\327\231\327\240)", QChar::DirR },
{ "data20", "abc (\327\240\327\231\327\241\327\225\327\231)", "abc (\327\231\327\225\327\241\327\231\327\240)", QChar::DirL },
{ "data21", "\327\240\327\231\327\241\327\225\327\231 23 \327\231\327\227\327\231\327\223 abc", "abc \327\223\327\231\327\227\327\231 23 \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data22", "#@$ \327\240\327\231\327\241\327\225\327\231", "\327\231\327\225\327\241\327\231\327\240 $@#", QChar::DirR },
{ "data23", "\327\240\327\231\327\241\327\225\327\231 ~~~23%%% \327\231\327\227\327\231\327\223 abc ", " abc \327\223\327\231\327\227\327\231 23%%%~~~ \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data24", "\327\240\327\231\327\241\327\225\327\231 abc ~~~23%%% \327\231\327\227\327\231\327\223 abc", "abc \327\223\327\231\327\227\327\231 abc ~~~23%%% \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data25", "\327\240\327\231\327\241\327\225\327\231 abc@23@cde \327\231\327\227\327\231\327\223", "\327\223\327\231\327\227\327\231 abc@23@cde \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data26", "\327\240\327\231\327\241\327\225\327\231 abc 23 cde \327\231\327\227\327\231\327\223", "\327\223\327\231\327\227\327\231 abc 23 cde \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data27", "\327\240\327\231\327\241\327\225\327\231 abc 23 \327\231\327\227\327\231\327\223 cde", "cde \327\223\327\231\327\227\327\231 abc 23 \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data28", "\327\222a 2 \327\251", "\327\251 a 2\327\222", QChar::DirR },
{ "data29", "\327\244\327\252\327\225\327\250 1*5 1-5 1/5 1+5", "1+5 1/5 1-5 5*1 \327\250\327\225\327\252\327\244", QChar::DirR },
{ "data30", "\330\272 1*5 1-5 1/5 1+5", "5+1 1/5 5-1 5*1 \330\272", QChar::DirR },
{ "data31", "\330\250 \333\261\333\262.\333\263", "\333\261\333\262.\333\263 \330\250", QChar::DirR },
{ "data32", "\330\250 12.3", "12.3 \330\250", QChar::DirR },
{ "data33", "\330\250 1.23", "1.23 \330\250", QChar::DirR },
{ "data34", "\331\276 \333\261.\333\262\333\263", "\333\261.\333\262\333\263 \331\276", QChar::DirR },
{ "data35", "\331\276\333\261.\333\262\333\263", "\333\261.\333\262\333\263\331\276", QChar::DirR },
{ "data36", "1) \327\251", "\327\251 (1", QChar::DirR },
{ "data37", "1) \327\251", "1) \327\251", QChar::DirL },
{ "data38", "\327\224-w3c", "w3c-\327\224", QChar::DirR },
{ "data39", "\327\224-w3c", "\327\224-w3c", QChar::DirL },
{ "data40", "17:25, foo", "17:25, foo", QChar::DirL },
{ "data41", "5, foo", "foo ,5", QChar::DirR },
{ "data42", "foo\nfoo", "foo\nfoo", QChar::DirL },
{ "data43", "\327\251\327\234\327\225\327\235\n\327\251\327\234\327\225\327\235", "\327\235\327\225\327\234\327\251\n\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data44", "foo\n\327\251\327\234\327\225\327\235", "foo\n\327\235\327\225\327\234\327\251", QChar::DirL },
{ "data45", "\327\251\327\234\327\225\327\235\nfoo", "foo\n\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data46", "\330\250 1.23 \330\250", "\330\250 1.23 \330\250", QChar::DirR },
{ "data47", "\331\204\330\250 1.23 \331\202\330\250", "\330\250\331\202 1.23 \330\250\331\204", QChar::DirR },
{ "data48", "\330\250 1.2 \330\250", "\330\250 1.2 \330\250", QChar::DirR },
{ "data49", "\331\204\330\250 1.2 \331\202\330\250", "\330\250\331\202 1.2 \330\250\331\204", QChar::DirR },
{ "data50", "a\331\204\330\250 1.2 \331\202\330\250", "a\330\250\331\202 1.2 \330\250\331\204", QChar::DirL },
{ "data51", "ab(\327\240\327\231)cd", "ab(\327\231\327\240)cd", QChar::DirL },
{ "data52", "ab(\327\240\327\231)cd", "cd(\327\231\327\240)ab", QChar::DirR },
{ "data53", "a(\327\231)c", "a(\327\231)c", QChar::DirL },
{ "data54", "a(\327\231)c", "c(\327\231)a", QChar::DirR },
{ "data55", "\"[\327\220]\"", "\"[\327\220]\"", QChar::DirR },
{ "data56", "\"[\327\220]\"", "\"[\327\220]\"", QChar::DirL },
{ "data57", "\331\204\330\250 \331\202\330\250", "\330\250\331\202 \330\250\331\204", QChar::DirR },
{ "data58", "\331\204\330\250 \331\202\330\250", "\330\250\331\202 \330\250\331\204", QChar::DirL },
{ "data59", "3layout", "3layout", QChar::DirL },
{ "data60", "3layout", "3layout", QChar::DirR },
{ "data61", "3l", "3l", QChar::DirR },
{ "data62", "3la", "3la", QChar::DirR },
{ "data63", "3lay", "3lay", QChar::DirR },
// explicit levels
// LRE: \342\200\252
// RLE: \342\200\253
// PDF: \342\200\254
// LRO: \342\200\255
// RLO: \342\200\256
{ "override1", "\342\200\256hello\342\200\254", "\342\200\256olleh\342\200\254", QChar::DirL },
{ "override2", "\342\200\255hello\342\200\254", "\342\200\255hello\342\200\254", QChar::DirL },
{ "override3", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", QChar::DirL },
{ "override4", "\342\200\256\327\251\327\234\327\225\327\235\342\200\254", "\342\200\256\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "override5", "\342\200\256hello\342\200\254", "\342\200\254olleh\342\200\256", QChar::DirR },
{ "override6", "\342\200\255hello\342\200\254", "\342\200\254hello\342\200\255", QChar::DirR },
{ "override7", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\251\327\234\327\225\327\235\342\200\255", QChar::DirR },
{ "override8", "\342\200\256\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\256", QChar::DirR },
{ "override9", "\327\224\342\200\255\327\251\327\234\342\200\256hello\342\200\254\327\225\327\235\342\200\254",
"\327\251\327\234\342\200\256olleh\342\200\254\327\225\327\235\342\200\255\327\224\342\200\254", QChar::DirL },
{ "override10", "\327\224\342\200\255\327\251\327\234\342\200\256hello\342\200\254\327\225\327\235\342\200\254",
"\342\200\254\327\251\327\234\342\200\256olleh\342\200\254\327\225\327\235\342\200\255\327\224", QChar::DirR },
{ "embed1", "\342\200\252hello\342\200\254", "\342\200\252hello\342\200\254", QChar::DirL },
{ "embed2", "\342\200\253hello\342\200\254", "\342\200\253hello\342\200\254", QChar::DirL },
{ "embed3", "\342\200\252hello\342\200\254", "\342\200\254hello\342\200\252", QChar::DirR },
{ "embed4", "\342\200\253hello\342\200\254", "\342\200\254hello\342\200\253", QChar::DirR },
{ "embed5", "\342\200\252\327\251\327\234\327\225\327\235\342\200\254", "\342\200\252\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "embed6", "\342\200\253\327\251\327\234\327\225\327\235\342\200\254", "\342\200\253\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "embed7", "\342\200\252\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\252", QChar::DirR },
{ "embed8", "\342\200\253\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\253", QChar::DirR },
{ "embed9", "\342\200\252x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\252x \327\235\327\225\327\234\327\251 y\342\200\254", QChar::DirL },
{ "embed10", "\342\200\253x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\253y \327\235\327\225\327\234\327\251 x\342\200\254", QChar::DirL },
{ "embed11", "\342\200\252x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\254x \327\235\327\225\327\234\327\251 y\342\200\252", QChar::DirR },
{ "embed12", "\342\200\253x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\254y \327\235\327\225\327\234\327\251 x\342\200\253", QChar::DirR },
{ "zwsp", "+0\342\200\213f-1", "+0\342\200\213f-1", QChar::DirL },
// Alef: \xD7\x90
{ "bracketpair_1_ltr", "\xD7\x90(\xD7\x90[&a]!)a", "\xD7\x90(\xD7\x90[&a]!)a", QChar::DirL },
{ "bracketpair_1_rtl", "\xD7\x90(\xD7\x90[&a]!)a", "a(![a&]\xD7\x90)\xD7\x90", QChar::DirR },
{ "bracketpair_2_ltr", "a(\xD7\x90[&a]!)\xD7\x90", "a(\xD7\x90[&a]!)\xD7\x90", QChar::DirL },
{ "bracketpair_2_rtl", "a(\xD7\x90[&a]!)\xD7\x90", "\xD7\x90(![a&]\xD7\x90)a", QChar::DirR },
{ "bracketpair_3_ltr", "\xD7\x90(a[&\xD7\x90]!)a", "\xD7\x90(a[&\xD7\x90]!)a", QChar::DirL },
{ "bracketpair_3_rtl", "\xD7\x90(a[&\xD7\x90]!)a", "a(![\xD7\x90&]a)\xD7\x90", QChar::DirR },
{ "bracketpair_4_ltr", "a (a \xD7\x90) \xD7\x90", "a (a \xD7\x90) \xD7\x90", QChar::DirL },
{ "bracketpair_4_rtl", "a (a \xD7\x90) \xD7\x90", "\xD7\x90 (\xD7\x90 a) a", QChar::DirR },
{ "bracketpair_5_ltr", "\xD7\x90 (a \xD7\x90) a", "\xD7\x90 (a \xD7\x90) a", QChar::DirL },
{ "bracketpair_5_rtl", "\xD7\x90 (a \xD7\x90) a", "a (\xD7\x90 a) \xD7\x90", QChar::DirR },
{ "bracketpair_6_ltr", "a (\xD7\x90 a) \xD7\x90", "a (\xD7\x90 a) \xD7\x90", QChar::DirL },
{ "bracketpair_6_rtl", "a (\xD7\x90 a) \xD7\x90", "\xD7\x90 (a \xD7\x90) a", QChar::DirR },
{ "bracketpair_7_ltr", "\xD7\x90\xD7\x90 book(s)", "\xD7\x90\xD7\x90 book(s)", QChar::DirL },
{ "bracketpair_7_rtl", "\xD7\x90\xD7\x90 book(s)", "book(s) \xD7\x90\xD7\x90", QChar::DirR },
{ "bracketpair_8_ltr", "a \xD7\x90\xD7\x90(\xD7\x90)", "a (\xD7\x90)\xD7\x90\xD7\x90", QChar::DirL },
{ "bracketpair_8_rtl", "a \xD7\x90\xD7\x90(\xD7\x90)", "(\xD7\x90)\xD7\x90\xD7\x90 a", QChar::DirR },
{ 0, 0, 0, QChar::DirON }
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,515 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtGui/QtGui>
#include <private/qtextengine_p.h>
#include "bidireorderstring.h"
class tst_QComplexText : public QObject
{
Q_OBJECT
private slots:
void bidiReorderString_data();
void bidiReorderString();
void bidiCursor_qtbug2795();
void bidiCursor_PDF();
void bidiCursorMovement_data();
void bidiCursorMovement();
void bidiCursorLogicalMovement_data();
void bidiCursorLogicalMovement();
void bidiInvalidCursorNoMovement_data();
void bidiInvalidCursorNoMovement();
void bidiCharacterTest();
void bidiTest();
};
void tst_QComplexText::bidiReorderString_data()
{
QTest::addColumn<QString>("logical");
QTest::addColumn<QString>("VISUAL");
QTest::addColumn<int>("basicDir");
const LV *data = logical_visual;
while ( data->name ) {
//next we fill it with data
QTest::newRow( data->name )
<< QString::fromUtf8( data->logical )
<< QString::fromUtf8( data->visual )
<< (int) data->basicDir;
QTest::newRow( QByteArray(data->name) + " (doubled)" )
<< (QString::fromUtf8( data->logical ) + QChar(QChar::ParagraphSeparator) + QString::fromUtf8( data->logical ))
<< (QString::fromUtf8( data->visual ) + QChar(QChar::ParagraphSeparator) + QString::fromUtf8( data->visual ))
<< (int) data->basicDir;
data++;
}
QString isolateAndBoundary = QString(QChar(0x2068 /* DirFSI */)) + QChar(0x1c /* DirB */) + QChar(0x2069 /* DirPDI */);
QTest::newRow( "isolateAndBoundary" )
<< QString::fromUtf8( data->logical )
<< QString::fromUtf8( data->visual )
<< (int) QChar::DirL;
}
void tst_QComplexText::bidiReorderString()
{
QFETCH( QString, logical );
QFETCH( int, basicDir );
// replace \n with Unicode newline. The new algorithm ignores \n
logical.replace(QChar('\n'), QChar(0x2028));
QTextEngine e(logical, QFont());
e.option.setTextDirection((QChar::Direction)basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
e.itemize();
quint8 levels[256];
int visualOrder[256];
int nitems = e.layoutData->items.size();
int i;
for (i = 0; i < nitems; ++i) {
//qDebug("item %d bidiLevel=%d", i, e.items[i].analysis.bidiLevel);
levels[i] = e.layoutData->items[i].analysis.bidiLevel;
}
e.bidiReorder(nitems, levels, visualOrder);
QString visual;
for (i = 0; i < nitems; ++i) {
QScriptItem &si = e.layoutData->items[visualOrder[i]];
QString sub = logical.mid(si.position, e.length(visualOrder[i]));
if (si.analysis.bidiLevel % 2) {
// reverse sub
QChar *a = sub.data();
QChar *b = a + sub.size() - 1;
while (a < b) {
QChar tmp = *a;
*a = *b;
*b = tmp;
++a;
--b;
}
a = (QChar *)sub.unicode();
b = a + sub.size();
while (a<b) {
*a = a->mirroredChar();
++a;
}
}
visual += sub;
}
// replace Unicode newline back with \n to compare.
visual.replace(QChar(0x2028), QChar('\n'));
QTEST(visual, "VISUAL");
}
void tst_QComplexText::bidiCursor_qtbug2795()
{
QString str = QString::fromUtf8("الجزيرة نت");
QTextLayout l1(str);
l1.beginLayout();
l1.setCacheEnabled(true);
QTextLine line1 = l1.createLine();
l1.endLayout();
qreal x1 = line1.cursorToX(0) - line1.cursorToX(str.size());
str.append("1");
QTextLayout l2(str);
l2.setCacheEnabled(true);
l2.beginLayout();
QTextLine line2 = l2.createLine();
l2.endLayout();
qreal x2 = line2.cursorToX(0) - line2.cursorToX(str.size());
// The cursor should remain at the same position after a digit is appended
QCOMPARE(x1, x2);
}
void tst_QComplexText::bidiCursorMovement_data()
{
QTest::addColumn<QString>("logical");
QTest::addColumn<int>("basicDir");
const LV *data = logical_visual;
while ( data->name ) {
//next we fill it with data
QTest::newRow( data->name )
<< QString::fromUtf8( data->logical )
<< (int) data->basicDir;
data++;
}
}
void tst_QComplexText::bidiCursorMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
layout.setCacheEnabled(true);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
layout.setCursorMoveStyle(Qt::VisualMoveStyle);
bool moved;
int oldPos, newPos = 0;
qreal x, newX;
layout.beginLayout();
QTextLine line = layout.createLine();
layout.endLayout();
newX = line.cursorToX(0);
do {
oldPos = newPos;
x = newX;
newX = line.cursorToX(oldPos);
if (basicDir == QChar::DirL) {
QVERIFY(newX >= x);
newPos = layout.rightCursorPosition(oldPos);
} else
{
QVERIFY(newX <= x);
newPos = layout.leftCursorPosition(oldPos);
}
moved = (oldPos != newPos);
} while (moved);
}
void tst_QComplexText::bidiCursorLogicalMovement_data()
{
bidiCursorMovement_data();
}
void tst_QComplexText::bidiCursorLogicalMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
bool moved;
int oldPos, newPos = 0;
do {
oldPos = newPos;
newPos = layout.nextCursorPosition(oldPos);
QVERIFY(newPos >= oldPos);
moved = (oldPos != newPos);
} while (moved);
do {
oldPos = newPos;
newPos = layout.previousCursorPosition(oldPos);
QVERIFY(newPos <= oldPos);
moved = (oldPos != newPos);
} while (moved);
}
void tst_QComplexText::bidiInvalidCursorNoMovement_data()
{
bidiCursorMovement_data();
}
void tst_QComplexText::bidiInvalidCursorNoMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
// visual
QCOMPARE(layout.rightCursorPosition(-1000), -1000);
QCOMPARE(layout.rightCursorPosition(1000), 1000);
QCOMPARE(layout.leftCursorPosition(-1000), -1000);
QCOMPARE(layout.leftCursorPosition(1000), 1000);
// logical
QCOMPARE(layout.nextCursorPosition(-1000), -1000);
QCOMPARE(layout.nextCursorPosition(1000), 1000);
QCOMPARE(layout.previousCursorPosition(-1000), -1000);
QCOMPARE(layout.previousCursorPosition(1000), 1000);
}
void tst_QComplexText::bidiCursor_PDF()
{
QString str = QString::fromUtf8("\342\200\252hello\342\200\254");
QTextLayout layout(str);
layout.setCacheEnabled(true);
layout.beginLayout();
QTextLine line = layout.createLine();
layout.endLayout();
int size = str.size();
QVERIFY(line.cursorToX(size) == line.cursorToX(size - 1));
}
static void testBidiString(const QString &data, int paragraphDirection,
const QList<int> &resolvedLevels, const QList<int> &visualOrder)
{
Q_UNUSED(resolvedLevels);
QTextEngine e(data, QFont());
Qt::LayoutDirection pDir = Qt::LeftToRight;
if (paragraphDirection == 1)
pDir = Qt::RightToLeft;
else if (paragraphDirection == 2)
pDir = Qt::LayoutDirectionAuto;
e.option.setTextDirection(pDir);
e.itemize();
quint8 levels[1024];
int visual[1024];
int nitems = e.layoutData->items.size();
int i;
for (i = 0; i < nitems; ++i) {
//qDebug("item %d bidiLevel=%d", i, e.items[i].analysis.bidiLevel);
levels[i] = e.layoutData->items[i].analysis.bidiLevel;
}
e.bidiReorder(nitems, levels, visual);
QString visualString;
for (i = 0; i < nitems; ++i) {
QScriptItem &si = e.layoutData->items[visual[i]];
QString sub;
for (int j = si.position; j < si.position + e.length(visual[i]); ++j) {
switch (data.at(j).direction()) {
case QChar::DirLRE:
case QChar::DirRLE:
case QChar::DirLRO:
case QChar::DirRLO:
case QChar::DirPDF:
case QChar::DirBN:
continue;
default:
break;
}
sub += data.at(j);
}
// remove explicit embedding characters, as the test data has them removed as well
sub.remove(QChar(0x202a));
sub.remove(QChar(0x202b));
sub.remove(QChar(0x202c));
sub.remove(QChar(0x202d));
sub.remove(QChar(0x202e));
if (si.analysis.bidiLevel % 2) {
// reverse sub
QChar *a = sub.data();
QChar *b = a + sub.size() - 1;
while (a < b) {
QChar tmp = *a;
*a = *b;
*b = tmp;
++a;
--b;
}
a = (QChar *)sub.unicode();
b = a + sub.size();
// while (a<b) {
// *a = a->mirroredChar();
// ++a;
// }
}
visualString += sub;
}
QString expected;
// qDebug() << "expected visual order";
for (int i : visualOrder) {
// qDebug() << " " << i << hex << data[i].unicode();
expected.append(data[i]);
}
QCOMPARE(visualString, expected);
}
void tst_QComplexText::bidiCharacterTest()
{
QString testFile = QFINDTESTDATA("data/BidiCharacterTest.txt");
QFile f(testFile);
QVERIFY(f.exists());
f.open(QIODevice::ReadOnly);
int linenum = 0;
while (!f.atEnd()) {
linenum++;
QByteArray line = f.readLine().simplified();
if (line.startsWith('#') || line.isEmpty())
continue;
QVERIFY(!line.contains('#'));
QList<QByteArray> parts = line.split(';');
QVERIFY(parts.size() == 5);
QString data;
QList<QByteArray> dataParts = parts.at(0).split(' ');
for (const auto &p : dataParts) {
bool ok;
data += QChar((ushort)p.toInt(&ok, 16));
QVERIFY(ok);
}
int paragraphDirection = parts.at(1).toInt();
// int resolvedParagraphLevel = parts.at(2).toInt();
QList<int> resolvedLevels;
QList<QByteArray> levelParts = parts.at(3).split(' ');
for (const auto &p : levelParts) {
if (p == "x") {
resolvedLevels += -1;
} else {
bool ok;
resolvedLevels += p.toInt(&ok);
QVERIFY(ok);
}
}
QList<int> visualOrder;
QList<QByteArray> orderParts = parts.at(4).split(' ');
for (const auto &p : orderParts) {
bool ok;
visualOrder += p.toInt(&ok);
QVERIFY(ok);
}
const QByteArray nm = "line #" + QByteArray::number(linenum);
testBidiString(data, paragraphDirection, resolvedLevels, visualOrder);
}
}
ushort unicodeForDirection(const QByteArray &direction)
{
struct {
const char *string;
ushort unicode;
} dirToUnicode[] = {
{ "L", 0x41 },
{ "R", 0x5d0 },
{ "EN", 0x30 },
{ "ES", 0x2b },
{ "ET", 0x24 },
{ "AN", 0x660 },
{ "CS", 0x2c },
{ "B", '\n' },
{ "S", 0x9 },
{ "WS", 0x20 },
{ "ON", 0x2a },
{ "LRE", 0x202a },
{ "LRO", 0x202d },
{ "AL", 0x627 },
{ "RLE", 0x202b },
{ "RLO", 0x202e },
{ "PDF", 0x202c },
{ "NSM", 0x300 },
{ "BN", 0xad },
{ "LRI", 0x2066 },
{ "RLI", 0x2067 },
{ "FSI", 0x2068 },
{ "PDI", 0x2069 }
};
for (const auto &e : dirToUnicode) {
if (e.string == direction)
return e.unicode;
}
Q_UNREACHABLE();
}
void tst_QComplexText::bidiTest()
{
QString testFile = QFINDTESTDATA("data/BidiTest.txt");
QFile f(testFile);
QVERIFY(f.exists());
f.open(QIODevice::ReadOnly);
int linenum = 0;
QList<int> resolvedLevels;
QList<int> visualOrder;
while (!f.atEnd()) {
linenum++;
QByteArray line = f.readLine().simplified();
if (line.startsWith('#') || line.isEmpty())
continue;
QVERIFY(!line.contains('#'));
if (line.startsWith("@Levels:")) {
line = line.mid(strlen("@Levels:")).simplified();
resolvedLevels.clear();
QList<QByteArray> levelParts = line.split(' ');
for (const auto &p : levelParts) {
if (p == "x") {
resolvedLevels += -1;
} else {
bool ok;
resolvedLevels += p.toInt(&ok);
QVERIFY(ok);
}
}
continue;
} else if (line.startsWith("@Reorder:")) {
line = line.mid(strlen("@Reorder:")).simplified();
visualOrder.clear();
QList<QByteArray> orderParts = line.split(' ');
for (const auto &p : orderParts) {
if (p.isEmpty())
continue;
bool ok;
visualOrder += p.toInt(&ok);
QVERIFY(ok);
}
continue;
}
QList<QByteArray> parts = line.split(';');
Q_ASSERT(parts.size() == 2);
QString data;
QList<QByteArray> dataParts = parts.at(0).split(' ');
for (const auto &p : dataParts) {
ushort uc = unicodeForDirection(p);
data += QChar(uc);
}
int paragraphDirections = parts.at(1).toInt();
const QByteArray nm = "line #" + QByteArray::number(linenum);
if (paragraphDirections & 1)
testBidiString(data, 2, resolvedLevels, visualOrder);
if (paragraphDirections & 2)
testBidiString(data, 0, resolvedLevels, visualOrder);
if (paragraphDirections & 4)
testBidiString(data, 1, resolvedLevels, visualOrder);
}
}
QTEST_MAIN(tst_QComplexText)
#include "tst_qcomplextext.moc"