From c20422af13fb30751eaa58e8755c7a9a7fd20a50 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 12 Mar 2013 18:37:07 +0200 Subject: Move Unicode script itemization code from text engine to UnicodeTools This is still the same trivial implementation with the only difference in that that it properly handles surrogate pairs and combining marks. This temporarily makes QTextEngine::itemize() insignificatly slower due to using intermediate buffer, until refactoring is done. Change-Id: I7987d6306b0b5cdb21b837968e292dd70abfe223 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/corelib/tools/qunicodetools.cpp | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/corelib/tools/qunicodetools.cpp') diff --git a/src/corelib/tools/qunicodetools.cpp b/src/corelib/tools/qunicodetools.cpp index 3102035684..4d5c978fd5 100644 --- a/src/corelib/tools/qunicodetools.cpp +++ b/src/corelib/tools/qunicodetools.cpp @@ -635,6 +635,51 @@ Q_CORE_EXPORT void initCharAttributes(const ushort *string, int length, } } + +// ---------------------------------------------------------------------------- +// +// The Unicode script property. See http://www.unicode.org/reports/tr24/ (some very old version) +// +// ---------------------------------------------------------------------------- + +Q_CORE_EXPORT void initScripts(const ushort *string, int length, uchar *scripts) +{ + int sor = 0; + int eor = -1; + uchar script = QChar::Script_Common; + for (int i = 0; i < length; ++i) { + eor = i; + uint ucs4 = string[i]; + if (QChar::isHighSurrogate(ucs4) && i + 1 < length) { + ushort low = string[i + 1]; + if (QChar::isLowSurrogate(low)) { + ucs4 = QChar::surrogateToUcs4(ucs4, low); + ++i; + } + } + + const QUnicodeTables::Properties *prop = QUnicodeTables::properties(ucs4); + + if (Q_LIKELY(prop->script == script || prop->script == QChar::Script_Inherited)) + continue; + + // Never break between a combining mark (gc= Mc, Mn or Me) and its base character. + // Thus, a combining mark — whatever its script property value is — should inherit + // the script property value of its base character. + static const int test = (FLAG(QChar::Mark_NonSpacing) | FLAG(QChar::Mark_SpacingCombining) | FLAG(QChar::Mark_Enclosing)); + if (Q_UNLIKELY(FLAG(prop->category) & test)) + continue; + + while (sor < eor) + scripts[sor++] = script; + + script = prop->script; + } + eor = length; + while (sor < eor) + scripts[sor++] = script; +} + } // namespace QUnicodeTools QT_END_NAMESPACE -- cgit v1.2.3