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