2007-06-28 13:02:43 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-06-28 13:02:43 -07:00
|
|
|
#include "nsBidiUtils.h"
|
2008-03-12 00:56:38 -07:00
|
|
|
#include "nsCharTraits.h"
|
2012-04-18 21:54:54 -07:00
|
|
|
#include "nsUnicodeProperties.h"
|
2007-06-30 22:56:10 -07:00
|
|
|
|
2007-06-28 13:02:44 -07:00
|
|
|
#define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
|
2009-01-30 00:13:17 -08:00
|
|
|
#define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS)
|
|
|
|
#define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS)
|
2007-06-28 13:02:44 -07:00
|
|
|
#define NUM_TO_ARABIC(c) \
|
|
|
|
((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) - (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
|
2009-01-30 00:13:17 -08:00
|
|
|
((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) - (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
|
2009-01-30 00:13:17 -08:00
|
|
|
(c)))
|
2007-06-28 13:02:44 -07:00
|
|
|
#define NUM_TO_HINDI(c) \
|
|
|
|
((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) + (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
|
2009-01-30 00:13:17 -08:00
|
|
|
((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) + (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
|
2009-01-30 00:13:17 -08:00
|
|
|
(c)))
|
|
|
|
#define NUM_TO_PERSIAN(c) \
|
|
|
|
((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) - (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
|
2009-01-30 00:13:17 -08:00
|
|
|
((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
|
2012-08-22 08:56:38 -07:00
|
|
|
((c) + (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
|
2009-01-30 00:13:17 -08:00
|
|
|
(c)))
|
2007-06-28 13:02:44 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
PRUnichar HandleNumberInChar(PRUnichar aChar, bool aPrevCharArabic, uint32_t aNumFlag)
|
2007-06-28 13:02:44 -07:00
|
|
|
{
|
2007-06-28 13:02:51 -07:00
|
|
|
// IBMBIDI_NUMERAL_NOMINAL *
|
|
|
|
// IBMBIDI_NUMERAL_REGULAR
|
2007-06-28 13:02:44 -07:00
|
|
|
// IBMBIDI_NUMERAL_HINDICONTEXT
|
|
|
|
// IBMBIDI_NUMERAL_ARABIC
|
|
|
|
// IBMBIDI_NUMERAL_HINDI
|
|
|
|
|
|
|
|
switch (aNumFlag) {
|
|
|
|
case IBMBIDI_NUMERAL_HINDI:
|
2008-12-18 12:12:53 -08:00
|
|
|
return NUM_TO_HINDI(aChar);
|
2007-06-28 13:02:44 -07:00
|
|
|
case IBMBIDI_NUMERAL_ARABIC:
|
2008-12-18 12:12:53 -08:00
|
|
|
return NUM_TO_ARABIC(aChar);
|
2009-01-30 00:13:17 -08:00
|
|
|
case IBMBIDI_NUMERAL_PERSIAN:
|
|
|
|
return NUM_TO_PERSIAN(aChar);
|
2007-06-28 13:02:51 -07:00
|
|
|
case IBMBIDI_NUMERAL_REGULAR:
|
|
|
|
case IBMBIDI_NUMERAL_HINDICONTEXT:
|
2009-01-30 00:13:17 -08:00
|
|
|
case IBMBIDI_NUMERAL_PERSIANCONTEXT:
|
2008-12-18 12:12:53 -08:00
|
|
|
// for clipboard handling
|
|
|
|
//XXX do we really want to convert numerals when copying text?
|
2009-01-30 00:13:17 -08:00
|
|
|
if (aPrevCharArabic) {
|
|
|
|
if (aNumFlag == IBMBIDI_NUMERAL_PERSIANCONTEXT)
|
|
|
|
return NUM_TO_PERSIAN(aChar);
|
|
|
|
else
|
|
|
|
return NUM_TO_HINDI(aChar);
|
|
|
|
}
|
|
|
|
else
|
2008-12-18 12:12:53 -08:00
|
|
|
return NUM_TO_ARABIC(aChar);
|
|
|
|
case IBMBIDI_NUMERAL_NOMINAL:
|
|
|
|
default:
|
|
|
|
return aChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult HandleNumbers(PRUnichar* aBuffer, uint32_t aSize, uint32_t aNumFlag)
|
2008-12-18 12:12:53 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2008-12-18 12:12:53 -08:00
|
|
|
|
|
|
|
switch (aNumFlag) {
|
|
|
|
case IBMBIDI_NUMERAL_HINDI:
|
|
|
|
case IBMBIDI_NUMERAL_ARABIC:
|
2009-01-30 00:13:17 -08:00
|
|
|
case IBMBIDI_NUMERAL_PERSIAN:
|
2008-12-18 12:12:53 -08:00
|
|
|
case IBMBIDI_NUMERAL_REGULAR:
|
|
|
|
case IBMBIDI_NUMERAL_HINDICONTEXT:
|
2009-01-30 00:13:17 -08:00
|
|
|
case IBMBIDI_NUMERAL_PERSIANCONTEXT:
|
2008-12-18 12:12:53 -08:00
|
|
|
for (i=0;i<aSize;i++)
|
2009-01-14 23:54:44 -08:00
|
|
|
aBuffer[i] = HandleNumberInChar(aBuffer[i], !!(i>0 ? aBuffer[i-1] : 0), aNumFlag);
|
2008-12-18 12:12:53 -08:00
|
|
|
break;
|
2007-06-28 13:02:51 -07:00
|
|
|
case IBMBIDI_NUMERAL_NOMINAL:
|
|
|
|
default:
|
2007-06-28 13:02:44 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-30 22:56:10 -07:00
|
|
|
#define LRM_CHAR 0x200e
|
2012-04-18 21:54:54 -07:00
|
|
|
#define LRE_CHAR 0x202a
|
|
|
|
#define RLO_CHAR 0x202e
|
2012-08-22 08:56:38 -07:00
|
|
|
bool IsBidiControl(uint32_t aChar)
|
2007-06-30 22:56:10 -07:00
|
|
|
{
|
|
|
|
// This method is used when stripping Bidi control characters for
|
2012-04-18 21:54:54 -07:00
|
|
|
// display, so it will return TRUE for LRM, RLM, LRE, RLE, PDF, LRO and RLO
|
|
|
|
return ((LRE_CHAR <= aChar && aChar <= RLO_CHAR) ||
|
|
|
|
((aChar)&0xfffffe)==LRM_CHAR);
|
2007-06-30 22:56:10 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasRTLChars(const nsAString& aString)
|
2008-03-12 00:56:38 -07:00
|
|
|
{
|
2009-07-12 06:32:48 -07:00
|
|
|
// 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.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t length = aString.Length();
|
|
|
|
for (int32_t i = 0; i < length; i++) {
|
2009-07-12 06:32:48 -07:00
|
|
|
PRUnichar ch = aString.CharAt(i);
|
|
|
|
if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-03-12 00:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-03-12 00:56:38 -07:00
|
|
|
}
|