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

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

Issue 10807082: Add RenderText DirectionalityMode enum and support; etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restore Label::SetURL; derive the direction from display text. Created 8 years, 4 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
« no previous file with comments | « no previous file | ui/gfx/render_text.h » ('j') | ui/gfx/render_text.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 22 // If necessary, force RTL/LTR base directionality.
23 // |flags| and |text| content. 23 // Returns true if the text will be rendered with an RTL base direction.
24 // Returns true if the text will be rendered right-to-left. 24 bool AdjustStringDirection(int flags,
25 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly. 25 const string16& text,
26 bool AdjustStringDirection(int flags, string16* text) { 26 gfx::RenderText* render_text) {
27 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now. 27 if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY)
28 28 render_text->SetDirectionalityMode(gfx::FORCE_LTR);
29 // If the string is empty or LTR was forced, simply return false since the 29 else if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY)
30 // default RenderText directionality is already LTR. 30 render_text->SetDirectionalityMode(gfx::FORCE_RTL);
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 31
40 // If a direction wasn't forced but the UI language is RTL and there were 32 // If a direction wasn't forced but the UI language is RTL and there were
41 // strong RTL characters, ensure RTL is applied. 33 // strong RTL characters, ensure RTL is applied.
42 if (base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(*text)) { 34 if (base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(text))
43 base::i18n::WrapStringWithRTLFormatting(text); 35 render_text->SetDirectionalityMode(gfx::FORCE_RTL);
44 return true;
45 }
46 36
47 // In the default case, the string should be rendered as LTR. RenderText's 37 return render_text->GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
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 } 38 }
54 39
55 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If 40 // 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 41 // 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 42 // 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| 43 // 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 44 // will modify the bitmap as it goes, and cleared pixels shouldn't count as
60 // changed. 45 // changed.
61 bool PixelShouldGetHalo(const SkBitmap& bitmap, 46 bool PixelShouldGetHalo(const SkBitmap& bitmap,
62 int x, int y, 47 int x, int y,
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 void Canvas::SizeStringInt(const string16& text, 186 void Canvas::SizeStringInt(const string16& text,
202 const gfx::Font& font, 187 const gfx::Font& font,
203 int* width, int* height, 188 int* width, int* height,
204 int flags) { 189 int flags) {
205 DCHECK_GE(*width, 0); 190 DCHECK_GE(*width, 0);
206 DCHECK_GE(*height, 0); 191 DCHECK_GE(*height, 0);
207 192
208 flags = AdjustPlatformSpecificFlags(text, flags); 193 flags = AdjustPlatformSpecificFlags(text, flags);
209 194
210 string16 adjusted_text = text; 195 string16 adjusted_text = text;
196 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
197
211 #if defined(OS_WIN) 198 #if defined(OS_WIN)
212 AdjustStringDirection(flags, &adjusted_text); 199 AdjustStringDirection(flags, adjusted_text, render_text.get());
213 #endif 200 #endif
214 201
215 if ((flags & MULTI_LINE) && *width != 0) { 202 if ((flags & MULTI_LINE) && *width != 0) {
216 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; 203 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS;
217 if (flags & CHARACTER_BREAK) 204 if (flags & CHARACTER_BREAK)
218 wrap_behavior = ui::WRAP_LONG_WORDS; 205 wrap_behavior = ui::WRAP_LONG_WORDS;
219 else if (!(flags & NO_ELLIPSIS)) 206 else if (!(flags & NO_ELLIPSIS))
220 wrap_behavior = ui::ELIDE_LONG_WORDS; 207 wrap_behavior = ui::ELIDE_LONG_WORDS;
221 208
222 gfx::Rect rect(*width, INT_MAX); 209 gfx::Rect rect(*width, INT_MAX);
223 std::vector<string16> strings; 210 std::vector<string16> strings;
224 ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(), 211 ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(),
225 wrap_behavior, &strings); 212 wrap_behavior, &strings);
226 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
227 UpdateRenderText(rect, string16(), font, flags, 0, render_text.get()); 213 UpdateRenderText(rect, string16(), font, flags, 0, render_text.get());
228 214
229 int h = 0; 215 int h = 0;
230 int w = 0; 216 int w = 0;
231 for (size_t i = 0; i < strings.size(); ++i) { 217 for (size_t i = 0; i < strings.size(); ++i) {
232 StripAcceleratorChars(flags, &strings[i]); 218 StripAcceleratorChars(flags, &strings[i]);
233 render_text->SetText(strings[i]); 219 render_text->SetText(strings[i]);
234 const Size string_size = render_text->GetStringSize(); 220 const Size string_size = render_text->GetStringSize();
235 w = std::max(w, string_size.width()); 221 w = std::max(w, string_size.width());
236 h += string_size.height(); 222 h += string_size.height();
237 } 223 }
238 *width = w; 224 *width = w;
239 *height = h; 225 *height = h;
240 } else { 226 } else {
241 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| 227 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()|
242 // will inexplicably fail with result E_INVALIDARG. Guard against this. 228 // will inexplicably fail with result E_INVALIDARG. Guard against this.
243 const size_t kMaxRenderTextLength = 5000; 229 const size_t kMaxRenderTextLength = 5000;
244 if (adjusted_text.length() >= kMaxRenderTextLength) { 230 if (adjusted_text.length() >= kMaxRenderTextLength) {
245 *width = adjusted_text.length() * font.GetAverageCharacterWidth(); 231 *width = adjusted_text.length() * font.GetAverageCharacterWidth();
246 *height = font.GetHeight(); 232 *height = font.GetHeight();
247 } else { 233 } else {
248 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
249 gfx::Rect rect(*width, *height); 234 gfx::Rect rect(*width, *height);
250 StripAcceleratorChars(flags, &adjusted_text); 235 StripAcceleratorChars(flags, &adjusted_text);
251 UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get()); 236 UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get());
252 const Size string_size = render_text->GetStringSize(); 237 const Size string_size = render_text->GetStringSize();
253 *width = string_size.width(); 238 *width = string_size.width();
254 *height = string_size.height(); 239 *height = string_size.height();
255 } 240 }
256 } 241 }
257 } 242 }
258 243
(...skipping 19 matching lines...) Expand all
278 263
279 gfx::Rect clip_rect(text_bounds); 264 gfx::Rect clip_rect(text_bounds);
280 clip_rect.Inset(ShadowValue::GetMargin(shadows)); 265 clip_rect.Inset(ShadowValue::GetMargin(shadows));
281 266
282 canvas_->save(SkCanvas::kClip_SaveFlag); 267 canvas_->save(SkCanvas::kClip_SaveFlag);
283 ClipRect(clip_rect); 268 ClipRect(clip_rect);
284 269
285 gfx::Rect rect(text_bounds); 270 gfx::Rect rect(text_bounds);
286 string16 adjusted_text = text; 271 string16 adjusted_text = text;
287 272
288 #if defined(OS_WIN)
289 AdjustStringDirection(flags, &adjusted_text);
290 #endif
291
292 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 273 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
293 render_text->SetTextShadows(shadows); 274 render_text->SetTextShadows(shadows);
294 275
276 #if defined(OS_WIN)
277 AdjustStringDirection(flags, adjusted_text, render_text.get());
278 #endif
279
295 if (flags & MULTI_LINE) { 280 if (flags & MULTI_LINE) {
296 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; 281 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS;
297 if (flags & CHARACTER_BREAK) 282 if (flags & CHARACTER_BREAK)
298 wrap_behavior = ui::WRAP_LONG_WORDS; 283 wrap_behavior = ui::WRAP_LONG_WORDS;
299 else if (!(flags & NO_ELLIPSIS)) 284 else if (!(flags & NO_ELLIPSIS))
300 wrap_behavior = ui::ELIDE_LONG_WORDS; 285 wrap_behavior = ui::ELIDE_LONG_WORDS;
301 286
302 std::vector<string16> strings; 287 std::vector<string16> strings;
303 ui::ElideRectangleText(adjusted_text, 288 ui::ElideRectangleText(adjusted_text,
304 font, 289 font,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 406
422 // If the whole string fits in the destination then just draw it directly. 407 // If the whole string fits in the destination then just draw it directly.
423 if (GetStringWidth(text, font) <= display_rect.width()) { 408 if (GetStringWidth(text, font) <= display_rect.width()) {
424 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), 409 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(),
425 display_rect.width(), display_rect.height(), flags); 410 display_rect.width(), display_rect.height(), flags);
426 return; 411 return;
427 } 412 }
428 413
429 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 414 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
430 string16 clipped_text = text; 415 string16 clipped_text = text;
431 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); 416 const bool is_rtl =
417 AdjustStringDirection(flags, clipped_text, render_text.get());
432 418
433 switch (truncate_mode) { 419 switch (truncate_mode) {
434 case TruncateFadeTail: 420 case TruncateFadeTail:
435 render_text->set_fade_tail(true); 421 render_text->set_fade_tail(true);
436 if (is_rtl) 422 if (is_rtl)
437 flags |= TEXT_ALIGN_RIGHT; 423 flags |= TEXT_ALIGN_RIGHT;
438 break; 424 break;
439 case TruncateFadeHead: 425 case TruncateFadeHead:
440 render_text->set_fade_head(true); 426 render_text->set_fade_head(true);
441 if (!is_rtl) 427 if (!is_rtl)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 rect.set_height(line_height); 461 rect.set_height(line_height);
476 render_text->SetDisplayRect(rect); 462 render_text->SetDisplayRect(rect);
477 463
478 canvas_->save(SkCanvas::kClip_SaveFlag); 464 canvas_->save(SkCanvas::kClip_SaveFlag);
479 ClipRect(display_rect); 465 ClipRect(display_rect);
480 render_text->Draw(this); 466 render_text->Draw(this);
481 canvas_->restore(); 467 canvas_->restore();
482 } 468 }
483 469
484 } // namespace gfx 470 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/render_text.h » ('j') | ui/gfx/render_text.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698