Skip to content

Commit

Permalink
Small cleanups/style fixes. Simplifies RTL functions slightly.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/1932006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46419 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pkasting@chromium.org committed May 5, 2010
1 parent 4f2fe46 commit 883ce72
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
32 changes: 17 additions & 15 deletions base/i18n/rtl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,21 @@ void SetICUDefaultLocale(const std::string& locale_string) {
g_icu_text_direction = UNKNOWN_DIRECTION;
}

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() {
bool IsRTL() {
#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;
return (gtk_dir == GTK_TEXT_DIR_RTL);
#else
return GetICUTextDirection();
return ICUIsRTL();
#endif
}

bool IsRTL() {
return GetTextDirection() == RIGHT_TO_LEFT;
bool ICUIsRTL() {
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 == RIGHT_TO_LEFT;
}

TextDirection GetTextDirectionForLocale(const char* locale_name) {
Expand Down Expand Up @@ -142,7 +138,7 @@ TextDirection GetFirstStrongCharacterDirection(const std::wstring& text) {

bool AdjustStringForLocaleDirection(const std::wstring& text,
std::wstring* localized_text) {
if (GetTextDirection() == LEFT_TO_RIGHT || text.length() == 0)
if (!IsRTL() || text.empty())
return false;

// Marking the string as LTR if the locale is RTL and the string does not
Expand Down Expand Up @@ -184,6 +180,9 @@ bool StringContainsStrongRTLChars(const std::wstring& text) {
}

void WrapStringWithLTRFormatting(std::wstring* text) {
if (text->empty())
return;

// Inserting an LRE (Left-To-Right Embedding) mark as the first character.
text->insert(0, 1, static_cast<wchar_t>(kLeftToRightEmbeddingMark));

Expand All @@ -192,6 +191,9 @@ void WrapStringWithLTRFormatting(std::wstring* text) {
}

void WrapStringWithRTLFormatting(std::wstring* text) {
if (text->empty())
return;

// Inserting an RLE (Right-To-Left Embedding) mark as the first character.
text->insert(0, 1, static_cast<wchar_t>(kRightToLeftEmbeddingMark));

Expand All @@ -218,7 +220,7 @@ void WrapPathWithLTRFormatting(const FilePath& path,
}

std::wstring GetDisplayStringInLTRDirectionality(std::wstring* text) {
if (GetTextDirection() == RIGHT_TO_LEFT)
if (IsRTL())
WrapStringWithLTRFormatting(text);
return *text;
}
Expand Down
19 changes: 6 additions & 13 deletions base/i18n/rtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const char16 kLeftToRightEmbeddingMark = 0x202A;
const char16 kRightToLeftEmbeddingMark = 0x202B;
const char16 kPopDirectionalFormatting = 0x202C;

// Represents the text direction returned by the GetTextDirection() function.
enum TextDirection {
UNKNOWN_DIRECTION,
RIGHT_TO_LEFT,
Expand All @@ -37,21 +36,15 @@ void GetLanguageAndRegionFromOS(std::string* lang, std::string* region);
// that this is called before any locale-dependent API is called.
void SetICUDefaultLocale(const std::string& locale_string);

// 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 true if the application text direction is right-to-left.
bool IsRTL();

// Returns whether the text direction for the default ICU locale is RTL. This
// assumes that SetICUDefaultLocale has been called to set the default locale to
// the UI locale of Chrome.
// NOTE: Generally, you should call IsRTL() instead of this.
bool ICUIsRTL();

// Returns the text direction for |locale_name|.
TextDirection GetTextDirectionForLocale(const char* locale_name);

Expand Down
7 changes: 2 additions & 5 deletions chrome/browser/autocomplete/autocomplete_edit_view_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1966,11 +1966,8 @@ LONG AutocompleteEditViewWin::ClipXCoordToVisibleText(
// Calculation of the clipped coordinate is more complicated if the paragraph
// layout is RTL layout, or if there is RTL characters inside the LTR layout
// paragraph.
bool ltr_text_in_ltr_layout = true;
if ((pf2.wEffects & PFE_RTLPARA) ||
base::i18n::StringContainsStrongRTLChars(GetText())) {
ltr_text_in_ltr_layout = false;
}
const bool ltr_text_in_ltr_layout = !(pf2.wEffects & PFE_RTLPARA) &&
!base::i18n::StringContainsStrongRTLChars(GetText());
const int length = GetTextLength();
RECT r;
GetRect(&r);
Expand Down
8 changes: 4 additions & 4 deletions chrome/renderer/localized_error.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand Down Expand Up @@ -107,9 +107,9 @@ WebErrorNetErrorMap net_error_options[] = {

bool LocaleIsRTL() {
#if defined(TOOLKIT_GTK)
// base::i18n::GetTextDirection uses the GTK text direction, which doesn't work
// within the renderer sandbox.
return base::i18n::GetICUTextDirection() == base::i18n::RIGHT_TO_LEFT;
// base::i18n::IsRTL() uses the GTK text direction, which doesn't work within
// the renderer sandbox.
return base::i18n::ICUIsRTL();
#else
return base::i18n::IsRTL();
#endif
Expand Down
6 changes: 3 additions & 3 deletions gfx/canvas.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand Down Expand Up @@ -208,8 +208,8 @@ class Canvas : public skia::PlatformCanvas {
// Attempts to fit the text with the provided width and height. Increases
// height and then width as needed to make the text fit. This method
// supports multiple lines.
static void SizeStringInt(const std::wstring& test, const gfx::Font& font,
int *width, int* height, int flags);
static void SizeStringInt(const std::wstring& text, 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
Expand Down
2 changes: 1 addition & 1 deletion gfx/canvas_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Canvas::~Canvas() {
// static
void Canvas::SizeStringInt(const std::wstring& text,
const gfx::Font& font,
int *width, int *height, int flags) {
int* width, int* height, int flags) {
// Clamp the max amount of text we'll measure to 2K. When the string is
// actually drawn, it will be clipped to whatever size box is provided, and
// the time to do that doesn't depend on the length being clipped off.
Expand Down

0 comments on commit 883ce72

Please sign in to comment.