|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/canvas_skia.h" | |
6 | |
7 #include "base/i18n/rtl.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "ui/base/text/text_elider.h" | |
11 #include "ui/gfx/font.h" | |
12 #include "ui/gfx/font_list.h" | |
13 #include "ui/gfx/rect.h" | |
14 #include "ui/gfx/render_text.h" | |
15 #include "ui/gfx/skia_util.h" | |
16 | |
17 namespace { | |
18 | |
19 // Based on |flags| and |text| content, returns whether text should be | |
20 // rendered right-to-left. | |
21 bool IsTextRTL(int flags, const string16& text) { | |
22 if (flags & gfx::Canvas::FORCE_RTL_DIRECTIONALITY) | |
23 return true; | |
24 if (flags & gfx::Canvas::FORCE_LTR_DIRECTIONALITY) | |
25 return false; | |
26 return base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(text); | |
27 } | |
28 | |
29 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If | |
30 // any of them are not the halo color, returns true. This defines the halo of | |
31 // pixels that will appear around the text. Note that we have to check each | |
32 // pixel against both the halo color and transparent since |DrawStringWithHalo| | |
33 // will modify the bitmap as it goes, and cleared pixels shouldn't count as | |
34 // changed. | |
35 bool PixelShouldGetHalo(const SkBitmap& bitmap, | |
36 int x, int y, | |
37 SkColor halo_color) { | |
38 if (x > 0 && | |
39 *bitmap.getAddr32(x - 1, y) != halo_color && | |
40 *bitmap.getAddr32(x - 1, y) != 0) | |
41 return true; // Touched pixel to the left. | |
42 if (x < bitmap.width() - 1 && | |
43 *bitmap.getAddr32(x + 1, y) != halo_color && | |
44 *bitmap.getAddr32(x + 1, y) != 0) | |
45 return true; // Touched pixel to the right. | |
46 if (y > 0 && | |
47 *bitmap.getAddr32(x, y - 1) != halo_color && | |
48 *bitmap.getAddr32(x, y - 1) != 0) | |
49 return true; // Touched pixel above. | |
50 if (y < bitmap.height() - 1 && | |
51 *bitmap.getAddr32(x, y + 1) != halo_color && | |
52 *bitmap.getAddr32(x, y + 1) != 0) | |
53 return true; // Touched pixel below. | |
54 return false; | |
55 } | |
56 | |
57 // Apply vertical alignment per |flags|. Returns y-coordinate delta. | |
58 int VAlignText(const gfx::Font& font, | |
59 int line_count, | |
60 int flags, | |
61 int available_height) { | |
62 const int text_size = font.GetFontSize(); | |
63 int offset; | |
64 if (flags & gfx::Canvas::TEXT_VALIGN_TOP) { | |
65 offset = text_size; | |
sky
2012/01/23 23:01:17
nit: I would be tempted to return here and in the
Alexei Svitkine (slow)
2012/01/24 16:22:24
Done.
| |
66 } else if (flags & gfx::Canvas::TEXT_VALIGN_BOTTOM) { | |
67 offset = available_height + text_size - font.GetHeight() - 1; | |
sky
2012/01/23 23:01:17
Why the -1 here and +1 @ 71?
Alexei Svitkine (slow)
2012/01/24 16:22:24
These values were picked empirically by observing
sky
2012/01/24 17:05:28
Please add a comment to this effect then.
| |
68 if (line_count > 1) | |
69 offset -= (line_count * text_size); | |
70 } else { | |
71 int half_height = available_height + text_size + 1; | |
72 if (line_count > 1) | |
73 half_height -= (line_count * text_size); | |
74 offset = half_height/2 - 2; | |
sky
2012/01/23 23:01:17
nit: 'half_height/2' -> 'half_height / 2'
Alexei Svitkine (slow)
2012/01/24 16:22:24
Done. I've also changed the variable name to bette
| |
75 } | |
76 return offset; | |
77 } | |
78 | |
79 // Updates |render_text| from the specified parameters. | |
80 void UpdateRenderText(gfx::RenderText* render_text, | |
81 const gfx::Rect& rect, | |
82 const string16& text, | |
83 const gfx::Font& font, | |
84 int flags, | |
85 SkColor color) { | |
86 int accelerated_char_pos = -1; | |
87 int accelerated_char_span = 0; | |
88 string16 transformed_text = text; | |
89 | |
90 // Strip accelerator character prefixes. | |
91 if (flags & (gfx::Canvas::SHOW_PREFIX | gfx::Canvas::HIDE_PREFIX)) { | |
92 transformed_text = gfx::RemoveAcceleratorChar(text, | |
93 '&', | |
94 &accelerated_char_pos, | |
95 &accelerated_char_span); | |
96 } | |
97 | |
98 render_text->SetFontList(gfx::FontList(font)); | |
99 render_text->SetText(transformed_text); | |
100 render_text->SetCursorEnabled(false); | |
101 | |
102 gfx::Rect display_rect = rect; | |
103 display_rect.Offset(0, -font.GetFontSize()); | |
104 display_rect.set_height(font.GetHeight()); | |
105 render_text->SetDisplayRect(display_rect); | |
106 | |
107 if (flags & gfx::Canvas::TEXT_ALIGN_RIGHT) | |
108 render_text->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
109 else if (flags & gfx::Canvas::TEXT_ALIGN_CENTER) | |
110 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
111 else | |
112 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
113 | |
114 gfx::StyleRange style; | |
115 style.foreground = color; | |
116 style.font_style = font.GetStyle(); | |
117 if (font.GetStyle() & gfx::Font::UNDERLINED) | |
118 style.underline = true; | |
119 render_text->set_default_style(style); | |
120 render_text->ApplyDefaultStyle(); | |
121 | |
122 // Underline the accelerator char, if present. | |
123 if ((flags & gfx::Canvas::SHOW_PREFIX) && accelerated_char_pos != -1 && | |
124 !style.underline) { | |
125 gfx::StyleRange underline_style = style; | |
126 int accelerated_char_end = accelerated_char_pos + accelerated_char_span; | |
127 underline_style.range.set_start(accelerated_char_pos); | |
128 underline_style.range.set_end(accelerated_char_end); | |
129 underline_style.underline = true; | |
130 render_text->ApplyStyleRange(underline_style); | |
131 } | |
132 } | |
133 | |
134 } // anonymous namespace | |
135 | |
136 namespace gfx { | |
137 | |
138 // static | |
139 void CanvasSkia::SizeStringInt(const string16& text, | |
140 const gfx::Font& font, | |
141 int* width, int* height, | |
142 int flags) { | |
143 DCHECK_GE(*width, 0); | |
144 DCHECK_GE(*height, 0); | |
145 | |
146 if ((flags & MULTI_LINE) && *width != 0) { | |
147 ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; | |
148 if (flags & CHARACTER_BREAK) | |
149 wrap_behavior = ui::WRAP_LONG_WORDS; | |
150 else if (!(flags & NO_ELLIPSIS)) | |
151 wrap_behavior = ui::ELIDE_LONG_WORDS; | |
152 | |
153 gfx::Rect rect(*width, INT_MAX); | |
154 std::vector<string16> strings; | |
155 ui::ElideRectangleText(text, font, rect.width(), rect.height(), | |
156 wrap_behavior, &strings); | |
157 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
158 UpdateRenderText(render_text.get(), rect, string16(), font, flags, 0); | |
159 | |
160 int h = 0; | |
161 int w = 0; | |
162 for (size_t i = 0; i < strings.size(); ++i) { | |
163 if (flags & (SHOW_PREFIX | HIDE_PREFIX)) | |
164 strings[i] = gfx::RemoveAcceleratorChar(strings[i], '&', NULL, NULL); | |
165 render_text->SetText(strings[i]); | |
166 w = std::max(w, render_text->GetStringWidth()); | |
167 h += font.GetHeight(); | |
168 } | |
169 *width = w; | |
170 *height = h; | |
171 } else { | |
172 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()| | |
173 // will inexplicably fail with result E_INVALIDARG. Guard against this. | |
174 const size_t kMaxRenderTextLength = 5000; | |
175 if (text.length() >= kMaxRenderTextLength) { | |
176 *width = text.length() * font.GetAverageCharacterWidth(); | |
177 } else { | |
178 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
179 gfx::Rect rect(*width, *height); | |
180 UpdateRenderText(render_text.get(), rect, text, font, flags, 0); | |
181 *width = render_text->GetStringWidth(); | |
182 } | |
183 *height = font.GetHeight(); | |
184 } | |
185 } | |
186 | |
187 void CanvasSkia::DrawStringInt(const string16& text, | |
188 const gfx::Font& font, | |
189 const SkColor& color, | |
190 int x, int y, int w, int h, | |
191 int flags) { | |
192 if (!IntersectsClipRectInt(x, y, w, h)) | |
193 return; | |
194 | |
195 // TODO(asvitkine): On Windows, MULTI_LINE implies top alignment. | |
196 // http://crbug.com/107357 | |
197 if (flags & gfx::Canvas::MULTI_LINE) { | |
198 flags &= ~(gfx::Canvas::TEXT_VALIGN_MIDDLE | | |
199 gfx::Canvas::TEXT_VALIGN_BOTTOM); | |
200 flags |= gfx::Canvas::TEXT_VALIGN_TOP; | |
201 } | |
202 | |
203 gfx::Rect rect(x, y, w, h); | |
204 canvas_->save(SkCanvas::kClip_SaveFlag); | |
205 ClipRect(rect); | |
206 | |
207 string16 adjusted_text = text; | |
208 if (IsTextRTL(flags, text)) | |
209 base::i18n::AdjustStringForLocaleDirection(&adjusted_text); | |
210 | |
211 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
212 | |
213 if (flags & MULTI_LINE) { | |
214 ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; | |
215 if (flags & CHARACTER_BREAK) | |
216 wrap_behavior = ui::WRAP_LONG_WORDS; | |
217 else if (!(flags & NO_ELLIPSIS)) | |
218 wrap_behavior = ui::ELIDE_LONG_WORDS; | |
219 | |
220 std::vector<string16> strings; | |
221 ui::ElideRectangleText(adjusted_text, font, w, h, wrap_behavior, | |
222 &strings); | |
223 | |
224 rect.Offset(0, VAlignText(font, strings.size(), flags, h)); | |
225 for (size_t i = 0; i < strings.size(); i++) { | |
226 UpdateRenderText(render_text.get(), rect, strings[i], font, flags, color); | |
227 render_text->Draw(this); | |
228 rect.Offset(0, font.GetHeight()); | |
229 } | |
230 } else { | |
231 if (!(flags & NO_ELLIPSIS)) | |
232 adjusted_text = ui::ElideText(adjusted_text, font, w, ui::ELIDE_AT_END); | |
233 | |
234 rect.Offset(0, VAlignText(font, 1, flags, h)); | |
235 UpdateRenderText(render_text.get(), rect, adjusted_text, font, flags, | |
236 color); | |
237 render_text->Draw(this); | |
238 } | |
239 | |
240 canvas_->restore(); | |
241 } | |
242 | |
243 void CanvasSkia::DrawStringWithHalo(const string16& text, | |
244 const gfx::Font& font, | |
245 const SkColor& text_color, | |
246 const SkColor& halo_color_in, | |
247 int x, int y, int w, int h, | |
248 int flags) { | |
249 // Some callers will have semitransparent halo colors, which we don't handle | |
250 // (since the resulting image can have 1-bit transparency only). | |
251 SkColor halo_color = halo_color_in | 0xFF000000; | |
252 | |
253 // Create a temporary buffer filled with the halo color. It must leave room | |
254 // for the 1-pixel border around the text. | |
255 gfx::Size size(w + 2, h + 2); | |
256 CanvasSkia text_canvas(size, true); | |
257 SkPaint bkgnd_paint; | |
258 bkgnd_paint.setColor(halo_color); | |
259 text_canvas.DrawRect(gfx::Rect(size), bkgnd_paint); | |
260 | |
261 // Draw the text into the temporary buffer. This will have correct | |
262 // ClearType since the background color is the same as the halo color. | |
263 text_canvas.DrawStringInt(text, font, text_color, 1, 1, w, h, flags); | |
264 | |
265 uint32_t halo_premul = SkPreMultiplyColor(halo_color); | |
266 SkBitmap& text_bitmap = const_cast<SkBitmap&>( | |
267 skia::GetTopDevice(*text_canvas.sk_canvas())->accessBitmap(true)); | |
268 | |
269 for (int cur_y = 0; cur_y < h + 2; cur_y++) { | |
270 uint32_t* text_row = text_bitmap.getAddr32(0, cur_y); | |
271 for (int cur_x = 0; cur_x < w + 2; cur_x++) { | |
272 if (text_row[cur_x] == halo_premul) { | |
273 // This pixel was not touched by the text routines. See if it borders | |
274 // a touched pixel in any of the 4 directions (not diagonally). | |
275 if (!PixelShouldGetHalo(text_bitmap, cur_x, cur_y, halo_premul)) | |
276 text_row[cur_x] = 0; // Make transparent. | |
277 } else { | |
278 text_row[cur_x] |= 0xff << SK_A32_SHIFT; // Make opaque. | |
279 } | |
280 } | |
281 } | |
282 | |
283 // Draw the halo bitmap with blur. | |
284 DrawBitmapInt(text_bitmap, x - 1, y - 1); | |
285 } | |
286 | |
287 void CanvasSkia::DrawFadeTruncatingString( | |
288 const string16& text, | |
289 CanvasSkia::TruncateFadeMode truncate_mode, | |
290 size_t desired_characters_to_truncate_from_head, | |
291 const gfx::Font& font, | |
292 const SkColor& color, | |
293 const gfx::Rect& display_rect) { | |
294 int flags = NO_ELLIPSIS; | |
295 | |
296 // If the whole string fits in the destination then just draw it directly. | |
297 if (font.GetStringWidth(text) <= display_rect.width()) { | |
298 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | |
299 display_rect.width(), display_rect.height(), flags); | |
300 return; | |
301 } | |
302 | |
303 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
304 string16 clipped_text = text; | |
305 const bool is_rtl = IsTextRTL(flags, text); | |
306 if (is_rtl) | |
307 base::i18n::AdjustStringForLocaleDirection(&clipped_text); | |
308 | |
309 switch (truncate_mode) { | |
310 case TruncateFadeTail: | |
311 render_text->set_fade_tail(true); | |
312 if (is_rtl) | |
313 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | |
314 break; | |
315 case TruncateFadeHead: | |
316 render_text->set_fade_head(true); | |
317 if (!is_rtl) | |
318 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | |
319 break; | |
320 case TruncateFadeHeadAndTail: | |
321 DCHECK_GT(desired_characters_to_truncate_from_head, 0u); | |
322 // Due to the fade effect the first character is hard to see. | |
323 // We want to make sure that the first character starting at | |
324 // |desired_characters_to_truncate_from_head| is readable so we reduce | |
325 // the offset by a little bit. | |
326 desired_characters_to_truncate_from_head = | |
327 std::max<int>(0, desired_characters_to_truncate_from_head - 2); | |
328 | |
329 if (desired_characters_to_truncate_from_head) { | |
330 // Make sure to clip the text at a UTF16 boundary. | |
331 U16_SET_CP_LIMIT(text.data(), 0, | |
332 desired_characters_to_truncate_from_head, | |
333 text.length()); | |
334 clipped_text = text.substr(desired_characters_to_truncate_from_head); | |
335 } | |
336 | |
337 render_text->set_fade_tail(true); | |
338 render_text->set_fade_head(true); | |
339 break; | |
340 } | |
341 | |
342 gfx::Rect text_rect = display_rect; | |
343 text_rect.Offset(0, VAlignText(font, 1, flags, display_rect.height())); | |
344 UpdateRenderText(render_text.get(), text_rect, clipped_text, font, flags, | |
345 color); | |
346 | |
347 canvas_->save(SkCanvas::kClip_SaveFlag); | |
348 ClipRect(display_rect); | |
349 render_text->Draw(this); | |
350 canvas_->restore(); | |
351 } | |
352 | |
353 } // namespace gfx | |
OLD | NEW |