intl/unicharutil/util/nsBidiUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial