rtl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_I18N_RTL_H_
  5. #define BASE_I18N_RTL_H_
  6. #include <string>
  7. #include "base/compiler_specific.h"
  8. #include "base/i18n/base_i18n_export.h"
  9. #include "base/strings/string16.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. class FilePath;
  13. namespace i18n {
  14. const char16 kRightToLeftMark = 0x200F;
  15. const char16 kLeftToRightMark = 0x200E;
  16. const char16 kLeftToRightEmbeddingMark = 0x202A;
  17. const char16 kRightToLeftEmbeddingMark = 0x202B;
  18. const char16 kPopDirectionalFormatting = 0x202C;
  19. const char16 kLeftToRightOverride = 0x202D;
  20. const char16 kRightToLeftOverride = 0x202E;
  21. // Locale.java mirrored this enum TextDirection. Please keep in sync.
  22. enum TextDirection {
  23. UNKNOWN_DIRECTION = 0,
  24. RIGHT_TO_LEFT = 1,
  25. LEFT_TO_RIGHT = 2,
  26. TEXT_DIRECTION_MAX = LEFT_TO_RIGHT,
  27. };
  28. // Get the locale that the currently running process has been configured to use.
  29. // The return value is of the form language[-country] (e.g., en-US) where the
  30. // language is the 2 or 3 letter code from ISO-639.
  31. BASE_I18N_EXPORT std::string GetConfiguredLocale();
  32. // Canonicalize a string (eg. a POSIX locale string) to a Chrome locale name.
  33. BASE_I18N_EXPORT std::string GetCanonicalLocale(const std::string& locale);
  34. // Sets the default locale of ICU.
  35. // Once the application locale of Chrome in GetApplicationLocale is determined,
  36. // the default locale of ICU need to be changed to match the application locale
  37. // so that ICU functions work correctly in a locale-dependent manner.
  38. // This is handy in that we don't have to call GetApplicationLocale()
  39. // everytime we call locale-dependent ICU APIs as long as we make sure
  40. // that this is called before any locale-dependent API is called.
  41. BASE_I18N_EXPORT void SetICUDefaultLocale(const std::string& locale_string);
  42. // Returns true if the application text direction is right-to-left.
  43. BASE_I18N_EXPORT bool IsRTL();
  44. // A test utility function to set the application default text direction.
  45. BASE_I18N_EXPORT void SetRTLForTesting(bool rtl);
  46. // Returns whether the text direction for the default ICU locale is RTL. This
  47. // assumes that SetICUDefaultLocale has been called to set the default locale to
  48. // the UI locale of Chrome.
  49. // NOTE: Generally, you should call IsRTL() instead of this.
  50. BASE_I18N_EXPORT bool ICUIsRTL();
  51. // Gets the explicitly forced text direction for debugging. If no forcing is
  52. // applied, returns UNKNOWN_DIRECTION.
  53. BASE_I18N_EXPORT TextDirection GetForcedTextDirection();
  54. // Returns the text direction for |locale_name|.
  55. // As a startup optimization, this method checks the locale against a list of
  56. // Chrome-supported RTL locales.
  57. BASE_I18N_EXPORT TextDirection
  58. GetTextDirectionForLocaleInStartUp(const char* locale_name);
  59. // Returns the text direction for |locale_name|.
  60. BASE_I18N_EXPORT TextDirection GetTextDirectionForLocale(
  61. const char* locale_name);
  62. // Given the string in |text|, returns the directionality of the first or last
  63. // character with strong directionality in the string. If no character in the
  64. // text has strong directionality, LEFT_TO_RIGHT is returned. The Bidi
  65. // character types L, LRE, LRO, R, AL, RLE, and RLO are considered as strong
  66. // directionality characters. Please refer to http://unicode.org/reports/tr9/
  67. // for more information.
  68. BASE_I18N_EXPORT TextDirection GetFirstStrongCharacterDirection(
  69. const string16& text);
  70. BASE_I18N_EXPORT TextDirection GetLastStrongCharacterDirection(
  71. const string16& text);
  72. // Given the string in |text|, returns LEFT_TO_RIGHT or RIGHT_TO_LEFT if all the
  73. // strong directionality characters in the string are of the same
  74. // directionality. It returns UNKNOWN_DIRECTION if the string contains a mix of
  75. // LTR and RTL strong directionality characters. Defaults to LEFT_TO_RIGHT if
  76. // the string does not contain directionality characters. Please refer to
  77. // http://unicode.org/reports/tr9/ for more information.
  78. BASE_I18N_EXPORT TextDirection GetStringDirection(const string16& text);
  79. // Given the string in |text|, this function modifies the string in place with
  80. // the appropriate Unicode formatting marks that mark the string direction
  81. // (either left-to-right or right-to-left). The function checks both the current
  82. // locale and the contents of the string in order to determine the direction of
  83. // the returned string. The function returns true if the string in |text| was
  84. // properly adjusted.
  85. //
  86. // Certain LTR strings are not rendered correctly when the context is RTL. For
  87. // example, the string "Foo!" will appear as "!Foo" if it is rendered as is in
  88. // an RTL context. Calling this function will make sure the returned localized
  89. // string is always treated as a right-to-left string. This is done by
  90. // inserting certain Unicode formatting marks into the returned string.
  91. //
  92. // ** Notes about the Windows version of this function:
  93. // TODO(idana) bug 6806: this function adjusts the string in question only
  94. // if the current locale is right-to-left. The function does not take care of
  95. // the opposite case (an RTL string displayed in an LTR context) since
  96. // adjusting the string involves inserting Unicode formatting characters that
  97. // Windows does not handle well unless right-to-left language support is
  98. // installed. Since the English version of Windows doesn't have right-to-left
  99. // language support installed by default, inserting the direction Unicode mark
  100. // results in Windows displaying squares.
  101. BASE_I18N_EXPORT bool AdjustStringForLocaleDirection(string16* text);
  102. // Undoes the actions of the above function (AdjustStringForLocaleDirection).
  103. BASE_I18N_EXPORT bool UnadjustStringForLocaleDirection(string16* text);
  104. // Ensures |text| contains no unterminated directional formatting characters, by
  105. // appending the appropriate pop-directional-formatting characters to the end of
  106. // |text|.
  107. BASE_I18N_EXPORT void EnsureTerminatedDirectionalFormatting(string16* text);
  108. // Sanitizes the |text| by terminating any directional override/embedding
  109. // characters and then adjusting the string for locale direction.
  110. BASE_I18N_EXPORT void SanitizeUserSuppliedString(string16* text);
  111. // Returns true if the string contains at least one character with strong right
  112. // to left directionality; that is, a character with either R or AL Unicode
  113. // BiDi character type.
  114. BASE_I18N_EXPORT bool StringContainsStrongRTLChars(const string16& text);
  115. // Wraps a string with an LRE-PDF pair which essentialy marks the string as a
  116. // Left-To-Right string. Doing this is useful in order to make sure LTR
  117. // strings are rendered properly in an RTL context.
  118. BASE_I18N_EXPORT void WrapStringWithLTRFormatting(string16* text);
  119. // Wraps a string with an RLE-PDF pair which essentialy marks the string as a
  120. // Right-To-Left string. Doing this is useful in order to make sure RTL
  121. // strings are rendered properly in an LTR context.
  122. BASE_I18N_EXPORT void WrapStringWithRTLFormatting(string16* text);
  123. // Wraps file path to get it to display correctly in RTL UI. All filepaths
  124. // should be passed through this function before display in UI for RTL locales.
  125. BASE_I18N_EXPORT void WrapPathWithLTRFormatting(const FilePath& path,
  126. string16* rtl_safe_path);
  127. // Return the string in |text| wrapped with LRE (Left-To-Right Embedding) and
  128. // PDF (Pop Directional Formatting) marks, if needed for UI display purposes.
  129. BASE_I18N_EXPORT string16 GetDisplayStringInLTRDirectionality(
  130. const string16& text) WARN_UNUSED_RESULT;
  131. // Strip the beginning (U+202A..U+202B, U+202D..U+202E) and/or ending (U+202C)
  132. // explicit bidi control characters from |text|, if there are any. Otherwise,
  133. // return the text itself. Explicit bidi control characters display and have
  134. // semantic effect. They can be deleted so they might not always appear in a
  135. // pair.
  136. BASE_I18N_EXPORT string16 StripWrappingBidiControlCharacters(
  137. const string16& text) WARN_UNUSED_RESULT;
  138. } // namespace i18n
  139. } // namespace base
  140. #endif // BASE_I18N_RTL_H_