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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); | 93 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); |
94 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) | 94 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) |
95 return Range(char_pos, char_pos + char_span); | 95 return Range(char_pos, char_pos + char_span); |
96 } | 96 } |
97 return Range::InvalidRange(); | 97 return Range::InvalidRange(); |
98 } | 98 } |
99 | 99 |
100 // Elides |text| and adjusts |range| appropriately. If eliding causes |range| | 100 // Elides |text| and adjusts |range| appropriately. If eliding causes |range| |
101 // to no longer point to the same character in |text|, |range| is made invalid. | 101 // to no longer point to the same character in |text|, |range| is made invalid. |
102 void ElideTextAndAdjustRange(const FontList& font_list, | 102 void ElideTextAndAdjustRange(const FontList& font_list, |
103 int width, | 103 float width, |
104 base::string16* text, | 104 base::string16* text, |
105 Range* range) { | 105 Range* range) { |
106 const base::char16 start_char = | 106 const base::char16 start_char = |
107 (range->IsValid() ? text->at(range->start()) : 0); | 107 (range->IsValid() ? text->at(range->start()) : 0); |
108 *text = ElideText(*text, font_list, width, ELIDE_TAIL); | 108 *text = ElideText(*text, font_list, width, ELIDE_TAIL); |
109 if (!range->IsValid()) | 109 if (!range->IsValid()) |
110 return; | 110 return; |
111 if (range->start() >= text->length() || | 111 if (range->start() >= text->length() || |
112 text->at(range->start()) != start_char) { | 112 text->at(range->start()) != start_char) { |
113 *range = Range::InvalidRange(); | 113 *range = Range::InvalidRange(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 AdjustStringDirection(flags, &adjusted_text); | 167 AdjustStringDirection(flags, &adjusted_text); |
168 #endif | 168 #endif |
169 | 169 |
170 if ((flags & MULTI_LINE) && *width != 0) { | 170 if ((flags & MULTI_LINE) && *width != 0) { |
171 WordWrapBehavior wrap_behavior = TRUNCATE_LONG_WORDS; | 171 WordWrapBehavior wrap_behavior = TRUNCATE_LONG_WORDS; |
172 if (flags & CHARACTER_BREAK) | 172 if (flags & CHARACTER_BREAK) |
173 wrap_behavior = WRAP_LONG_WORDS; | 173 wrap_behavior = WRAP_LONG_WORDS; |
174 else if (!(flags & NO_ELLIPSIS)) | 174 else if (!(flags & NO_ELLIPSIS)) |
175 wrap_behavior = ELIDE_LONG_WORDS; | 175 wrap_behavior = ELIDE_LONG_WORDS; |
176 | 176 |
177 Rect rect(*width, INT_MAX); | 177 Rect rect(static_cast<int>(*width), INT_MAX); |
msw
2014/10/17 22:13:23
Hmm, it seems silly to cast float->int->float for
Peter Kasting
2014/10/21 01:20:45
Done.
| |
178 std::vector<base::string16> strings; | 178 std::vector<base::string16> strings; |
179 ElideRectangleText(adjusted_text, font_list, rect.width(), rect.height(), | 179 ElideRectangleText(adjusted_text, font_list, |
180 static_cast<float>(rect.width()), rect.height(), | |
180 wrap_behavior, &strings); | 181 wrap_behavior, &strings); |
181 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 182 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
182 UpdateRenderText(rect, base::string16(), font_list, flags, 0, | 183 UpdateRenderText(rect, base::string16(), font_list, flags, 0, |
183 render_text.get()); | 184 render_text.get()); |
184 | 185 |
185 float h = 0; | 186 float h = 0; |
186 float w = 0; | 187 float w = 0; |
187 for (size_t i = 0; i < strings.size(); ++i) { | 188 for (size_t i = 0; i < strings.size(); ++i) { |
188 StripAcceleratorChars(flags, &strings[i]); | 189 StripAcceleratorChars(flags, &strings[i]); |
189 render_text->SetText(strings[i]); | 190 render_text->SetText(strings[i]); |
190 const SizeF& string_size = render_text->GetStringSizeF(); | 191 const SizeF& string_size = render_text->GetStringSizeF(); |
191 w = std::max(w, string_size.width()); | 192 w = std::max(w, string_size.width()); |
192 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); | 193 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); |
193 } | 194 } |
194 *width = w; | 195 *width = w; |
195 *height = h; | 196 *height = h; |
196 } else { | 197 } else { |
197 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| | 198 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| |
198 // will inexplicably fail with result E_INVALIDARG. Guard against this. | 199 // will inexplicably fail with result E_INVALIDARG. Guard against this. |
199 const size_t kMaxRenderTextLength = 5000; | 200 const size_t kMaxRenderTextLength = 5000; |
200 if (adjusted_text.length() >= kMaxRenderTextLength) { | 201 if (adjusted_text.length() >= kMaxRenderTextLength) { |
201 *width = font_list.GetExpectedTextWidth(adjusted_text.length()); | 202 *width = static_cast<float>( |
202 *height = font_list.GetHeight(); | 203 font_list.GetExpectedTextWidth(adjusted_text.length())); |
204 *height = static_cast<float>(font_list.GetHeight()); | |
203 } else { | 205 } else { |
204 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 206 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
205 Rect rect(*width, *height); | 207 Rect rect(static_cast<int>(*width), static_cast<int>(*height)); |
msw
2014/10/17 22:13:23
ditto nit q: DCHECK here?
| |
206 StripAcceleratorChars(flags, &adjusted_text); | 208 StripAcceleratorChars(flags, &adjusted_text); |
207 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, | 209 UpdateRenderText(rect, adjusted_text, font_list, flags, 0, |
208 render_text.get()); | 210 render_text.get()); |
209 const SizeF& string_size = render_text->GetStringSizeF(); | 211 const SizeF& string_size = render_text->GetStringSizeF(); |
210 *width = string_size.width(); | 212 *width = string_size.width(); |
211 *height = string_size.height(); | 213 *height = string_size.height(); |
212 } | 214 } |
213 } | 215 } |
214 } | 216 } |
215 | 217 |
(...skipping 24 matching lines...) Expand all Loading... | |
240 render_text->set_shadows(shadows); | 242 render_text->set_shadows(shadows); |
241 | 243 |
242 if (flags & MULTI_LINE) { | 244 if (flags & MULTI_LINE) { |
243 WordWrapBehavior wrap_behavior = IGNORE_LONG_WORDS; | 245 WordWrapBehavior wrap_behavior = IGNORE_LONG_WORDS; |
244 if (flags & CHARACTER_BREAK) | 246 if (flags & CHARACTER_BREAK) |
245 wrap_behavior = WRAP_LONG_WORDS; | 247 wrap_behavior = WRAP_LONG_WORDS; |
246 else if (!(flags & NO_ELLIPSIS)) | 248 else if (!(flags & NO_ELLIPSIS)) |
247 wrap_behavior = ELIDE_LONG_WORDS; | 249 wrap_behavior = ELIDE_LONG_WORDS; |
248 | 250 |
249 std::vector<base::string16> strings; | 251 std::vector<base::string16> strings; |
250 ElideRectangleText(adjusted_text, font_list, text_bounds.width(), | 252 ElideRectangleText(adjusted_text, font_list, |
253 static_cast<float>(text_bounds.width()), | |
251 text_bounds.height(), wrap_behavior, &strings); | 254 text_bounds.height(), wrap_behavior, &strings); |
252 | 255 |
253 for (size_t i = 0; i < strings.size(); i++) { | 256 for (size_t i = 0; i < strings.size(); i++) { |
254 Range range = StripAcceleratorChars(flags, &strings[i]); | 257 Range range = StripAcceleratorChars(flags, &strings[i]); |
255 UpdateRenderText(rect, strings[i], font_list, flags, color, | 258 UpdateRenderText(rect, strings[i], font_list, flags, color, |
256 render_text.get()); | 259 render_text.get()); |
257 int line_padding = 0; | 260 int line_padding = 0; |
258 if (line_height > 0) | 261 if (line_height > 0) |
259 line_padding = line_height - render_text->GetStringSize().height(); | 262 line_padding = line_height - render_text->GetStringSize().height(); |
260 else | 263 else |
(...skipping 26 matching lines...) Expand all Loading... | |
287 if (elide_text) { | 290 if (elide_text) { |
288 render_text->SetText(adjusted_text); | 291 render_text->SetText(adjusted_text); |
289 if (render_text->GetTextDirection() == base::i18n::LEFT_TO_RIGHT) { | 292 if (render_text->GetTextDirection() == base::i18n::LEFT_TO_RIGHT) { |
290 render_text->SetElideBehavior(FADE_TAIL); | 293 render_text->SetElideBehavior(FADE_TAIL); |
291 elide_text = false; | 294 elide_text = false; |
292 } | 295 } |
293 } | 296 } |
294 #endif | 297 #endif |
295 | 298 |
296 if (elide_text) { | 299 if (elide_text) { |
297 ElideTextAndAdjustRange(font_list, text_bounds.width(), &adjusted_text, | 300 ElideTextAndAdjustRange(font_list, |
298 &range); | 301 static_cast<float>(text_bounds.width()), |
302 &adjusted_text, &range); | |
299 } | 303 } |
300 | 304 |
301 UpdateRenderText(rect, adjusted_text, font_list, flags, color, | 305 UpdateRenderText(rect, adjusted_text, font_list, flags, color, |
302 render_text.get()); | 306 render_text.get()); |
303 if (range.IsValid()) | 307 if (range.IsValid()) |
304 render_text->ApplyStyle(UNDERLINE, true, range); | 308 render_text->ApplyStyle(UNDERLINE, true, range); |
305 render_text->Draw(this); | 309 render_text->Draw(this); |
306 } | 310 } |
307 | 311 |
308 canvas_->restore(); | 312 canvas_->restore(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 UpdateRenderText(rect, text, font_list, flags, color, render_text.get()); | 392 UpdateRenderText(rect, text, font_list, flags, color, render_text.get()); |
389 render_text->SetElideBehavior(FADE_TAIL); | 393 render_text->SetElideBehavior(FADE_TAIL); |
390 | 394 |
391 canvas_->save(); | 395 canvas_->save(); |
392 ClipRect(display_rect); | 396 ClipRect(display_rect); |
393 render_text->Draw(this); | 397 render_text->Draw(this); |
394 canvas_->restore(); | 398 canvas_->restore(); |
395 } | 399 } |
396 | 400 |
397 } // namespace gfx | 401 } // namespace gfx |
OLD | NEW |