michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "nsBidiUtils.h" michael@0: michael@0: #define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS) michael@0: #define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS) michael@0: #define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS) michael@0: #define NUM_TO_ARABIC(c) \ michael@0: ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \ michael@0: ((c) - (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \ michael@0: ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \ michael@0: ((c) - (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \ michael@0: (c))) michael@0: #define NUM_TO_HINDI(c) \ michael@0: ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \ michael@0: ((c) + (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT): \ michael@0: ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \ michael@0: ((c) + (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \ michael@0: (c))) michael@0: #define NUM_TO_PERSIAN(c) \ michael@0: ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \ michael@0: ((c) - (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \ michael@0: ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \ michael@0: ((c) + (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \ michael@0: (c))) michael@0: michael@0: char16_t HandleNumberInChar(char16_t aChar, bool aPrevCharArabic, uint32_t aNumFlag) michael@0: { michael@0: // IBMBIDI_NUMERAL_NOMINAL * michael@0: // IBMBIDI_NUMERAL_REGULAR michael@0: // IBMBIDI_NUMERAL_HINDICONTEXT michael@0: // IBMBIDI_NUMERAL_ARABIC michael@0: // IBMBIDI_NUMERAL_HINDI michael@0: michael@0: switch (aNumFlag) { michael@0: case IBMBIDI_NUMERAL_HINDI: michael@0: return NUM_TO_HINDI(aChar); michael@0: case IBMBIDI_NUMERAL_ARABIC: michael@0: return NUM_TO_ARABIC(aChar); michael@0: case IBMBIDI_NUMERAL_PERSIAN: michael@0: return NUM_TO_PERSIAN(aChar); michael@0: case IBMBIDI_NUMERAL_REGULAR: michael@0: case IBMBIDI_NUMERAL_HINDICONTEXT: michael@0: case IBMBIDI_NUMERAL_PERSIANCONTEXT: michael@0: // for clipboard handling michael@0: //XXX do we really want to convert numerals when copying text? michael@0: if (aPrevCharArabic) { michael@0: if (aNumFlag == IBMBIDI_NUMERAL_PERSIANCONTEXT) michael@0: return NUM_TO_PERSIAN(aChar); michael@0: else michael@0: return NUM_TO_HINDI(aChar); michael@0: } michael@0: else michael@0: return NUM_TO_ARABIC(aChar); michael@0: case IBMBIDI_NUMERAL_NOMINAL: michael@0: default: michael@0: return aChar; michael@0: } michael@0: } michael@0: michael@0: nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag) michael@0: { michael@0: uint32_t i; michael@0: michael@0: switch (aNumFlag) { michael@0: case IBMBIDI_NUMERAL_HINDI: michael@0: case IBMBIDI_NUMERAL_ARABIC: michael@0: case IBMBIDI_NUMERAL_PERSIAN: michael@0: case IBMBIDI_NUMERAL_REGULAR: michael@0: case IBMBIDI_NUMERAL_HINDICONTEXT: michael@0: case IBMBIDI_NUMERAL_PERSIANCONTEXT: michael@0: for (i=0;i0 ? aBuffer[i-1] : 0), aNumFlag); michael@0: break; michael@0: case IBMBIDI_NUMERAL_NOMINAL: michael@0: default: michael@0: break; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool HasRTLChars(const nsAString& aString) michael@0: { michael@0: // This is used to determine whether to enable bidi if a string has michael@0: // right-to-left characters. To simplify things, anything that could be a michael@0: // surrogate or RTL presentation form is covered just by testing >= 0xD800). michael@0: // It's fine to enable bidi in rare cases where it actually isn't needed. michael@0: int32_t length = aString.Length(); michael@0: for (int32_t i = 0; i < length; i++) { michael@0: char16_t ch = aString.CharAt(i); michael@0: if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: }