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