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

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

Issue 24012002: Move Range code to gfx. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: d Created 7 years, 3 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 | « ui/gfx/break_list_unittest.cc ('k') | ui/gfx/range/OWNERS » ('j') | no next file with comments »
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"
11 #include "ui/base/text/text_elider.h" 10 #include "ui/base/text/text_elider.h"
12 #include "ui/gfx/font_list.h" 11 #include "ui/gfx/font_list.h"
13 #include "ui/gfx/insets.h" 12 #include "ui/gfx/insets.h"
13 #include "ui/gfx/range/range.h"
14 #include "ui/gfx/rect.h" 14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/render_text.h" 15 #include "ui/gfx/render_text.h"
16 #include "ui/gfx/shadow_value.h" 16 #include "ui/gfx/shadow_value.h"
17 #include "ui/gfx/text_utils.h" 17 #include "ui/gfx/text_utils.h"
18 18
19 namespace gfx { 19 namespace gfx {
20 20
21 namespace { 21 namespace {
22 22
23 // If necessary, wraps |text| with RTL/LTR directionality characters based on 23 // If necessary, wraps |text| with RTL/LTR directionality characters based on
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 *bitmap.getAddr32(x, y - 1) != 0) 75 *bitmap.getAddr32(x, y - 1) != 0)
76 return true; // Touched pixel above. 76 return true; // Touched pixel above.
77 if (y < bitmap.height() - 1 && 77 if (y < bitmap.height() - 1 &&
78 *bitmap.getAddr32(x, y + 1) != halo_color && 78 *bitmap.getAddr32(x, y + 1) != halo_color &&
79 *bitmap.getAddr32(x, y + 1) != 0) 79 *bitmap.getAddr32(x, y + 1) != 0)
80 return true; // Touched pixel below. 80 return true; // Touched pixel below.
81 return false; 81 return false;
82 } 82 }
83 83
84 // Strips accelerator character prefixes in |text| if needed, based on |flags|. 84 // Strips accelerator character prefixes in |text| if needed, based on |flags|.
85 // Returns a range in |text| to underline or ui::Range::InvalidRange() if 85 // Returns a range in |text| to underline or gfx::Range::InvalidRange() if
86 // underlining is not needed. 86 // underlining is not needed.
87 ui::Range StripAcceleratorChars(int flags, base::string16* text) { 87 gfx::Range StripAcceleratorChars(int flags, base::string16* text) {
88 if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) { 88 if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) {
89 int char_pos = -1; 89 int char_pos = -1;
90 int char_span = 0; 90 int char_span = 0;
91 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); 91 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span);
92 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) 92 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1)
93 return ui::Range(char_pos, char_pos + char_span); 93 return gfx::Range(char_pos, char_pos + char_span);
94 } 94 }
95 return ui::Range::InvalidRange(); 95 return gfx::Range::InvalidRange();
96 } 96 }
97 97
98 // Elides |text| and adjusts |range| appropriately. If eliding causes |range| 98 // Elides |text| and adjusts |range| appropriately. If eliding causes |range|
99 // to no longer point to the same character in |text|, |range| is made invalid. 99 // to no longer point to the same character in |text|, |range| is made invalid.
100 void ElideTextAndAdjustRange(const FontList& font_list, 100 void ElideTextAndAdjustRange(const FontList& font_list,
101 int width, 101 int width,
102 base::string16* text, 102 base::string16* text,
103 ui::Range* range) { 103 gfx::Range* range) {
104 const base::char16 start_char = 104 const base::char16 start_char =
105 (range->IsValid() ? text->at(range->start()) : 0); 105 (range->IsValid() ? text->at(range->start()) : 0);
106 *text = ui::ElideText(*text, font_list, width, ui::ELIDE_AT_END); 106 *text = ui::ElideText(*text, font_list, width, ui::ELIDE_AT_END);
107 if (!range->IsValid()) 107 if (!range->IsValid())
108 return; 108 return;
109 if (range->start() >= text->length() || 109 if (range->start() >= text->length() ||
110 text->at(range->start()) != start_char) { 110 text->at(range->start()) != start_char) {
111 *range = ui::Range::InvalidRange(); 111 *range = gfx::Range::InvalidRange();
112 } 112 }
113 } 113 }
114 114
115 // Updates |render_text| from the specified parameters. 115 // Updates |render_text| from the specified parameters.
116 void UpdateRenderText(const Rect& rect, 116 void UpdateRenderText(const Rect& rect,
117 const base::string16& text, 117 const base::string16& text,
118 const FontList& font_list, 118 const FontList& font_list,
119 int flags, 119 int flags,
120 SkColor color, 120 SkColor color,
121 RenderText* render_text) { 121 RenderText* render_text) {
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 wrap_behavior = ui::ELIDE_LONG_WORDS; 265 wrap_behavior = ui::ELIDE_LONG_WORDS;
266 266
267 std::vector<base::string16> strings; 267 std::vector<base::string16> strings;
268 ui::ElideRectangleText(adjusted_text, 268 ui::ElideRectangleText(adjusted_text,
269 font_list, 269 font_list,
270 text_bounds.width(), text_bounds.height(), 270 text_bounds.width(), text_bounds.height(),
271 wrap_behavior, 271 wrap_behavior,
272 &strings); 272 &strings);
273 273
274 for (size_t i = 0; i < strings.size(); i++) { 274 for (size_t i = 0; i < strings.size(); i++) {
275 ui::Range range = StripAcceleratorChars(flags, &strings[i]); 275 gfx::Range range = StripAcceleratorChars(flags, &strings[i]);
276 UpdateRenderText(rect, strings[i], font_list, flags, color, 276 UpdateRenderText(rect, strings[i], font_list, flags, color,
277 render_text.get()); 277 render_text.get());
278 int line_padding = 0; 278 int line_padding = 0;
279 if (line_height > 0) 279 if (line_height > 0)
280 line_padding = line_height - render_text->GetStringSize().height(); 280 line_padding = line_height - render_text->GetStringSize().height();
281 else 281 else
282 line_height = render_text->GetStringSize().height(); 282 line_height = render_text->GetStringSize().height();
283 283
284 // TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357 284 // TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357
285 #if !defined(OS_WIN) 285 #if !defined(OS_WIN)
286 if (i == 0) { 286 if (i == 0) {
287 // TODO(msw|asvitkine): Support multi-line text with varied heights. 287 // TODO(msw|asvitkine): Support multi-line text with varied heights.
288 const int text_height = strings.size() * line_height - line_padding; 288 const int text_height = strings.size() * line_height - line_padding;
289 rect += Vector2d(0, (text_bounds.height() - text_height) / 2); 289 rect += Vector2d(0, (text_bounds.height() - text_height) / 2);
290 } 290 }
291 #endif 291 #endif
292 292
293 rect.set_height(line_height - line_padding); 293 rect.set_height(line_height - line_padding);
294 294
295 if (range.IsValid()) 295 if (range.IsValid())
296 render_text->ApplyStyle(UNDERLINE, true, range); 296 render_text->ApplyStyle(UNDERLINE, true, range);
297 render_text->SetDisplayRect(rect); 297 render_text->SetDisplayRect(rect);
298 render_text->Draw(this); 298 render_text->Draw(this);
299 rect += Vector2d(0, line_height); 299 rect += Vector2d(0, line_height);
300 } 300 }
301 } else { 301 } else {
302 ui::Range range = StripAcceleratorChars(flags, &adjusted_text); 302 gfx::Range range = StripAcceleratorChars(flags, &adjusted_text);
303 bool elide_text = ((flags & NO_ELLIPSIS) == 0); 303 bool elide_text = ((flags & NO_ELLIPSIS) == 0);
304 304
305 #if defined(OS_LINUX) 305 #if defined(OS_LINUX)
306 // On Linux, eliding really means fading the end of the string. But only 306 // On Linux, eliding really means fading the end of the string. But only
307 // for LTR text. RTL text is still elided (on the left) with "...". 307 // for LTR text. RTL text is still elided (on the left) with "...".
308 if (elide_text) { 308 if (elide_text) {
309 render_text->SetText(adjusted_text); 309 render_text->SetText(adjusted_text);
310 if (render_text->GetTextDirection() == base::i18n::LEFT_TO_RIGHT) { 310 if (render_text->GetTextDirection() == base::i18n::LEFT_TO_RIGHT) {
311 render_text->set_fade_tail(true); 311 render_text->set_fade_tail(true);
312 elide_text = false; 312 elide_text = false;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 size_t desired_characters_to_truncate_from_head, 463 size_t desired_characters_to_truncate_from_head,
464 const Font& font, 464 const Font& font,
465 SkColor color, 465 SkColor color,
466 const Rect& display_rect) { 466 const Rect& display_rect) {
467 DrawFadeTruncatingStringRect(text, truncate_mode, 467 DrawFadeTruncatingStringRect(text, truncate_mode,
468 desired_characters_to_truncate_from_head, 468 desired_characters_to_truncate_from_head,
469 FontList(font), color, display_rect); 469 FontList(font), color, display_rect);
470 } 470 }
471 471
472 } // namespace gfx 472 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/break_list_unittest.cc ('k') | ui/gfx/range/OWNERS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698