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 // If necessary, wraps |text| with RTL/LTR directionality characters based on | |
23 // |flags| and |text| content. | |
24 // Returns true if the text will be rendered right-to-left. | |
25 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly. | |
26 bool AdjustStringDirection(int flags, string16* text) { | |
27 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now. | |
28 | |
29 // If the string is empty or LTR was forced, simply return false since the | |
30 // default RenderText directionality is already LTR. | |
31 if (text->empty() || (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY)) | |
32 return false; | |
33 | |
34 // If RTL is forced, apply it to the string. | |
35 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) { | |
36 base::i18n::WrapStringWithRTLFormatting(text); | |
37 return true; | |
38 } | |
39 | |
40 // If a direction wasn't forced but the UI language is RTL and there were | |
41 // strong RTL characters, ensure RTL is applied. | |
42 if (base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(*text)) { | |
43 base::i18n::WrapStringWithRTLFormatting(text); | |
44 return true; | |
45 } | |
46 | |
47 // In the default case, the string should be rendered as LTR. RenderText's | |
48 // default directionality is LTR, so the text doesn't need to be wrapped. | |
49 // Note that individual runs within the string may still be rendered RTL | |
50 // (which will be the case for RTL text under non-RTL locales, since under RTL | |
51 // locales it will be handled by the if statement above). | |
52 return false; | |
53 } | |
54 | |
55 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If | 22 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If |
56 // any of them are not the halo color, returns true. This defines the halo of | 23 // any of them are not the halo color, returns true. This defines the halo of |
57 // pixels that will appear around the text. Note that we have to check each | 24 // pixels that will appear around the text. Note that we have to check each |
58 // pixel against both the halo color and transparent since |DrawStringWithHalo| | 25 // pixel against both the halo color and transparent since |DrawStringWithHalo| |
59 // will modify the bitmap as it goes, and cleared pixels shouldn't count as | 26 // will modify the bitmap as it goes, and cleared pixels shouldn't count as |
60 // changed. | 27 // changed. |
61 bool PixelShouldGetHalo(const SkBitmap& bitmap, | 28 bool PixelShouldGetHalo(const SkBitmap& bitmap, |
62 int x, int y, | 29 int x, int y, |
63 SkColor halo_color) { | 30 SkColor halo_color) { |
64 if (x > 0 && | 31 if (x > 0 && |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 166 |
200 // static | 167 // static |
201 void Canvas::SizeStringInt(const string16& text, | 168 void Canvas::SizeStringInt(const string16& text, |
202 const gfx::Font& font, | 169 const gfx::Font& font, |
203 int* width, int* height, | 170 int* width, int* height, |
204 int flags) { | 171 int flags) { |
205 DCHECK_GE(*width, 0); | 172 DCHECK_GE(*width, 0); |
206 DCHECK_GE(*height, 0); | 173 DCHECK_GE(*height, 0); |
207 | 174 |
208 flags = AdjustPlatformSpecificFlags(text, flags); | 175 flags = AdjustPlatformSpecificFlags(text, flags); |
209 | |
210 string16 adjusted_text = text; | 176 string16 adjusted_text = text; |
211 #if defined(OS_WIN) | |
212 AdjustStringDirection(flags, &adjusted_text); | |
213 #endif | |
214 | 177 |
215 if ((flags & MULTI_LINE) && *width != 0) { | 178 if ((flags & MULTI_LINE) && *width != 0) { |
216 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; | 179 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; |
217 if (flags & CHARACTER_BREAK) | 180 if (flags & CHARACTER_BREAK) |
218 wrap_behavior = ui::WRAP_LONG_WORDS; | 181 wrap_behavior = ui::WRAP_LONG_WORDS; |
219 else if (!(flags & NO_ELLIPSIS)) | 182 else if (!(flags & NO_ELLIPSIS)) |
220 wrap_behavior = ui::ELIDE_LONG_WORDS; | 183 wrap_behavior = ui::ELIDE_LONG_WORDS; |
221 | 184 |
222 gfx::Rect rect(*width, INT_MAX); | 185 gfx::Rect rect(*width, INT_MAX); |
223 std::vector<string16> strings; | 186 std::vector<string16> strings; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 241 |
279 gfx::Rect clip_rect(text_bounds); | 242 gfx::Rect clip_rect(text_bounds); |
280 clip_rect.Inset(ShadowValue::GetMargin(shadows)); | 243 clip_rect.Inset(ShadowValue::GetMargin(shadows)); |
281 | 244 |
282 canvas_->save(SkCanvas::kClip_SaveFlag); | 245 canvas_->save(SkCanvas::kClip_SaveFlag); |
283 ClipRect(clip_rect); | 246 ClipRect(clip_rect); |
284 | 247 |
285 gfx::Rect rect(text_bounds); | 248 gfx::Rect rect(text_bounds); |
286 string16 adjusted_text = text; | 249 string16 adjusted_text = text; |
287 | 250 |
288 #if defined(OS_WIN) | |
289 AdjustStringDirection(flags, &adjusted_text); | |
290 #endif | |
291 | |
292 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 251 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
293 render_text->SetTextShadows(shadows); | 252 render_text->SetTextShadows(shadows); |
294 | 253 |
295 if (flags & MULTI_LINE) { | 254 if (flags & MULTI_LINE) { |
296 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; | 255 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; |
297 if (flags & CHARACTER_BREAK) | 256 if (flags & CHARACTER_BREAK) |
298 wrap_behavior = ui::WRAP_LONG_WORDS; | 257 wrap_behavior = ui::WRAP_LONG_WORDS; |
299 else if (!(flags & NO_ELLIPSIS)) | 258 else if (!(flags & NO_ELLIPSIS)) |
300 wrap_behavior = ui::ELIDE_LONG_WORDS; | 259 wrap_behavior = ui::ELIDE_LONG_WORDS; |
301 | 260 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 | 380 |
422 // If the whole string fits in the destination then just draw it directly. | 381 // If the whole string fits in the destination then just draw it directly. |
423 if (GetStringWidth(text, font) <= display_rect.width()) { | 382 if (GetStringWidth(text, font) <= display_rect.width()) { |
424 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 383 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
425 display_rect.width(), display_rect.height(), flags); | 384 display_rect.width(), display_rect.height(), flags); |
426 return; | 385 return; |
427 } | 386 } |
428 | 387 |
429 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 388 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
430 string16 clipped_text = text; | 389 string16 clipped_text = text; |
431 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); | 390 |
| 391 // TODO(msw): Text alignment and fading should probably just derive from the |
| 392 // text directionality, and ignore the UI locale. But the legacy behavior uses |
| 393 // the UI locale, except for LTR text in RTL locales. See crbug.com/134746 |
| 394 const bool text_rtl = base::i18n::RIGHT_TO_LEFT == |
| 395 base::i18n::GetFirstStrongCharacterDirection(text); |
| 396 const bool use_rtl = base::i18n::IsRTL() && text_rtl; |
432 | 397 |
433 switch (truncate_mode) { | 398 switch (truncate_mode) { |
434 case TruncateFadeTail: | 399 case TruncateFadeTail: |
435 render_text->set_fade_tail(true); | 400 render_text->set_fade_tail(true); |
436 if (is_rtl) | 401 if (use_rtl) |
437 flags |= TEXT_ALIGN_RIGHT; | 402 flags |= TEXT_ALIGN_RIGHT; |
438 break; | 403 break; |
439 case TruncateFadeHead: | 404 case TruncateFadeHead: |
440 render_text->set_fade_head(true); | 405 render_text->set_fade_head(true); |
441 if (!is_rtl) | 406 if (!use_rtl) |
442 flags |= TEXT_ALIGN_RIGHT; | 407 flags |= TEXT_ALIGN_RIGHT; |
443 break; | 408 break; |
444 case TruncateFadeHeadAndTail: | 409 case TruncateFadeHeadAndTail: |
445 DCHECK_GT(desired_characters_to_truncate_from_head, 0u); | 410 DCHECK_GT(desired_characters_to_truncate_from_head, 0u); |
446 // Due to the fade effect the first character is hard to see. | 411 // Due to the fade effect the first character is hard to see. |
447 // We want to make sure that the first character starting at | 412 // We want to make sure that the first character starting at |
448 // |desired_characters_to_truncate_from_head| is readable so we reduce | 413 // |desired_characters_to_truncate_from_head| is readable so we reduce |
449 // the offset by a little bit. | 414 // the offset by a little bit. |
450 desired_characters_to_truncate_from_head = | 415 desired_characters_to_truncate_from_head = |
451 std::max<int>(0, desired_characters_to_truncate_from_head - 2); | 416 std::max<int>(0, desired_characters_to_truncate_from_head - 2); |
(...skipping 23 matching lines...) Expand all Loading... |
475 rect.set_height(line_height); | 440 rect.set_height(line_height); |
476 render_text->SetDisplayRect(rect); | 441 render_text->SetDisplayRect(rect); |
477 | 442 |
478 canvas_->save(SkCanvas::kClip_SaveFlag); | 443 canvas_->save(SkCanvas::kClip_SaveFlag); |
479 ClipRect(display_rect); | 444 ClipRect(display_rect); |
480 render_text->Draw(this); | 445 render_text->Draw(this); |
481 canvas_->restore(); | 446 canvas_->restore(); |
482 } | 447 } |
483 | 448 |
484 } // namespace gfx | 449 } // namespace gfx |
OLD | NEW |