Bug 1358275 - Preliminary cleanup: refactor nsTextFragment::UpdateBidiFlag(), moving its logic into nsBidiUtils function HasRTLChars() and making that function more precise in the process. r=dholbert

This commit is contained in:
Jonathan Kew 2017-04-26 23:25:48 +01:00
parent df1af0cf56
commit 26f06fc0de
3 changed files with 22 additions and 26 deletions

View File

@ -450,21 +450,8 @@ void
nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength)
{
if (mState.mIs2b && !mState.mIsBidi) {
const char16_t* cp = aBuffer;
const char16_t* end = cp + aLength;
while (cp < end) {
char16_t ch1 = *cp++;
uint32_t utf32Char = ch1;
if (NS_IS_HIGH_SURROGATE(ch1) &&
cp < end &&
NS_IS_LOW_SURROGATE(*cp)) {
char16_t ch2 = *cp++;
utf32Char = SURROGATE_TO_UCS4(ch1, ch2);
}
if (UTF32_CHAR_IS_BIDI(utf32Char) || IsBidiControlRTL(utf32Char)) {
mState.mIsBidi = true;
break;
}
if (HasRTLChars(aBuffer, aLength)) {
mState.mIsBidi = true;
}
}
}

View File

@ -82,16 +82,18 @@ nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag)
return NS_OK;
}
bool HasRTLChars(const nsAString& aString)
bool HasRTLChars(const char16_t* aText, uint32_t aLength)
{
// This is used to determine whether to enable bidi if a string has
// right-to-left characters. To simplify things, anything that could be a
// surrogate or RTL presentation form is covered just by testing >= 0xD800).
// It's fine to enable bidi in rare cases where it actually isn't needed.
int32_t length = aString.Length();
for (int32_t i = 0; i < length; i++) {
char16_t ch = aString.CharAt(i);
if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) {
// This is used to determine whether a string has right-to-left characters
// that mean it will require bidi processing.
const char16_t* cp = aText;
const char16_t* end = cp + aLength;
while (cp < end) {
uint32_t ch = *cp++;
if (NS_IS_HIGH_SURROGATE(ch) && cp < end && NS_IS_LOW_SURROGATE(*cp)) {
ch = SURROGATE_TO_UCS4(ch, *cp++);
}
if (UTF32_CHAR_IS_BIDI(ch) || IsBidiControlRTL(ch)) {
return true;
}
}

View File

@ -147,10 +147,17 @@ typedef enum nsCharType nsCharType;
}
/**
* Give an nsString.
* Give a 16-bit (UTF-16) text buffer and length
* @return true if the string contains right-to-left characters
*/
bool HasRTLChars(const nsAString& aString);
bool HasRTLChars(const char16_t* aText, uint32_t aLength);
/**
* Convenience function to call the above on an nsAString.
*/
inline bool HasRTLChars(const nsAString& aString) {
return HasRTLChars(aString.BeginReading(), aString.Length());
}
// These values are shared with Preferences dialog
// ------------------