15569331642446be05292e3e1f8a51218827168cdclaireho/*
25569331642446be05292e3e1f8a51218827168cdclaireho * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
35569331642446be05292e3e1f8a51218827168cdclaireho *
45569331642446be05292e3e1f8a51218827168cdclaireho * This is part of HarfBuzz, an OpenType Layout engine library.
55569331642446be05292e3e1f8a51218827168cdclaireho *
65569331642446be05292e3e1f8a51218827168cdclaireho * Permission is hereby granted, without written agreement and without
75569331642446be05292e3e1f8a51218827168cdclaireho * license or royalty fees, to use, copy, modify, and distribute this
85569331642446be05292e3e1f8a51218827168cdclaireho * software and its documentation for any purpose, provided that the
95569331642446be05292e3e1f8a51218827168cdclaireho * above copyright notice and the following two paragraphs appear in
105569331642446be05292e3e1f8a51218827168cdclaireho * all copies of this software.
115569331642446be05292e3e1f8a51218827168cdclaireho *
125569331642446be05292e3e1f8a51218827168cdclaireho * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
135569331642446be05292e3e1f8a51218827168cdclaireho * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
145569331642446be05292e3e1f8a51218827168cdclaireho * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
155569331642446be05292e3e1f8a51218827168cdclaireho * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
165569331642446be05292e3e1f8a51218827168cdclaireho * DAMAGE.
175569331642446be05292e3e1f8a51218827168cdclaireho *
185569331642446be05292e3e1f8a51218827168cdclaireho * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
195569331642446be05292e3e1f8a51218827168cdclaireho * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
205569331642446be05292e3e1f8a51218827168cdclaireho * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
215569331642446be05292e3e1f8a51218827168cdclaireho * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
225569331642446be05292e3e1f8a51218827168cdclaireho * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
235569331642446be05292e3e1f8a51218827168cdclaireho */
245569331642446be05292e3e1f8a51218827168cdclaireho
255569331642446be05292e3e1f8a51218827168cdclaireho/*
265569331642446be05292e3e1f8a51218827168cdclaireho    !!!!!! Warning !!!!!
275569331642446be05292e3e1f8a51218827168cdclaireho    Please don't save this file in emacs. It contains utf8 text sequences emacs will
285569331642446be05292e3e1f8a51218827168cdclaireho    silently convert to a series of question marks.
295569331642446be05292e3e1f8a51218827168cdclaireho */
305569331642446be05292e3e1f8a51218827168cdclaireho#include <QtTest/QtTest>
315569331642446be05292e3e1f8a51218827168cdclaireho#include <QtCore/qdebug.h>
325569331642446be05292e3e1f8a51218827168cdclaireho
335569331642446be05292e3e1f8a51218827168cdclaireho#include <harfbuzz-shaper.h>
345569331642446be05292e3e1f8a51218827168cdclaireho
355569331642446be05292e3e1f8a51218827168cdclairehostatic QVector<HB_CharAttributes> getCharAttributes(const QString &str, HB_Script script = HB_Script_Common)
365569331642446be05292e3e1f8a51218827168cdclaireho{
375569331642446be05292e3e1f8a51218827168cdclaireho    QVector<HB_CharAttributes> attrs(str.length());
385569331642446be05292e3e1f8a51218827168cdclaireho    HB_ScriptItem item;
395569331642446be05292e3e1f8a51218827168cdclaireho    item.pos = 0;
405569331642446be05292e3e1f8a51218827168cdclaireho    item.length = str.length();
415569331642446be05292e3e1f8a51218827168cdclaireho    item.script = script;
425569331642446be05292e3e1f8a51218827168cdclaireho    HB_GetCharAttributes(str.utf16(), str.length(),
435569331642446be05292e3e1f8a51218827168cdclaireho                         &item, 1,
445569331642446be05292e3e1f8a51218827168cdclaireho                         attrs.data());
455569331642446be05292e3e1f8a51218827168cdclaireho    return attrs;
465569331642446be05292e3e1f8a51218827168cdclaireho}
475569331642446be05292e3e1f8a51218827168cdclaireho
485569331642446be05292e3e1f8a51218827168cdclairehoclass tst_CharAttributes : public QObject
495569331642446be05292e3e1f8a51218827168cdclaireho{
505569331642446be05292e3e1f8a51218827168cdclaireho    Q_OBJECT
515569331642446be05292e3e1f8a51218827168cdclaireho
525569331642446be05292e3e1f8a51218827168cdclairehopublic:
535569331642446be05292e3e1f8a51218827168cdclaireho    tst_CharAttributes();
545569331642446be05292e3e1f8a51218827168cdclaireho    virtual ~tst_CharAttributes();
555569331642446be05292e3e1f8a51218827168cdclaireho
565569331642446be05292e3e1f8a51218827168cdclairehopublic slots:
575569331642446be05292e3e1f8a51218827168cdclaireho    void init();
585569331642446be05292e3e1f8a51218827168cdclaireho    void cleanup();
595569331642446be05292e3e1f8a51218827168cdclairehoprivate slots:
605569331642446be05292e3e1f8a51218827168cdclaireho    void lineBreaking();
615569331642446be05292e3e1f8a51218827168cdclaireho    void charWordStopOnLineSeparator();
625569331642446be05292e3e1f8a51218827168cdclaireho    void charStopForSurrogatePairs();
635569331642446be05292e3e1f8a51218827168cdclaireho    void thaiWordBreak();
645569331642446be05292e3e1f8a51218827168cdclaireho};
655569331642446be05292e3e1f8a51218827168cdclaireho
665569331642446be05292e3e1f8a51218827168cdclaireho
675569331642446be05292e3e1f8a51218827168cdclairehotst_CharAttributes::tst_CharAttributes()
685569331642446be05292e3e1f8a51218827168cdclaireho{
695569331642446be05292e3e1f8a51218827168cdclaireho}
705569331642446be05292e3e1f8a51218827168cdclaireho
715569331642446be05292e3e1f8a51218827168cdclairehotst_CharAttributes::~tst_CharAttributes()
725569331642446be05292e3e1f8a51218827168cdclaireho{
735569331642446be05292e3e1f8a51218827168cdclaireho}
745569331642446be05292e3e1f8a51218827168cdclaireho
755569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::init()
765569331642446be05292e3e1f8a51218827168cdclaireho{
775569331642446be05292e3e1f8a51218827168cdclaireho}
785569331642446be05292e3e1f8a51218827168cdclaireho
795569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::cleanup()
805569331642446be05292e3e1f8a51218827168cdclaireho{
815569331642446be05292e3e1f8a51218827168cdclaireho}
825569331642446be05292e3e1f8a51218827168cdclaireho
835569331642446be05292e3e1f8a51218827168cdclaireho
845569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::lineBreaking()
855569331642446be05292e3e1f8a51218827168cdclaireho{
865569331642446be05292e3e1f8a51218827168cdclaireho    struct Breaks {
875569331642446be05292e3e1f8a51218827168cdclaireho	const char *utf8;
885569331642446be05292e3e1f8a51218827168cdclaireho	uchar breaks[32];
895569331642446be05292e3e1f8a51218827168cdclaireho    };
905569331642446be05292e3e1f8a51218827168cdclaireho    Breaks brks[] = {
915569331642446be05292e3e1f8a51218827168cdclaireho	{ "11", { false, 0xff } },
925569331642446be05292e3e1f8a51218827168cdclaireho	{ "aa", { false, 0xff } },
935569331642446be05292e3e1f8a51218827168cdclaireho	{ "++", { false, 0xff } },
945569331642446be05292e3e1f8a51218827168cdclaireho	{ "--", { false, 0xff } },
955569331642446be05292e3e1f8a51218827168cdclaireho	{ "((", { false, 0xff } },
965569331642446be05292e3e1f8a51218827168cdclaireho	{ "))", { false, 0xff } },
975569331642446be05292e3e1f8a51218827168cdclaireho	{ "..", { false, 0xff } },
985569331642446be05292e3e1f8a51218827168cdclaireho	{ "\"\"", { false, 0xff } },
995569331642446be05292e3e1f8a51218827168cdclaireho	{ "$$", { false, 0xff } },
1005569331642446be05292e3e1f8a51218827168cdclaireho	{ "!!", { false, 0xff } },
1015569331642446be05292e3e1f8a51218827168cdclaireho	{ "??", { false, 0xff } },
1025569331642446be05292e3e1f8a51218827168cdclaireho	{ ",,", { false, 0xff } },
1035569331642446be05292e3e1f8a51218827168cdclaireho
1045569331642446be05292e3e1f8a51218827168cdclaireho	{ ")()", { true, false, 0xff } },
1055569331642446be05292e3e1f8a51218827168cdclaireho	{ "?!?", { false, false, 0xff } },
1065569331642446be05292e3e1f8a51218827168cdclaireho	{ ".,.", { false, false, 0xff } },
1075569331642446be05292e3e1f8a51218827168cdclaireho	{ "+-+", { false, false, 0xff } },
1085569331642446be05292e3e1f8a51218827168cdclaireho	{ "+=+", { false, false, 0xff } },
1095569331642446be05292e3e1f8a51218827168cdclaireho	{ "+(+", { false, false, 0xff } },
1105569331642446be05292e3e1f8a51218827168cdclaireho	{ "+)+", { false, false, 0xff } },
1115569331642446be05292e3e1f8a51218827168cdclaireho
1125569331642446be05292e3e1f8a51218827168cdclaireho	{ "a b", { false, true, 0xff } },
1135569331642446be05292e3e1f8a51218827168cdclaireho	{ "a(b", { false, false, 0xff } },
1145569331642446be05292e3e1f8a51218827168cdclaireho	{ "a)b", { false, false, 0xff } },
1155569331642446be05292e3e1f8a51218827168cdclaireho	{ "a-b", { false, true, 0xff } },
1165569331642446be05292e3e1f8a51218827168cdclaireho	{ "a.b", { false, false, 0xff } },
1175569331642446be05292e3e1f8a51218827168cdclaireho	{ "a+b", { false, false, 0xff } },
1185569331642446be05292e3e1f8a51218827168cdclaireho	{ "a?b", { false, false, 0xff } },
1195569331642446be05292e3e1f8a51218827168cdclaireho	{ "a!b", { false, false, 0xff } },
1205569331642446be05292e3e1f8a51218827168cdclaireho	{ "a$b", { false, false, 0xff } },
1215569331642446be05292e3e1f8a51218827168cdclaireho	{ "a,b", { false, false, 0xff } },
1225569331642446be05292e3e1f8a51218827168cdclaireho	{ "a/b", { false, false, 0xff } },
1235569331642446be05292e3e1f8a51218827168cdclaireho	{ "1/2", { false, false, 0xff } },
1245569331642446be05292e3e1f8a51218827168cdclaireho	{ "./.", { false, false, 0xff } },
1255569331642446be05292e3e1f8a51218827168cdclaireho	{ ",/,", { false, false, 0xff } },
1265569331642446be05292e3e1f8a51218827168cdclaireho	{ "!/!", { false, false, 0xff } },
1275569331642446be05292e3e1f8a51218827168cdclaireho	{ "\\/\\", { false, false, 0xff } },
1285569331642446be05292e3e1f8a51218827168cdclaireho	{ "1 2", { false, true, 0xff } },
1295569331642446be05292e3e1f8a51218827168cdclaireho	{ "1(2", { false, false, 0xff } },
1305569331642446be05292e3e1f8a51218827168cdclaireho	{ "1)2", { false, false, 0xff } },
1315569331642446be05292e3e1f8a51218827168cdclaireho	{ "1-2", { false, false, 0xff } },
1325569331642446be05292e3e1f8a51218827168cdclaireho	{ "1.2", { false, false, 0xff } },
1335569331642446be05292e3e1f8a51218827168cdclaireho	{ "1+2", { false, false, 0xff } },
1345569331642446be05292e3e1f8a51218827168cdclaireho	{ "1?2", { false, true, 0xff } },
1355569331642446be05292e3e1f8a51218827168cdclaireho	{ "1!2", { false, true, 0xff } },
1365569331642446be05292e3e1f8a51218827168cdclaireho	{ "1$2", { false, false, 0xff } },
1375569331642446be05292e3e1f8a51218827168cdclaireho	{ "1,2", { false, false, 0xff } },
1385569331642446be05292e3e1f8a51218827168cdclaireho	{ "1/2", { false, false, 0xff } },
1395569331642446be05292e3e1f8a51218827168cdclaireho	{ "\330\260\331\216\331\204\331\220\331\203\331\216", { false, false, false, false, false, 0xff } },
1405569331642446be05292e3e1f8a51218827168cdclaireho	{ "\330\247\331\204\331\205 \330\247\331\204\331\205", { false, false, false, true, false, false, 0xff } },
1415569331642446be05292e3e1f8a51218827168cdclaireho	{ "1#2", { false, false, 0xff } },
1425569331642446be05292e3e1f8a51218827168cdclaireho	{ "!#!", { false, false, 0xff } },
1435569331642446be05292e3e1f8a51218827168cdclaireho	{ 0, {} }
1445569331642446be05292e3e1f8a51218827168cdclaireho    };
1455569331642446be05292e3e1f8a51218827168cdclaireho    Breaks *b = brks;
1465569331642446be05292e3e1f8a51218827168cdclaireho    while (b->utf8) {
1475569331642446be05292e3e1f8a51218827168cdclaireho        QString str = QString::fromUtf8(b->utf8);
1485569331642446be05292e3e1f8a51218827168cdclaireho
1495569331642446be05292e3e1f8a51218827168cdclaireho        QVector<HB_CharAttributes> attrs = getCharAttributes(str);
1505569331642446be05292e3e1f8a51218827168cdclaireho
1515569331642446be05292e3e1f8a51218827168cdclaireho        int i;
1525569331642446be05292e3e1f8a51218827168cdclaireho        for (i = 0; i < (int)str.length() - 1; ++i) {
1535569331642446be05292e3e1f8a51218827168cdclaireho            QVERIFY(b->breaks[i] != 0xff);
1545569331642446be05292e3e1f8a51218827168cdclaireho            if ( (attrs[i].lineBreakType != HB_NoBreak) != (bool)b->breaks[i] ) {
1555569331642446be05292e3e1f8a51218827168cdclaireho                qDebug("test case \"%s\" failed at char %d; break type: %d", b->utf8, i, attrs[i].lineBreakType);
1565569331642446be05292e3e1f8a51218827168cdclaireho                QCOMPARE( (attrs[i].lineBreakType != HB_NoBreak), (bool)b->breaks[i] );
1575569331642446be05292e3e1f8a51218827168cdclaireho            }
1585569331642446be05292e3e1f8a51218827168cdclaireho        }
1595569331642446be05292e3e1f8a51218827168cdclaireho        QVERIFY(attrs[i].lineBreakType == HB_ForcedBreak);
1605569331642446be05292e3e1f8a51218827168cdclaireho        QCOMPARE(b->breaks[i], (uchar)0xff);
1615569331642446be05292e3e1f8a51218827168cdclaireho        ++b;
1625569331642446be05292e3e1f8a51218827168cdclaireho    }
1635569331642446be05292e3e1f8a51218827168cdclaireho}
1645569331642446be05292e3e1f8a51218827168cdclaireho
1655569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::charWordStopOnLineSeparator()
1665569331642446be05292e3e1f8a51218827168cdclaireho{
1675569331642446be05292e3e1f8a51218827168cdclaireho    const QChar lineSeparator(QChar::LineSeparator);
1685569331642446be05292e3e1f8a51218827168cdclaireho    QString txt;
1695569331642446be05292e3e1f8a51218827168cdclaireho    txt.append(lineSeparator);
1705569331642446be05292e3e1f8a51218827168cdclaireho    txt.append(lineSeparator);
1715569331642446be05292e3e1f8a51218827168cdclaireho    QVector<HB_CharAttributes> attrs = getCharAttributes(txt);
1725569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[1].charStop);
1735569331642446be05292e3e1f8a51218827168cdclaireho}
1745569331642446be05292e3e1f8a51218827168cdclaireho
1755569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::charStopForSurrogatePairs()
1765569331642446be05292e3e1f8a51218827168cdclaireho{
1775569331642446be05292e3e1f8a51218827168cdclaireho    QString txt;
1785569331642446be05292e3e1f8a51218827168cdclaireho    txt.append("a");
1795569331642446be05292e3e1f8a51218827168cdclaireho    txt.append(0xd87e);
1805569331642446be05292e3e1f8a51218827168cdclaireho    txt.append(0xdc25);
1815569331642446be05292e3e1f8a51218827168cdclaireho    txt.append("b");
1825569331642446be05292e3e1f8a51218827168cdclaireho    QVector<HB_CharAttributes> attrs = getCharAttributes(txt);
1835569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[0].charStop);
1845569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[1].charStop);
1855569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(!attrs[2].charStop);
1865569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[3].charStop);
1875569331642446be05292e3e1f8a51218827168cdclaireho}
1885569331642446be05292e3e1f8a51218827168cdclaireho
1895569331642446be05292e3e1f8a51218827168cdclairehovoid tst_CharAttributes::thaiWordBreak()
1905569331642446be05292e3e1f8a51218827168cdclaireho{
1915569331642446be05292e3e1f8a51218827168cdclaireho    // สวัสดีครับ นี่เป็นการงทดสอบตัวเอ
1925569331642446be05292e3e1f8a51218827168cdclaireho    QTextCodec *codec = QTextCodec::codecForMib(2259);
1935569331642446be05292e3e1f8a51218827168cdclaireho    QString txt = codec->toUnicode(QByteArray("\xca\xc7\xd1\xca\xb4\xd5\xa4\xc3\xd1\xba\x20\xb9\xd5\xe8\xe0\xbb\xe7\xb9\xa1\xd2\xc3\xb7\xb4\xca\xcd\xba\xb5\xd1\xc7\xe0\xcd\xa7"));
1945569331642446be05292e3e1f8a51218827168cdclaireho
1955569331642446be05292e3e1f8a51218827168cdclaireho
1965569331642446be05292e3e1f8a51218827168cdclaireho    QCOMPARE(txt.length(), 32);
1975569331642446be05292e3e1f8a51218827168cdclaireho    QVector<HB_CharAttributes> attrs = getCharAttributes(txt, HB_Script_Thai);
1985569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[0].lineBreakType == HB_NoBreak);
1995569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[1].lineBreakType == HB_NoBreak);
2005569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[2].lineBreakType == HB_NoBreak);
2015569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[3].lineBreakType == HB_NoBreak);
2025569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[4].lineBreakType == HB_NoBreak);
2035569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[5].lineBreakType == HB_Break);
2045569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[6].lineBreakType == HB_NoBreak);
2055569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[7].lineBreakType == HB_NoBreak);
2065569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[8].lineBreakType == HB_NoBreak);
2075569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[9].lineBreakType == HB_NoBreak);
2085569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[10].lineBreakType == HB_Break);
2095569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[11].lineBreakType == HB_NoBreak);
2105569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[12].lineBreakType == HB_NoBreak);
2115569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[13].lineBreakType == HB_Break);
2125569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[14].lineBreakType == HB_NoBreak);
2135569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[15].lineBreakType == HB_NoBreak);
2145569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[16].lineBreakType == HB_NoBreak);
2155569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[17].lineBreakType == HB_Break);
2165569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[18].lineBreakType == HB_NoBreak);
2175569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[19].lineBreakType == HB_NoBreak);
2185569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[20].lineBreakType == HB_Break);
2195569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[21].lineBreakType == HB_NoBreak);
2205569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[22].lineBreakType == HB_NoBreak);
2215569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[23].lineBreakType == HB_NoBreak);
2225569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[24].lineBreakType == HB_NoBreak);
2235569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[25].lineBreakType == HB_Break);
2245569331642446be05292e3e1f8a51218827168cdclaireho    QVERIFY(attrs[26].lineBreakType == HB_NoBreak);
2255569331642446be05292e3e1f8a51218827168cdclaireho    for (int i = 27; i < 32; ++i)
2265569331642446be05292e3e1f8a51218827168cdclaireho        QVERIFY(attrs[i].lineBreakType == HB_NoBreak);
2275569331642446be05292e3e1f8a51218827168cdclaireho}
2285569331642446be05292e3e1f8a51218827168cdclaireho
2295569331642446be05292e3e1f8a51218827168cdclairehoQTEST_MAIN(tst_CharAttributes)
2305569331642446be05292e3e1f8a51218827168cdclaireho#include "main.moc"
231