Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: ui/gfx/canvas_skia.cc

Issue 10695101: Remove code that forces text directionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove directionality forcing and modes; may be incorrect... Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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::CreateRenderText()); 251 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText());
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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 int flags = NO_ELLIPSIS; 377 int flags = NO_ELLIPSIS;
419 378
420 // If the whole string fits in the destination then just draw it directly. 379 // If the whole string fits in the destination then just draw it directly.
421 if (GetStringWidth(text, font) <= display_rect.width()) { 380 if (GetStringWidth(text, font) <= display_rect.width()) {
422 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), 381 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(),
423 display_rect.width(), display_rect.height(), flags); 382 display_rect.width(), display_rect.height(), flags);
424 return; 383 return;
425 } 384 }
426 385
427 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); 386 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText());
387 const bool is_rtl = base::i18n::RIGHT_TO_LEFT ==
388 base::i18n::GetFirstStrongCharacterDirection(text);
428 string16 clipped_text = text; 389 string16 clipped_text = text;
429 const bool is_rtl = AdjustStringDirection(flags, &clipped_text);
430 390
431 switch (truncate_mode) { 391 switch (truncate_mode) {
432 case TruncateFadeTail: 392 case TruncateFadeTail:
433 render_text->set_fade_tail(true); 393 render_text->set_fade_tail(true);
434 if (is_rtl) 394 if (is_rtl)
435 flags |= TEXT_ALIGN_RIGHT; 395 flags |= TEXT_ALIGN_RIGHT;
436 break; 396 break;
437 case TruncateFadeHead: 397 case TruncateFadeHead:
438 render_text->set_fade_head(true); 398 render_text->set_fade_head(true);
439 if (!is_rtl) 399 if (!is_rtl)
(...skipping 29 matching lines...) Expand all
469 rect.set_height(line_height); 429 rect.set_height(line_height);
470 render_text->SetDisplayRect(rect); 430 render_text->SetDisplayRect(rect);
471 431
472 canvas_->save(SkCanvas::kClip_SaveFlag); 432 canvas_->save(SkCanvas::kClip_SaveFlag);
473 ClipRect(display_rect); 433 ClipRect(display_rect);
474 render_text->Draw(this); 434 render_text->Draw(this);
475 canvas_->restore(); 435 canvas_->restore();
476 } 436 }
477 437
478 } // namespace gfx 438 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_win.cc » ('j') | ui/views/controls/label.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698