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

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

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More fixes per feedback Created 7 years, 2 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/gfx/font_list.h" 10 #include "ui/gfx/font_list.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 if (text.find('\n') != base::string16::npos) 160 if (text.find('\n') != base::string16::npos)
161 flags |= Canvas::MULTI_LINE; 161 flags |= Canvas::MULTI_LINE;
162 #endif 162 #endif
163 163
164 return flags; 164 return flags;
165 } 165 }
166 166
167 } // namespace 167 } // namespace
168 168
169 // static 169 // static
170 void Canvas::SizeStringInt(const base::string16& text, 170 void Canvas::SizeStringToFit(const base::string16& text,
171 const FontList& font_list, 171 const FontList& font_list,
172 int* width, int* height, 172 float* width,
173 int line_height, 173 float* height,
174 int flags) { 174 int line_height,
175 int flags) {
175 DCHECK_GE(*width, 0); 176 DCHECK_GE(*width, 0);
176 DCHECK_GE(*height, 0); 177 DCHECK_GE(*height, 0);
177 178
178 flags = AdjustPlatformSpecificFlags(text, flags); 179 flags = AdjustPlatformSpecificFlags(text, flags);
179 180
180 base::string16 adjusted_text = text; 181 base::string16 adjusted_text = text;
181 #if defined(OS_WIN) 182 #if defined(OS_WIN)
182 AdjustStringDirection(flags, &adjusted_text); 183 AdjustStringDirection(flags, &adjusted_text);
183 #endif 184 #endif
184 185
185 if ((flags & MULTI_LINE) && *width != 0) { 186 if ((flags & MULTI_LINE) && *width != 0) {
186 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; 187 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS;
187 if (flags & CHARACTER_BREAK) 188 if (flags & CHARACTER_BREAK)
188 wrap_behavior = gfx::WRAP_LONG_WORDS; 189 wrap_behavior = gfx::WRAP_LONG_WORDS;
189 else if (!(flags & NO_ELLIPSIS)) 190 else if (!(flags & NO_ELLIPSIS))
190 wrap_behavior = gfx::ELIDE_LONG_WORDS; 191 wrap_behavior = gfx::ELIDE_LONG_WORDS;
191 192
192 Rect rect(*width, INT_MAX); 193 Rect rect(*width, INT_MAX);
193 std::vector<base::string16> strings; 194 std::vector<base::string16> strings;
194 gfx::ElideRectangleText(adjusted_text, font_list, 195 gfx::ElideRectangleText(adjusted_text, font_list,
195 rect.width(), rect.height(), 196 rect.width(), rect.height(),
196 wrap_behavior, &strings); 197 wrap_behavior, &strings);
197 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 198 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
198 UpdateRenderText(rect, base::string16(), font_list, flags, 0, 199 UpdateRenderText(rect, base::string16(), font_list, flags, 0,
199 render_text.get()); 200 render_text.get());
200 201
201 int h = 0; 202 float h = 0;
202 int w = 0; 203 float w = 0;
203 for (size_t i = 0; i < strings.size(); ++i) { 204 for (size_t i = 0; i < strings.size(); ++i) {
204 StripAcceleratorChars(flags, &strings[i]); 205 StripAcceleratorChars(flags, &strings[i]);
205 render_text->SetText(strings[i]); 206 render_text->SetText(strings[i]);
206 const Size& string_size = render_text->GetStringSize(); 207 const SizeF& string_size = render_text->GetStringSize();
207 w = std::max(w, string_size.width()); 208 w = std::max(w, string_size.width());
208 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); 209 h += (i > 0 && line_height > 0) ? line_height : string_size.height();
209 } 210 }
210 *width = w; 211 *width = w;
211 *height = h; 212 *height = h;
212 } else { 213 } else {
213 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| 214 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()|
214 // will inexplicably fail with result E_INVALIDARG. Guard against this. 215 // will inexplicably fail with result E_INVALIDARG. Guard against this.
215 const size_t kMaxRenderTextLength = 5000; 216 const size_t kMaxRenderTextLength = 5000;
216 if (adjusted_text.length() >= kMaxRenderTextLength) { 217 if (adjusted_text.length() >= kMaxRenderTextLength) {
217 *width = font_list.GetExpectedTextWidth(adjusted_text.length()); 218 *width = font_list.GetExpectedTextWidth(adjusted_text.length());
218 *height = font_list.GetHeight(); 219 *height = font_list.GetHeight();
219 } else { 220 } else {
220 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 221 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
221 Rect rect(*width, *height); 222 Rect rect(*width, *height);
222 StripAcceleratorChars(flags, &adjusted_text); 223 StripAcceleratorChars(flags, &adjusted_text);
223 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, 224 UpdateRenderText(rect, adjusted_text, font_list, flags, 0,
224 render_text.get()); 225 render_text.get());
225 const Size& string_size = render_text->GetStringSize(); 226 const SizeF& string_size = render_text->GetStringSizeF();
226 *width = string_size.width(); 227 *width = string_size.width();
227 *height = string_size.height(); 228 *height = string_size.height();
228 } 229 }
229 } 230 }
230 } 231 }
231 232
232 void Canvas::DrawStringRectWithShadows(const base::string16& text, 233 void Canvas::DrawStringRectWithShadows(const base::string16& text,
233 const FontList& font_list, 234 const FontList& font_list,
234 SkColor color, 235 SkColor color,
235 const Rect& text_bounds, 236 const Rect& text_bounds,
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 size_t desired_characters_to_truncate_from_head, 464 size_t desired_characters_to_truncate_from_head,
464 const Font& font, 465 const Font& font,
465 SkColor color, 466 SkColor color,
466 const Rect& display_rect) { 467 const Rect& display_rect) {
467 DrawFadeTruncatingStringRect(text, truncate_mode, 468 DrawFadeTruncatingStringRect(text, truncate_mode,
468 desired_characters_to_truncate_from_head, 469 desired_characters_to_truncate_from_head,
469 FontList(font), color, display_rect); 470 FontList(font), color, display_rect);
470 } 471 }
471 472
472 } // namespace gfx 473 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698