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/geometry/insets.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/safe_integer_conversions.h"
14 #include "ui/gfx/range/range.h"
15 #include "ui/gfx/render_text.h"
16 #include "ui/gfx/shadow_value.h"
17 #include "ui/gfx/text_elider.h"
18 #include "ui/gfx/text_utils.h"
24 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If
25 // any of them are not the halo color, returns true. This defines the halo of
26 // pixels that will appear around the text. Note that we have to check each
27 // pixel against both the halo color and transparent since
28 // |DrawStringRectWithHalo| will modify the bitmap as it goes, and cleared
29 // pixels shouldn't count as changed.
30 bool PixelShouldGetHalo(const SkBitmap
& bitmap
,
34 *bitmap
.getAddr32(x
- 1, y
) != halo_color
&&
35 *bitmap
.getAddr32(x
- 1, y
) != 0)
36 return true; // Touched pixel to the left.
37 if (x
< bitmap
.width() - 1 &&
38 *bitmap
.getAddr32(x
+ 1, y
) != halo_color
&&
39 *bitmap
.getAddr32(x
+ 1, y
) != 0)
40 return true; // Touched pixel to the right.
42 *bitmap
.getAddr32(x
, y
- 1) != halo_color
&&
43 *bitmap
.getAddr32(x
, y
- 1) != 0)
44 return true; // Touched pixel above.
45 if (y
< bitmap
.height() - 1 &&
46 *bitmap
.getAddr32(x
, y
+ 1) != halo_color
&&
47 *bitmap
.getAddr32(x
, y
+ 1) != 0)
48 return true; // Touched pixel below.
52 // Strips accelerator character prefixes in |text| if needed, based on |flags|.
53 // Returns a range in |text| to underline or Range::InvalidRange() if
54 // underlining is not needed.
55 Range
StripAcceleratorChars(int flags
, base::string16
* text
) {
56 if (flags
& (Canvas::SHOW_PREFIX
| Canvas::HIDE_PREFIX
)) {
59 *text
= RemoveAcceleratorChar(*text
, '&', &char_pos
, &char_span
);
60 if ((flags
& Canvas::SHOW_PREFIX
) && char_pos
!= -1)
61 return Range(char_pos
, char_pos
+ char_span
);
63 return Range::InvalidRange();
66 // Elides |text| and adjusts |range| appropriately. If eliding causes |range|
67 // to no longer point to the same character in |text|, |range| is made invalid.
68 void ElideTextAndAdjustRange(const FontList
& font_list
,
72 const base::char16 start_char
=
73 (range
->IsValid() ? text
->at(range
->start()) : 0);
74 *text
= ElideText(*text
, font_list
, width
, ELIDE_TAIL
);
75 if (!range
->IsValid())
77 if (range
->start() >= text
->length() ||
78 text
->at(range
->start()) != start_char
) {
79 *range
= Range::InvalidRange();
83 // Updates |render_text| from the specified parameters.
84 void UpdateRenderText(const Rect
& rect
,
85 const base::string16
& text
,
86 const FontList
& font_list
,
89 RenderText
* render_text
) {
90 render_text
->SetFontList(font_list
);
91 render_text
->SetText(text
);
92 render_text
->SetCursorEnabled(false);
93 render_text
->SetDisplayRect(rect
);
95 // Set the text alignment explicitly based on the directionality of the UI,
97 if (!(flags
& (Canvas::TEXT_ALIGN_CENTER
|
98 Canvas::TEXT_ALIGN_RIGHT
|
99 Canvas::TEXT_ALIGN_LEFT
|
100 Canvas::TEXT_ALIGN_TO_HEAD
))) {
101 flags
|= Canvas::DefaultCanvasTextAlignment();
104 if (flags
& Canvas::TEXT_ALIGN_TO_HEAD
)
105 render_text
->SetHorizontalAlignment(ALIGN_TO_HEAD
);
106 else if (flags
& Canvas::TEXT_ALIGN_RIGHT
)
107 render_text
->SetHorizontalAlignment(ALIGN_RIGHT
);
108 else if (flags
& Canvas::TEXT_ALIGN_CENTER
)
109 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
111 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
113 render_text
->set_subpixel_rendering_suppressed(
114 (flags
& Canvas::NO_SUBPIXEL_RENDERING
) != 0);
116 render_text
->SetColor(color
);
117 const int font_style
= font_list
.GetFontStyle();
118 render_text
->SetStyle(BOLD
, (font_style
& Font::BOLD
) != 0);
119 render_text
->SetStyle(ITALIC
, (font_style
& Font::ITALIC
) != 0);
120 render_text
->SetStyle(UNDERLINE
, (font_style
& Font::UNDERLINE
) != 0);
126 void Canvas::SizeStringFloat(const base::string16
& text
,
127 const FontList
& font_list
,
128 float* width
, float* height
,
131 DCHECK_GE(*width
, 0);
132 DCHECK_GE(*height
, 0);
134 if ((flags
& MULTI_LINE
) && *width
!= 0) {
135 WordWrapBehavior wrap_behavior
= TRUNCATE_LONG_WORDS
;
136 if (flags
& CHARACTER_BREAK
)
137 wrap_behavior
= WRAP_LONG_WORDS
;
138 else if (!(flags
& NO_ELLIPSIS
))
139 wrap_behavior
= ELIDE_LONG_WORDS
;
141 std::vector
<base::string16
> strings
;
142 ElideRectangleText(text
, font_list
, *width
, INT_MAX
, wrap_behavior
,
144 Rect
rect(ClampToInt(*width
), INT_MAX
);
145 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
146 UpdateRenderText(rect
, base::string16(), font_list
, flags
, 0,
151 for (size_t i
= 0; i
< strings
.size(); ++i
) {
152 StripAcceleratorChars(flags
, &strings
[i
]);
153 render_text
->SetText(strings
[i
]);
154 const SizeF
& string_size
= render_text
->GetStringSizeF();
155 w
= std::max(w
, string_size
.width());
156 h
+= (i
> 0 && line_height
> 0) ?
157 std::max(static_cast<float>(line_height
), string_size
.height())
158 : string_size
.height();
163 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
164 Rect
rect(ClampToInt(*width
), ClampToInt(*height
));
165 base::string16 adjusted_text
= text
;
166 StripAcceleratorChars(flags
, &adjusted_text
);
167 UpdateRenderText(rect
, adjusted_text
, font_list
, flags
, 0,
169 const SizeF
& string_size
= render_text
->GetStringSizeF();
170 *width
= string_size
.width();
171 *height
= string_size
.height();
175 void Canvas::DrawStringRectWithShadows(const base::string16
& text
,
176 const FontList
& font_list
,
178 const Rect
& text_bounds
,
181 const ShadowValues
& shadows
) {
182 if (!IntersectsClipRect(text_bounds
))
185 Rect
clip_rect(text_bounds
);
186 clip_rect
.Inset(ShadowValue::GetMargin(shadows
));
191 Rect
rect(text_bounds
);
193 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
194 render_text
->set_shadows(shadows
);
196 if (flags
& MULTI_LINE
) {
197 WordWrapBehavior wrap_behavior
= IGNORE_LONG_WORDS
;
198 if (flags
& CHARACTER_BREAK
)
199 wrap_behavior
= WRAP_LONG_WORDS
;
200 else if (!(flags
& NO_ELLIPSIS
))
201 wrap_behavior
= ELIDE_LONG_WORDS
;
203 std::vector
<base::string16
> strings
;
204 ElideRectangleText(text
, font_list
,
205 static_cast<float>(text_bounds
.width()),
206 text_bounds
.height(), wrap_behavior
, &strings
);
208 for (size_t i
= 0; i
< strings
.size(); i
++) {
209 Range range
= StripAcceleratorChars(flags
, &strings
[i
]);
210 UpdateRenderText(rect
, strings
[i
], font_list
, flags
, color
,
212 int line_padding
= 0;
214 line_padding
= line_height
- render_text
->GetStringSize().height();
216 line_height
= render_text
->GetStringSize().height();
218 // TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357
221 // TODO(msw|asvitkine): Support multi-line text with varied heights.
222 const int text_height
= strings
.size() * line_height
- line_padding
;
223 rect
+= Vector2d(0, (text_bounds
.height() - text_height
) / 2);
227 rect
.set_height(line_height
- line_padding
);
230 render_text
->ApplyStyle(UNDERLINE
, true, range
);
231 render_text
->SetDisplayRect(rect
);
232 render_text
->Draw(this);
233 rect
+= Vector2d(0, line_height
);
236 base::string16 adjusted_text
= text
;
237 Range range
= StripAcceleratorChars(flags
, &adjusted_text
);
238 bool elide_text
= ((flags
& NO_ELLIPSIS
) == 0);
240 #if defined(OS_LINUX)
241 // On Linux, eliding really means fading the end of the string. But only
242 // for LTR text. RTL text is still elided (on the left) with "...".
244 render_text
->SetText(adjusted_text
);
245 if (render_text
->GetDisplayTextDirection() == base::i18n::LEFT_TO_RIGHT
) {
246 render_text
->SetElideBehavior(FADE_TAIL
);
253 ElideTextAndAdjustRange(font_list
,
254 static_cast<float>(text_bounds
.width()),
255 &adjusted_text
, &range
);
258 UpdateRenderText(rect
, adjusted_text
, font_list
, flags
, color
,
261 render_text
->ApplyStyle(UNDERLINE
, true, range
);
262 render_text
->Draw(this);
268 void Canvas::DrawStringRectWithHalo(const base::string16
& text
,
269 const FontList
& font_list
,
271 SkColor halo_color_in
,
272 const Rect
& display_rect
,
274 // Some callers will have semitransparent halo colors, which we don't handle
275 // (since the resulting image can have 1-bit transparency only).
276 SkColor halo_color
= SkColorSetA(halo_color_in
, 0xFF);
278 // Create a temporary buffer filled with the halo color. It must leave room
279 // for the 1-pixel border around the text.
280 Size
size(display_rect
.width() + 2, display_rect
.height() + 2);
281 Canvas
text_canvas(size
, image_scale(), false);
283 bkgnd_paint
.setColor(halo_color
);
284 text_canvas
.DrawRect(Rect(size
), bkgnd_paint
);
286 // Draw the text into the temporary buffer. This will have correct
287 // ClearType since the background color is the same as the halo color.
288 text_canvas
.DrawStringRectWithFlags(
289 text
, font_list
, text_color
,
290 Rect(1, 1, display_rect
.width(), display_rect
.height()), flags
);
292 uint32_t halo_premul
= SkPreMultiplyColor(halo_color
);
293 SkBitmap
& text_bitmap
= const_cast<SkBitmap
&>(
294 skia::GetTopDevice(*text_canvas
.sk_canvas())->accessBitmap(true));
296 for (int cur_y
= 0; cur_y
< text_bitmap
.height(); cur_y
++) {
297 uint32_t* text_row
= text_bitmap
.getAddr32(0, cur_y
);
298 for (int cur_x
= 0; cur_x
< text_bitmap
.width(); cur_x
++) {
299 if (text_row
[cur_x
] == halo_premul
) {
300 // This pixel was not touched by the text routines. See if it borders
301 // a touched pixel in any of the 4 directions (not diagonally).
302 if (!PixelShouldGetHalo(text_bitmap
, cur_x
, cur_y
, halo_premul
))
303 text_row
[cur_x
] = 0; // Make transparent.
305 text_row
[cur_x
] |= 0xff << SK_A32_SHIFT
; // Make opaque.
310 // Draw the halo bitmap with blur.
311 ImageSkia text_image
= ImageSkia(ImageSkiaRep(text_bitmap
,
312 text_canvas
.image_scale()));
313 DrawImageInt(text_image
, display_rect
.x() - 1, display_rect
.y() - 1);
316 void Canvas::DrawFadedString(const base::string16
& text
,
317 const FontList
& font_list
,
319 const Rect
& display_rect
,
321 // If the whole string fits in the destination then just draw it directly.
322 if (GetStringWidth(text
, font_list
) <= display_rect
.width()) {
323 DrawStringRectWithFlags(text
, font_list
, color
, display_rect
, flags
);
326 // Align with content directionality instead of fading both ends.
327 flags
&= ~TEXT_ALIGN_CENTER
;
328 if (!(flags
& (TEXT_ALIGN_LEFT
| TEXT_ALIGN_RIGHT
)))
329 flags
|= TEXT_ALIGN_TO_HEAD
;
330 flags
|= NO_ELLIPSIS
;
332 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
333 Rect rect
= display_rect
;
334 UpdateRenderText(rect
, text
, font_list
, flags
, color
, render_text
.get());
335 render_text
->SetElideBehavior(FADE_TAIL
);
338 ClipRect(display_rect
);
339 render_text
->Draw(this);