OLD | NEW |
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 Loading... |
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::SizeStringFloat(const base::string16& text, |
171 const FontList& font_list, | 171 const FontList& font_list, |
172 int* width, int* height, | 172 float* width, float* height, |
173 int line_height, | 173 int line_height, |
174 int flags) { | 174 int flags) { |
175 DCHECK_GE(*width, 0); | 175 DCHECK_GE(*width, 0); |
176 DCHECK_GE(*height, 0); | 176 DCHECK_GE(*height, 0); |
177 | 177 |
178 flags = AdjustPlatformSpecificFlags(text, flags); | 178 flags = AdjustPlatformSpecificFlags(text, flags); |
179 | 179 |
180 base::string16 adjusted_text = text; | 180 base::string16 adjusted_text = text; |
181 #if defined(OS_WIN) | 181 #if defined(OS_WIN) |
182 AdjustStringDirection(flags, &adjusted_text); | 182 AdjustStringDirection(flags, &adjusted_text); |
183 #endif | 183 #endif |
184 | 184 |
185 if ((flags & MULTI_LINE) && *width != 0) { | 185 if ((flags & MULTI_LINE) && *width != 0) { |
186 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; | 186 gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; |
187 if (flags & CHARACTER_BREAK) | 187 if (flags & CHARACTER_BREAK) |
188 wrap_behavior = gfx::WRAP_LONG_WORDS; | 188 wrap_behavior = gfx::WRAP_LONG_WORDS; |
189 else if (!(flags & NO_ELLIPSIS)) | 189 else if (!(flags & NO_ELLIPSIS)) |
190 wrap_behavior = gfx::ELIDE_LONG_WORDS; | 190 wrap_behavior = gfx::ELIDE_LONG_WORDS; |
191 | 191 |
192 Rect rect(*width, INT_MAX); | 192 Rect rect(*width, INT_MAX); |
193 std::vector<base::string16> strings; | 193 std::vector<base::string16> strings; |
194 gfx::ElideRectangleText(adjusted_text, font_list, | 194 gfx::ElideRectangleText(adjusted_text, font_list, |
195 rect.width(), rect.height(), | 195 rect.width(), rect.height(), |
196 wrap_behavior, &strings); | 196 wrap_behavior, &strings); |
197 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 197 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
198 UpdateRenderText(rect, base::string16(), font_list, flags, 0, | 198 UpdateRenderText(rect, base::string16(), font_list, flags, 0, |
199 render_text.get()); | 199 render_text.get()); |
200 | 200 |
201 int h = 0; | 201 float h = 0; |
202 int w = 0; | 202 float w = 0; |
203 for (size_t i = 0; i < strings.size(); ++i) { | 203 for (size_t i = 0; i < strings.size(); ++i) { |
204 StripAcceleratorChars(flags, &strings[i]); | 204 StripAcceleratorChars(flags, &strings[i]); |
205 render_text->SetText(strings[i]); | 205 render_text->SetText(strings[i]); |
206 const Size& string_size = render_text->GetStringSize(); | 206 const SizeF& string_size = render_text->GetStringSizeF(); |
207 w = std::max(w, string_size.width()); | 207 w = std::max(w, string_size.width()); |
208 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); | 208 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); |
209 } | 209 } |
210 *width = w; | 210 *width = w; |
211 *height = h; | 211 *height = h; |
212 } else { | 212 } else { |
213 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| | 213 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| |
214 // will inexplicably fail with result E_INVALIDARG. Guard against this. | 214 // will inexplicably fail with result E_INVALIDARG. Guard against this. |
215 const size_t kMaxRenderTextLength = 5000; | 215 const size_t kMaxRenderTextLength = 5000; |
216 if (adjusted_text.length() >= kMaxRenderTextLength) { | 216 if (adjusted_text.length() >= kMaxRenderTextLength) { |
217 *width = font_list.GetExpectedTextWidth(adjusted_text.length()); | 217 *width = font_list.GetExpectedTextWidth(adjusted_text.length()); |
218 *height = font_list.GetHeight(); | 218 *height = font_list.GetHeight(); |
219 } else { | 219 } else { |
220 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 220 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
221 Rect rect(*width, *height); | 221 Rect rect(*width, *height); |
222 StripAcceleratorChars(flags, &adjusted_text); | 222 StripAcceleratorChars(flags, &adjusted_text); |
223 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, | 223 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, |
224 render_text.get()); | 224 render_text.get()); |
225 const Size& string_size = render_text->GetStringSize(); | 225 const SizeF& string_size = render_text->GetStringSizeF(); |
226 *width = string_size.width(); | 226 *width = string_size.width(); |
227 *height = string_size.height(); | 227 *height = string_size.height(); |
228 } | 228 } |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 void Canvas::DrawStringRectWithShadows(const base::string16& text, | 232 void Canvas::DrawStringRectWithShadows(const base::string16& text, |
233 const FontList& font_list, | 233 const FontList& font_list, |
234 SkColor color, | 234 SkColor color, |
235 const Rect& text_bounds, | 235 const Rect& text_bounds, |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
OLD | NEW |