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 should be rendered right-to-left. |
25 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) | 25 bool AdjustStringDirection(int flags, string16* text) { |
26 return true; | 26 bool is_rtl = false; |
27 if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY) | 27 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) { |
28 return false; | 28 base::i18n::WrapStringWithRTLFormatting(text); |
29 return base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(text); | 29 is_rtl = true; |
30 } else if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY) { | |
31 base::i18n::WrapStringWithLTRFormatting(text); | |
xji
2012/05/14 22:50:51
This is a bit tricky. In systems without RTL suppo
xji
2012/05/14 23:09:10
long term wise, I think we should be able to set t
| |
32 is_rtl = false; | |
33 } else if (base::i18n::IsRTL() && | |
34 base::i18n::StringContainsStrongRTLChars(*text)) { | |
35 base::i18n::WrapStringWithRTLFormatting(text); | |
36 is_rtl = true; | |
37 } | |
38 return is_rtl; | |
30 } | 39 } |
31 | 40 |
32 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If | 41 // 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 | 42 // 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 | 43 // 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| | 44 // 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 | 45 // will modify the bitmap as it goes, and cleared pixels shouldn't count as |
37 // changed. | 46 // changed. |
38 bool PixelShouldGetHalo(const SkBitmap& bitmap, | 47 bool PixelShouldGetHalo(const SkBitmap& bitmap, |
39 int x, int y, | 48 int x, int y, |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 gfx::Rect clip_rect(text_bounds); | 252 gfx::Rect clip_rect(text_bounds); |
244 clip_rect.Inset(ShadowValue::GetMargin(shadows)); | 253 clip_rect.Inset(ShadowValue::GetMargin(shadows)); |
245 | 254 |
246 canvas_->save(SkCanvas::kClip_SaveFlag); | 255 canvas_->save(SkCanvas::kClip_SaveFlag); |
247 ClipRect(clip_rect); | 256 ClipRect(clip_rect); |
248 | 257 |
249 gfx::Rect rect(text_bounds); | 258 gfx::Rect rect(text_bounds); |
250 string16 adjusted_text = text; | 259 string16 adjusted_text = text; |
251 | 260 |
252 #if defined(OS_WIN) | 261 #if defined(OS_WIN) |
253 if (IsTextRTL(flags, adjusted_text)) | 262 AdjustStringDirection(flags, &adjusted_text); |
254 base::i18n::AdjustStringForLocaleDirection(&adjusted_text); | |
255 #endif | 263 #endif |
256 | 264 |
257 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 265 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
258 render_text->SetTextShadows(shadows); | 266 render_text->SetTextShadows(shadows); |
259 | 267 |
260 if (flags & MULTI_LINE) { | 268 if (flags & MULTI_LINE) { |
261 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; | 269 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; |
262 if (flags & CHARACTER_BREAK) | 270 if (flags & CHARACTER_BREAK) |
263 wrap_behavior = ui::WRAP_LONG_WORDS; | 271 wrap_behavior = ui::WRAP_LONG_WORDS; |
264 else if (!(flags & NO_ELLIPSIS)) | 272 else if (!(flags & NO_ELLIPSIS)) |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 | 393 |
386 // If the whole string fits in the destination then just draw it directly. | 394 // If the whole string fits in the destination then just draw it directly. |
387 if (GetStringWidth(text, font) <= display_rect.width()) { | 395 if (GetStringWidth(text, font) <= display_rect.width()) { |
388 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 396 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
389 display_rect.width(), display_rect.height(), flags); | 397 display_rect.width(), display_rect.height(), flags); |
390 return; | 398 return; |
391 } | 399 } |
392 | 400 |
393 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 401 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
394 string16 clipped_text = text; | 402 string16 clipped_text = text; |
395 const bool is_rtl = IsTextRTL(flags, text); | 403 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); |
396 if (is_rtl) | |
397 base::i18n::AdjustStringForLocaleDirection(&clipped_text); | |
398 | 404 |
399 switch (truncate_mode) { | 405 switch (truncate_mode) { |
400 case TruncateFadeTail: | 406 case TruncateFadeTail: |
401 render_text->set_fade_tail(true); | 407 render_text->set_fade_tail(true); |
402 if (is_rtl) | 408 if (is_rtl) |
403 flags |= TEXT_ALIGN_RIGHT; | 409 flags |= TEXT_ALIGN_RIGHT; |
404 break; | 410 break; |
405 case TruncateFadeHead: | 411 case TruncateFadeHead: |
406 render_text->set_fade_head(true); | 412 render_text->set_fade_head(true); |
407 if (!is_rtl) | 413 if (!is_rtl) |
(...skipping 30 matching lines...) Expand all Loading... | |
438 render_text->SetDisplayRect(rect); | 444 render_text->SetDisplayRect(rect); |
439 | 445 |
440 canvas_->save(SkCanvas::kClip_SaveFlag); | 446 canvas_->save(SkCanvas::kClip_SaveFlag); |
441 ClipRect(display_rect); | 447 ClipRect(display_rect); |
442 render_text->Draw(this); | 448 render_text->Draw(this); |
443 canvas_->restore(); | 449 canvas_->restore(); |
444 } | 450 } |
445 #endif | 451 #endif |
446 | 452 |
447 } // namespace gfx | 453 } // namespace gfx |
OLD | NEW |