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

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

Issue 1164063005: Replace gfx::ClampToInt with base::saturated_cast. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clamp: . Created 5 years, 6 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
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 "base/numerics/safe_conversions.h"
10 #include "ui/gfx/font_list.h" 11 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/geometry/insets.h" 12 #include "ui/gfx/geometry/insets.h"
12 #include "ui/gfx/geometry/rect.h" 13 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/safe_integer_conversions.h"
14 #include "ui/gfx/range/range.h" 14 #include "ui/gfx/range/range.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_elider.h" 17 #include "ui/gfx/text_elider.h"
18 #include "ui/gfx/text_utils.h" 18 #include "ui/gfx/text_utils.h"
19 19
20 namespace gfx { 20 namespace gfx {
21 21
22 namespace { 22 namespace {
23 23
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 if ((flags & MULTI_LINE) && *width != 0) { 134 if ((flags & MULTI_LINE) && *width != 0) {
135 WordWrapBehavior wrap_behavior = TRUNCATE_LONG_WORDS; 135 WordWrapBehavior wrap_behavior = TRUNCATE_LONG_WORDS;
136 if (flags & CHARACTER_BREAK) 136 if (flags & CHARACTER_BREAK)
137 wrap_behavior = WRAP_LONG_WORDS; 137 wrap_behavior = WRAP_LONG_WORDS;
138 else if (!(flags & NO_ELLIPSIS)) 138 else if (!(flags & NO_ELLIPSIS))
139 wrap_behavior = ELIDE_LONG_WORDS; 139 wrap_behavior = ELIDE_LONG_WORDS;
140 140
141 std::vector<base::string16> strings; 141 std::vector<base::string16> strings;
142 ElideRectangleText(text, font_list, *width, INT_MAX, wrap_behavior, 142 ElideRectangleText(text, font_list, *width, INT_MAX, wrap_behavior,
143 &strings); 143 &strings);
144 Rect rect(ClampToInt(*width), INT_MAX); 144 Rect rect(base::saturated_cast<int>(*width), INT_MAX);
145 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 145 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
146 UpdateRenderText(rect, base::string16(), font_list, flags, 0, 146 UpdateRenderText(rect, base::string16(), font_list, flags, 0,
147 render_text.get()); 147 render_text.get());
148 148
149 float h = 0; 149 float h = 0;
150 float w = 0; 150 float w = 0;
151 for (size_t i = 0; i < strings.size(); ++i) { 151 for (size_t i = 0; i < strings.size(); ++i) {
152 StripAcceleratorChars(flags, &strings[i]); 152 StripAcceleratorChars(flags, &strings[i]);
153 render_text->SetText(strings[i]); 153 render_text->SetText(strings[i]);
154 const SizeF& string_size = render_text->GetStringSizeF(); 154 const SizeF& string_size = render_text->GetStringSizeF();
155 w = std::max(w, string_size.width()); 155 w = std::max(w, string_size.width());
156 h += (i > 0 && line_height > 0) ? 156 h += (i > 0 && line_height > 0) ?
157 std::max(static_cast<float>(line_height), string_size.height()) 157 std::max(static_cast<float>(line_height), string_size.height())
158 : string_size.height(); 158 : string_size.height();
159 } 159 }
160 *width = w; 160 *width = w;
161 *height = h; 161 *height = h;
162 } else { 162 } else {
163 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 163 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
164 Rect rect(ClampToInt(*width), ClampToInt(*height)); 164 Rect rect(base::saturated_cast<int>(*width),
165 base::saturated_cast<int>(*height));
165 base::string16 adjusted_text = text; 166 base::string16 adjusted_text = text;
166 StripAcceleratorChars(flags, &adjusted_text); 167 StripAcceleratorChars(flags, &adjusted_text);
167 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, 168 UpdateRenderText(rect, adjusted_text, font_list, flags, 0,
168 render_text.get()); 169 render_text.get());
169 const SizeF& string_size = render_text->GetStringSizeF(); 170 const SizeF& string_size = render_text->GetStringSizeF();
170 *width = string_size.width(); 171 *width = string_size.width();
171 *height = string_size.height(); 172 *height = string_size.height();
172 } 173 }
173 } 174 }
174 175
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 UpdateRenderText(rect, text, font_list, flags, color, render_text.get()); 335 UpdateRenderText(rect, text, font_list, flags, color, render_text.get());
335 render_text->SetElideBehavior(FADE_TAIL); 336 render_text->SetElideBehavior(FADE_TAIL);
336 337
337 canvas_->save(); 338 canvas_->save();
338 ClipRect(display_rect); 339 ClipRect(display_rect);
339 render_text->Draw(this); 340 render_text->Draw(this);
340 canvas_->restore(); 341 canvas_->restore();
341 } 342 }
342 343
343 } // namespace gfx 344 } // namespace gfx
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698