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/base/range/range.h" | 10 #include "ui/base/range/range.h" |
11 #include "ui/base/text/text_elider.h" | 11 #include "ui/base/text/text_elider.h" |
12 #include "ui/gfx/font.h" | 12 #include "ui/gfx/font.h" |
13 #include "ui/gfx/font_list.h" | 13 #include "ui/gfx/font_list.h" |
14 #include "ui/gfx/insets.h" | 14 #include "ui/gfx/insets.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/render_text.h" | 16 #include "ui/gfx/render_text.h" |
17 #include "ui/gfx/shadow_value.h" | 17 #include "ui/gfx/shadow_value.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 |
24 // If necessary, wraps |text| with RTL/LTR directionality characters based on | 24 // If necessary, wraps |text| with RTL/LTR directionality characters based on |
25 // |flags| and |text| content. | 25 // |flags| and |text| content. |
26 // Returns true if the text will be rendered right-to-left. | 26 // Returns true if the text will be rendered right-to-left. |
27 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly. | 27 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly. |
28 bool AdjustStringDirection(int flags, string16* text) { | 28 bool AdjustStringDirection(int flags, base::string16* text) { |
29 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now. | 29 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now. |
30 | 30 |
31 // If the string is empty or LTR was forced, simply return false since the | 31 // If the string is empty or LTR was forced, simply return false since the |
32 // default RenderText directionality is already LTR. | 32 // default RenderText directionality is already LTR. |
33 if (text->empty() || (flags & Canvas::FORCE_LTR_DIRECTIONALITY)) | 33 if (text->empty() || (flags & Canvas::FORCE_LTR_DIRECTIONALITY)) |
34 return false; | 34 return false; |
35 | 35 |
36 // If RTL is forced, apply it to the string. | 36 // If RTL is forced, apply it to the string. |
37 if (flags & Canvas::FORCE_RTL_DIRECTIONALITY) { | 37 if (flags & Canvas::FORCE_RTL_DIRECTIONALITY) { |
38 base::i18n::WrapStringWithRTLFormatting(text); | 38 base::i18n::WrapStringWithRTLFormatting(text); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 if (y < bitmap.height() - 1 && | 78 if (y < bitmap.height() - 1 && |
79 *bitmap.getAddr32(x, y + 1) != halo_color && | 79 *bitmap.getAddr32(x, y + 1) != halo_color && |
80 *bitmap.getAddr32(x, y + 1) != 0) | 80 *bitmap.getAddr32(x, y + 1) != 0) |
81 return true; // Touched pixel below. | 81 return true; // Touched pixel below. |
82 return false; | 82 return false; |
83 } | 83 } |
84 | 84 |
85 // Strips accelerator character prefixes in |text| if needed, based on |flags|. | 85 // Strips accelerator character prefixes in |text| if needed, based on |flags|. |
86 // Returns a range in |text| to underline or ui::Range::InvalidRange() if | 86 // Returns a range in |text| to underline or ui::Range::InvalidRange() if |
87 // underlining is not needed. | 87 // underlining is not needed. |
88 ui::Range StripAcceleratorChars(int flags, string16* text) { | 88 ui::Range StripAcceleratorChars(int flags, base::string16* text) { |
89 if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) { | 89 if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) { |
90 int char_pos = -1; | 90 int char_pos = -1; |
91 int char_span = 0; | 91 int char_span = 0; |
92 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); | 92 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); |
93 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) | 93 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) |
94 return ui::Range(char_pos, char_pos + char_span); | 94 return ui::Range(char_pos, char_pos + char_span); |
95 } | 95 } |
96 return ui::Range::InvalidRange(); | 96 return ui::Range::InvalidRange(); |
97 } | 97 } |
98 | 98 |
99 // Elides |text| and adjusts |range| appropriately. If eliding causes |range| | 99 // Elides |text| and adjusts |range| appropriately. If eliding causes |range| |
100 // to no longer point to the same character in |text|, |range| is made invalid. | 100 // to no longer point to the same character in |text|, |range| is made invalid. |
101 void ElideTextAndAdjustRange(const Font& font, | 101 void ElideTextAndAdjustRange(const Font& font, |
102 int width, | 102 int width, |
103 string16* text, | 103 base::string16* text, |
104 ui::Range* range) { | 104 ui::Range* range) { |
105 const char16 start_char = (range->IsValid() ? text->at(range->start()) : 0); | 105 const base::char16 start_char = |
| 106 (range->IsValid() ? text->at(range->start()) : 0); |
106 *text = ui::ElideText(*text, font, width, ui::ELIDE_AT_END); | 107 *text = ui::ElideText(*text, font, width, ui::ELIDE_AT_END); |
107 if (!range->IsValid()) | 108 if (!range->IsValid()) |
108 return; | 109 return; |
109 if (range->start() >= text->length() || | 110 if (range->start() >= text->length() || |
110 text->at(range->start()) != start_char) { | 111 text->at(range->start()) != start_char) { |
111 *range = ui::Range::InvalidRange(); | 112 *range = ui::Range::InvalidRange(); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 // Updates |render_text| from the specified parameters. | 116 // Updates |render_text| from the specified parameters. |
116 void UpdateRenderText(const Rect& rect, | 117 void UpdateRenderText(const Rect& rect, |
117 const string16& text, | 118 const base::string16& text, |
118 const Font& font, | 119 const Font& font, |
119 int flags, | 120 int flags, |
120 SkColor color, | 121 SkColor color, |
121 RenderText* render_text) { | 122 RenderText* render_text) { |
122 render_text->SetFont(font); | 123 render_text->SetFont(font); |
123 render_text->SetText(text); | 124 render_text->SetText(text); |
124 render_text->SetCursorEnabled(false); | 125 render_text->SetCursorEnabled(false); |
125 | 126 |
126 Rect display_rect = rect; | 127 Rect display_rect = rect; |
127 display_rect.set_height(font.GetHeight()); | 128 display_rect.set_height(font.GetHeight()); |
(...skipping 17 matching lines...) Expand all Loading... |
145 if (flags & Canvas::NO_SUBPIXEL_RENDERING) | 146 if (flags & Canvas::NO_SUBPIXEL_RENDERING) |
146 render_text->set_background_is_transparent(true); | 147 render_text->set_background_is_transparent(true); |
147 | 148 |
148 render_text->SetColor(color); | 149 render_text->SetColor(color); |
149 render_text->SetStyle(BOLD, (font.GetStyle() & Font::BOLD) != 0); | 150 render_text->SetStyle(BOLD, (font.GetStyle() & Font::BOLD) != 0); |
150 render_text->SetStyle(ITALIC, (font.GetStyle() & Font::ITALIC) != 0); | 151 render_text->SetStyle(ITALIC, (font.GetStyle() & Font::ITALIC) != 0); |
151 render_text->SetStyle(UNDERLINE, (font.GetStyle() & Font::UNDERLINE) != 0); | 152 render_text->SetStyle(UNDERLINE, (font.GetStyle() & Font::UNDERLINE) != 0); |
152 } | 153 } |
153 | 154 |
154 // Returns updated |flags| to match platform-specific expected behavior. | 155 // Returns updated |flags| to match platform-specific expected behavior. |
155 int AdjustPlatformSpecificFlags(const string16& text, int flags) { | 156 int AdjustPlatformSpecificFlags(const base::string16& text, int flags) { |
156 #if defined(OS_LINUX) | 157 #if defined(OS_LINUX) |
157 // TODO(asvitkine): ash/tooltips/tooltip_controller.cc adds \n's to the string | 158 // TODO(asvitkine): ash/tooltips/tooltip_controller.cc adds \n's to the string |
158 // without passing MULTI_LINE. | 159 // without passing MULTI_LINE. |
159 if (text.find('\n') != string16::npos) | 160 if (text.find('\n') != base::string16::npos) |
160 flags |= Canvas::MULTI_LINE; | 161 flags |= Canvas::MULTI_LINE; |
161 #endif | 162 #endif |
162 | 163 |
163 return flags; | 164 return flags; |
164 } | 165 } |
165 | 166 |
166 } // namespace | 167 } // namespace |
167 | 168 |
168 // static | 169 // static |
169 void Canvas::SizeStringInt(const string16& text, | 170 void Canvas::SizeStringInt(const base::string16& text, |
170 const Font& font, | 171 const Font& font, |
171 int* width, int* height, | 172 int* width, int* height, |
172 int line_height, | 173 int line_height, |
173 int flags) { | 174 int flags) { |
174 DCHECK_GE(*width, 0); | 175 DCHECK_GE(*width, 0); |
175 DCHECK_GE(*height, 0); | 176 DCHECK_GE(*height, 0); |
176 | 177 |
177 flags = AdjustPlatformSpecificFlags(text, flags); | 178 flags = AdjustPlatformSpecificFlags(text, flags); |
178 | 179 |
179 string16 adjusted_text = text; | 180 base::string16 adjusted_text = text; |
180 #if defined(OS_WIN) | 181 #if defined(OS_WIN) |
181 AdjustStringDirection(flags, &adjusted_text); | 182 AdjustStringDirection(flags, &adjusted_text); |
182 #endif | 183 #endif |
183 | 184 |
184 if ((flags & MULTI_LINE) && *width != 0) { | 185 if ((flags & MULTI_LINE) && *width != 0) { |
185 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; | 186 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; |
186 if (flags & CHARACTER_BREAK) | 187 if (flags & CHARACTER_BREAK) |
187 wrap_behavior = ui::WRAP_LONG_WORDS; | 188 wrap_behavior = ui::WRAP_LONG_WORDS; |
188 else if (!(flags & NO_ELLIPSIS)) | 189 else if (!(flags & NO_ELLIPSIS)) |
189 wrap_behavior = ui::ELIDE_LONG_WORDS; | 190 wrap_behavior = ui::ELIDE_LONG_WORDS; |
190 | 191 |
191 Rect rect(*width, INT_MAX); | 192 Rect rect(*width, INT_MAX); |
192 std::vector<string16> strings; | 193 std::vector<base::string16> strings; |
193 ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(), | 194 ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(), |
194 wrap_behavior, &strings); | 195 wrap_behavior, &strings); |
195 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 196 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
196 UpdateRenderText(rect, string16(), font, flags, 0, render_text.get()); | 197 UpdateRenderText(rect, base::string16(), font, flags, 0, render_text.get()); |
197 | 198 |
198 int h = 0; | 199 int h = 0; |
199 int w = 0; | 200 int w = 0; |
200 for (size_t i = 0; i < strings.size(); ++i) { | 201 for (size_t i = 0; i < strings.size(); ++i) { |
201 StripAcceleratorChars(flags, &strings[i]); | 202 StripAcceleratorChars(flags, &strings[i]); |
202 render_text->SetText(strings[i]); | 203 render_text->SetText(strings[i]); |
203 const Size string_size = render_text->GetStringSize(); | 204 const Size string_size = render_text->GetStringSize(); |
204 w = std::max(w, string_size.width()); | 205 w = std::max(w, string_size.width()); |
205 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); | 206 h += (i > 0 && line_height > 0) ? line_height : string_size.height(); |
206 } | 207 } |
(...skipping 11 matching lines...) Expand all Loading... |
218 Rect rect(*width, *height); | 219 Rect rect(*width, *height); |
219 StripAcceleratorChars(flags, &adjusted_text); | 220 StripAcceleratorChars(flags, &adjusted_text); |
220 UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get()); | 221 UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get()); |
221 const Size string_size = render_text->GetStringSize(); | 222 const Size string_size = render_text->GetStringSize(); |
222 *width = string_size.width(); | 223 *width = string_size.width(); |
223 *height = string_size.height(); | 224 *height = string_size.height(); |
224 } | 225 } |
225 } | 226 } |
226 } | 227 } |
227 | 228 |
228 void Canvas::DrawStringWithShadows(const string16& text, | 229 void Canvas::DrawStringWithShadows(const base::string16& text, |
229 const Font& font, | 230 const Font& font, |
230 SkColor color, | 231 SkColor color, |
231 const Rect& text_bounds, | 232 const Rect& text_bounds, |
232 int line_height, | 233 int line_height, |
233 int flags, | 234 int flags, |
234 const ShadowValues& shadows) { | 235 const ShadowValues& shadows) { |
235 if (!IntersectsClipRect(text_bounds)) | 236 if (!IntersectsClipRect(text_bounds)) |
236 return; | 237 return; |
237 | 238 |
238 flags = AdjustPlatformSpecificFlags(text, flags); | 239 flags = AdjustPlatformSpecificFlags(text, flags); |
239 | 240 |
240 Rect clip_rect(text_bounds); | 241 Rect clip_rect(text_bounds); |
241 clip_rect.Inset(ShadowValue::GetMargin(shadows)); | 242 clip_rect.Inset(ShadowValue::GetMargin(shadows)); |
242 | 243 |
243 canvas_->save(SkCanvas::kClip_SaveFlag); | 244 canvas_->save(SkCanvas::kClip_SaveFlag); |
244 ClipRect(clip_rect); | 245 ClipRect(clip_rect); |
245 | 246 |
246 Rect rect(text_bounds); | 247 Rect rect(text_bounds); |
247 string16 adjusted_text = text; | 248 base::string16 adjusted_text = text; |
248 | 249 |
249 #if defined(OS_WIN) | 250 #if defined(OS_WIN) |
250 AdjustStringDirection(flags, &adjusted_text); | 251 AdjustStringDirection(flags, &adjusted_text); |
251 #endif | 252 #endif |
252 | 253 |
253 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 254 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
254 render_text->SetTextShadows(shadows); | 255 render_text->SetTextShadows(shadows); |
255 | 256 |
256 if (flags & MULTI_LINE) { | 257 if (flags & MULTI_LINE) { |
257 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; | 258 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; |
258 if (flags & CHARACTER_BREAK) | 259 if (flags & CHARACTER_BREAK) |
259 wrap_behavior = ui::WRAP_LONG_WORDS; | 260 wrap_behavior = ui::WRAP_LONG_WORDS; |
260 else if (!(flags & NO_ELLIPSIS)) | 261 else if (!(flags & NO_ELLIPSIS)) |
261 wrap_behavior = ui::ELIDE_LONG_WORDS; | 262 wrap_behavior = ui::ELIDE_LONG_WORDS; |
262 | 263 |
263 std::vector<string16> strings; | 264 std::vector<base::string16> strings; |
264 ui::ElideRectangleText(adjusted_text, | 265 ui::ElideRectangleText(adjusted_text, |
265 font, | 266 font, |
266 text_bounds.width(), text_bounds.height(), | 267 text_bounds.width(), text_bounds.height(), |
267 wrap_behavior, | 268 wrap_behavior, |
268 &strings); | 269 &strings); |
269 | 270 |
270 for (size_t i = 0; i < strings.size(); i++) { | 271 for (size_t i = 0; i < strings.size(); i++) { |
271 ui::Range range = StripAcceleratorChars(flags, &strings[i]); | 272 ui::Range range = StripAcceleratorChars(flags, &strings[i]); |
272 UpdateRenderText(rect, strings[i], font, flags, color, render_text.get()); | 273 UpdateRenderText(rect, strings[i], font, flags, color, render_text.get()); |
273 int line_padding = 0; | 274 int line_padding = 0; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 rect.set_height(text_height); | 326 rect.set_height(text_height); |
326 render_text->SetDisplayRect(rect); | 327 render_text->SetDisplayRect(rect); |
327 if (range.IsValid()) | 328 if (range.IsValid()) |
328 render_text->ApplyStyle(UNDERLINE, true, range); | 329 render_text->ApplyStyle(UNDERLINE, true, range); |
329 render_text->Draw(this); | 330 render_text->Draw(this); |
330 } | 331 } |
331 | 332 |
332 canvas_->restore(); | 333 canvas_->restore(); |
333 } | 334 } |
334 | 335 |
335 void Canvas::DrawStringWithHalo(const string16& text, | 336 void Canvas::DrawStringWithHalo(const base::string16& text, |
336 const Font& font, | 337 const Font& font, |
337 SkColor text_color, | 338 SkColor text_color, |
338 SkColor halo_color_in, | 339 SkColor halo_color_in, |
339 int x, int y, int w, int h, | 340 int x, int y, int w, int h, |
340 int flags) { | 341 int flags) { |
341 // Some callers will have semitransparent halo colors, which we don't handle | 342 // Some callers will have semitransparent halo colors, which we don't handle |
342 // (since the resulting image can have 1-bit transparency only). | 343 // (since the resulting image can have 1-bit transparency only). |
343 SkColor halo_color = SkColorSetA(halo_color_in, 0xFF); | 344 SkColor halo_color = SkColorSetA(halo_color_in, 0xFF); |
344 | 345 |
345 // Create a temporary buffer filled with the halo color. It must leave room | 346 // Create a temporary buffer filled with the halo color. It must leave room |
(...skipping 26 matching lines...) Expand all Loading... |
372 } | 373 } |
373 } | 374 } |
374 | 375 |
375 // Draw the halo bitmap with blur. | 376 // Draw the halo bitmap with blur. |
376 ImageSkia text_image = ImageSkia(ImageSkiaRep(text_bitmap, | 377 ImageSkia text_image = ImageSkia(ImageSkiaRep(text_bitmap, |
377 text_canvas.scale_factor())); | 378 text_canvas.scale_factor())); |
378 DrawImageInt(text_image, x - 1, y - 1); | 379 DrawImageInt(text_image, x - 1, y - 1); |
379 } | 380 } |
380 | 381 |
381 void Canvas::DrawFadeTruncatingString( | 382 void Canvas::DrawFadeTruncatingString( |
382 const string16& text, | 383 const base::string16& text, |
383 TruncateFadeMode truncate_mode, | 384 TruncateFadeMode truncate_mode, |
384 size_t desired_characters_to_truncate_from_head, | 385 size_t desired_characters_to_truncate_from_head, |
385 const Font& font, | 386 const Font& font, |
386 SkColor color, | 387 SkColor color, |
387 const Rect& display_rect) { | 388 const Rect& display_rect) { |
388 int flags = NO_ELLIPSIS; | 389 int flags = NO_ELLIPSIS; |
389 | 390 |
390 // If the whole string fits in the destination then just draw it directly. | 391 // If the whole string fits in the destination then just draw it directly. |
391 if (GetStringWidth(text, font) <= display_rect.width()) { | 392 if (GetStringWidth(text, font) <= display_rect.width()) { |
392 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 393 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
393 display_rect.width(), display_rect.height(), flags); | 394 display_rect.width(), display_rect.height(), flags); |
394 return; | 395 return; |
395 } | 396 } |
396 | 397 |
397 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 398 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
398 string16 clipped_text = text; | 399 base::string16 clipped_text = text; |
399 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); | 400 const bool is_rtl = AdjustStringDirection(flags, &clipped_text); |
400 | 401 |
401 switch (truncate_mode) { | 402 switch (truncate_mode) { |
402 case TruncateFadeTail: | 403 case TruncateFadeTail: |
403 render_text->set_fade_tail(true); | 404 render_text->set_fade_tail(true); |
404 if (is_rtl) | 405 if (is_rtl) |
405 flags |= TEXT_ALIGN_RIGHT; | 406 flags |= TEXT_ALIGN_RIGHT; |
406 break; | 407 break; |
407 case TruncateFadeHead: | 408 case TruncateFadeHead: |
408 render_text->set_fade_head(true); | 409 render_text->set_fade_head(true); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 rect.set_height(line_height); | 445 rect.set_height(line_height); |
445 render_text->SetDisplayRect(rect); | 446 render_text->SetDisplayRect(rect); |
446 | 447 |
447 canvas_->save(SkCanvas::kClip_SaveFlag); | 448 canvas_->save(SkCanvas::kClip_SaveFlag); |
448 ClipRect(display_rect); | 449 ClipRect(display_rect); |
449 render_text->Draw(this); | 450 render_text->Draw(this); |
450 canvas_->restore(); | 451 canvas_->restore(); |
451 } | 452 } |
452 | 453 |
453 } // namespace gfx | 454 } // namespace gfx |
OLD | NEW |