Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/canvas.h" | 5 #include "ui/gfx/canvas.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "ui/base/range/range.h" | 10 #include "ui/base/range/range.h" |
| 11 #include "ui/base/text/text_elider.h" | 11 #include "ui/base/text/text_elider.h" |
| 12 #include "ui/gfx/font.h" | 12 #include "ui/gfx/font.h" |
| 13 #include "ui/gfx/font_list.h" | 13 #include "ui/gfx/font_list.h" |
| 14 #include "ui/gfx/insets.h" | 14 #include "ui/gfx/insets.h" |
| 15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/render_text.h" | 16 #include "ui/gfx/render_text.h" |
| 17 #include "ui/gfx/shadow_value.h" | 17 #include "ui/gfx/shadow_value.h" |
| 18 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Based on |flags| and |text| content, returns whether text should be | 22 // If necessary, wraps |text| with RTL/LTR directionality characters based on |
| 23 // rendered right-to-left. | 23 // |flags| and |text| content. |
| 24 bool IsTextRTL(int flags, const string16& text) { | 24 // Returns true if the text will be rendered right-to-left. |
| 25 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) | 25 bool AdjustStringDirection(int flags, string16* text) { |
| 26 if (text->empty()) | |
| 27 return false; | |
| 28 | |
| 29 // First, if RTL was forced, simply apply it. | |
| 30 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) { | |
| 31 base::i18n::WrapStringWithRTLFormatting(text); | |
| 26 return true; | 32 return true; |
| 27 if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY) | 33 } |
| 34 | |
| 35 // If RTL wasn't forced and the string does not have strong RTL chars, it | |
| 36 // will be rendered LTR already, so no need to do anything special. | |
| 37 if (!base::i18n::StringContainsStrongRTLChars(*text)) | |
| 28 return false; | 38 return false; |
| 29 return base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(text); | 39 |
| 40 // If LTR is forced for a string with strong RTL characters, apply it here. | |
| 41 if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY) { | |
| 42 base::i18n::WrapStringWithLTRFormatting(text); | |
|
xji
2012/05/15 19:42:07
I think in our current Uniscribe implementation of
| |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 // Finally, if a direction wasn't forced but the UI language is RTL and there | |
| 47 // were strong RTL characters, ensure RTL is applied. | |
| 48 if (base::i18n::IsRTL()) { | |
| 49 base::i18n::WrapStringWithRTLFormatting(text); | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 return false; | |
|
xji
2012/05/15 19:42:07
I'd prefer the old 'if'/'else' structure.
| |
| 30 } | 54 } |
| 31 | 55 |
| 32 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If | 56 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If |
| 33 // any of them are not the halo color, returns true. This defines the halo of | 57 // any of them are not the halo color, returns true. This defines the halo of |
| 34 // pixels that will appear around the text. Note that we have to check each | 58 // pixels that will appear around the text. Note that we have to check each |
| 35 // pixel against both the halo color and transparent since |DrawStringWithHalo| | 59 // pixel against both the halo color and transparent since |DrawStringWithHalo| |
| 36 // will modify the bitmap as it goes, and cleared pixels shouldn't count as | 60 // will modify the bitmap as it goes, and cleared pixels shouldn't count as |
| 37 // changed. | 61 // changed. |
| 38 bool PixelShouldGetHalo(const SkBitmap& bitmap, | 62 bool PixelShouldGetHalo(const SkBitmap& bitmap, |
| 39 int x, int y, | 63 int x, int y, |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 gfx::Rect clip_rect(text_bounds); | 267 gfx::Rect clip_rect(text_bounds); |
| 244 clip_rect.Inset(ShadowValue::GetMargin(shadows)); | 268 clip_rect.Inset(ShadowValue::GetMargin(shadows)); |
| 245 | 269 |
| 246 canvas_->save(SkCanvas::kClip_SaveFlag); | 270 canvas_->save(SkCanvas::kClip_SaveFlag); |
| 247 ClipRect(clip_rect); | 271 ClipRect(clip_rect); |
| 248 | 272 |
| 249 gfx::Rect rect(text_bounds); | 273 gfx::Rect rect(text_bounds); |
| 250 string16 adjusted_text = text; | 274 string16 adjusted_text = text; |
| 251 | 275 |
| 252 #if defined(OS_WIN) | 276 #if defined(OS_WIN) |
| 253 if (IsTextRTL(flags, adjusted_text)) | 277 AdjustStringDirection(flags, &adjusted_text); |
| 254 base::i18n::AdjustStringForLocaleDirection(&adjusted_text); | |
| 255 #endif | 278 #endif |
| 256 | 279 |
| 257 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 280 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
| 258 render_text->SetTextShadows(shadows); | 281 render_text->SetTextShadows(shadows); |
| 259 | 282 |
| 260 if (flags & MULTI_LINE) { | 283 if (flags & MULTI_LINE) { |
| 261 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; | 284 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; |
| 262 if (flags & CHARACTER_BREAK) | 285 if (flags & CHARACTER_BREAK) |
| 263 wrap_behavior = ui::WRAP_LONG_WORDS; | 286 wrap_behavior = ui::WRAP_LONG_WORDS; |
| 264 else if (!(flags & NO_ELLIPSIS)) | 287 else if (!(flags & NO_ELLIPSIS)) |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 | 408 |
| 386 // If the whole string fits in the destination then just draw it directly. | 409 // If the whole string fits in the destination then just draw it directly. |
| 387 if (GetStringWidth(text, font) <= display_rect.width()) { | 410 if (GetStringWidth(text, font) <= display_rect.width()) { |
| 388 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 411 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
| 389 display_rect.width(), display_rect.height(), flags); | 412 display_rect.width(), display_rect.height(), flags); |
| 390 return; | 413 return; |
| 391 } | 414 } |
| 392 | 415 |
| 393 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 416 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
| 394 string16 clipped_text = text; | 417 string16 clipped_text = text; |
| 395 const bool is_rtl = IsTextRTL(flags, text); | 418 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); |
| 396 if (is_rtl) | |
| 397 base::i18n::AdjustStringForLocaleDirection(&clipped_text); | |
| 398 | 419 |
| 399 switch (truncate_mode) { | 420 switch (truncate_mode) { |
| 400 case TruncateFadeTail: | 421 case TruncateFadeTail: |
| 401 render_text->set_fade_tail(true); | 422 render_text->set_fade_tail(true); |
| 402 if (is_rtl) | 423 if (is_rtl) |
| 403 flags |= TEXT_ALIGN_RIGHT; | 424 flags |= TEXT_ALIGN_RIGHT; |
| 404 break; | 425 break; |
| 405 case TruncateFadeHead: | 426 case TruncateFadeHead: |
| 406 render_text->set_fade_head(true); | 427 render_text->set_fade_head(true); |
| 407 if (!is_rtl) | 428 if (!is_rtl) |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 438 render_text->SetDisplayRect(rect); | 459 render_text->SetDisplayRect(rect); |
| 439 | 460 |
| 440 canvas_->save(SkCanvas::kClip_SaveFlag); | 461 canvas_->save(SkCanvas::kClip_SaveFlag); |
| 441 ClipRect(display_rect); | 462 ClipRect(display_rect); |
| 442 render_text->Draw(this); | 463 render_text->Draw(this); |
| 443 canvas_->restore(); | 464 canvas_->restore(); |
| 444 } | 465 } |
| 445 #endif | 466 #endif |
| 446 | 467 |
| 447 } // namespace gfx | 468 } // namespace gfx |
| OLD | NEW |