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/safe_integer_conversions.h"
12 #include "ui/gfx/insets.h"
13 #include "ui/gfx/range/range.h"
14 #include "ui/gfx/rect.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"
25 // If necessary, wraps |text| with RTL/LTR directionality characters based on
26 // |flags| and |text| content.
27 // Returns true if the text will be rendered right-to-left.
28 // TODO(msw): Nix this, now that RenderTextWin supports directionality directly.
29 bool AdjustStringDirection(int flags
, base::string16
* text
) {
30 // TODO(msw): FORCE_LTR_DIRECTIONALITY does not work for RTL text now.
32 // If the string is empty or LTR was forced, simply return false since the
33 // default RenderText directionality is already LTR.
34 if (text
->empty() || (flags
& Canvas::FORCE_LTR_DIRECTIONALITY
))
37 // If RTL is forced, apply it to the string.
38 if (flags
& Canvas::FORCE_RTL_DIRECTIONALITY
) {
39 base::i18n::WrapStringWithRTLFormatting(text
);
43 // If a direction wasn't forced but the UI language is RTL and there were
44 // strong RTL characters, ensure RTL is applied.
45 if (base::i18n::IsRTL() && base::i18n::StringContainsStrongRTLChars(*text
)) {
46 base::i18n::WrapStringWithRTLFormatting(text
);
50 // In the default case, the string should be rendered as LTR. RenderText's
51 // default directionality is LTR, so the text doesn't need to be wrapped.
52 // Note that individual runs within the string may still be rendered RTL
53 // (which will be the case for RTL text under non-RTL locales, since under RTL
54 // locales it will be handled by the if statement above).
57 #endif // defined(OS_WIN)
59 // Checks each pixel immediately adjacent to the given pixel in the bitmap. If
60 // any of them are not the halo color, returns true. This defines the halo of
61 // pixels that will appear around the text. Note that we have to check each
62 // pixel against both the halo color and transparent since
63 // |DrawStringRectWithHalo| will modify the bitmap as it goes, and cleared
64 // pixels shouldn't count as changed.
65 bool PixelShouldGetHalo(const SkBitmap
& bitmap
,
69 *bitmap
.getAddr32(x
- 1, y
) != halo_color
&&
70 *bitmap
.getAddr32(x
- 1, y
) != 0)
71 return true; // Touched pixel to the left.
72 if (x
< bitmap
.width() - 1 &&
73 *bitmap
.getAddr32(x
+ 1, y
) != halo_color
&&
74 *bitmap
.getAddr32(x
+ 1, y
) != 0)
75 return true; // Touched pixel to the right.
77 *bitmap
.getAddr32(x
, y
- 1) != halo_color
&&
78 *bitmap
.getAddr32(x
, y
- 1) != 0)
79 return true; // Touched pixel above.
80 if (y
< bitmap
.height() - 1 &&
81 *bitmap
.getAddr32(x
, y
+ 1) != halo_color
&&
82 *bitmap
.getAddr32(x
, y
+ 1) != 0)
83 return true; // Touched pixel below.
87 // Strips accelerator character prefixes in |text| if needed, based on |flags|.
88 // Returns a range in |text| to underline or Range::InvalidRange() if
89 // underlining is not needed.
90 Range
StripAcceleratorChars(int flags
, base::string16
* text
) {
91 if (flags
& (Canvas::SHOW_PREFIX
| Canvas::HIDE_PREFIX
)) {
94 *text
= RemoveAcceleratorChar(*text
, '&', &char_pos
, &char_span
);
95 if ((flags
& Canvas::SHOW_PREFIX
) && char_pos
!= -1)
96 return Range(char_pos
, char_pos
+ char_span
);
98 return Range::InvalidRange();
101 // Elides |text| and adjusts |range| appropriately. If eliding causes |range|
102 // to no longer point to the same character in |text|, |range| is made invalid.
103 void ElideTextAndAdjustRange(const FontList
& font_list
,
105 base::string16
* text
,
107 const base::char16 start_char
=
108 (range
->IsValid() ? text
->at(range
->start()) : 0);
109 *text
= ElideText(*text
, font_list
, width
, ELIDE_TAIL
);
110 if (!range
->IsValid())
112 if (range
->start() >= text
->length() ||
113 text
->at(range
->start()) != start_char
) {
114 *range
= Range::InvalidRange();
118 // Updates |render_text| from the specified parameters.
119 void UpdateRenderText(const Rect
& rect
,
120 const base::string16
& text
,
121 const FontList
& font_list
,
124 RenderText
* render_text
) {
125 render_text
->SetFontList(font_list
);
126 render_text
->SetText(text
);
127 render_text
->SetCursorEnabled(false);
128 render_text
->SetDisplayRect(rect
);
130 // Set the text alignment explicitly based on the directionality of the UI,
132 if (!(flags
& (Canvas::TEXT_ALIGN_CENTER
|
133 Canvas::TEXT_ALIGN_RIGHT
|
134 Canvas::TEXT_ALIGN_LEFT
))) {
135 flags
|= Canvas::DefaultCanvasTextAlignment();
138 if (flags
& Canvas::TEXT_ALIGN_RIGHT
)
139 render_text
->SetHorizontalAlignment(ALIGN_RIGHT
);
140 else if (flags
& Canvas::TEXT_ALIGN_CENTER
)
141 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
143 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
145 if (flags
& Canvas::NO_SUBPIXEL_RENDERING
)
146 render_text
->set_background_is_transparent(true);
148 render_text
->SetColor(color
);
149 const int font_style
= font_list
.GetFontStyle();
150 render_text
->SetStyle(BOLD
, (font_style
& Font::BOLD
) != 0);
151 render_text
->SetStyle(ITALIC
, (font_style
& Font::ITALIC
) != 0);
152 render_text
->SetStyle(UNDERLINE
, (font_style
& Font::UNDERLINE
) != 0);
158 void Canvas::SizeStringFloat(const base::string16
& text
,
159 const FontList
& font_list
,
160 float* width
, float* height
,
163 DCHECK_GE(*width
, 0);
164 DCHECK_GE(*height
, 0);
166 base::string16 adjusted_text
= text
;
168 AdjustStringDirection(flags
, &adjusted_text
);
171 if ((flags
& MULTI_LINE
) && *width
!= 0) {
172 WordWrapBehavior wrap_behavior
= TRUNCATE_LONG_WORDS
;
173 if (flags
& CHARACTER_BREAK
)
174 wrap_behavior
= WRAP_LONG_WORDS
;
175 else if (!(flags
& NO_ELLIPSIS
))
176 wrap_behavior
= ELIDE_LONG_WORDS
;
178 std::vector
<base::string16
> strings
;
179 ElideRectangleText(adjusted_text
, font_list
,
181 wrap_behavior
, &strings
);
182 Rect
rect(ClampToInt(*width
), INT_MAX
);
183 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
184 UpdateRenderText(rect
, base::string16(), font_list
, flags
, 0,
189 for (size_t i
= 0; i
< strings
.size(); ++i
) {
190 StripAcceleratorChars(flags
, &strings
[i
]);
191 render_text
->SetText(strings
[i
]);
192 const SizeF
& string_size
= render_text
->GetStringSizeF();
193 w
= std::max(w
, string_size
.width());
194 h
+= (i
> 0 && line_height
> 0) ?
195 std::max(static_cast<float>(line_height
), string_size
.height())
196 : string_size
.height();
201 // If the string is too long, the call by |RenderTextWin| to |ScriptShape()|
202 // will inexplicably fail with result E_INVALIDARG. Guard against this.
203 const size_t kMaxRenderTextLength
= 5000;
204 if (adjusted_text
.length() >= kMaxRenderTextLength
) {
205 *width
= static_cast<float>(
206 font_list
.GetExpectedTextWidth(adjusted_text
.length()));
207 *height
= static_cast<float>(font_list
.GetHeight());
209 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
210 Rect
rect(ClampToInt(*width
), ClampToInt(*height
));
211 StripAcceleratorChars(flags
, &adjusted_text
);
212 UpdateRenderText(rect
, adjusted_text
, font_list
, flags
, 0,
214 const SizeF
& string_size
= render_text
->GetStringSizeF();
215 *width
= string_size
.width();
216 *height
= string_size
.height();
221 void Canvas::DrawStringRectWithShadows(const base::string16
& text
,
222 const FontList
& font_list
,
224 const Rect
& text_bounds
,
227 const ShadowValues
& shadows
) {
228 if (!IntersectsClipRect(text_bounds
))
231 Rect
clip_rect(text_bounds
);
232 clip_rect
.Inset(ShadowValue::GetMargin(shadows
));
237 Rect
rect(text_bounds
);
238 base::string16 adjusted_text
= text
;
241 AdjustStringDirection(flags
, &adjusted_text
);
244 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
245 render_text
->set_shadows(shadows
);
247 if (flags
& MULTI_LINE
) {
248 WordWrapBehavior wrap_behavior
= IGNORE_LONG_WORDS
;
249 if (flags
& CHARACTER_BREAK
)
250 wrap_behavior
= WRAP_LONG_WORDS
;
251 else if (!(flags
& NO_ELLIPSIS
))
252 wrap_behavior
= ELIDE_LONG_WORDS
;
254 std::vector
<base::string16
> strings
;
255 ElideRectangleText(adjusted_text
, font_list
,
256 static_cast<float>(text_bounds
.width()),
257 text_bounds
.height(), wrap_behavior
, &strings
);
259 for (size_t i
= 0; i
< strings
.size(); i
++) {
260 Range range
= StripAcceleratorChars(flags
, &strings
[i
]);
261 UpdateRenderText(rect
, strings
[i
], font_list
, flags
, color
,
263 int line_padding
= 0;
265 line_padding
= line_height
- render_text
->GetStringSize().height();
267 line_height
= render_text
->GetStringSize().height();
269 // TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357
272 // TODO(msw|asvitkine): Support multi-line text with varied heights.
273 const int text_height
= strings
.size() * line_height
- line_padding
;
274 rect
+= Vector2d(0, (text_bounds
.height() - text_height
) / 2);
278 rect
.set_height(line_height
- line_padding
);
281 render_text
->ApplyStyle(UNDERLINE
, true, range
);
282 render_text
->SetDisplayRect(rect
);
283 render_text
->Draw(this);
284 rect
+= Vector2d(0, line_height
);
287 Range range
= StripAcceleratorChars(flags
, &adjusted_text
);
288 bool elide_text
= ((flags
& NO_ELLIPSIS
) == 0);
290 #if defined(OS_LINUX)
291 // On Linux, eliding really means fading the end of the string. But only
292 // for LTR text. RTL text is still elided (on the left) with "...".
294 render_text
->SetText(adjusted_text
);
295 if (render_text
->GetTextDirection() == base::i18n::LEFT_TO_RIGHT
) {
296 render_text
->SetElideBehavior(FADE_TAIL
);
303 ElideTextAndAdjustRange(font_list
,
304 static_cast<float>(text_bounds
.width()),
305 &adjusted_text
, &range
);
308 UpdateRenderText(rect
, adjusted_text
, font_list
, flags
, color
,
311 render_text
->ApplyStyle(UNDERLINE
, true, range
);
312 render_text
->Draw(this);
318 void Canvas::DrawStringRectWithHalo(const base::string16
& text
,
319 const FontList
& font_list
,
321 SkColor halo_color_in
,
322 const Rect
& display_rect
,
324 // Some callers will have semitransparent halo colors, which we don't handle
325 // (since the resulting image can have 1-bit transparency only).
326 SkColor halo_color
= SkColorSetA(halo_color_in
, 0xFF);
328 // Create a temporary buffer filled with the halo color. It must leave room
329 // for the 1-pixel border around the text.
330 Size
size(display_rect
.width() + 2, display_rect
.height() + 2);
331 Canvas
text_canvas(size
, image_scale(), false);
333 bkgnd_paint
.setColor(halo_color
);
334 text_canvas
.DrawRect(Rect(size
), bkgnd_paint
);
336 // Draw the text into the temporary buffer. This will have correct
337 // ClearType since the background color is the same as the halo color.
338 text_canvas
.DrawStringRectWithFlags(
339 text
, font_list
, text_color
,
340 Rect(1, 1, display_rect
.width(), display_rect
.height()), flags
);
342 uint32_t halo_premul
= SkPreMultiplyColor(halo_color
);
343 SkBitmap
& text_bitmap
= const_cast<SkBitmap
&>(
344 skia::GetTopDevice(*text_canvas
.sk_canvas())->accessBitmap(true));
346 for (int cur_y
= 0; cur_y
< text_bitmap
.height(); cur_y
++) {
347 uint32_t* text_row
= text_bitmap
.getAddr32(0, cur_y
);
348 for (int cur_x
= 0; cur_x
< text_bitmap
.width(); cur_x
++) {
349 if (text_row
[cur_x
] == halo_premul
) {
350 // This pixel was not touched by the text routines. See if it borders
351 // a touched pixel in any of the 4 directions (not diagonally).
352 if (!PixelShouldGetHalo(text_bitmap
, cur_x
, cur_y
, halo_premul
))
353 text_row
[cur_x
] = 0; // Make transparent.
355 text_row
[cur_x
] |= 0xff << SK_A32_SHIFT
; // Make opaque.
360 // Draw the halo bitmap with blur.
361 ImageSkia text_image
= ImageSkia(ImageSkiaRep(text_bitmap
,
362 text_canvas
.image_scale()));
363 DrawImageInt(text_image
, display_rect
.x() - 1, display_rect
.y() - 1);
366 void Canvas::DrawFadedString(const base::string16
& text
,
367 const FontList
& font_list
,
369 const Rect
& display_rect
,
371 // If the whole string fits in the destination then just draw it directly.
372 if (GetStringWidth(text
, font_list
) <= display_rect
.width()) {
373 DrawStringRectWithFlags(text
, font_list
, color
, display_rect
, flags
);
377 // Align with forced content directionality, overriding alignment flags.
378 if (flags
& FORCE_RTL_DIRECTIONALITY
) {
379 flags
&= ~(TEXT_ALIGN_CENTER
| TEXT_ALIGN_LEFT
);
380 flags
|= TEXT_ALIGN_RIGHT
;
381 } else if (flags
& FORCE_LTR_DIRECTIONALITY
) {
382 flags
&= ~(TEXT_ALIGN_CENTER
| TEXT_ALIGN_RIGHT
);
383 flags
|= TEXT_ALIGN_LEFT
;
384 } else if (!(flags
& TEXT_ALIGN_LEFT
) && !(flags
& TEXT_ALIGN_RIGHT
)) {
385 // Also align with content directionality instead of fading both ends.
386 flags
&= ~TEXT_ALIGN_CENTER
;
387 const bool is_rtl
= base::i18n::GetFirstStrongCharacterDirection(text
) ==
388 base::i18n::RIGHT_TO_LEFT
;
389 flags
|= is_rtl
? TEXT_ALIGN_RIGHT
: TEXT_ALIGN_LEFT
;
391 flags
|= NO_ELLIPSIS
;
393 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
394 Rect rect
= display_rect
;
395 UpdateRenderText(rect
, text
, font_list
, flags
, color
, render_text
.get());
396 render_text
->SetElideBehavior(FADE_TAIL
);
399 ClipRect(display_rect
);
400 render_text
->Draw(this);