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