diff options
-rw-r--r-- | ui/gfx/canvas.h | 4 | ||||
-rw-r--r-- | ui/gfx/canvas_skia_win.cc | 19 | ||||
-rw-r--r-- | views/controls/label.cc | 9 | ||||
-rw-r--r-- | views/controls/label_unittest.cc | 78 |
4 files changed, 61 insertions, 49 deletions
diff --git a/ui/gfx/canvas.h b/ui/gfx/canvas.h index 5fdce31..5fa067e 100644 --- a/ui/gfx/canvas.h +++ b/ui/gfx/canvas.h @@ -68,6 +68,10 @@ class UI_EXPORT Canvas { // installed) don't support these characters. Thus, this flag should be // used to render text using RTL directionality when the locale is LTR. FORCE_RTL_DIRECTIONALITY = 2048, + + // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right. + // See FORCE_RTL_DIRECTIONALITY for details. + FORCE_LTR_DIRECTIONALITY = 4096, }; virtual ~Canvas() {} diff --git a/ui/gfx/canvas_skia_win.cc b/ui/gfx/canvas_skia_win.cc index 001c55b..e71fefd 100644 --- a/ui/gfx/canvas_skia_win.cc +++ b/ui/gfx/canvas_skia_win.cc @@ -112,12 +112,15 @@ int ComputeFormatFlags(int flags, const string16& text) { // first character with strong directionality. If the directionality of the // first character with strong directionality in the text is LTR, the // alignment is set to DT_LEFT, and the directionality should not be set as - // DT_RTLREADING. + // DT_RTLREADING. If the directionality of the first character with strong + // directionality in the text is RTL, its alignment is set to DT_RIGHT, and + // its directionality is set as DT_RTLREADING through + // FORCE_RTL_DIRECTIONALITY. // // This heuristic doesn't work for Chrome UI strings since even in RTL // locales, some of those might start with English text but we know they're - // localized so we always want them to be right aligned, and their - // directionality should be set as DT_RTLREADING. + // localized so their directionality should be set as DT_RTLREADING if it + // contains strong RTL characters. // // Caveat: If the string is purely LTR, don't set DTL_RTLREADING since when // the flag is set, LRE-PDF don't have the desired effect of rendering @@ -126,11 +129,13 @@ int ComputeFormatFlags(int flags, const string16& text) { // Note that if the caller is explicitly requesting displaying the 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) || - (base::i18n::IsRTL() && - (f & DT_RIGHT) && base::i18n::StringContainsStrongRTLChars(text))) { + int force_rtl = (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY); + int force_ltr = (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY); + bool is_rtl = base::i18n::IsRTL(); + bool string_contains_strong_rtl_chars = + base::i18n::StringContainsStrongRTLChars(text); + if (force_rtl || (!force_ltr && is_rtl && string_contains_strong_rtl_chars)) f |= DT_RTLREADING; - } return f; } diff --git a/views/controls/label.cc b/views/controls/label.cc index fe32884..54978ec 100644 --- a/views/controls/label.cc +++ b/views/controls/label.cc @@ -463,6 +463,15 @@ void Label::CalculateDrawStringParams(std::wstring* paint_text, *text_bounds = GetTextBounds(); *flags = ComputeMultiLineFlags(); + // If rtl_alignment_mode_ is AUTO_DETECT_ALIGNMENT (such as for text from + // webpage, not from chrome's UI), its directionality is forced to be RTL if + // it is right aligned. Otherwise, its directionality is forced to be LTR. + if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { + if (horiz_alignment_ == ALIGN_RIGHT) + *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; + else + *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; + } } } // namespace views diff --git a/views/controls/label_unittest.cc b/views/controls/label_unittest.cc index 358a136..d0a162f 100644 --- a/views/controls/label_unittest.cc +++ b/views/controls/label_unittest.cc @@ -306,7 +306,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); // Left aligned text. label.SetHorizontalAlignment(Label::ALIGN_LEFT); @@ -319,7 +319,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); // Right aligned text. label.SetHorizontalAlignment(Label::ALIGN_RIGHT); @@ -332,7 +332,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); // Test single line drawing with a border. gfx::Insets border(39, 34, 8, 96); @@ -362,7 +362,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); // Left aligned text with border. label.SetHorizontalAlignment(Label::ALIGN_LEFT); @@ -375,7 +375,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); // Right aligned text. label.SetHorizontalAlignment(Label::ALIGN_RIGHT); @@ -388,7 +388,7 @@ TEST(LabelTest, DrawSingleLineString) { EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); EXPECT_EQ(required_size.width(), text_bounds.width()); EXPECT_EQ(required_size.height(), text_bounds.height()); - EXPECT_EQ(0, flags); + EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); } // On Linux the underlying pango routines require a max height in order to @@ -422,14 +422,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(extra.height() / 2, text_bounds.y()); EXPECT_GT(text_bounds.width(), kMinTextDimension); EXPECT_GT(text_bounds.height(), kMinTextDimension); + int expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_CENTER | + gfx::Canvas::FORCE_LTR_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_CENTER, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_CENTER | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif gfx::Rect center_bounds(text_bounds); @@ -442,14 +441,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(extra.height() / 2, text_bounds.y()); EXPECT_GT(text_bounds.width(), kMinTextDimension); EXPECT_GT(text_bounds.height(), kMinTextDimension); + expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_LEFT | + gfx::Canvas::FORCE_LTR_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_LEFT, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_LEFT | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif label.SetHorizontalAlignment(Label::ALIGN_RIGHT); @@ -461,14 +459,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(extra.height() / 2, text_bounds.y()); EXPECT_GT(text_bounds.width(), kMinTextDimension); EXPECT_GT(text_bounds.height(), kMinTextDimension); + expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_RIGHT | + gfx::Canvas::FORCE_RTL_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_RIGHT, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_RIGHT | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif // Test multiline drawing with a border. @@ -492,14 +489,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); EXPECT_EQ(center_bounds.width(), text_bounds.width()); EXPECT_EQ(center_bounds.height(), text_bounds.height()); + expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_CENTER | + gfx::Canvas::FORCE_LTR_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_CENTER, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_CENTER | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif label.SetHorizontalAlignment(Label::ALIGN_LEFT); @@ -511,14 +507,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); EXPECT_EQ(center_bounds.width(), text_bounds.width()); EXPECT_EQ(center_bounds.height(), text_bounds.height()); + expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_LEFT | + gfx::Canvas::FORCE_LTR_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_LEFT, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_LEFT | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif label.SetHorizontalAlignment(Label::ALIGN_RIGHT); @@ -530,14 +525,13 @@ TEST(LabelTest, DrawMultiLineString) { EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); EXPECT_EQ(center_bounds.width(), text_bounds.width()); EXPECT_EQ(center_bounds.height(), text_bounds.height()); + expected_flags = gfx::Canvas::MULTI_LINE | + gfx::Canvas::TEXT_ALIGN_RIGHT | + gfx::Canvas::FORCE_RTL_DIRECTIONALITY; #if defined(OS_WIN) - EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_RIGHT, flags); + EXPECT_EQ(expected_flags, flags); #else - EXPECT_EQ( - gfx::Canvas::MULTI_LINE | - gfx::Canvas::TEXT_ALIGN_RIGHT | - gfx::Canvas::NO_ELLIPSIS, - flags); + EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); #endif } |