[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / canvas_skia.cc
blob8d1311faf391614b335471409ca015e07d0c2191
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.
5 #include "ui/gfx/canvas.h"
7 #include "base/i18n/rtl.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/insets.h"
12 #include "ui/gfx/range/range.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/render_text.h"
15 #include "ui/gfx/shadow_value.h"
16 #include "ui/gfx/text_elider.h"
17 #include "ui/gfx/text_utils.h"
19 namespace gfx {
21 namespace {
23 #if defined(OS_WIN)
24 // If necessary, wraps |text| with RTL/LTR directionality characters based on
25 // |flags| and |text| content.
26 // Returns true if the text will be rendered right-to-left.
27 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly.
28 bool AdjustStringDirection(int flags, base::string16* text) {
29 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now.
31 // If the string is empty or LTR was forced, simply return false since the
32 // default RenderText directionality is already LTR.
33 if (text->empty() || (flags & Canvas::FORCE_LTR_DIRECTIONALITY))
34 return false;
36 // If RTL is forced, apply it to the string.
37 if (flags & Canvas::FORCE_RTL_DIRECTIONALITY) {
38 base::i18n::WrapStringWithRTLFormatting(text);
39 return true;
42 // If a direction wasn't forced but the UI language is RTL and there were
43 // strong RTL characters, ensure RTL is applied.
44 if (base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(*text)) {
45 base::i18n::WrapStringWithRTLFormatting(text);
46 return true;
49 // In the default case, the string should be rendered as LTR. RenderText's
50 // default directionality is LTR, so the text doesn't need to be wrapped.
51 // Note that individual runs within the string may still be rendered RTL
52 // (which will be the case for RTL text under non-RTL locales, since under RTL
53 // locales it will be handled by the if statement above).
54 return false;
56 #endif // defined(OS_WIN)
58 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If
59 // any of them are not the halo color, returns true. This defines the halo of
60 // pixels that will appear around the text. Note that we have to check each
61 // pixel against both the halo color and transparent since
62 // |DrawStringRectWithHalo| will modify the bitmap as it goes, and cleared
63 // pixels shouldn't count as changed.
64 bool PixelShouldGetHalo(const SkBitmap& bitmap,
65 int x, int y,
66 SkColor halo_color) {
67 if (x > 0 &&
68 *bitmap.getAddr32(x - 1, y) != halo_color &&
69 *bitmap.getAddr32(x - 1, y) != 0)
70 return true; // Touched pixel to the left.
71 if (x < bitmap.width() - 1 &&
72 *bitmap.getAddr32(x + 1, y) != halo_color &&
73 *bitmap.getAddr32(x + 1, y) != 0)
74 return true; // Touched pixel to the right.
75 if (y > 0 &&
76 *bitmap.getAddr32(x, y - 1) != halo_color &&
77 *bitmap.getAddr32(x, y - 1) != 0)
78 return true; // Touched pixel above.
79 if (y < bitmap.height() - 1 &&
80 *bitmap.getAddr32(x, y + 1) != halo_color &&
81 *bitmap.getAddr32(x, y + 1) != 0)
82 return true; // Touched pixel below.
83 return false;
86 // Strips accelerator character prefixes in |text| if needed, based on |flags|.
87 // Returns a range in |text| to underline or Range::InvalidRange() if
88 // underlining is not needed.
89 Range StripAcceleratorChars(int flags, base::string16* text) {
90 if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) {
91 int char_pos = -1;
92 int char_span = 0;
93 *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span);
94 if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1)
95 return Range(char_pos, char_pos + char_span);
97 return Range::InvalidRange();
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.
102 void ElideTextAndAdjustRange(const FontList& font_list,
103 int width,
104 base::string16* text,
105 Range* range) {
106 const base::char16 start_char =
107 (range->IsValid() ? text->at(range->start()) : 0);
108 *text = ElideText(*text, font_list, width, ELIDE_TAIL);
109 if (!range->IsValid())
110 return;
111 if (range->start() >= text->length() ||
112 text->at(range->start()) != start_char) {
113 *range = Range::InvalidRange();
117 // Updates |render_text| from the specified parameters.
118 void UpdateRenderText(const Rect& rect,
119 const base::string16& text,
120 const FontList& font_list,
121 int flags,
122 SkColor color,
123 RenderText* render_text) {
124 render_text->SetFontList(font_list);
125 render_text->SetText(text);
126 render_text->SetCursorEnabled(false);
128 Rect display_rect = rect;
129 display_rect.set_height(font_list.GetHeight());
130 render_text->SetDisplayRect(display_rect);
132 // Set the text alignment explicitly based on the directionality of the UI,
133 // if not specified.
134 if (!(flags & (Canvas::TEXT_ALIGN_CENTER |
135 Canvas::TEXT_ALIGN_RIGHT |
136 Canvas::TEXT_ALIGN_LEFT))) {
137 flags |= Canvas::DefaultCanvasTextAlignment();
140 if (flags & Canvas::TEXT_ALIGN_RIGHT)
141 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
142 else if (flags & Canvas::TEXT_ALIGN_CENTER)
143 render_text->SetHorizontalAlignment(ALIGN_CENTER);
144 else
145 render_text->SetHorizontalAlignment(ALIGN_LEFT);
147 if (flags & Canvas::NO_SUBPIXEL_RENDERING)
148 render_text->set_background_is_transparent(true);
150 render_text->SetColor(color);
151 const int font_style = font_list.GetFontStyle();
152 render_text->SetStyle(BOLD, (font_style & Font::BOLD) != 0);
153 render_text->SetStyle(ITALIC, (font_style & Font::ITALIC) != 0);
154 render_text->SetStyle(UNDERLINE, (font_style & Font::UNDERLINE) != 0);
157 } // namespace
159 // static
160 void Canvas::SizeStringFloat(const base::string16& text,
161 const FontList& font_list,
162 float* width, float* height,
163 int line_height,
164 int flags) {
165 DCHECK_GE(*width, 0);
166 DCHECK_GE(*height, 0);
168 base::string16 adjusted_text = text;
169 #if defined(OS_WIN)
170 AdjustStringDirection(flags, &adjusted_text);
171 #endif
173 if ((flags & MULTI_LINE) && *width != 0) {
174 WordWrapBehavior wrap_behavior = TRUNCATE_LONG_WORDS;
175 if (flags & CHARACTER_BREAK)
176 wrap_behavior = WRAP_LONG_WORDS;
177 else if (!(flags & NO_ELLIPSIS))
178 wrap_behavior = ELIDE_LONG_WORDS;
180 Rect rect(*width, INT_MAX);
181 std::vector<base::string16> strings;
182 ElideRectangleText(adjusted_text, font_list, rect.width(), rect.height(),
183 wrap_behavior, &strings);
184 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
185 UpdateRenderText(rect, base::string16(), font_list, flags, 0,
186 render_text.get());
188 float h = 0;
189 float w = 0;
190 for (size_t i = 0; i < strings.size(); ++i) {
191 StripAcceleratorChars(flags, &strings[i]);
192 render_text->SetText(strings[i]);
193 const SizeF& string_size = render_text->GetStringSizeF();
194 w = std::max(w, string_size.width());
195 h += (i > 0 && line_height > 0) ? line_height : string_size.height();
197 *width = w;
198 *height = h;
199 } else {
200 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()|
201 // will inexplicably fail with result E_INVALIDARG. Guard against this.
202 const size_t kMaxRenderTextLength = 5000;
203 if (adjusted_text.length() >= kMaxRenderTextLength) {
204 *width = font_list.GetExpectedTextWidth(adjusted_text.length());
205 *height = font_list.GetHeight();
206 } else {
207 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
208 Rect rect(*width, *height);
209 StripAcceleratorChars(flags, &adjusted_text);
210 UpdateRenderText(rect, adjusted_text, font_list, flags, 0,
211 render_text.get());
212 const SizeF& string_size = render_text->GetStringSizeF();
213 *width = string_size.width();
214 *height = string_size.height();
219 void Canvas::DrawStringRectWithShadows(const base::string16& text,
220 const FontList& font_list,
221 SkColor color,
222 const Rect& text_bounds,
223 int line_height,
224 int flags,
225 const ShadowValues& shadows) {
226 if (!IntersectsClipRect(text_bounds))
227 return;
229 Rect clip_rect(text_bounds);
230 clip_rect.Inset(ShadowValue::GetMargin(shadows));
232 canvas_->save();
233 ClipRect(clip_rect);
235 Rect rect(text_bounds);
236 base::string16 adjusted_text = text;
238 #if defined(OS_WIN)
239 AdjustStringDirection(flags, &adjusted_text);
240 #endif
242 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
243 render_text->set_shadows(shadows);
245 if (flags & MULTI_LINE) {
246 WordWrapBehavior wrap_behavior = IGNORE_LONG_WORDS;
247 if (flags & CHARACTER_BREAK)
248 wrap_behavior = WRAP_LONG_WORDS;
249 else if (!(flags & NO_ELLIPSIS))
250 wrap_behavior = ELIDE_LONG_WORDS;
252 std::vector<base::string16> strings;
253 ElideRectangleText(adjusted_text, font_list, text_bounds.width(),
254 text_bounds.height(), wrap_behavior, &strings);
256 for (size_t i = 0; i < strings.size(); i++) {
257 Range range = StripAcceleratorChars(flags, &strings[i]);
258 UpdateRenderText(rect, strings[i], font_list, flags, color,
259 render_text.get());
260 int line_padding = 0;
261 if (line_height > 0)
262 line_padding = line_height - render_text->GetStringSize().height();
263 else
264 line_height = render_text->GetStringSize().height();
266 // TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357
267 #if !defined(OS_WIN)
268 if (i == 0) {
269 // TODO(msw|asvitkine): Support multi-line text with varied heights.
270 const int text_height = strings.size() * line_height - line_padding;
271 rect += Vector2d(0, (text_bounds.height() - text_height) / 2);
273 #endif
275 rect.set_height(line_height - line_padding);
277 if (range.IsValid())
278 render_text->ApplyStyle(UNDERLINE, true, range);
279 render_text->SetDisplayRect(rect);
280 render_text->Draw(this);
281 rect += Vector2d(0, line_height);
283 } else {
284 Range range = StripAcceleratorChars(flags, &adjusted_text);
285 bool elide_text = ((flags & NO_ELLIPSIS) == 0);
287 #if defined(OS_LINUX)
288 // On Linux, eliding really means fading the end of the string. But only
289 // for LTR text. RTL text is still elided (on the left) with "...".
290 if (elide_text) {
291 render_text->SetText(adjusted_text);
292 if (render_text->GetTextDirection() == base::i18n::LEFT_TO_RIGHT) {
293 render_text->SetElideBehavior(FADE_TAIL);
294 elide_text = false;
297 #endif
299 if (elide_text) {
300 ElideTextAndAdjustRange(font_list, text_bounds.width(), &adjusted_text,
301 &range);
304 UpdateRenderText(rect, adjusted_text, font_list, flags, color,
305 render_text.get());
307 const int text_height = render_text->GetStringSize().height();
308 rect += Vector2d(0, (text_bounds.height() - text_height) / 2);
309 rect.set_height(text_height);
310 render_text->SetDisplayRect(rect);
311 if (range.IsValid())
312 render_text->ApplyStyle(UNDERLINE, true, range);
313 render_text->Draw(this);
316 canvas_->restore();
319 void Canvas::DrawStringRectWithHalo(const base::string16& text,
320 const FontList& font_list,
321 SkColor text_color,
322 SkColor halo_color_in,
323 const Rect& display_rect,
324 int flags) {
325 // Some callers will have semitransparent halo colors, which we don't handle
326 // (since the resulting image can have 1-bit transparency only).
327 SkColor halo_color = SkColorSetA(halo_color_in, 0xFF);
329 // Create a temporary buffer filled with the halo color. It must leave room
330 // for the 1-pixel border around the text.
331 Size size(display_rect.width() + 2, display_rect.height() + 2);
332 Canvas text_canvas(size, image_scale(), false);
333 SkPaint bkgnd_paint;
334 bkgnd_paint.setColor(halo_color);
335 text_canvas.DrawRect(Rect(size), bkgnd_paint);
337 // Draw the text into the temporary buffer. This will have correct
338 // ClearType since the background color is the same as the halo color.
339 text_canvas.DrawStringRectWithFlags(
340 text, font_list, text_color,
341 Rect(1, 1, display_rect.width(), display_rect.height()), flags);
343 uint32_t halo_premul = SkPreMultiplyColor(halo_color);
344 SkBitmap& text_bitmap = const_cast<SkBitmap&>(
345 skia::GetTopDevice(*text_canvas.sk_canvas())->accessBitmap(true));
347 for (int cur_y = 0; cur_y < text_bitmap.height(); cur_y++) {
348 uint32_t* text_row = text_bitmap.getAddr32(0, cur_y);
349 for (int cur_x = 0; cur_x < text_bitmap.width(); cur_x++) {
350 if (text_row[cur_x] == halo_premul) {
351 // This pixel was not touched by the text routines. See if it borders
352 // a touched pixel in any of the 4 directions (not diagonally).
353 if (!PixelShouldGetHalo(text_bitmap, cur_x, cur_y, halo_premul))
354 text_row[cur_x] = 0; // Make transparent.
355 } else {
356 text_row[cur_x] |= 0xff << SK_A32_SHIFT; // Make opaque.
361 // Draw the halo bitmap with blur.
362 ImageSkia text_image = ImageSkia(ImageSkiaRep(text_bitmap,
363 text_canvas.image_scale()));
364 DrawImageInt(text_image, display_rect.x() - 1, display_rect.y() - 1);
367 void Canvas::DrawFadedString(const base::string16& text,
368 const FontList& font_list,
369 SkColor color,
370 const Rect& display_rect,
371 int flags) {
372 // If the whole string fits in the destination then just draw it directly.
373 if (GetStringWidth(text, font_list) <= display_rect.width()) {
374 DrawStringRectWithFlags(text, font_list, color, display_rect, flags);
375 return;
378 // Align with forced content directionality, overriding alignment flags.
379 if (flags & FORCE_RTL_DIRECTIONALITY) {
380 flags &= ~(TEXT_ALIGN_CENTER | TEXT_ALIGN_LEFT);
381 flags |= TEXT_ALIGN_RIGHT;
382 } else if (flags & FORCE_LTR_DIRECTIONALITY) {
383 flags &= ~(TEXT_ALIGN_CENTER | TEXT_ALIGN_RIGHT);
384 flags |= TEXT_ALIGN_LEFT;
385 } else if (!(flags & TEXT_ALIGN_LEFT) && !(flags & TEXT_ALIGN_RIGHT)) {
386 // Also align with content directionality instead of fading both ends.
387 flags &= ~TEXT_ALIGN_CENTER;
388 const bool is_rtl = base::i18n::GetFirstStrongCharacterDirection(text) ==
389 base::i18n::RIGHT_TO_LEFT;
390 flags |= is_rtl ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT;
392 flags |= NO_ELLIPSIS;
394 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
395 Rect rect = display_rect;
396 UpdateRenderText(rect, text, font_list, flags, color, render_text.get());
397 render_text->SetElideBehavior(FADE_TAIL);
399 const int line_height = render_text->GetStringSize().height();
400 rect += Vector2d(0, (display_rect.height() - line_height) / 2);
401 rect.set_height(line_height);
402 render_text->SetDisplayRect(rect);
404 canvas_->save();
405 ClipRect(display_rect);
406 render_text->Draw(this);
407 canvas_->restore();
410 } // namespace gfx