rtl.h revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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#pragma once 8 9#include <string> 10 11#include "base/compiler_specific.h" 12#include "base/string16.h" 13#include "build/build_config.h" 14 15class FilePath; 16 17namespace base { 18namespace i18n { 19 20const char16 kRightToLeftMark = 0x200F; 21const char16 kLeftToRightMark = 0x200E; 22const char16 kLeftToRightEmbeddingMark = 0x202A; 23const char16 kRightToLeftEmbeddingMark = 0x202B; 24const char16 kPopDirectionalFormatting = 0x202C; 25const char16 kLeftToRightOverride = 0x202D; 26const char16 kRightToLeftOverride = 0x202E; 27 28enum TextDirection { 29 UNKNOWN_DIRECTION, 30 RIGHT_TO_LEFT, 31 LEFT_TO_RIGHT, 32}; 33 34// Get the locale that the currently running process has been configured to use. 35// The return value is of the form language[-country] (e.g., en-US) where the 36// language is the 2 or 3 letter code from ISO-639. 37std::string GetConfiguredLocale(); 38 39// Canonicalize a string (eg. a POSIX locale string) to a Chrome locale name. 40std::string GetCanonicalLocale(const char* locale); 41 42// Sets the default locale of ICU. 43// Once the application locale of Chrome in GetApplicationLocale is determined, 44// the default locale of ICU need to be changed to match the application locale 45// so that ICU functions work correctly in a locale-dependent manner. 46// This is handy in that we don't have to call GetApplicationLocale() 47// everytime we call locale-dependent ICU APIs as long as we make sure 48// that this is called before any locale-dependent API is called. 49void SetICUDefaultLocale(const std::string& locale_string); 50 51// Returns true if the application text direction is right-to-left. 52bool IsRTL(); 53 54// Returns whether the text direction for the default ICU locale is RTL. This 55// assumes that SetICUDefaultLocale has been called to set the default locale to 56// the UI locale of Chrome. 57// NOTE: Generally, you should call IsRTL() instead of this. 58bool ICUIsRTL(); 59 60// Returns the text direction for |locale_name|. 61TextDirection GetTextDirectionForLocale(const char* locale_name); 62 63// Given the string in |text|, returns the directionality of the first 64// character with strong directionality in the string. If no character in the 65// text has strong directionality, LEFT_TO_RIGHT is returned. The Bidi 66// character types L, LRE, LRO, R, AL, RLE, and RLO are considered as strong 67// directionality characters. Please refer to http://unicode.org/reports/tr9/ 68// for more information. 69TextDirection GetFirstStrongCharacterDirection(const string16& text); 70#if defined(WCHAR_T_IS_UTF32) 71TextDirection GetFirstStrongCharacterDirection(const std::wstring& text); 72#endif 73 74// Given the string in |text|, this function creates a copy of the string with 75// the appropriate Unicode formatting marks that mark the string direction 76// (either left-to-right or right-to-left). The new string is returned in 77// |localized_text|. The function checks both the current locale and the 78// contents of the string in order to determine the direction of the returned 79// string. The function returns true if the string in |text| was properly 80// adjusted. 81// 82// Certain LTR strings are not rendered correctly when the context is RTL. For 83// example, the string "Foo!" will appear as "!Foo" if it is rendered as is in 84// an RTL context. Calling this function will make sure the returned localized 85// string is always treated as a right-to-left string. This is done by 86// inserting certain Unicode formatting marks into the returned string. 87// 88// TODO(brettw) bug 47194: This funciton is confusing. If it does no adjustment 89// becuase the current locale is not RTL, it will do nothing and return false. 90// This means you have to check the return value in many cases which doesn't 91// make sense. This should be cleaned up and probably just take a single 92// argument that's a pointer to a string that it modifies as necessary. In the 93// meantime, the recommended usage is to use the same arg as input & output, 94// which will work without extra checks: 95// AdjustStringForLocaleDirection(text, &text); 96// 97// TODO(idana) bug# 1206120: this function adjusts the string in question only 98// if the current locale is right-to-left. The function does not take care of 99// the opposite case (an RTL string displayed in an LTR context) since 100// adjusting the string involves inserting Unicode formatting characters that 101// Windows does not handle well unless right-to-left language support is 102// installed. Since the English version of Windows doesn't have right-to-left 103// language support installed by default, inserting the direction Unicode mark 104// results in Windows displaying squares. 105bool AdjustStringForLocaleDirection(const string16& text, 106 string16* localized_text); 107#if defined(WCHAR_T_IS_UTF32) 108bool AdjustStringForLocaleDirection(const std::wstring& text, 109 std::wstring* localized_text); 110#endif 111 112// Returns true if the string contains at least one character with strong right 113// to left directionality; that is, a character with either R or AL Unicode 114// BiDi character type. 115bool StringContainsStrongRTLChars(const string16& text); 116#if defined(WCHAR_T_IS_UTF32) 117bool StringContainsStrongRTLChars(const std::wstring& text); 118#endif 119 120// Wraps a string with an LRE-PDF pair which essentialy marks the string as a 121// Left-To-Right string. Doing this is useful in order to make sure LTR 122// strings are rendered properly in an RTL context. 123void WrapStringWithLTRFormatting(string16* text); 124#if defined(WCHAR_T_IS_UTF32) 125void WrapStringWithLTRFormatting(std::wstring* text); 126#endif 127 128// Wraps a string with an RLE-PDF pair which essentialy marks the string as a 129// Right-To-Left string. Doing this is useful in order to make sure RTL 130// strings are rendered properly in an LTR context. 131void WrapStringWithRTLFormatting(string16* text); 132#if defined(WCHAR_T_IS_UTF32) 133void WrapStringWithRTLFormatting(std::wstring* text); 134#endif 135 136// Wraps file path to get it to display correctly in RTL UI. All filepaths 137// should be passed through this function before display in UI for RTL locales. 138void WrapPathWithLTRFormatting(const FilePath& path, 139 string16* rtl_safe_path); 140 141// Given the string in |text|, this function returns the adjusted string having 142// LTR directionality for display purpose. Which means that in RTL locale the 143// string is wrapped with LRE (Left-To-Right Embedding) and PDF (Pop 144// Directional Formatting) marks and returned. In LTR locale, the string itself 145// is returned. 146string16 GetDisplayStringInLTRDirectionality(const string16& text) 147 WARN_UNUSED_RESULT; 148 149// Strip the beginning (U+202A..U+202B, U+202D..U+202E) and/or ending (U+202C) 150// explicit bidi control characters from |text|, if there are any. Otherwise, 151// return the text itself. Explicit bidi control characters display and have 152// semantic effect. They can be deleted so they might not always appear in a 153// pair. 154const string16 StripWrappingBidiControlCharacters(const string16& text) 155 WARN_UNUSED_RESULT; 156 157} // namespace i18n 158} // namespace base 159 160#endif // BASE_I18N_RTL_H_ 161