1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * IBM Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2000
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Maha Abou El Rous <mahar@eg.ibm.com>
25 : * Lina Kemmel <lkemmel@il.ibm.com>
26 : * Simon Montagu <smontagu@netscape.com>
27 : * Roozbeh Pournader <roozbeh@sharif.edu>
28 : * Ehsan Akhgari <ehsan.akhgari@gmail.com>
29 : *
30 : * Alternatively, the contents of this file may be used under the terms of
31 : * either of the GNU General Public License Version 2 or later (the "GPL"),
32 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 : * in which case the provisions of the GPL or the LGPL are applicable instead
34 : * of those above. If you wish to allow use of your version of this file only
35 : * under the terms of either the GPL or the LGPL, and not to allow others to
36 : * use your version of this file under the terms of the MPL, indicate your
37 : * decision by deleting the provisions above and replace them with the notice
38 : * and other provisions required by the GPL or the LGPL. If you do not delete
39 : * the provisions above, a recipient may use your version of this file under
40 : * the terms of any one of the MPL, the GPL or the LGPL.
41 : *
42 : * ***** END LICENSE BLOCK ***** */
43 : #include "nsBidiUtils.h"
44 : #include "bidicattable.h"
45 : #include "nsCharTraits.h"
46 :
47 : static nsCharType ebc2ucd[15] = {
48 : eCharType_OtherNeutral, /* Placeholder -- there will never be a 0 index value */
49 : eCharType_LeftToRight,
50 : eCharType_RightToLeft,
51 : eCharType_RightToLeftArabic,
52 : eCharType_ArabicNumber,
53 : eCharType_EuropeanNumber,
54 : eCharType_EuropeanNumberSeparator,
55 : eCharType_EuropeanNumberTerminator,
56 : eCharType_CommonNumberSeparator,
57 : eCharType_OtherNeutral,
58 : eCharType_DirNonSpacingMark,
59 : eCharType_BoundaryNeutral,
60 : eCharType_BlockSeparator,
61 : eCharType_SegmentSeparator,
62 : eCharType_WhiteSpaceNeutral
63 : };
64 :
65 : static nsCharType cc2ucd[5] = {
66 : eCharType_LeftToRightEmbedding,
67 : eCharType_RightToLeftEmbedding,
68 : eCharType_PopDirectionalFormat,
69 : eCharType_LeftToRightOverride,
70 : eCharType_RightToLeftOverride
71 : };
72 :
73 : #define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
74 : #define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS)
75 : #define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS)
76 : #define NUM_TO_ARABIC(c) \
77 : ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
78 : ((c) - (PRUint16)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
79 : ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
80 : ((c) - (PRUint16)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
81 : (c)))
82 : #define NUM_TO_HINDI(c) \
83 : ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
84 : ((c) + (PRUint16)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
85 : ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
86 : ((c) + (PRUint16)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
87 : (c)))
88 : #define NUM_TO_PERSIAN(c) \
89 : ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
90 : ((c) - (PRUint16)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
91 : ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
92 : ((c) + (PRUint16)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
93 : (c)))
94 :
95 0 : PRUnichar HandleNumberInChar(PRUnichar aChar, bool aPrevCharArabic, PRUint32 aNumFlag)
96 : {
97 : // IBMBIDI_NUMERAL_NOMINAL *
98 : // IBMBIDI_NUMERAL_REGULAR
99 : // IBMBIDI_NUMERAL_HINDICONTEXT
100 : // IBMBIDI_NUMERAL_ARABIC
101 : // IBMBIDI_NUMERAL_HINDI
102 :
103 0 : switch (aNumFlag) {
104 : case IBMBIDI_NUMERAL_HINDI:
105 0 : return NUM_TO_HINDI(aChar);
106 : case IBMBIDI_NUMERAL_ARABIC:
107 0 : return NUM_TO_ARABIC(aChar);
108 : case IBMBIDI_NUMERAL_PERSIAN:
109 0 : return NUM_TO_PERSIAN(aChar);
110 : case IBMBIDI_NUMERAL_REGULAR:
111 : case IBMBIDI_NUMERAL_HINDICONTEXT:
112 : case IBMBIDI_NUMERAL_PERSIANCONTEXT:
113 : // for clipboard handling
114 : //XXX do we really want to convert numerals when copying text?
115 0 : if (aPrevCharArabic) {
116 0 : if (aNumFlag == IBMBIDI_NUMERAL_PERSIANCONTEXT)
117 0 : return NUM_TO_PERSIAN(aChar);
118 : else
119 0 : return NUM_TO_HINDI(aChar);
120 : }
121 : else
122 0 : return NUM_TO_ARABIC(aChar);
123 : case IBMBIDI_NUMERAL_NOMINAL:
124 : default:
125 0 : return aChar;
126 : }
127 : }
128 :
129 0 : nsresult HandleNumbers(PRUnichar* aBuffer, PRUint32 aSize, PRUint32 aNumFlag)
130 : {
131 : PRUint32 i;
132 :
133 0 : switch (aNumFlag) {
134 : case IBMBIDI_NUMERAL_HINDI:
135 : case IBMBIDI_NUMERAL_ARABIC:
136 : case IBMBIDI_NUMERAL_PERSIAN:
137 : case IBMBIDI_NUMERAL_REGULAR:
138 : case IBMBIDI_NUMERAL_HINDICONTEXT:
139 : case IBMBIDI_NUMERAL_PERSIANCONTEXT:
140 0 : for (i=0;i<aSize;i++)
141 0 : aBuffer[i] = HandleNumberInChar(aBuffer[i], !!(i>0 ? aBuffer[i-1] : 0), aNumFlag);
142 0 : break;
143 : case IBMBIDI_NUMERAL_NOMINAL:
144 : default:
145 0 : break;
146 : }
147 0 : return NS_OK;
148 : }
149 :
150 : #define LRM_CHAR 0x200e
151 0 : bool IsBidiControl(PRUint32 aChar)
152 : {
153 : // This method is used when stripping Bidi control characters for
154 : // display, so it will return TRUE for LRM and RLM as
155 : // well as the characters with category eBidiCat_CC
156 0 : return (eBidiCat_CC == GetBidiCat(aChar) || ((aChar)&0xfffffe)==LRM_CHAR);
157 : }
158 :
159 0 : bool HasRTLChars(const nsAString& aString)
160 : {
161 : // This is used to determine whether to enable bidi if a string has
162 : // right-to-left characters. To simplify things, anything that could be a
163 : // surrogate or RTL presentation form is covered just by testing >= 0xD800).
164 : // It's fine to enable bidi in rare cases where it actually isn't needed.
165 0 : PRInt32 length = aString.Length();
166 0 : for (PRInt32 i = 0; i < length; i++) {
167 0 : PRUnichar ch = aString.CharAt(i);
168 0 : if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) {
169 0 : return true;
170 : }
171 : }
172 0 : return false;
173 : }
174 :
175 0 : nsCharType GetCharType(PRUint32 aChar)
176 : {
177 : nsCharType oResult;
178 0 : eBidiCategory bCat = GetBidiCat(aChar);
179 0 : if (eBidiCat_CC != bCat) {
180 0 : NS_ASSERTION((PRUint32) bCat < (sizeof(ebc2ucd)/sizeof(nsCharType)), "size mismatch");
181 0 : if((PRUint32) bCat < (sizeof(ebc2ucd)/sizeof(nsCharType)))
182 0 : oResult = ebc2ucd[bCat];
183 : else
184 0 : oResult = ebc2ucd[0]; // something is very wrong, but we need to return a value
185 : } else {
186 0 : NS_ASSERTION((aChar-0x202a) < (sizeof(cc2ucd)/sizeof(nsCharType)), "size mismatch");
187 0 : if((aChar-0x202a) < (sizeof(cc2ucd)/sizeof(nsCharType)))
188 0 : oResult = cc2ucd[aChar - 0x202a];
189 : else
190 0 : oResult = ebc2ucd[0]; // something is very wrong, but we need to return a value
191 : }
192 0 : return oResult;
193 : }
|