diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-20 06:37:01 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-20 06:37:01 +0000 |
commit | 7cf1b6ced3b14cce1d66ca0ddc713851f0d37536 (patch) | |
tree | 1c2fc9f4d52bf3046addf820d5eec03a2e150749 /app | |
parent | f9f4841b14a9f309ce5ee613f0d4de6afad88767 (diff) | |
download | chromium_src-7cf1b6ced3b14cce1d66ca0ddc713851f0d37536.zip chromium_src-7cf1b6ced3b14cce1d66ca0ddc713851f0d37536.tar.gz chromium_src-7cf1b6ced3b14cce1d66ca0ddc713851f0d37536.tar.bz2 |
Move RTL related functions from app/l10n_util to base/i18n/rtl
TBR=darin
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/1073005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42182 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/gfx/canvas.cc | 10 | ||||
-rw-r--r-- | app/gfx/canvas.h | 9 | ||||
-rw-r--r-- | app/gfx/canvas_win.cc | 10 | ||||
-rw-r--r-- | app/l10n_util.cc | 238 | ||||
-rw-r--r-- | app/l10n_util.h | 98 | ||||
-rw-r--r-- | app/l10n_util_dummy.cc | 3 | ||||
-rw-r--r-- | app/l10n_util_unittest.cc | 242 | ||||
-rw-r--r-- | app/l10n_util_win.cc | 8 | ||||
-rw-r--r-- | app/resource_bundle_linux.cc | 4 | ||||
-rw-r--r-- | app/text_elider.cc | 9 | ||||
-rw-r--r-- | app/text_elider.h | 2 | ||||
-rw-r--r-- | app/text_elider_unittest.cc | 5 | ||||
-rw-r--r-- | app/win_util.cc | 7 |
13 files changed, 49 insertions, 596 deletions
diff --git a/app/gfx/canvas.cc b/app/gfx/canvas.cc index 75f8011..75ec0a1 100644 --- a/app/gfx/canvas.cc +++ b/app/gfx/canvas.cc @@ -8,6 +8,7 @@ #include "app/gfx/font.h" #include "app/l10n_util.h" +#include "base/i18n/rtl.h" #include "base/logging.h" #include "gfx/rect.h" #include "third_party/skia/include/core/SkShader.h" @@ -218,7 +219,7 @@ void Canvas::DrawStringInt(const std::wstring& text, const SkColor& color, int x, int y, int w, int h) { DrawStringInt(text, font, color, x, y, w, h, - l10n_util::DefaultCanvasTextAlignment()); + gfx::Canvas::DefaultCanvasTextAlignment()); } void Canvas::DrawStringInt(const std::wstring& text, @@ -267,4 +268,11 @@ SkBitmap Canvas::ExtractBitmap() const { return result; } +// static +int Canvas::DefaultCanvasTextAlignment() { + if (!base::i18n::IsRTL()) + return gfx::Canvas::TEXT_ALIGN_LEFT; + return gfx::Canvas::TEXT_ALIGN_RIGHT; +} + } // namespace gfx diff --git a/app/gfx/canvas.h b/app/gfx/canvas.h index 79eed9c..ea4fbf3 100644 --- a/app/gfx/canvas.h +++ b/app/gfx/canvas.h @@ -211,6 +211,15 @@ class Canvas : public skia::PlatformCanvas { static void SizeStringInt(const std::wstring& test, const gfx::Font& font, int *width, int* height, int flags); + // Returns the default text alignment to be used when drawing text on a + // gfx::Canvas based on the directionality of the system locale language. This + // function is used by gfx::Canvas::DrawStringInt when the text alignment is + // not specified. + // + // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or + // gfx::Canvas::TEXT_ALIGN_RIGHT. + static int DefaultCanvasTextAlignment(); + private: #if defined(OS_WIN) // Draws text with the specified color, font and location. The text is diff --git a/app/gfx/canvas_win.cc b/app/gfx/canvas_win.cc index de9d45a..abe722c 100644 --- a/app/gfx/canvas_win.cc +++ b/app/gfx/canvas_win.cc @@ -7,7 +7,7 @@ #include <limits> #include "app/gfx/font.h" -#include "app/l10n_util.h" +#include "base/i18n/rtl.h" #include "gfx/rect.h" #include "third_party/skia/include/core/SkShader.h" @@ -24,7 +24,7 @@ void DoDrawText(HDC hdc, const std::wstring& text, // 1. The current locale is RTL. // 2. The string itself has RTL directionality. if (flags & DT_RTLREADING) { - if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) { + if (base::i18n::AdjustStringForLocaleDirection(text, &localized_text)) { string_ptr = localized_text.c_str(); string_size = static_cast<int>(localized_text.length()); } @@ -42,7 +42,7 @@ int ComputeFormatFlags(int flags, const std::wstring& text) { if (!(flags & (gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT))) { - flags |= l10n_util::DefaultCanvasTextAlignment(); + flags |= gfx::Canvas::DefaultCanvasTextAlignment(); } // horizontal alignment @@ -113,8 +113,8 @@ int ComputeFormatFlags(int flags, const std::wstring& text) { // using RTL directionality then we respect that and pass DT_RTLREADING to // ::DrawText even if the locale is LTR. if ((flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) || - ((l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) && - (f & DT_RIGHT) && l10n_util::StringContainsStrongRTLChars(text))) { + (base::i18n::IsRTL() && + (f & DT_RIGHT) && base::i18n::StringContainsStrongRTLChars(text))) { f |= DT_RTLREADING; } diff --git a/app/l10n_util.cc b/app/l10n_util.cc index 503f708..d51dc89 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -14,6 +14,7 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/i18n/file_util_icu.h" +#include "base/i18n/rtl.h" #include "base/path_service.h" #include "base/scoped_ptr.h" #include "base/string16.h" @@ -21,15 +22,7 @@ #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "build/build_config.h" -#include "unicode/coll.h" -#include "unicode/locid.h" #include "unicode/rbbi.h" -#include "unicode/uchar.h" -#include "unicode/uscript.h" - -#if defined(TOOLKIT_GTK) -#include <gtk/gtk.h> -#endif #if defined(OS_MACOSX) #include "app/l10n_util_mac.h" @@ -188,71 +181,6 @@ static const char* const kAcceptLanguageList[] = { "zu", // Zulu }; - -// Get language and region from the OS. -void GetLanguageAndRegionFromOS(std::string* lang, std::string* region) { - // Later we may have to change this to be OS-dependent so that - // it's not affected by ICU's default locale. It's all right - // to do this way because SetICUDefaultLocale is internal - // to this file and we know that it's not yet called when this function - // is called. - icu::Locale locale = icu::Locale::getDefault(); - const char* language = locale.getLanguage(); - const char* country = locale.getCountry(); - DCHECK(language); - *lang = language; - *region = country; -} - -// Convert Chrome locale name to ICU locale name -std::string ICULocaleName(const std::string& locale_string) { - // If not Spanish, just return it. - if (locale_string.substr(0, 2) != "es") - return locale_string; - // Expand es to es-ES. - if (LowerCaseEqualsASCII(locale_string, "es")) - return "es-ES"; - // Map es-419 (Latin American Spanish) to es-FOO depending on the system - // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map - // to es-MX (the most populous in Spanish-speaking Latin America). - if (LowerCaseEqualsASCII(locale_string, "es-419")) { - std::string lang, region; - GetLanguageAndRegionFromOS(&lang, ®ion); - if (LowerCaseEqualsASCII(lang, "es") && - !LowerCaseEqualsASCII(region, "es")) { - lang.append("-"); - lang.append(region); - return lang; - } - return "es-MX"; - } - // Currently, Chrome has only "es" and "es-419", but later we may have - // more specific "es-RR". - return locale_string; -} - -// Represents the locale-specific ICU text direction. -l10n_util::TextDirection g_icu_text_direction = l10n_util::UNKNOWN_DIRECTION; - -// Sets the default locale of ICU. -// Once the application locale of Chrome in GetApplicationLocale is determined, -// the default locale of ICU need to be changed to match the application locale -// so that ICU functions work correctly in a locale-dependent manner. -// This is handy in that we don't have to call GetApplicationLocale() -// everytime we call locale-dependent ICU APIs as long as we make sure -// that this is called before any locale-dependent API is called. -void SetICUDefaultLocale(const std::string& locale_string) { - icu::Locale locale(ICULocaleName(locale_string).c_str()); - UErrorCode error_code = U_ZERO_ERROR; - icu::Locale::setDefault(locale, error_code); - // This return value is actually bogus because Locale object is - // an ID and setDefault seems to always succeed (regardless of the - // presence of actual locale data). However, - // it does not hurt to have it as a sanity check. - DCHECK(U_SUCCESS(error_code)); - g_icu_text_direction = l10n_util::UNKNOWN_DIRECTION; -} - // Returns true if |locale_name| has an alias in the ICU data file. bool IsDuplicateName(const std::string& locale_name) { static const char* const kDuplicateNames[] = { @@ -386,7 +314,7 @@ bool CheckAndResolveLocale(const std::string& locale, // ISO-639. std::string GetSystemLocale() { std::string language, region; - GetLanguageAndRegionFromOS(&language, ®ion); + base::i18n::GetLanguageAndRegionFromOS(&language, ®ion); std::string ret; if (!language.empty()) ret.append(language); @@ -490,7 +418,7 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) { std::vector<std::string>::const_iterator i = candidates.begin(); for (; i != candidates.end(); ++i) { if (CheckAndResolveLocale(*i, locale_path, &resolved_locale)) { - SetICUDefaultLocale(resolved_locale); + base::i18n::SetICUDefaultLocale(resolved_locale); return resolved_locale; } } @@ -498,7 +426,7 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) { // Fallback on en-US. const std::string fallback_locale("en-US"); if (IsLocaleAvailable(fallback_locale, locale_path)) { - SetICUDefaultLocale(fallback_locale); + base::i18n::SetICUDefaultLocale(fallback_locale); return fallback_locale; } @@ -527,7 +455,7 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) { // Mac doesn't use a locale directory tree of resources (it uses Mac style // resources), so mirror the Windows/Linux behavior of calling // SetICUDefaultLocale. - SetICUDefaultLocale(app_locale); + base::i18n::SetICUDefaultLocale(app_locale); return app_locale; #endif // !defined(OS_MACOSX) } @@ -567,9 +495,8 @@ string16 GetDisplayNameForLocale(const std::string& locale, DCHECK(U_SUCCESS(error)); display_name.resize(actual_size); // Add an RTL mark so parentheses are properly placed. - if (is_for_ui && GetTextDirection() == RIGHT_TO_LEFT) { - display_name.push_back(static_cast<char16>(kRightToLeftMark)); - } + if (is_for_ui && base::i18n::IsRTL()) + display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); return display_name; } @@ -817,157 +744,6 @@ string16 ToUpper(const string16& string) { return result; } -TextDirection GetICUTextDirection() { - if (g_icu_text_direction == UNKNOWN_DIRECTION) { - const icu::Locale& locale = icu::Locale::getDefault(); - g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); - } - return g_icu_text_direction; -} - -TextDirection GetTextDirection() { -#if defined(TOOLKIT_GTK) - GtkTextDirection gtk_dir = gtk_widget_get_default_direction(); - return (gtk_dir == GTK_TEXT_DIR_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; -#else - return GetICUTextDirection(); -#endif -} - -TextDirection GetTextDirectionForLocale(const char* locale_name) { - UErrorCode status = U_ZERO_ERROR; - ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); - DCHECK(U_SUCCESS(status)); - // Treat anything other than RTL as LTR. - return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; -} - -TextDirection GetFirstStrongCharacterDirection(const std::wstring& text) { -#if defined(WCHAR_T_IS_UTF32) - string16 text_utf16 = WideToUTF16(text); - const UChar* string = text_utf16.c_str(); -#else - const UChar* string = text.c_str(); -#endif - size_t length = text.length(); - size_t position = 0; - while (position < length) { - UChar32 character; - size_t next_position = position; - U16_NEXT(string, next_position, length, character); - - // Now that we have the character, we use ICU in order to query for the - // appropriate Unicode BiDi character type. - int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); - if ((property == U_RIGHT_TO_LEFT) || - (property == U_RIGHT_TO_LEFT_ARABIC) || - (property == U_RIGHT_TO_LEFT_EMBEDDING) || - (property == U_RIGHT_TO_LEFT_OVERRIDE)) { - return RIGHT_TO_LEFT; - } else if ((property == U_LEFT_TO_RIGHT) || - (property == U_LEFT_TO_RIGHT_EMBEDDING) || - (property == U_LEFT_TO_RIGHT_OVERRIDE)) { - return LEFT_TO_RIGHT; - } - - position = next_position; - } - - return LEFT_TO_RIGHT; -} - -bool AdjustStringForLocaleDirection(const std::wstring& text, - std::wstring* localized_text) { - if (GetTextDirection() == LEFT_TO_RIGHT || text.length() == 0) - return false; - - // Marking the string as LTR if the locale is RTL and the string does not - // contain strong RTL characters. Otherwise, mark the string as RTL. - *localized_text = text; - bool has_rtl_chars = StringContainsStrongRTLChars(text); - if (!has_rtl_chars) - WrapStringWithLTRFormatting(localized_text); - else - WrapStringWithRTLFormatting(localized_text); - - return true; -} - -bool StringContainsStrongRTLChars(const std::wstring& text) { -#if defined(WCHAR_T_IS_UTF32) - string16 text_utf16 = WideToUTF16(text); - const UChar* string = text_utf16.c_str(); -#else - const UChar* string = text.c_str(); -#endif - size_t length = text.length(); - size_t position = 0; - while (position < length) { - UChar32 character; - size_t next_position = position; - U16_NEXT(string, next_position, length, character); - - // Now that we have the character, we use ICU in order to query for the - // appropriate Unicode BiDi character type. - int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); - if ((property == U_RIGHT_TO_LEFT) || (property == U_RIGHT_TO_LEFT_ARABIC)) - return true; - - position = next_position; - } - - return false; -} - -void WrapStringWithLTRFormatting(std::wstring* text) { - // Inserting an LRE (Left-To-Right Embedding) mark as the first character. - text->insert(0, 1, static_cast<wchar_t>(kLeftToRightEmbeddingMark)); - - // Inserting a PDF (Pop Directional Formatting) mark as the last character. - text->push_back(static_cast<wchar_t>(kPopDirectionalFormatting)); -} - -void WrapStringWithRTLFormatting(std::wstring* text) { - // Inserting an RLE (Right-To-Left Embedding) mark as the first character. - text->insert(0, 1, static_cast<wchar_t>(kRightToLeftEmbeddingMark)); - - // Inserting a PDF (Pop Directional Formatting) mark as the last character. - text->push_back(static_cast<wchar_t>(kPopDirectionalFormatting)); -} - -void WrapPathWithLTRFormatting(const FilePath& path, - string16* rtl_safe_path) { - // Wrap the overall path with LRE-PDF pair which essentialy marks the - // string as a Left-To-Right string. - // Inserting an LRE (Left-To-Right Embedding) mark as the first character. - rtl_safe_path->push_back(kLeftToRightEmbeddingMark); -#if defined(OS_MACOSX) - rtl_safe_path->append(UTF8ToUTF16(path.value())); -#elif defined(OS_WIN) - rtl_safe_path->append(path.value()); -#else // defined(OS_POSIX) && !defined(OS_MACOSX) - std::wstring wide_path = base::SysNativeMBToWide(path.value()); - rtl_safe_path->append(WideToUTF16(wide_path)); -#endif - // Inserting a PDF (Pop Directional Formatting) mark as the last character. - rtl_safe_path->push_back(kPopDirectionalFormatting); -} - -std::wstring GetDisplayStringInLTRDirectionality(std::wstring* text) { - if (GetTextDirection() == RIGHT_TO_LEFT) - WrapStringWithLTRFormatting(text); - return *text; -} - -int DefaultCanvasTextAlignment() { - if (GetTextDirection() == LEFT_TO_RIGHT) { - return gfx::Canvas::TEXT_ALIGN_LEFT; - } else { - return gfx::Canvas::TEXT_ALIGN_RIGHT; - } -} - - // Compares the character data stored in two different strings by specified // Collator instance. UCollationResult CompareStringWithCollator(const icu::Collator* collator, diff --git a/app/l10n_util.h b/app/l10n_util.h index b87442c..c176f73f 100644 --- a/app/l10n_util.h +++ b/app/l10n_util.h @@ -25,17 +25,10 @@ #include "app/l10n_util_mac.h" #endif // OS_MACOSX -class FilePath; class PrefService; namespace l10n_util { -const char16 kRightToLeftMark = 0x200f; -const char16 kLeftToRightMark = 0x200e; -const char16 kLeftToRightEmbeddingMark = 0x202A; -const char16 kRightToLeftEmbeddingMark = 0x202B; -const char16 kPopDirectionalFormatting = 0x202C; - // This method is responsible for determining the locale as defined below. In // nearly all cases you shouldn't call this, rather use GetApplicationLocale // defined on browser_process. @@ -181,97 +174,6 @@ string16 ToLower(const string16& string); // Returns the upper case equivalent of string. string16 ToUpper(const string16& string); -// Represents the text direction returned by the GetTextDirection() function. -enum TextDirection { - UNKNOWN_DIRECTION, - RIGHT_TO_LEFT, - LEFT_TO_RIGHT, -}; - -// Returns the text direction for the default ICU locale. It is assumed -// that SetICUDefaultLocale has been called to set the default locale to -// the UI locale of Chrome. Its return is one of the following three: -// * LEFT_TO_RIGHT: Left-To-Right (e.g. English, Chinese, etc.); -// * RIGHT_TO_LEFT: Right-To-Left (e.g. Arabic, Hebrew, etc.), and; -// * UNKNOWN_DIRECTION: unknown (or error). -TextDirection GetICUTextDirection(); - -// Get the application text direction. (This is just the ICU direction, -// except on GTK.) -TextDirection GetTextDirection(); - -// Returns the text direction for |locale_name|. -TextDirection GetTextDirectionForLocale(const char* locale_name); - -// Given the string in |text|, returns the directionality of the first -// character with strong directionality in the string. If no character in the -// text has strong directionality, LEFT_TO_RIGHT is returned. The Bidi -// character types L, LRE, LRO, R, AL, RLE, and RLO are considered as strong -// directionality characters. Please refer to http://unicode.org/reports/tr9/ -// for more information. -TextDirection GetFirstStrongCharacterDirection(const std::wstring& text); - -// Given the string in |text|, this function creates a copy of the string with -// the appropriate Unicode formatting marks that mark the string direction -// (either left-to-right or right-to-left). The new string is returned in -// |localized_text|. The function checks both the current locale and the -// contents of the string in order to determine the direction of the returned -// string. The function returns true if the string in |text| was properly -// adjusted. -// -// Certain LTR strings are not rendered correctly when the context is RTL. For -// example, the string "Foo!" will appear as "!Foo" if it is rendered as is in -// an RTL context. Calling this function will make sure the returned localized -// string is always treated as a right-to-left string. This is done by -// inserting certain Unicode formatting marks into the returned string. -// -// TODO(idana) bug# 1206120: this function adjusts the string in question only -// if the current locale is right-to-left. The function does not take care of -// the opposite case (an RTL string displayed in an LTR context) since -// adjusting the string involves inserting Unicode formatting characters that -// Windows does not handle well unless right-to-left language support is -// installed. Since the English version of Windows doesn't have right-to-left -// language support installed by default, inserting the direction Unicode mark -// results in Windows displaying squares. -bool AdjustStringForLocaleDirection(const std::wstring& text, - std::wstring* localized_text); - -// Returns true if the string contains at least one character with strong right -// to left directionality; that is, a character with either R or AL Unicode -// BiDi character type. -bool StringContainsStrongRTLChars(const std::wstring& text); - -// Wraps a string with an LRE-PDF pair which essentialy marks the string as a -// Left-To-Right string. Doing this is useful in order to make sure LTR -// strings are rendered properly in an RTL context. -void WrapStringWithLTRFormatting(std::wstring* text); - -// Wraps a string with an RLE-PDF pair which essentialy marks the string as a -// Right-To-Left string. Doing this is useful in order to make sure RTL -// strings are rendered properly in an LTR context. -void WrapStringWithRTLFormatting(std::wstring* text); - -// Wraps file path to get it to display correctly in RTL UI. All filepaths -// should be passed through this function before display in UI for RTL locales. -void WrapPathWithLTRFormatting(const FilePath& path, - string16* rtl_safe_path); - -// Given the string in |text|, this function returns the adjusted string having -// LTR directionality for display purpose. Which means that in RTL locale the -// string is wrapped with LRE (Left-To-Right Embedding) and PDF (Pop -// Directional Formatting) marks and returned. In LTR locale, the string itself -// is returned. -std::wstring GetDisplayStringInLTRDirectionality(std::wstring* text); - -// Returns the default text alignment to be used when drawing text on a -// gfx::Canvas based on the directionality of the system locale language. This -// function is used by gfx::Canvas::DrawStringInt when the text alignment is -// not specified. -// -// This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or -// gfx::Canvas::TEXT_ALIGN_RIGHT. -int DefaultCanvasTextAlignment(); - // In place sorting of strings using collation rules for |locale|. // TODO(port): this should take string16. void SortStrings(const std::string& locale, diff --git a/app/l10n_util_dummy.cc b/app/l10n_util_dummy.cc index a9a4a92..0907da5 100644 --- a/app/l10n_util_dummy.cc +++ b/app/l10n_util_dummy.cc @@ -16,9 +16,6 @@ namespace l10n_util { -// Represents the locale-specific text direction. -static TextDirection g_text_direction = UNKNOWN_DIRECTION; - std::wstring GetString(int message_id) { ResourceBundle& rb = ResourceBundle::GetSharedInstance(); return UTF16ToWide(rb.GetLocalizedString(message_id)); diff --git a/app/l10n_util_unittest.cc b/app/l10n_util_unittest.cc index cd299d1..94dd8fd 100644 --- a/app/l10n_util_unittest.cc +++ b/app/l10n_util_unittest.cc @@ -39,10 +39,6 @@ class StringWrapper { DISALLOW_COPY_AND_ASSIGN(StringWrapper); }; -l10n_util::TextDirection GetTextDirection(const char* locale_name) { - return l10n_util::GetTextDirectionForLocale(locale_name); -} - } // namespace class L10nUtilTest : public PlatformTest { @@ -274,244 +270,6 @@ TEST_F(L10nUtilTest, SortStringsUsingFunction) { STLDeleteElements(&strings); } -TEST_F(L10nUtilTest, GetFirstStrongCharacterDirection) { - // Test pure LTR string. - std::wstring string(L"foo bar"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type L. - string.assign(L"foo \x05d0 bar"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type R. - string.assign(L"\x05d0 foo bar"); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string which starts with a character with weak directionality - // and in which the first character with strong directionality is a character - // with type L. - string.assign(L"!foo \x05d0 bar"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string which starts with a character with weak directionality - // and in which the first character with strong directionality is a character - // with type R. - string.assign(L",\x05d0 foo bar"); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type LRE. - string.assign(L"\x202a \x05d0 foo bar"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type LRO. - string.assign(L"\x202d \x05d0 foo bar"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type RLE. - string.assign(L"\x202b foo \x05d0 bar"); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type RLO. - string.assign(L"\x202e foo \x05d0 bar"); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test bidi string in which the first character with strong directionality - // is a character with type AL. - string.assign(L"\x0622 foo \x05d0 bar"); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test a string without strong directionality characters. - string.assign(L",!.{}"); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test empty string. - string.assign(L""); - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); - - // Test characters in non-BMP (e.g. Phoenician letters. Please refer to - // http://demo.icu-project.org/icu-bin/ubrowse?scr=151&b=10910 for more - // information). -#if defined(WCHAR_T_IS_UTF32) - string.assign(L" ! \x10910" L"abc 123"); -#elif defined(WCHAR_T_IS_UTF16) - string.assign(L" ! \xd802\xdd10" L"abc 123"); -#else -#error wchar_t should be either UTF-16 or UTF-32 -#endif - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, - l10n_util::GetFirstStrongCharacterDirection(string)); - -#if defined(WCHAR_T_IS_UTF32) - string.assign(L" ! \x10401" L"abc 123"); -#elif defined(WCHAR_T_IS_UTF16) - string.assign(L" ! \xd801\xdc01" L"abc 123"); -#else -#error wchar_t should be either UTF-16 or UTF-32 -#endif - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, - l10n_util::GetFirstStrongCharacterDirection(string)); -} - -typedef struct { - std::wstring path; - std::wstring wrapped_path; -} PathAndWrappedPath; - -TEST_F(L10nUtilTest, WrapPathWithLTRFormatting) { - std::wstring kSeparator; - kSeparator.push_back(static_cast<wchar_t>(FilePath::kSeparators[0])); - const PathAndWrappedPath test_data[] = { - // Test common path, such as "c:\foo\bar". - { L"c:" + kSeparator + L"foo" + kSeparator + L"bar", - L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + - L"bar\x202c" - }, - // Test path with file name, such as "c:\foo\bar\test.jpg". - { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + - L"test.jpg", - L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + - L"bar" + kSeparator + L"test.jpg\x202c" - }, - // Test path ending with punctuation, such as "c:\(foo)\bar.". - { L"c:" + kSeparator + L"(foo)" + kSeparator + L"bar.", - L"\x202a"L"c:" + kSeparator + L"(foo)" + kSeparator + - L"bar.\x202c" - }, - // Test path ending with separator, such as "c:\foo\bar\". - { L"c:" + kSeparator + L"foo" + kSeparator + L"bar" + kSeparator, - L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + - L"bar" + kSeparator + L"\x202c", - }, - // Test path with RTL character. - { L"c:" + kSeparator + L"\x05d0", - L"\x202a"L"c:" + kSeparator + L"\x05d0\x202c", - }, - // Test path with 2 level RTL directory names. - { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622", - L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + - L"\x0622\x202c", - }, - // Test path with mixed RTL/LTR directory names and ending with punctuation. - { L"c:" + kSeparator + L"\x05d0" + kSeparator + L"\x0622" + kSeparator + - L"(foo)" + kSeparator + L"b.a.r.", - L"\x202a"L"c:" + kSeparator + L"\x05d0" + kSeparator + - L"\x0622" + kSeparator + L"(foo)" + kSeparator + - L"b.a.r.\x202c", - }, - // Test path without driver name, such as "/foo/bar/test/jpg". - { kSeparator + L"foo" + kSeparator + L"bar" + kSeparator + L"test.jpg", - L"\x202a" + kSeparator + L"foo" + kSeparator + L"bar" + - kSeparator + L"test.jpg" + L"\x202c" - }, - // Test path start with current directory, such as "./foo". - { L"." + kSeparator + L"foo", - L"\x202a"L"." + kSeparator + L"foo" + L"\x202c" - }, - // Test path start with parent directory, such as "../foo/bar.jpg". - { L".." + kSeparator + L"foo" + kSeparator + L"bar.jpg", - L"\x202a"L".." + kSeparator + L"foo" + kSeparator + - L"bar.jpg" + L"\x202c" - }, - // Test absolute path, such as "//foo/bar.jpg". - { kSeparator + kSeparator + L"foo" + kSeparator + L"bar.jpg", - L"\x202a" + kSeparator + kSeparator + L"foo" + kSeparator + - L"bar.jpg" + L"\x202c" - }, - // Test path with mixed RTL/LTR directory names. - { L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + kSeparator + - L"\x0622" + kSeparator + L"\x05d1.jpg", - L"\x202a"L"c:" + kSeparator + L"foo" + kSeparator + L"\x05d0" + - kSeparator + L"\x0622" + kSeparator + L"\x05d1.jpg" + L"\x202c", - }, - // Test empty path. - { L"", - L"\x202a\x202c" - } - }; - for (unsigned int i = 0; i < arraysize(test_data); ++i) { - string16 localized_file_path_string; - FilePath path = FilePath::FromWStringHack(test_data[i].path); - l10n_util::WrapPathWithLTRFormatting(path, &localized_file_path_string); - std::wstring wrapped_path = UTF16ToWide(localized_file_path_string); - EXPECT_EQ(wrapped_path, test_data[i].wrapped_path); - } -} - -typedef struct { - std::wstring raw_filename; - std::wstring display_string; -} StringAndLTRString; - -TEST_F(L10nUtilTest, GetDisplayStringInLTRDirectionality) { - const StringAndLTRString test_data[] = { - { L"test", L"\x202atest\x202c" }, - { L"test.html", L"\x202atest.html\x202c" }, - { L"\x05d0\x05d1\x05d2", L"\x202a\x05d0\x05d1\x05d2\x202c" }, - { L"\x05d0\x05d1\x05d2.txt", L"\x202a\x05d0\x05d1\x05d2.txt\x202c" }, - { L"\x05d0"L"abc", L"\x202a\x05d0"L"abc\x202c" }, - { L"\x05d0"L"abc.txt", L"\x202a\x05d0"L"abc.txt\x202c" }, - { L"abc\x05d0\x05d1", L"\x202a"L"abc\x05d0\x05d1\x202c" }, - { L"abc\x05d0\x05d1.jpg", L"\x202a"L"abc\x05d0\x05d1.jpg\x202c" }, - }; - for (unsigned int i = 0; i < arraysize(test_data); ++i) { - std::wstring input = test_data[i].raw_filename; - std::wstring expected = - l10n_util::GetDisplayStringInLTRDirectionality(&input); - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - EXPECT_EQ(test_data[i].display_string, expected); - else - EXPECT_EQ(input, expected); - } -} - -TEST_F(L10nUtilTest, GetTextDirection) { - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar")); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ar_EG")); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he")); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("he_IL")); - // iw is an obsolete code for Hebrew. - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("iw")); - // Although we're not yet localized to Farsi and Urdu, we - // do have the text layout direction information for them. - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("fa")); - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("ur")); -#if 0 - // Enable these when we include the minimal locale data for Azerbaijani - // written in Arabic and Dhivehi. At the moment, our copy of - // ICU data does not have entries for them. - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("az_Arab")); - // Dhivehi that uses Thaana script. - EXPECT_EQ(l10n_util::RIGHT_TO_LEFT, GetTextDirection("dv")); -#endif - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("en")); - // Chinese in China with '-'. - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("zh-CN")); - // Filipino : 3-letter code - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("fil")); - // Russian - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ru")); - // Japanese that uses multiple scripts - EXPECT_EQ(l10n_util::LEFT_TO_RIGHT, GetTextDirection("ja")); -} - // Test upper and lower case string conversion. TEST_F(L10nUtilTest, UpperLower) { string16 mixed(ASCIIToUTF16("Text with UPPer & lowER casE.")); diff --git a/app/l10n_util_win.cc b/app/l10n_util_win.cc index 0ffdad7..92dfac1 100644 --- a/app/l10n_util_win.cc +++ b/app/l10n_util_win.cc @@ -3,11 +3,12 @@ // found in the LICENSE file. #include "app/l10n_util.h" -#include "app/l10n_util_win.h" #include <algorithm> #include <windowsx.h> +#include "app/l10n_util_win.h" +#include "base/i18n/rtl.h" #include "base/string_util.h" #include "base/win_util.h" @@ -39,12 +40,11 @@ void AdjustLogFont(const std::wstring& font_family, namespace l10n_util { int GetExtendedStyles() { - return GetTextDirection() == LEFT_TO_RIGHT ? 0 : - WS_EX_LAYOUTRTL | WS_EX_RTLREADING; + return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL | WS_EX_RTLREADING; } int GetExtendedTooltipStyles() { - return GetTextDirection() == LEFT_TO_RIGHT ? 0 : WS_EX_LAYOUTRTL; + return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL; } void HWNDSetRTLLayout(HWND hwnd) { diff --git a/app/resource_bundle_linux.cc b/app/resource_bundle_linux.cc index 49c271a..cc9dbad 100644 --- a/app/resource_bundle_linux.cc +++ b/app/resource_bundle_linux.cc @@ -13,6 +13,7 @@ #include "base/data_pack.h" #include "base/file_path.h" #include "base/file_util.h" +#include "base/i18n/rtl.h" #include "base/logging.h" #include "base/path_service.h" #include "base/string_piece.h" @@ -40,8 +41,7 @@ GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { if (!pixbuf) return NULL; - if ((l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) && - rtl_enabled) { + if (base::i18n::IsRTL() && rtl_enabled) { // |pixbuf| will get unreffed and destroyed (see below). The returned value // has ref count 1. return gdk_pixbuf_flip(pixbuf, TRUE); diff --git a/app/text_elider.cc b/app/text_elider.cc index 21d5201..98b9090 100644 --- a/app/text_elider.cc +++ b/app/text_elider.cc @@ -8,6 +8,7 @@ #include "app/text_elider.h" #include "app/l10n_util.h" #include "base/file_path.h" +#include "base/i18n/rtl.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" @@ -263,7 +264,7 @@ std::wstring ElideFilename(const FilePath& filename, int full_width = font.GetStringWidth(filename.ToWStringHack()); if (full_width <= available_pixel_width) { std::wstring elided_name = filename.ToWStringHack(); - return l10n_util::GetDisplayStringInLTRDirectionality(&elided_name); + return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); } #if defined(OS_WIN) @@ -277,7 +278,7 @@ std::wstring ElideFilename(const FilePath& filename, if (rootname.empty() || extension.empty()) { std::wstring elided_name = ElideText(filename.ToWStringHack(), font, available_pixel_width); - return l10n_util::GetDisplayStringInLTRDirectionality(&elided_name); + return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); } int ext_width = font.GetStringWidth(extension); @@ -286,13 +287,13 @@ std::wstring ElideFilename(const FilePath& filename, // We may have trimmed the path. if (root_width + ext_width <= available_pixel_width) { std::wstring elided_name = rootname + extension; - return l10n_util::GetDisplayStringInLTRDirectionality(&elided_name); + return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); } int available_root_width = available_pixel_width - ext_width; std::wstring elided_name = ElideText(rootname, font, available_root_width); elided_name += extension; - return l10n_util::GetDisplayStringInLTRDirectionality(&elided_name); + return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); } // This function adds an ellipsis at the end of the text if the text diff --git a/app/text_elider.h b/app/text_elider.h index aa33c29..f93b944 100644 --- a/app/text_elider.h +++ b/app/text_elider.h @@ -28,7 +28,7 @@ namespace gfx { // // Note: in RTL locales, if the URL returned by this function is going to be // displayed in the UI, then it is likely that the string needs to be marked -// as an LTR string (using l10n_util::WrapStringWithLTRFormatting()) so that it +// as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it // is displayed properly in an RTL context. Please refer to // http://crbug.com/6487 for more information. std::wstring ElideUrl(const GURL& url, diff --git a/app/text_elider_unittest.cc b/app/text_elider_unittest.cc index f8a30ef..f2a81f4f 100644 --- a/app/text_elider_unittest.cc +++ b/app/text_elider_unittest.cc @@ -6,6 +6,7 @@ #include "app/text_elider.h" #include "app/l10n_util.h" #include "base/file_path.h" +#include "base/i18n/rtl.h" #include "base/string_util.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -178,8 +179,8 @@ TEST(TextEliderTest, TestFilenameEliding) { for (size_t i = 0; i < arraysize(testcases); ++i) { FilePath filepath(testcases[i].input); std::wstring expected = testcases[i].output; - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - l10n_util::WrapStringWithLTRFormatting(&expected); + if (base::i18n::IsRTL()) + base::i18n::WrapStringWithLTRFormatting(&expected); EXPECT_EQ(expected, ElideFilename(filepath, font, font.GetStringWidth(testcases[i].output))); diff --git a/app/win_util.cc b/app/win_util.cc index a85ca9b..b043db3 100644 --- a/app/win_util.cc +++ b/app/win_util.cc @@ -16,6 +16,7 @@ #include "base/base_switches.h" #include "base/command_line.h" #include "base/file_util.h" +#include "base/i18n/rtl.h" #include "base/logging.h" #include "base/native_library.h" #include "base/registry.h" @@ -519,17 +520,17 @@ int MessageBox(HWND hwnd, return IDOK; UINT actual_flags = flags; - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + if (base::i18n::IsRTL()) actual_flags |= MB_RIGHT | MB_RTLREADING; std::wstring localized_text; const wchar_t* text_ptr = text.c_str(); - if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) + if (base::i18n::AdjustStringForLocaleDirection(text, &localized_text)) text_ptr = localized_text.c_str(); std::wstring localized_caption; const wchar_t* caption_ptr = caption.c_str(); - if (l10n_util::AdjustStringForLocaleDirection(caption, &localized_caption)) + if (base::i18n::AdjustStringForLocaleDirection(caption, &localized_caption)) caption_ptr = localized_caption.c_str(); return ::MessageBox(hwnd, text_ptr, caption_ptr, actual_flags); |