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

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: Sync 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
« no previous file with comments | « ui/gfx/canvas_android.cc ('k') | ui/gfx/canvas_unittest_mac.mm » ('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/gfx/font_list.h" 10 #include "ui/gfx/font_list.h"
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 render_text->SetColor(color); 148 render_text->SetColor(color);
149 const int font_style = font_list.GetFontStyle(); 149 const int font_style = font_list.GetFontStyle();
150 render_text->SetStyle(BOLD, (font_style & Font::BOLD) != 0); 150 render_text->SetStyle(BOLD, (font_style & Font::BOLD) != 0);
151 render_text->SetStyle(ITALIC, (font_style & Font::ITALIC) != 0); 151 render_text->SetStyle(ITALIC, (font_style & Font::ITALIC) != 0);
152 render_text->SetStyle(UNDERLINE, (font_style & Font::UNDERLINE) != 0); 152 render_text->SetStyle(UNDERLINE, (font_style & Font::UNDERLINE) != 0);
153 } 153 }
154 154
155 } // namespace 155 } // namespace
156 156
157 // static 157 // static
158 void Canvas::SizeStringInt(const base::string16& text, 158 void Canvas::SizeStringFloat(const base::string16& text,
159 const FontList& font_list, 159 const FontList& font_list,
160 int* width, int* height, 160 float* width, float* height,
161 int line_height, 161 int line_height,
162 int flags) { 162 int flags) {
163 DCHECK_GE(*width, 0); 163 DCHECK_GE(*width, 0);
164 DCHECK_GE(*height, 0); 164 DCHECK_GE(*height, 0);
165 165
166 base::string16 adjusted_text = text; 166 base::string16 adjusted_text = text;
167 #if defined(OS_WIN) 167 #if defined(OS_WIN)
168 AdjustStringDirection(flags, &adjusted_text); 168 AdjustStringDirection(flags, &adjusted_text);
169 #endif 169 #endif
170 170
171 if ((flags & MULTI_LINE) && *width != 0) { 171 if ((flags & MULTI_LINE) && *width != 0) {
172 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; 172 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS;
173 if (flags & CHARACTER_BREAK) 173 if (flags & CHARACTER_BREAK)
174 wrap_behavior = gfx::WRAP_LONG_WORDS; 174 wrap_behavior = gfx::WRAP_LONG_WORDS;
175 else if (!(flags & NO_ELLIPSIS)) 175 else if (!(flags & NO_ELLIPSIS))
176 wrap_behavior = gfx::ELIDE_LONG_WORDS; 176 wrap_behavior = gfx::ELIDE_LONG_WORDS;
177 177
178 Rect rect(*width, INT_MAX); 178 Rect rect(*width, INT_MAX);
179 std::vector<base::string16> strings; 179 std::vector<base::string16> strings;
180 gfx::ElideRectangleText(adjusted_text, font_list, 180 gfx::ElideRectangleText(adjusted_text, font_list,
181 rect.width(), rect.height(), 181 rect.width(), rect.height(),
182 wrap_behavior, &strings); 182 wrap_behavior, &strings);
183 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 183 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
184 UpdateRenderText(rect, base::string16(), font_list, flags, 0, 184 UpdateRenderText(rect, base::string16(), font_list, flags, 0,
185 render_text.get()); 185 render_text.get());
186 186
187 int h = 0; 187 float h = 0;
188 int w = 0; 188 float w = 0;
189 for (size_t i = 0; i < strings.size(); ++i) { 189 for (size_t i = 0; i < strings.size(); ++i) {
190 StripAcceleratorChars(flags, &strings[i]); 190 StripAcceleratorChars(flags, &strings[i]);
191 render_text->SetText(strings[i]); 191 render_text->SetText(strings[i]);
192 const Size& string_size = render_text->GetStringSize(); 192 const SizeF& string_size = render_text->GetStringSizeF();
193 w = std::max(w, string_size.width()); 193 w = std::max(w, string_size.width());
194 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); 194 h += (i > 0 && line_height > 0) ? line_height : string_size.height();
195 } 195 }
196 *width = w; 196 *width = w;
197 *height = h; 197 *height = h;
198 } else { 198 } else {
199 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| 199 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()|
200 // will inexplicably fail with result E_INVALIDARG. Guard against this. 200 // will inexplicably fail with result E_INVALIDARG. Guard against this.
201 const size_t kMaxRenderTextLength = 5000; 201 const size_t kMaxRenderTextLength = 5000;
202 if (adjusted_text.length() >= kMaxRenderTextLength) { 202 if (adjusted_text.length() >= kMaxRenderTextLength) {
203 *width = font_list.GetExpectedTextWidth(adjusted_text.length()); 203 *width = font_list.GetExpectedTextWidth(adjusted_text.length());
204 *height = font_list.GetHeight(); 204 *height = font_list.GetHeight();
205 } else { 205 } else {
206 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 206 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
207 Rect rect(*width, *height); 207 Rect rect(*width, *height);
208 StripAcceleratorChars(flags, &adjusted_text); 208 StripAcceleratorChars(flags, &adjusted_text);
209 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, 209 UpdateRenderText(rect, adjusted_text, font_list, flags, 0,
210 render_text.get()); 210 render_text.get());
211 const Size& string_size = render_text->GetStringSize(); 211 const SizeF& string_size = render_text->GetStringSizeF();
212 *width = string_size.width(); 212 *width = string_size.width();
213 *height = string_size.height(); 213 *height = string_size.height();
214 } 214 }
215 } 215 }
216 } 216 }
217 217
218 void Canvas::DrawStringRectWithShadows(const base::string16& text, 218 void Canvas::DrawStringRectWithShadows(const base::string16& text,
219 const FontList& font_list, 219 const FontList& font_list,
220 SkColor color, 220 SkColor color,
221 const Rect& text_bounds, 221 const Rect& text_bounds,
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 size_t desired_characters_to_truncate_from_head, 447 size_t desired_characters_to_truncate_from_head,
448 const Font& font, 448 const Font& font,
449 SkColor color, 449 SkColor color,
450 const Rect& display_rect) { 450 const Rect& display_rect) {
451 DrawFadeTruncatingStringRect(text, truncate_mode, 451 DrawFadeTruncatingStringRect(text, truncate_mode,
452 desired_characters_to_truncate_from_head, 452 desired_characters_to_truncate_from_head,
453 FontList(font), color, display_rect); 453 FontList(font), color, display_rect);
454 } 454 }
455 455
456 } // namespace gfx 456 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas_android.cc ('k') | ui/gfx/canvas_unittest_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698