nsBidiUtils.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
  8. #define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS)
  9. #define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS)
  10. #define NUM_TO_ARABIC(c) \
  11. ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
  12. ((c) - (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
  13. ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
  14. ((c) - (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
  15. (c)))
  16. #define NUM_TO_HINDI(c) \
  17. ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
  18. ((c) + (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
  19. ((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
  20. ((c) + (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
  21. (c)))
  22. #define NUM_TO_PERSIAN(c) \
  23. ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
  24. ((c) - (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
  25. ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
  26. ((c) + (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
  27. (c)))
  28. char16_t HandleNumberInChar(char16_t aChar, bool aPrevCharArabic, uint32_t aNumFlag)
  29. {
  30. // IBMBIDI_NUMERAL_NOMINAL *
  31. // IBMBIDI_NUMERAL_REGULAR
  32. // IBMBIDI_NUMERAL_HINDICONTEXT
  33. // IBMBIDI_NUMERAL_ARABIC
  34. // IBMBIDI_NUMERAL_HINDI
  35. switch (aNumFlag) {
  36. case IBMBIDI_NUMERAL_HINDI:
  37. return NUM_TO_HINDI(aChar);
  38. case IBMBIDI_NUMERAL_ARABIC:
  39. return NUM_TO_ARABIC(aChar);
  40. case IBMBIDI_NUMERAL_PERSIAN:
  41. return NUM_TO_PERSIAN(aChar);
  42. case IBMBIDI_NUMERAL_REGULAR:
  43. case IBMBIDI_NUMERAL_HINDICONTEXT:
  44. case IBMBIDI_NUMERAL_PERSIANCONTEXT:
  45. // for clipboard handling
  46. //XXX do we really want to convert numerals when copying text?
  47. if (aPrevCharArabic) {
  48. if (aNumFlag == IBMBIDI_NUMERAL_PERSIANCONTEXT)
  49. return NUM_TO_PERSIAN(aChar);
  50. else
  51. return NUM_TO_HINDI(aChar);
  52. }
  53. else
  54. return NUM_TO_ARABIC(aChar);
  55. case IBMBIDI_NUMERAL_NOMINAL:
  56. default:
  57. return aChar;
  58. }
  59. }
  60. nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag)
  61. {
  62. uint32_t i;
  63. switch (aNumFlag) {
  64. case IBMBIDI_NUMERAL_HINDI:
  65. case IBMBIDI_NUMERAL_ARABIC:
  66. case IBMBIDI_NUMERAL_PERSIAN:
  67. case IBMBIDI_NUMERAL_REGULAR:
  68. case IBMBIDI_NUMERAL_HINDICONTEXT:
  69. case IBMBIDI_NUMERAL_PERSIANCONTEXT:
  70. for (i=0;i<aSize;i++)
  71. aBuffer[i] = HandleNumberInChar(aBuffer[i], !!(i>0 ? aBuffer[i-1] : 0), aNumFlag);
  72. break;
  73. case IBMBIDI_NUMERAL_NOMINAL:
  74. default:
  75. break;
  76. }
  77. return NS_OK;
  78. }
  79. bool HasRTLChars(const nsAString& aString)
  80. {
  81. // This is used to determine whether to enable bidi if a string has
  82. // right-to-left characters. To simplify things, anything that could be a
  83. // surrogate or RTL presentation form is covered just by testing >= 0xD800).
  84. // It's fine to enable bidi in rare cases where it actually isn't needed.
  85. int32_t length = aString.Length();
  86. for (int32_t i = 0; i < length; i++) {
  87. char16_t ch = aString.CharAt(i);
  88. if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }