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