From 32c39330ef3b7a7adc00ab955549ab3a4742352c Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 29 May 2012 05:15:53 +0300 Subject: [PATCH] Introduce QUnicodeTools Add QUnicodeTools namespace and rename qGetCharAttributes to initCharAttributes; Make it possible to disable tailoring globally by overriding qt_initcharattributes_default_algorithm_only value (useful for i.e. running the specification conformance tests); This is mostly a preparation step for the upcoming patches. Change-Id: I783879fd17b63b52d7983e25dad5b820f0515e7f Reviewed-by: Lars Knoll --- src/corelib/tools/qharfbuzz_p.h | 3 +++ src/corelib/tools/qtextboundaryfinder.cpp | 15 +++++++++------ src/corelib/tools/qunicodetools.cpp | 32 ++++++++++++++++++++----------- src/corelib/tools/qunicodetools_p.h | 26 +++++++++++++++---------- src/gui/text/qtextengine.cpp | 8 ++++---- 5 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/corelib/tools/qharfbuzz_p.h b/src/corelib/tools/qharfbuzz_p.h index 72d5bda..23de3ef 100644 --- a/src/corelib/tools/qharfbuzz_p.h +++ b/src/corelib/tools/qharfbuzz_p.h @@ -65,6 +65,9 @@ Q_CORE_EXPORT HB_Face qHBNewFace(void *font, HB_GetFontTableFunc tableFunc); Q_CORE_EXPORT void qHBFreeFace(HB_Face); Q_CORE_EXPORT HB_Face qHBLoadFace(HB_Face face); +Q_DECLARE_TYPEINFO(HB_CharAttributes, Q_PRIMITIVE_TYPE); +Q_DECLARE_TYPEINFO(HB_ScriptItem, Q_PRIMITIVE_TYPE); + Q_DECLARE_TYPEINFO(HB_GlyphAttributes, Q_PRIMITIVE_TYPE); Q_DECLARE_TYPEINFO(HB_FixedPoint, Q_PRIMITIVE_TYPE); diff --git a/src/corelib/tools/qtextboundaryfinder.cpp b/src/corelib/tools/qtextboundaryfinder.cpp index c6c88ea..b3b3d83 100644 --- a/src/corelib/tools/qtextboundaryfinder.cpp +++ b/src/corelib/tools/qtextboundaryfinder.cpp @@ -93,12 +93,15 @@ static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int scriptItems.append(item); } - QCharAttributeOptions options = 0; - if (type == QTextBoundaryFinder::Word) - options |= GetWordBreaks; - else if (type == QTextBoundaryFinder::Sentence) - options |= GetSentenceBreaks; - qGetCharAttributes(string, length, scriptItems.data(), scriptItems.count(), attributes, options); + QUnicodeTools::CharAttributeOptions options = QUnicodeTools::WhiteSpaces; + switch (type) { + case QTextBoundaryFinder::Grapheme: options |= QUnicodeTools::GraphemeBreaks; break; + case QTextBoundaryFinder::Word: options |= QUnicodeTools::WordBreaks; break; + case QTextBoundaryFinder::Sentence: options |= QUnicodeTools::SentenceBreaks; break; + case QTextBoundaryFinder::Line: options |= QUnicodeTools::LineBreaks; break; + default: break; + } + QUnicodeTools::initCharAttributes(string, length, scriptItems.data(), scriptItems.count(), attributes, options); } /*! diff --git a/src/corelib/tools/qunicodetools.cpp b/src/corelib/tools/qunicodetools.cpp index 814eba7..0b78401 100644 --- a/src/corelib/tools/qunicodetools.cpp +++ b/src/corelib/tools/qunicodetools.cpp @@ -45,6 +45,10 @@ QT_BEGIN_NAMESPACE +Q_AUTOTEST_EXPORT int qt_initcharattributes_default_algorithm_only = 0; + +namespace QUnicodeTools { + // ----------------------------------------------------------------------------------------------------- // // The line breaking algorithm. See http://www.unicode.org/reports/tr14/tr14-19.html @@ -55,8 +59,6 @@ QT_BEGIN_NAMESPACE // // ----------------------------------------------------------------------------------------------------- -namespace { - /* The Unicode algorithm does in our opinion allow line breaks at some places they shouldn't be allowed. The following changes were thus made in comparison to the Unicode reference: @@ -374,25 +376,33 @@ static void calcSentenceBreaks(const ushort *string, quint32 len, HB_CharAttribu } } -} // namespace - -Q_CORE_EXPORT void qGetCharAttributes(const ushort *string, int length, +Q_CORE_EXPORT void initCharAttributes(const ushort *string, int length, const HB_ScriptItem *items, int numItems, - HB_CharAttributes *attributes, QCharAttributeOptions options) + HB_CharAttributes *attributes, CharAttributeOptions options) { if (length <= 0) return; - memset(attributes, 0, length * sizeof(HB_CharAttributes)); + if (!(options & DontClearAttributes)) { + ::memset(attributes, 0, length * sizeof(HB_CharAttributes)); + if (options & (WordBreaks | SentenceBreaks)) + options |= GraphemeBreaks; + } - calcGraphemeAndLineBreaks(string, length, attributes); - if (options & GetWordBreaks) + if (options & (GraphemeBreaks | LineBreaks | WhiteSpaces)) + calcGraphemeAndLineBreaks(string, length, attributes); + if (options & WordBreaks) calcWordBreaks(string, length, attributes); - if (options & GetSentenceBreaks) + if (options & SentenceBreaks) calcSentenceBreaks(string, length, attributes); - HB_GetTailoredCharAttributes(string, length, items, numItems, attributes); + if (!items || numItems <= 0) + return; + if (!qt_initcharattributes_default_algorithm_only) + HB_GetTailoredCharAttributes(string, length, items, numItems, attributes); } +} // namespace QUnicodeTools + QT_END_NAMESPACE diff --git a/src/corelib/tools/qunicodetools_p.h b/src/corelib/tools/qunicodetools_p.h index f546481..ea407e7 100644 --- a/src/corelib/tools/qunicodetools_p.h +++ b/src/corelib/tools/qunicodetools_p.h @@ -53,23 +53,29 @@ // We mean it. // -#include -#include +#include QT_BEGIN_NAMESPACE -Q_DECLARE_TYPEINFO(HB_CharAttributes, Q_PRIMITIVE_TYPE); -Q_DECLARE_TYPEINFO(HB_ScriptItem, Q_PRIMITIVE_TYPE); +namespace QUnicodeTools { -enum QCharAttributeOption { - GetWordBreaks = 1, - GetSentenceBreaks = 2 +enum CharAttributeOption { + GraphemeBreaks = 0x01, + WordBreaks = 0x02, + SentenceBreaks = 0x04, + LineBreaks = 0x08, + WhiteSpaces = 0x10, + DefaultOptionsCompat = GraphemeBreaks | LineBreaks | WhiteSpaces, // ### remove + + DontClearAttributes = 0x1000 }; -Q_DECLARE_FLAGS(QCharAttributeOptions, QCharAttributeOption) +Q_DECLARE_FLAGS(CharAttributeOptions, CharAttributeOption) -Q_CORE_EXPORT void qGetCharAttributes(const ushort *string, int length, +Q_CORE_EXPORT void initCharAttributes(const ushort *string, int length, const HB_ScriptItem *items, int numItems, - HB_CharAttributes *attributes, QCharAttributeOptions options = QFlag(0)); + HB_CharAttributes *attributes, CharAttributeOptions options = DefaultOptionsCompat); + +} // namespace QUnicodeTools QT_END_NAMESPACE diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp index 637fab4..dbf1b45 100644 --- a/src/gui/text/qtextengine.cpp +++ b/src/gui/text/qtextengine.cpp @@ -1214,10 +1214,10 @@ const HB_CharAttributes *QTextEngine::attributes() const hbScriptItems[i].script = (HB_Script)si.analysis.script; } - qGetCharAttributes(reinterpret_cast(layoutData->string.constData()), - layoutData->string.length(), - hbScriptItems.data(), hbScriptItems.size(), - (HB_CharAttributes *)layoutData->memory); + QUnicodeTools::initCharAttributes(reinterpret_cast(layoutData->string.constData()), + layoutData->string.length(), + hbScriptItems.data(), hbScriptItems.size(), + (HB_CharAttributes *)layoutData->memory); layoutData->haveCharAttributes = true; -- 2.7.4