|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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" |
|
7 |
|
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) : \ |
|
16 (c))) |
|
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) : \ |
|
22 (c))) |
|
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) : \ |
|
28 (c))) |
|
29 |
|
30 char16_t HandleNumberInChar(char16_t aChar, bool aPrevCharArabic, uint32_t aNumFlag) |
|
31 { |
|
32 // IBMBIDI_NUMERAL_NOMINAL * |
|
33 // IBMBIDI_NUMERAL_REGULAR |
|
34 // IBMBIDI_NUMERAL_HINDICONTEXT |
|
35 // IBMBIDI_NUMERAL_ARABIC |
|
36 // IBMBIDI_NUMERAL_HINDI |
|
37 |
|
38 switch (aNumFlag) { |
|
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); |
|
53 else |
|
54 return NUM_TO_HINDI(aChar); |
|
55 } |
|
56 else |
|
57 return NUM_TO_ARABIC(aChar); |
|
58 case IBMBIDI_NUMERAL_NOMINAL: |
|
59 default: |
|
60 return aChar; |
|
61 } |
|
62 } |
|
63 |
|
64 nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag) |
|
65 { |
|
66 uint32_t i; |
|
67 |
|
68 switch (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: |
|
75 for (i=0;i<aSize;i++) |
|
76 aBuffer[i] = HandleNumberInChar(aBuffer[i], !!(i>0 ? aBuffer[i-1] : 0), aNumFlag); |
|
77 break; |
|
78 case IBMBIDI_NUMERAL_NOMINAL: |
|
79 default: |
|
80 break; |
|
81 } |
|
82 return NS_OK; |
|
83 } |
|
84 |
|
85 bool HasRTLChars(const nsAString& aString) |
|
86 { |
|
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)) { |
|
95 return true; |
|
96 } |
|
97 } |
|
98 return false; |
|
99 } |