1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsBidiUtils.h"
8 #define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
9 #define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS)
10 #define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS)
11 #define NUM_TO_ARABIC(c) \
12 ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
13 ((c) - (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
14 ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
15 ((c) - (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
17 #define NUM_TO_HINDI(c) \
18 ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
19 ((c) + (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
20 ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
21 ((c) + (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
23 #define NUM_TO_PERSIAN(c) \
24 ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
25 ((c) - (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
26 ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
27 ((c) + (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
30 char16_t
HandleNumberInChar(char16_t aChar
, bool aPrevCharArabic
, uint32_t aNumFlag
)
32 // IBMBIDI_NUMERAL_NOMINAL *
33 // IBMBIDI_NUMERAL_REGULAR
34 // IBMBIDI_NUMERAL_HINDICONTEXT
35 // IBMBIDI_NUMERAL_ARABIC
36 // IBMBIDI_NUMERAL_HINDI
39 case IBMBIDI_NUMERAL_HINDI
:
40 return NUM_TO_HINDI(aChar
);
41 case IBMBIDI_NUMERAL_ARABIC
:
42 return NUM_TO_ARABIC(aChar
);
43 case IBMBIDI_NUMERAL_PERSIAN
:
44 return NUM_TO_PERSIAN(aChar
);
45 case IBMBIDI_NUMERAL_REGULAR
:
46 case IBMBIDI_NUMERAL_HINDICONTEXT
:
47 case IBMBIDI_NUMERAL_PERSIANCONTEXT
:
48 // for clipboard handling
49 //XXX do we really want to convert numerals when copying text?
50 if (aPrevCharArabic
) {
51 if (aNumFlag
== IBMBIDI_NUMERAL_PERSIANCONTEXT
)
52 return NUM_TO_PERSIAN(aChar
);
54 return NUM_TO_HINDI(aChar
);
57 return NUM_TO_ARABIC(aChar
);
58 case IBMBIDI_NUMERAL_NOMINAL
:
64 nsresult
HandleNumbers(char16_t
* aBuffer
, uint32_t aSize
, uint32_t aNumFlag
)
69 case IBMBIDI_NUMERAL_HINDI
:
70 case IBMBIDI_NUMERAL_ARABIC
:
71 case IBMBIDI_NUMERAL_PERSIAN
:
72 case IBMBIDI_NUMERAL_REGULAR
:
73 case IBMBIDI_NUMERAL_HINDICONTEXT
:
74 case IBMBIDI_NUMERAL_PERSIANCONTEXT
:
76 aBuffer
[i
] = HandleNumberInChar(aBuffer
[i
], !!(i
>0 ? aBuffer
[i
-1] : 0), aNumFlag
);
78 case IBMBIDI_NUMERAL_NOMINAL
:
85 bool HasRTLChars(const nsAString
& aString
)
87 // This is used to determine whether to enable bidi if a string has
88 // right-to-left characters. To simplify things, anything that could be a
89 // surrogate or RTL presentation form is covered just by testing >= 0xD800).
90 // It's fine to enable bidi in rare cases where it actually isn't needed.
91 int32_t length
= aString
.Length();
92 for (int32_t i
= 0; i
< length
; i
++) {
93 char16_t ch
= aString
.CharAt(i
);
94 if (ch
>= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch
)) {