Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <i18nlangtag/mslangid.hxx>
21 : #include <rtl/ustrbuf.hxx>
22 : #include <sal/macros.h>
23 : #include <nativenumbersupplier.hxx>
24 : #include <localedata.hxx>
25 : #include <data/numberchar.h>
26 : #include <comphelper/string.hxx>
27 :
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::lang;
30 : using namespace ::rtl;
31 :
32 :
33 : typedef struct {
34 : sal_Int16 number;
35 : sal_Unicode *multiplierChar;
36 : sal_Int16 numberFlag;
37 : sal_Int16 exponentCount;
38 : sal_Int16 *multiplierExponent;
39 : } Number;
40 :
41 :
42 : #define NUMBER_OMIT_ZERO (1 << 0)
43 : #define NUMBER_OMIT_ONLY_ZERO (1 << 1)
44 : #define NUMBER_OMIT_ONE_1 (1 << 2)
45 : #define NUMBER_OMIT_ONE_2 (1 << 3)
46 : #define NUMBER_OMIT_ONE_3 (1 << 4)
47 : #define NUMBER_OMIT_ONE_4 (1 << 5)
48 : #define NUMBER_OMIT_ONE_5 (1 << 6)
49 : #define NUMBER_OMIT_ONE_6 (1 << 7)
50 : #define NUMBER_OMIT_ONE_7 (1 << 8)
51 : #define NUMBER_OMIT_ONE (NUMBER_OMIT_ONE_1|NUMBER_OMIT_ONE_2|NUMBER_OMIT_ONE_3|NUMBER_OMIT_ONE_4|NUMBER_OMIT_ONE_5|NUMBER_OMIT_ONE_6|NUMBER_OMIT_ONE_7)
52 : #define NUMBER_OMIT_ONE_CHECK(bit) (1 << (2 + bit))
53 : #define NUMBER_OMIT_ALL ( NUMBER_OMIT_ZERO|NUMBER_OMIT_ONE|NUMBER_OMIT_ONLY_ZERO )
54 : #define NUMBER_OMIT_ZERO_ONE ( NUMBER_OMIT_ZERO|NUMBER_OMIT_ONE )
55 : #define NUMBER_OMIT_ONE_67 (NUMBER_OMIT_ONE_6|NUMBER_OMIT_ONE_7)
56 : #define NUMBER_OMIT_ZERO_ONE_67 ( NUMBER_OMIT_ZERO|NUMBER_OMIT_ONE_67 )
57 :
58 : namespace com { namespace sun { namespace star { namespace i18n {
59 :
60 : OUString SAL_CALL getHebrewNativeNumberString(const OUString& aNumberString, sal_Bool useGeresh);
61 :
62 7 : OUString SAL_CALL AsciiToNativeChar( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
63 : Sequence< sal_Int32 >& offset, sal_Bool useOffset, sal_Int16 number ) throw(RuntimeException)
64 : {
65 7 : const sal_Unicode *src = inStr.getStr() + startPos;
66 7 : rtl_uString *newStr = rtl_uString_alloc(nCount);
67 7 : if (useOffset)
68 0 : offset.realloc(nCount);
69 :
70 25 : for (sal_Int32 i = 0; i < nCount; i++)
71 : {
72 18 : sal_Unicode ch = src[i];
73 18 : if (isNumber(ch))
74 17 : newStr->buffer[i] = NumberChar[number][ ch - NUMBER_ZERO ];
75 1 : else if (i+1 < nCount && isNumber(src[i+1])) {
76 2 : if (i > 0 && isNumber(src[i-1]) && isSeparator(ch))
77 0 : newStr->buffer[i] = SeparatorChar[number] ? SeparatorChar[number] : ch;
78 : else
79 2 : newStr->buffer[i] = isDecimal(ch) ? (DecimalChar[number] ? DecimalChar[number] : ch) :
80 2 : isMinus(ch) ? (MinusChar[number] ? MinusChar[number] : ch) : ch;
81 : }
82 : else
83 0 : newStr->buffer[i] = ch;
84 18 : if (useOffset)
85 0 : offset[i] = startPos + i;
86 : }
87 7 : return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
88 : }
89 :
90 0 : sal_Bool SAL_CALL AsciiToNative_numberMaker(const sal_Unicode *str, sal_Int32 begin, sal_Int32 len,
91 : sal_Unicode *dst, sal_Int32& count, sal_Int16 multiChar_index, Sequence< sal_Int32 >& offset, sal_Bool useOffset, sal_Int32 startPos,
92 : Number *number, sal_Unicode* numberChar)
93 : {
94 0 : sal_Unicode multiChar = (multiChar_index == -1 ? 0 : number->multiplierChar[multiChar_index]);
95 0 : if ( len <= number->multiplierExponent[number->exponentCount-1] ) {
96 0 : if (number->multiplierExponent[number->exponentCount-1] > 1) {
97 : sal_Int16 i;
98 0 : sal_Bool notZero = false;
99 0 : for (i = 0; i < len; i++, begin++) {
100 0 : if (notZero || str[begin] != NUMBER_ZERO) {
101 0 : dst[count] = numberChar[str[begin] - NUMBER_ZERO];
102 0 : if (useOffset)
103 0 : offset[count] = begin + startPos;
104 0 : count++;
105 0 : notZero = sal_True;
106 : }
107 : }
108 0 : if (notZero && multiChar > 0) {
109 0 : dst[count] = multiChar;
110 0 : if (useOffset)
111 0 : offset[count] = begin + startPos;
112 0 : count++;
113 : }
114 0 : return notZero;
115 0 : } else if (str[begin] != NUMBER_ZERO) {
116 0 : if (!(number->numberFlag & (multiChar_index < 0 ? 0 : NUMBER_OMIT_ONE_CHECK(multiChar_index))) || str[begin] != NUMBER_ONE) {
117 0 : dst[count] = numberChar[str[begin] - NUMBER_ZERO];
118 0 : if (useOffset)
119 0 : offset[count] = begin + startPos;
120 0 : count++;
121 : }
122 0 : if (multiChar > 0) {
123 0 : dst[count] = multiChar;
124 0 : if (useOffset)
125 0 : offset[count] = begin + startPos;
126 0 : count++;
127 : }
128 0 : } else if (!(number->numberFlag & NUMBER_OMIT_ZERO) && count > 0 && dst[count-1] != numberChar[0]) {
129 0 : dst[count] = numberChar[0];
130 0 : if (useOffset)
131 0 : offset[count] = begin + startPos;
132 0 : count++;
133 : }
134 0 : return str[begin] != NUMBER_ZERO;
135 : } else {
136 0 : sal_Bool printPower = sal_False;
137 : // sal_Int16 last = 0;
138 0 : for (sal_Int16 i = 1; i <= number->exponentCount; i++) {
139 0 : sal_Int32 tmp = len - (i == number->exponentCount ? 0 : number->multiplierExponent[i]);
140 0 : if (tmp > 0) {
141 : printPower |= AsciiToNative_numberMaker(str, begin, tmp, dst, count,
142 0 : (i == number->exponentCount ? -1 : i), offset, useOffset, startPos, number, numberChar);
143 0 : begin += tmp;
144 0 : len -= tmp;
145 : }
146 : }
147 0 : if (printPower) {
148 0 : if (count > 0 && number->multiplierExponent[number->exponentCount-1] == 1 &&
149 0 : dst[count-1] == numberChar[0])
150 0 : count--;
151 0 : if (multiChar > 0) {
152 0 : dst[count] = multiChar;
153 0 : if (useOffset)
154 0 : offset[count] = begin + startPos;
155 0 : count++;
156 : }
157 : }
158 0 : return printPower;
159 : }
160 : }
161 :
162 0 : OUString SAL_CALL AsciiToNative( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
163 : Sequence< sal_Int32 >& offset, sal_Bool useOffset, Number* number ) throw(RuntimeException)
164 : {
165 0 : OUString aRet;
166 :
167 0 : sal_Int32 strLen = inStr.getLength() - startPos;
168 0 : sal_Unicode *numberChar = NumberChar[number->number];
169 :
170 0 : if (nCount > strLen)
171 0 : nCount = strLen;
172 :
173 0 : if (nCount > 0)
174 : {
175 0 : const sal_Unicode *str = inStr.getStr() + startPos;
176 0 : sal_Unicode *newStr = new sal_Unicode[nCount * 2 + 1];
177 0 : sal_Unicode *srcStr = new sal_Unicode[nCount + 1]; // for keeping number without comma
178 0 : sal_Int32 i, len = 0, count = 0;
179 :
180 0 : if (useOffset)
181 0 : offset.realloc( nCount * 2 );
182 0 : sal_Bool doDecimal = sal_False;
183 :
184 0 : for (i = 0; i <= nCount; i++)
185 : {
186 0 : if (i < nCount && isNumber(str[i])) {
187 0 : if (doDecimal) {
188 0 : newStr[count] = numberChar[str[i] - NUMBER_ZERO];
189 0 : if (useOffset)
190 0 : offset[count] = i + startPos;
191 0 : count++;
192 : }
193 : else
194 0 : srcStr[len++] = str[i];
195 : } else {
196 0 : if (len > 0) {
197 0 : if (isSeparator(str[i]) && i < nCount-1 && isNumber(str[i+1]))
198 0 : continue; // skip comma inside number string
199 0 : sal_Bool notZero = sal_False;
200 0 : for (sal_Int32 begin = 0, end = len % number->multiplierExponent[0];
201 0 : end <= len; begin = end, end += number->multiplierExponent[0]) {
202 0 : if (end == 0) continue;
203 0 : sal_Int32 _count = count;
204 : notZero |= AsciiToNative_numberMaker(srcStr, begin, end - begin, newStr, count,
205 0 : end == len ? -1 : 0, offset, useOffset, i - len + startPos, number, numberChar);
206 0 : if (count > 0 && number->multiplierExponent[number->exponentCount-1] == 1 &&
207 0 : newStr[count-1] == numberChar[0])
208 0 : count--;
209 0 : if (notZero && _count == count) {
210 0 : if (end != len) {
211 0 : newStr[count] = number->multiplierChar[0];
212 0 : if (useOffset)
213 0 : offset[count] = i - len + startPos;
214 0 : count++;
215 : }
216 : }
217 : }
218 0 : if (! notZero && ! (number->numberFlag & NUMBER_OMIT_ONLY_ZERO)) {
219 0 : newStr[count] = numberChar[0];
220 0 : if (useOffset)
221 0 : offset[count] = i - len + startPos;
222 0 : count++;
223 : }
224 0 : len = 0;
225 : }
226 0 : if (i < nCount) {
227 0 : if ((doDecimal = (!doDecimal && isDecimal(str[i]) && i < nCount-1 && isNumber(str[i+1]))) != sal_False)
228 0 : newStr[count] = (DecimalChar[number->number] ? DecimalChar[number->number] : str[i]);
229 0 : else if (isMinus(str[i]) && i < nCount-1 && isNumber(str[i+1]))
230 0 : newStr[count] = (MinusChar[number->number] ? MinusChar[number->number] : str[i]);
231 0 : else if (isSeparator(str[i]) && i < nCount-1 && isNumber(str[i+1]))
232 0 : newStr[count] = (SeparatorChar[number->number] ? SeparatorChar[number->number] : str[i]);
233 : else
234 0 : newStr[count] = str[i];
235 0 : if (useOffset)
236 0 : offset[count] = i + startPos;
237 0 : count++;
238 : }
239 : }
240 : }
241 :
242 0 : delete[] srcStr;
243 :
244 0 : if (useOffset)
245 0 : offset.realloc(count);
246 0 : aRet = OUString(newStr, count);
247 0 : delete[] newStr;
248 : }
249 0 : return aRet;
250 : }
251 0 : static void SAL_CALL NativeToAscii_numberMaker(sal_Int16 max, sal_Int16 prev, const sal_Unicode *str,
252 : sal_Int32& i, sal_Int32 nCount, sal_Unicode *dst, sal_Int32& count, Sequence< sal_Int32 >& offset, sal_Bool useOffset,
253 : OUString& numberChar, OUString& multiplierChar)
254 : {
255 0 : sal_Int16 curr = 0, num = 0, end = 0, shift = 0;
256 0 : while (++i < nCount) {
257 0 : if ((curr = sal::static_int_cast<sal_Int16>( numberChar.indexOf(str[i]) )) >= 0) {
258 0 : if (num > 0)
259 0 : break;
260 0 : num = curr % 10;
261 0 : } else if ((curr = sal::static_int_cast<sal_Int16>( multiplierChar.indexOf(str[i]) )) >= 0) {
262 0 : curr = MultiplierExponent_7_CJK[curr % ExponentCount_7_CJK];
263 0 : if (prev > curr && num == 0) num = 1; // One may be omitted in informal format
264 0 : shift = end = 0;
265 0 : if (curr >= max)
266 0 : max = curr;
267 0 : else if (curr > prev)
268 0 : shift = max - curr;
269 : else
270 0 : end = curr;
271 0 : while (end++ < prev) {
272 0 : dst[count] = NUMBER_ZERO + (end == prev ? num : 0);
273 0 : if (useOffset)
274 0 : offset[count] = i;
275 0 : count++;
276 : }
277 0 : if (shift) {
278 0 : count -= max;
279 0 : for (sal_Int16 j = 0; j < shift; j++, count++) {
280 0 : dst[count] = dst[count + curr];
281 0 : if (useOffset)
282 0 : offset[count] = offset[count + curr];
283 : }
284 0 : max = curr;
285 : }
286 : NativeToAscii_numberMaker(max, curr, str, i, nCount, dst,
287 0 : count, offset, useOffset, numberChar, multiplierChar);
288 0 : return;
289 : } else
290 0 : break;
291 : }
292 0 : while (end++ < prev) {
293 0 : dst[count] = NUMBER_ZERO + (end == prev ? num : 0);
294 0 : if (useOffset)
295 0 : offset[count] = i - 1;
296 0 : count++;
297 : }
298 : }
299 :
300 0 : static OUString SAL_CALL NativeToAscii(const OUString& inStr,
301 : sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, sal_Bool useOffset ) throw(RuntimeException)
302 : {
303 0 : OUString aRet;
304 :
305 0 : sal_Int32 strLen = inStr.getLength() - startPos;
306 :
307 0 : if (nCount > strLen)
308 0 : nCount = strLen;
309 :
310 0 : if (nCount > 0) {
311 0 : const sal_Unicode *str = inStr.getStr() + startPos;
312 0 : sal_Unicode *newStr = new sal_Unicode[nCount * MultiplierExponent_7_CJK[0] + 2];
313 0 : if (useOffset)
314 0 : offset.realloc( nCount * MultiplierExponent_7_CJK[0] + 1 );
315 0 : sal_Int32 count = 0, index;
316 : sal_Int32 i;
317 :
318 0 : OUString numberChar, multiplierChar, decimalChar, minusChar, separatorChar;
319 0 : numberChar = OUString((sal_Unicode*)NumberChar, 10*NumberChar_Count);
320 0 : multiplierChar = OUString((sal_Unicode*) MultiplierChar_7_CJK, ExponentCount_7_CJK*Multiplier_Count);
321 0 : decimalChar = OUString(DecimalChar, NumberChar_Count);
322 0 : minusChar = OUString(MinusChar, NumberChar_Count);
323 0 : separatorChar = OUString(SeparatorChar, NumberChar_Count);
324 :
325 0 : for ( i = 0; i < nCount; i++) {
326 0 : if ((index = multiplierChar.indexOf(str[i])) >= 0) {
327 0 : if (count == 0 || !isNumber(newStr[count-1])) { // add 1 in front of multiplier
328 0 : newStr[count] = NUMBER_ONE;
329 0 : if (useOffset)
330 0 : offset[count] = i;
331 0 : count++;
332 : }
333 0 : index = MultiplierExponent_7_CJK[index % ExponentCount_7_CJK];
334 : NativeToAscii_numberMaker(
335 0 : sal::static_int_cast<sal_Int16>( index ), sal::static_int_cast<sal_Int16>( index ),
336 : str, i, nCount, newStr, count, offset, useOffset,
337 0 : numberChar, multiplierChar);
338 : } else {
339 0 : if ((index = numberChar.indexOf(str[i])) >= 0)
340 0 : newStr[count] = sal::static_int_cast<sal_Unicode>( (index % 10) + NUMBER_ZERO );
341 0 : else if ((index = separatorChar.indexOf(str[i])) >= 0 &&
342 0 : (i < nCount-1 && (numberChar.indexOf(str[i+1]) >= 0 ||
343 0 : multiplierChar.indexOf(str[i+1]) >= 0)))
344 0 : newStr[count] = SeparatorChar[NumberChar_HalfWidth];
345 0 : else if ((index = decimalChar.indexOf(str[i])) >= 0 &&
346 0 : (i < nCount-1 && (numberChar.indexOf(str[i+1]) >= 0 ||
347 0 : multiplierChar.indexOf(str[i+1]) >= 0)))
348 : // Only when decimal point is followed by numbers,
349 : // it will be convert to ASCII decimal point
350 0 : newStr[count] = DecimalChar[NumberChar_HalfWidth];
351 0 : else if ((index = minusChar.indexOf(str[i])) >= 0 &&
352 0 : (i < nCount-1 && (numberChar.indexOf(str[i+1]) >= 0 ||
353 0 : multiplierChar.indexOf(str[i+1]) >= 0)))
354 : // Only when minus is followed by numbers,
355 : // it will be convert to ASCII minus sign
356 0 : newStr[count] = MinusChar[NumberChar_HalfWidth];
357 : else
358 0 : newStr[count] = str[i];
359 0 : if (useOffset)
360 0 : offset[count] = i;
361 0 : count++;
362 : }
363 : }
364 :
365 0 : if (useOffset) {
366 0 : offset.realloc(count);
367 0 : for (i = 0; i < count; i++)
368 0 : offset[i] += startPos;
369 : }
370 0 : aRet = OUString(newStr, count);
371 0 : delete[] newStr;
372 : }
373 0 : return aRet;
374 : }
375 :
376 : static Number natnum4[4] = {
377 : { NumberChar_Lower_zh, MultiplierChar_6_CJK[Multiplier_Lower_zh], 0,
378 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
379 : { NumberChar_Lower_zh, MultiplierChar_6_CJK[Multiplier_Lower_zh_TW], 0,
380 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
381 : { NumberChar_Modern_ja, MultiplierChar_7_CJK[Multiplier_Modern_ja], NUMBER_OMIT_ZERO_ONE_67,
382 : ExponentCount_7_CJK, MultiplierExponent_7_CJK },
383 : { NumberChar_Lower_ko, MultiplierChar_6_CJK[Multiplier_Lower_ko], NUMBER_OMIT_ZERO,
384 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
385 : };
386 :
387 : static Number natnum5[4] = {
388 : { NumberChar_Upper_zh, MultiplierChar_6_CJK[Multiplier_Upper_zh], 0,
389 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
390 : { NumberChar_Upper_zh_TW, MultiplierChar_6_CJK[Multiplier_Upper_zh_TW], 0,
391 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
392 : { NumberChar_Traditional_ja, MultiplierChar_7_CJK[Multiplier_Traditional_ja], NUMBER_OMIT_ZERO_ONE_67,
393 : ExponentCount_7_CJK, MultiplierExponent_7_CJK },
394 : { NumberChar_Upper_ko, MultiplierChar_6_CJK[Multiplier_Upper_zh_TW], NUMBER_OMIT_ZERO,
395 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
396 : };
397 :
398 : static Number natnum6[4] = {
399 : { NumberChar_FullWidth, MultiplierChar_6_CJK[Multiplier_Lower_zh], 0,
400 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
401 : { NumberChar_FullWidth, MultiplierChar_6_CJK[Multiplier_Lower_zh_TW], 0,
402 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
403 : { NumberChar_FullWidth, MultiplierChar_7_CJK[Multiplier_Modern_ja], NUMBER_OMIT_ZERO_ONE_67,
404 : ExponentCount_7_CJK, MultiplierExponent_7_CJK },
405 : { NumberChar_FullWidth, MultiplierChar_6_CJK[Multiplier_Hangul_ko], NUMBER_OMIT_ZERO,
406 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
407 : };
408 :
409 : static Number natnum7[4] = {
410 : { NumberChar_Lower_zh, MultiplierChar_6_CJK[Multiplier_Lower_zh], NUMBER_OMIT_ALL,
411 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
412 : { NumberChar_Lower_zh, MultiplierChar_6_CJK[Multiplier_Lower_zh_TW], NUMBER_OMIT_ALL,
413 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
414 : { NumberChar_Modern_ja, MultiplierChar_2_CJK[Multiplier_Modern_ja], NUMBER_OMIT_ZERO_ONE,
415 : ExponentCount_2_CJK, MultiplierExponent_2_CJK },
416 : { NumberChar_Lower_ko, MultiplierChar_6_CJK[Multiplier_Lower_ko], NUMBER_OMIT_ALL,
417 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
418 : };
419 :
420 : static Number natnum8[4] = {
421 : { NumberChar_Upper_zh, MultiplierChar_6_CJK[Multiplier_Upper_zh], NUMBER_OMIT_ALL,
422 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
423 : { NumberChar_Upper_zh_TW, MultiplierChar_6_CJK[Multiplier_Upper_zh_TW], NUMBER_OMIT_ALL,
424 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
425 : { NumberChar_Traditional_ja, MultiplierChar_2_CJK[Multiplier_Traditional_ja], NUMBER_OMIT_ZERO_ONE,
426 : ExponentCount_2_CJK, MultiplierExponent_2_CJK },
427 : { NumberChar_Upper_ko, MultiplierChar_6_CJK[Multiplier_Upper_zh_TW], NUMBER_OMIT_ALL,
428 : ExponentCount_6_CJK, MultiplierExponent_6_CJK },
429 : };
430 :
431 : static Number natnum10 = { NumberChar_Hangul_ko, MultiplierChar_6_CJK[Multiplier_Hangul_ko], NUMBER_OMIT_ZERO,
432 : ExponentCount_6_CJK, MultiplierExponent_6_CJK };
433 : static Number natnum11 = { NumberChar_Hangul_ko, MultiplierChar_6_CJK[Multiplier_Hangul_ko], NUMBER_OMIT_ALL,
434 : ExponentCount_6_CJK, MultiplierExponent_6_CJK };
435 :
436 : //! ATTENTION: Do not change order of elements!
437 : //! Append new languages to the end of the list!
438 : static const sal_Char *natnum1Locales[] = {
439 : "zh_CN",
440 : "zh_TW",
441 : "ja",
442 : "ko",
443 : "he",
444 : "ar",
445 : "th",
446 : "hi",
447 : "or",
448 : "mr",
449 : "bn",
450 : "pa",
451 : "gu",
452 : "ta",
453 : "te",
454 : "kn",
455 : "ml",
456 : "lo",
457 : "bo",
458 : "my",
459 : "km",
460 : "mn",
461 : "ne",
462 : "dz",
463 : "fa"
464 : };
465 : static sal_Int16 nbOfLocale = SAL_N_ELEMENTS(natnum1Locales);
466 :
467 : //! ATTENTION: Do not change order of elements!
468 : //! Number and order must match elements of natnum1Locales!
469 : static sal_Int16 natnum1[] = {
470 : NumberChar_Lower_zh,
471 : NumberChar_Lower_zh,
472 : NumberChar_Modern_ja,
473 : NumberChar_Lower_ko,
474 : NumberChar_he,
475 : NumberChar_Indic_ar,
476 : NumberChar_th,
477 : NumberChar_hi,
478 : NumberChar_or,
479 : NumberChar_mr,
480 : NumberChar_bn,
481 : NumberChar_pa,
482 : NumberChar_gu,
483 : NumberChar_ta,
484 : NumberChar_te,
485 : NumberChar_kn,
486 : NumberChar_ml,
487 : NumberChar_lo,
488 : NumberChar_bo,
489 : NumberChar_my,
490 : NumberChar_km,
491 : NumberChar_mn,
492 : NumberChar_ne,
493 : NumberChar_dz,
494 : NumberChar_EastIndic_ar
495 : };
496 : static sal_Int16 sizeof_natnum1 = SAL_N_ELEMENTS(natnum1);
497 :
498 : //! ATTENTION: Do not change order of elements!
499 : //! Order must match first elements of natnum1Locales!
500 : static sal_Int16 natnum2[] = {
501 : NumberChar_Upper_zh,
502 : NumberChar_Upper_zh_TW,
503 : NumberChar_Traditional_ja,
504 : NumberChar_Upper_ko,
505 : NumberChar_he
506 : };
507 : static sal_Int16 sizeof_natnum2 = SAL_N_ELEMENTS(natnum2);
508 :
509 : #define isLang(lang) rLocale.Language.equalsAsciiL(lang, 2)
510 :
511 14 : static sal_Int16 SAL_CALL getLanguageNumber( const Locale& rLocale)
512 : {
513 : // return zh_TW for TW, HK and MO, return zh_CN for other zh locales.
514 14 : if (isLang("zh")) return MsLangId::isTraditionalChinese(rLocale) ? 1 : 0;
515 :
516 70 : for (sal_Int16 i = 2; i < nbOfLocale; i++)
517 70 : if (isLang(natnum1Locales[i]))
518 14 : return i;
519 :
520 0 : return -1;
521 : }
522 :
523 7 : OUString SAL_CALL NativeNumberSupplier::getNativeNumberString(const OUString& aNumberString, const Locale& rLocale,
524 : sal_Int16 nNativeNumberMode, Sequence< sal_Int32 >& offset) throw (RuntimeException)
525 : {
526 7 : Number *number = 0;
527 7 : sal_Int16 num = -1;
528 :
529 7 : if (isValidNatNum(rLocale, nNativeNumberMode)) {
530 7 : sal_Int16 langnum = getLanguageNumber(rLocale);
531 7 : switch (nNativeNumberMode) {
532 : case NativeNumberMode::NATNUM0: // Ascii
533 0 : return NativeToAscii(aNumberString, 0, aNumberString.getLength(), offset, useOffset);
534 : case NativeNumberMode::NATNUM1: // Char, Lower
535 7 : num = natnum1[langnum];
536 7 : break;
537 : case NativeNumberMode::NATNUM2: // Char, Upper
538 0 : num = natnum2[langnum];
539 0 : break;
540 : case NativeNumberMode::NATNUM3: // Char, FullWidth
541 0 : num = NumberChar_FullWidth;
542 0 : break;
543 : case NativeNumberMode::NATNUM4: // Text, Lower, Long
544 0 : number = &natnum4[langnum];
545 0 : break;
546 : case NativeNumberMode::NATNUM5: // Text, Upper, Long
547 0 : number = &natnum5[langnum];
548 0 : break;
549 : case NativeNumberMode::NATNUM6: // Text, FullWidth
550 0 : number = &natnum6[langnum];
551 0 : break;
552 : case NativeNumberMode::NATNUM7: // Text. Lower, Short
553 0 : number = &natnum7[langnum];
554 0 : break;
555 : case NativeNumberMode::NATNUM8: // Text, Upper, Short
556 0 : number = &natnum8[langnum];
557 0 : break;
558 : case NativeNumberMode::NATNUM9: // Char, Hangul
559 0 : num = NumberChar_Hangul_ko;
560 0 : break;
561 : case NativeNumberMode::NATNUM10: // Text, Hangul, Long
562 0 : number = &natnum10;
563 0 : break;
564 : case NativeNumberMode::NATNUM11: // Text, Hangul, Short
565 0 : number = &natnum11;
566 0 : break;
567 : default:
568 0 : break;
569 : }
570 : }
571 :
572 7 : if (number || num >= 0) {
573 19 : if (!aLocale.Language.equals(rLocale.Language) ||
574 12 : !aLocale.Country.equals(rLocale.Country) ||
575 5 : !aLocale.Variant.equals(rLocale.Variant)) {
576 2 : LocaleDataItem item = LocaleData().getLocaleItem( rLocale );
577 2 : aLocale = rLocale;
578 2 : DecimalChar[NumberChar_HalfWidth]=item.decimalSeparator.toChar();
579 2 : if (DecimalChar[NumberChar_HalfWidth] > 0x7E || DecimalChar[NumberChar_HalfWidth] < 0x21)
580 0 : DecimalChar[NumberChar_FullWidth]=0xFF0E;
581 : else
582 2 : DecimalChar[NumberChar_FullWidth]=DecimalChar[NumberChar_HalfWidth]+0xFEE0;
583 2 : SeparatorChar[NumberChar_HalfWidth]=item.thousandSeparator.toChar();
584 2 : if (SeparatorChar[NumberChar_HalfWidth] > 0x7E || SeparatorChar[NumberChar_HalfWidth] < 0x21)
585 0 : SeparatorChar[NumberChar_FullWidth]=0xFF0C;
586 : else
587 2 : SeparatorChar[NumberChar_FullWidth]=SeparatorChar[NumberChar_HalfWidth]+0xFEE0;
588 : }
589 7 : if (number)
590 0 : return AsciiToNative( aNumberString, 0, aNumberString.getLength(), offset, useOffset, number );
591 7 : else if (num == NumberChar_he)
592 : return getHebrewNativeNumberString(aNumberString,
593 0 : nNativeNumberMode == NativeNumberMode::NATNUM2);
594 : else
595 7 : return AsciiToNativeChar(aNumberString, 0, aNumberString.getLength(), offset, useOffset, num);
596 : }
597 : else
598 0 : return aNumberString;
599 : }
600 :
601 7 : OUString SAL_CALL NativeNumberSupplier::getNativeNumberString(const OUString& aNumberString, const Locale& rLocale,
602 : sal_Int16 nNativeNumberMode) throw (RuntimeException)
603 : {
604 7 : Sequence< sal_Int32 > offset;
605 7 : return getNativeNumberString(aNumberString, rLocale, nNativeNumberMode, offset);
606 : }
607 :
608 0 : sal_Unicode SAL_CALL NativeNumberSupplier::getNativeNumberChar( const sal_Unicode inChar, const Locale& rLocale, sal_Int16 nNativeNumberMode ) throw(com::sun::star::uno::RuntimeException)
609 : {
610 0 : if (nNativeNumberMode == NativeNumberMode::NATNUM0) { // Ascii
611 0 : for (sal_Int16 i = 0; i < NumberChar_Count; i++)
612 0 : for (sal_Int16 j = 0; j < 10; j++)
613 0 : if (inChar == NumberChar[i][j])
614 0 : return j;
615 0 : return inChar;
616 : }
617 0 : else if (isNumber(inChar) && isValidNatNum(rLocale, nNativeNumberMode)) {
618 0 : sal_Int16 langnum = getLanguageNumber(rLocale);
619 0 : switch (nNativeNumberMode) {
620 : case NativeNumberMode::NATNUM1: // Char, Lower
621 : case NativeNumberMode::NATNUM4: // Text, Lower, Long
622 : case NativeNumberMode::NATNUM7: // Text. Lower, Short
623 0 : return NumberChar[natnum1[langnum]][inChar - NUMBER_ZERO];
624 : case NativeNumberMode::NATNUM2: // Char, Upper
625 : case NativeNumberMode::NATNUM5: // Text, Upper, Long
626 : case NativeNumberMode::NATNUM8: // Text, Upper, Short
627 0 : return NumberChar[natnum2[langnum]][inChar - NUMBER_ZERO];
628 : case NativeNumberMode::NATNUM3: // Char, FullWidth
629 : case NativeNumberMode::NATNUM6: // Text, FullWidth
630 0 : return NumberChar[NumberChar_FullWidth][inChar - NUMBER_ZERO];
631 : case NativeNumberMode::NATNUM9: // Char, Hangul
632 : case NativeNumberMode::NATNUM10: // Text, Hangul, Long
633 : case NativeNumberMode::NATNUM11: // Text, Hangul, Short
634 0 : return NumberChar[NumberChar_Hangul_ko][inChar - NUMBER_ZERO];
635 : default:
636 0 : break;
637 : }
638 : }
639 0 : return inChar;
640 : }
641 :
642 7 : sal_Bool SAL_CALL NativeNumberSupplier::isValidNatNum( const Locale& rLocale, sal_Int16 nNativeNumberMode ) throw (RuntimeException)
643 : {
644 7 : sal_Int16 langnum = getLanguageNumber(rLocale);
645 :
646 7 : switch (nNativeNumberMode) {
647 : case NativeNumberMode::NATNUM0: // Ascii
648 : case NativeNumberMode::NATNUM3: // Char, FullWidth
649 0 : return sal_True;
650 : case NativeNumberMode::NATNUM1: // Char, Lower
651 7 : return (langnum >= 0);
652 : case NativeNumberMode::NATNUM2: // Char, Upper
653 0 : if (langnum == 4) // Hebrew numbering
654 0 : return sal_True;
655 : case NativeNumberMode::NATNUM4: // Text, Lower, Long
656 : case NativeNumberMode::NATNUM5: // Text, Upper, Long
657 : case NativeNumberMode::NATNUM6: // Text, FullWidth
658 : case NativeNumberMode::NATNUM7: // Text. Lower, Short
659 : case NativeNumberMode::NATNUM8: // Text, Upper, Short
660 0 : return (langnum >= 0 && langnum < 4); // CJK numbering
661 : case NativeNumberMode::NATNUM9: // Char, Hangul
662 : case NativeNumberMode::NATNUM10: // Text, Hangul, Long
663 : case NativeNumberMode::NATNUM11: // Text, Hangul, Short
664 0 : return (langnum == 3); // Korean numbering
665 : }
666 0 : return sal_False;
667 : }
668 :
669 0 : NativeNumberXmlAttributes SAL_CALL NativeNumberSupplier::convertToXmlAttributes( const Locale& rLocale, sal_Int16 nNativeNumberMode ) throw (RuntimeException)
670 : {
671 : static const sal_Int16 attShort = 0;
672 : static const sal_Int16 attMedium = 1;
673 : static const sal_Int16 attLong = 2;
674 : static const sal_Char *attType[] = { "short", "medium", "long" };
675 :
676 0 : sal_Int16 number = NumberChar_HalfWidth, type = attShort;
677 :
678 0 : if (isValidNatNum(rLocale, nNativeNumberMode)) {
679 0 : sal_Int16 langnum = getLanguageNumber(rLocale);
680 0 : switch (nNativeNumberMode) {
681 : case NativeNumberMode::NATNUM0: // Ascii
682 0 : number = NumberChar_HalfWidth;
683 0 : type = attShort;
684 0 : break;
685 : case NativeNumberMode::NATNUM1: // Char, Lower
686 0 : number = natnum1[langnum];
687 0 : type = attShort;
688 0 : break;
689 : case NativeNumberMode::NATNUM2: // Char, Upper
690 0 : number = natnum2[langnum];
691 0 : type = number == NumberChar_he ? attMedium : attShort;
692 0 : break;
693 : case NativeNumberMode::NATNUM3: // Char, FullWidth
694 0 : number = NumberChar_FullWidth;
695 0 : type = attShort;
696 0 : break;
697 : case NativeNumberMode::NATNUM4: // Text, Lower, Long
698 0 : number = natnum1[langnum];
699 0 : type = attLong;
700 0 : break;
701 : case NativeNumberMode::NATNUM5: // Text, Upper, Long
702 0 : number = natnum2[langnum];
703 0 : type = attLong;
704 0 : break;
705 : case NativeNumberMode::NATNUM6: // Text, FullWidth
706 0 : number = NumberChar_FullWidth;
707 0 : type = attLong;
708 0 : break;
709 : case NativeNumberMode::NATNUM7: // Text. Lower, Short
710 0 : number = natnum1[langnum];
711 0 : type = attMedium;
712 0 : break;
713 : case NativeNumberMode::NATNUM8: // Text, Upper, Short
714 0 : number = natnum2[langnum];
715 0 : type = attMedium;
716 0 : break;
717 : case NativeNumberMode::NATNUM9: // Char, Hangul
718 0 : number = NumberChar_Hangul_ko;
719 0 : type = attShort;
720 0 : break;
721 : case NativeNumberMode::NATNUM10: // Text, Hangul, Long
722 0 : number = NumberChar_Hangul_ko;
723 0 : type = attLong;
724 0 : break;
725 : case NativeNumberMode::NATNUM11: // Text, Hangul, Short
726 0 : number = NumberChar_Hangul_ko;
727 0 : type = attMedium;
728 0 : break;
729 : default:
730 0 : break;
731 : }
732 : }
733 0 : return NativeNumberXmlAttributes(rLocale, OUString(&NumberChar[number][1], 1),
734 0 : OUString::createFromAscii(attType[type]));
735 : }
736 :
737 1 : static sal_Bool natNumIn(sal_Int16 num, sal_Int16 natnum[], sal_Int16 len)
738 : {
739 7 : for (sal_Int16 i = 0; i < len; i++)
740 7 : if (natnum[i] == num)
741 1 : return sal_True;
742 0 : return sal_False;
743 : }
744 :
745 1 : sal_Int16 SAL_CALL NativeNumberSupplier::convertFromXmlAttributes( const NativeNumberXmlAttributes& aAttr ) throw (RuntimeException)
746 : {
747 : sal_Unicode numberChar[NumberChar_Count];
748 32 : for (sal_Int16 i = 0; i < NumberChar_Count; i++)
749 31 : numberChar[i] = NumberChar[i][1];
750 1 : OUString number(numberChar, NumberChar_Count);
751 :
752 1 : sal_Int16 num = sal::static_int_cast<sal_Int16>( number.indexOf(aAttr.Format) );
753 :
754 1 : if ( aAttr.Style == "short" ) {
755 1 : if (num == NumberChar_FullWidth)
756 0 : return NativeNumberMode::NATNUM3;
757 1 : else if (num == NumberChar_Hangul_ko)
758 0 : return NativeNumberMode::NATNUM9;
759 1 : else if (natNumIn(num, natnum1, sizeof_natnum1))
760 1 : return NativeNumberMode::NATNUM1;
761 0 : else if (natNumIn(num, natnum2, sizeof_natnum2))
762 0 : return NativeNumberMode::NATNUM2;
763 0 : } else if ( aAttr.Style == "medium" ) {
764 0 : if (num == NumberChar_Hangul_ko)
765 0 : return NativeNumberMode::NATNUM11;
766 0 : else if (num == NumberChar_he)
767 0 : return NativeNumberMode::NATNUM2;
768 0 : else if (natNumIn(num, natnum1, sizeof_natnum1))
769 0 : return NativeNumberMode::NATNUM7;
770 0 : else if (natNumIn(num, natnum2, sizeof_natnum2))
771 0 : return NativeNumberMode::NATNUM8;
772 0 : } else if ( aAttr.Style == "long" ) {
773 0 : if (num == NumberChar_FullWidth)
774 0 : return NativeNumberMode::NATNUM6;
775 0 : else if (num == NumberChar_Hangul_ko)
776 0 : return NativeNumberMode::NATNUM10;
777 0 : else if (natNumIn(num, natnum1, sizeof_natnum1))
778 0 : return NativeNumberMode::NATNUM4;
779 0 : else if (natNumIn(num, natnum2, sizeof_natnum2))
780 0 : return NativeNumberMode::NATNUM5;
781 : } else {
782 0 : throw RuntimeException();
783 : }
784 0 : return NativeNumberMode::NATNUM0;
785 : }
786 :
787 :
788 : // Following code generates Hebrew Number,
789 : // see numerical system in the Hebrew Numbering System in following link for details,
790 : // http://smontagu.org/writings/HebrewNumbers.html
791 :
792 : struct HebrewNumberChar {
793 : sal_Unicode code;
794 : sal_Int16 value;
795 : } HebrewNumberCharArray[] = {
796 : { 0x05ea, 400 },
797 : { 0x05ea, 400 },
798 : { 0x05e9, 300 },
799 : { 0x05e8, 200 },
800 : { 0x05e7, 100 },
801 : { 0x05e6, 90 },
802 : { 0x05e4, 80 },
803 : { 0x05e2, 70 },
804 : { 0x05e1, 60 },
805 : { 0x05e0, 50 },
806 : { 0x05de, 40 },
807 : { 0x05dc, 30 },
808 : { 0x05db, 20 },
809 : { 0x05d9, 10 },
810 : { 0x05d8, 9 },
811 : { 0x05d7, 8 },
812 : { 0x05d6, 7 },
813 : { 0x05d5, 6 },
814 : { 0x05d4, 5 },
815 : { 0x05d3, 4 },
816 : { 0x05d2, 3 },
817 : { 0x05d1, 2 },
818 : { 0x05d0, 1 }
819 : };
820 :
821 : static sal_Int16 nbOfHebrewNumberChar = sizeof(HebrewNumberCharArray)/sizeof(HebrewNumberChar);
822 :
823 : static sal_Unicode thousand[] = {0x05d0, 0x05dc, 0x05e3, 0x0};
824 : static sal_Unicode thousands[] = {0x05d0, 0x05dc, 0x05e4, 0x05d9, 0x0};
825 : static sal_Unicode thousands_last[] = {0x05d0, 0x05dc, 0x05e4, 0x05d9, 0x05dd, 0x0};
826 : static sal_Unicode geresh = 0x05f3;
827 : static sal_Unicode gershayim = 0x05f4;
828 :
829 0 : void makeHebrewNumber(sal_Int64 value, OUStringBuffer& output, sal_Bool isLast, sal_Bool useGeresh)
830 : {
831 0 : sal_Int16 num = sal::static_int_cast<sal_Int16>(value % 1000);
832 :
833 0 : if (value > 1000) {
834 0 : makeHebrewNumber(value / 1000, output, num != 0, useGeresh);
835 0 : output.appendAscii(" ");
836 : }
837 0 : if (num == 0) {
838 0 : output.append(value == 1000 ? thousand : isLast ? thousands_last : thousands);
839 : } else {
840 0 : sal_Int16 nbOfChar = 0;
841 0 : for (sal_Int32 j = 0; num > 0 && j < nbOfHebrewNumberChar; j++) {
842 0 : if (num - HebrewNumberCharArray[j].value >= 0) {
843 0 : nbOfChar++;
844 0 : if (num == 15 || num == 16) // substitution for 15 and 16
845 0 : j++;
846 0 : num = sal::static_int_cast<sal_Int16>( num - HebrewNumberCharArray[j].value );
847 0 : output.append(HebrewNumberCharArray[j].code);
848 : }
849 : }
850 0 : if (useGeresh) {
851 0 : if (nbOfChar > 1) // a number is written as more than one character
852 0 : output.insert(output.getLength() - 1, gershayim);
853 0 : else if (nbOfChar == 1) // a number is written as a single character
854 0 : output.append(geresh);
855 : }
856 : }
857 0 : }
858 :
859 0 : OUString SAL_CALL getHebrewNativeNumberString(const OUString& aNumberString, sal_Bool useGeresh)
860 : {
861 0 : sal_Int64 value = 0;
862 0 : sal_Int32 i, count = 0, len = aNumberString.getLength();
863 0 : const sal_Unicode *src = aNumberString.getStr();
864 :
865 0 : for (i = 0; i < len; i++) {
866 0 : sal_Unicode ch = src[i];
867 0 : if (isNumber(ch)) {
868 0 : if (++count >= 20) // Number is too long, could not be handled.
869 0 : return aNumberString;
870 0 : value = value * 10 + (ch - NUMBER_ZERO);
871 : }
872 0 : else if (isSeparator(ch) && count > 0) continue;
873 0 : else if (isMinus(ch) && count == 0) continue;
874 : else break;
875 : }
876 :
877 0 : if (value > 0) {
878 0 : OUStringBuffer output(count*2 + 2 + len - i);
879 :
880 0 : makeHebrewNumber(value, output, sal_True, useGeresh);
881 :
882 0 : if (i < len)
883 0 : output.append(aNumberString.copy(i));
884 :
885 0 : return output.makeStringAndClear();
886 : }
887 : else
888 0 : return aNumberString;
889 : }
890 :
891 : static const sal_Char* implementationName = "com.sun.star.i18n.NativeNumberSupplier";
892 :
893 0 : OUString SAL_CALL NativeNumberSupplier::getImplementationName() throw( RuntimeException )
894 : {
895 0 : return OUString::createFromAscii( implementationName );
896 : }
897 :
898 : sal_Bool SAL_CALL
899 0 : NativeNumberSupplier::supportsService(const OUString& rServiceName) throw( RuntimeException )
900 : {
901 0 : return rServiceName.compareToAscii(implementationName) == 0;
902 : }
903 :
904 : Sequence< OUString > SAL_CALL
905 0 : NativeNumberSupplier::getSupportedServiceNames() throw( RuntimeException )
906 : {
907 0 : Sequence< OUString > aRet(1);
908 0 : aRet[0] = OUString::createFromAscii( implementationName );
909 0 : return aRet;
910 : }
911 :
912 : } } } }
913 :
914 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|