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/render_text.h"
10 #include "base/i18n/break_iterator.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/icu/source/common/unicode/rbbi.h"
15 #include "third_party/icu/source/common/unicode/utf16.h"
16 #include "third_party/skia/include/core/SkTypeface.h"
17 #include "third_party/skia/include/effects/SkGradientShader.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/insets.h"
20 #include "ui/gfx/skia_util.h"
21 #include "ui/gfx/text_constants.h"
22 #include "ui/gfx/text_elider.h"
23 #include "ui/gfx/text_utils.h"
24 #include "ui/gfx/utf16_indexing.h"
30 // All chars are replaced by this char when the password style is set.
31 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*'
32 // that's available in the font (find_invisible_char() in gtkentry.c).
33 const base::char16 kPasswordReplacementChar = '*';
35 // Default color used for the text and cursor.
36 const SkColor kDefaultColor = SK_ColorBLACK;
38 // Default color used for drawing selection background.
39 const SkColor kDefaultSelectionBackgroundColor = SK_ColorGRAY;
41 // Fraction of the text size to lower a strike through below the baseline.
42 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21);
43 // Fraction of the text size to lower an underline below the baseline.
44 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9);
45 // Fraction of the text size to use for a strike through or under-line.
46 const SkScalar kLineThickness = (SK_Scalar1 / 18);
47 // Fraction of the text size to use for a top margin of a diagonal strike.
48 const SkScalar kDiagonalStrikeMarginOffset = (SK_Scalar1 / 4);
50 // Invalid value of baseline. Assigning this value to |baseline_| causes
51 // re-calculation of baseline.
52 const int kInvalidBaseline = INT_MAX;
54 // Returns the baseline, with which the text best appears vertically centered.
55 int DetermineBaselineCenteringText(const Rect& display_rect,
56 const FontList& font_list) {
57 const int display_height = display_rect.height();
58 const int font_height = font_list.GetHeight();
59 // Lower and upper bound of baseline shift as we try to show as much area of
60 // text as possible. In particular case of |display_height| == |font_height|,
61 // we do not want to shift the baseline.
62 const int min_shift = std::min(0, display_height - font_height);
63 const int max_shift = std::abs(display_height - font_height);
64 const int baseline = font_list.GetBaseline();
65 const int cap_height = font_list.GetCapHeight();
66 const int internal_leading = baseline - cap_height;
67 // Some platforms don't support getting the cap height, and simply return
68 // the entire font ascent from GetCapHeight(). Centering the ascent makes
69 // the font look too low, so if GetCapHeight() returns the ascent, center
70 // the entire font height instead.
72 display_height - ((internal_leading != 0) ? cap_height : font_height);
73 const int baseline_shift = space / 2 - internal_leading;
74 return baseline + std::max(min_shift, std::min(max_shift, baseline_shift));
77 // Converts |gfx::Font::FontStyle| flags to |SkTypeface::Style| flags.
78 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) {
79 int skia_style = SkTypeface::kNormal;
80 skia_style |= (font_style & gfx::Font::BOLD) ? SkTypeface::kBold : 0;
81 skia_style |= (font_style & gfx::Font::ITALIC) ? SkTypeface::kItalic : 0;
82 return static_cast<SkTypeface::Style>(skia_style);
85 // Given |font| and |display_width|, returns the width of the fade gradient.
86 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) {
87 // Fade in/out about 2.5 characters of the beginning/end of the string.
88 // The .5 here is helpful if one of the characters is a space.
89 // Use a quarter of the display width if the display width is very short.
90 const int average_character_width = font_list.GetExpectedTextWidth(1);
91 const double gradient_width = std::min(average_character_width * 2.5,
93 DCHECK_GE(gradient_width, 0.0);
94 return static_cast<int>(floor(gradient_width + 0.5));
97 // Appends to |positions| and |colors| values corresponding to the fade over
98 // |fade_rect| from color |c0| to color |c1|.
99 void AddFadeEffect(const Rect& text_rect,
100 const Rect& fade_rect,
103 std::vector<SkScalar>* positions,
104 std::vector<SkColor>* colors) {
105 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x());
106 const SkScalar width = static_cast<SkScalar>(fade_rect.width());
107 const SkScalar p0 = left / text_rect.width();
108 const SkScalar p1 = (left + width) / text_rect.width();
109 // Prepend 0.0 to |positions|, as required by Skia.
110 if (positions->empty() && p0 != 0.0) {
111 positions->push_back(0.0);
112 colors->push_back(c0);
114 positions->push_back(p0);
115 colors->push_back(c0);
116 positions->push_back(p1);
117 colors->push_back(c1);
120 // Creates a SkShader to fade the text, with |left_part| specifying the left
121 // fade effect, if any, and |right_part| specifying the right fade effect.
122 skia::RefPtr<SkShader> CreateFadeShader(const Rect& text_rect,
123 const Rect& left_part,
124 const Rect& right_part,
126 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color.
127 const SkColor fade_color = SkColorSetA(color, 51);
128 std::vector<SkScalar> positions;
129 std::vector<SkColor> colors;
131 if (!left_part.IsEmpty())
132 AddFadeEffect(text_rect, left_part, fade_color, color,
133 &positions, &colors);
134 if (!right_part.IsEmpty())
135 AddFadeEffect(text_rect, right_part, color, fade_color,
136 &positions, &colors);
137 DCHECK(!positions.empty());
139 // Terminate |positions| with 1.0, as required by Skia.
140 if (positions.back() != 1.0) {
141 positions.push_back(1.0);
142 colors.push_back(colors.back());
146 points[0].iset(text_rect.x(), text_rect.y());
147 points[1].iset(text_rect.right(), text_rect.y());
149 return skia::AdoptRef(
150 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0],
151 colors.size(), SkShader::kClamp_TileMode));
158 // Value of |underline_thickness_| that indicates that underline metrics have
159 // not been set explicitly.
160 const SkScalar kUnderlineMetricsNotSet = -1.0f;
162 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas)
163 : canvas_skia_(canvas->sk_canvas()),
164 started_drawing_(false),
165 underline_thickness_(kUnderlineMetricsNotSet),
166 underline_position_(0.0f) {
167 DCHECK(canvas_skia_);
168 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
169 paint_.setStyle(SkPaint::kFill_Style);
170 paint_.setAntiAlias(true);
171 paint_.setSubpixelText(true);
172 paint_.setLCDRenderText(true);
173 paint_.setHinting(SkPaint::kNormal_Hinting);
177 SkiaTextRenderer::~SkiaTextRenderer() {
178 // Work-around for http://crbug.com/122743, where non-ClearType text is
179 // rendered with incorrect gamma when using the fade shader. Draw the text
180 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode.
182 // TODO(asvitkine): Remove this work-around once the Skia bug is fixed.
183 // http://code.google.com/p/skia/issues/detail?id=590
184 if (deferred_fade_shader_.get()) {
185 paint_.setShader(deferred_fade_shader_.get());
186 paint_.setXfermodeMode(SkXfermode::kDstIn_Mode);
187 canvas_skia_->drawRect(bounds_, paint_);
188 canvas_skia_->restore();
192 void SkiaTextRenderer::SetDrawLooper(SkDrawLooper* draw_looper) {
193 paint_.setLooper(draw_looper);
196 void SkiaTextRenderer::SetFontSmoothingSettings(bool antialiasing,
197 bool subpixel_rendering,
198 bool subpixel_positioning) {
199 paint_.setAntiAlias(antialiasing);
200 paint_.setLCDRenderText(subpixel_rendering);
201 paint_.setSubpixelText(subpixel_positioning);
204 void SkiaTextRenderer::SetFontHinting(SkPaint::Hinting hinting) {
205 paint_.setHinting(hinting);
208 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) {
209 paint_.setTypeface(typeface);
212 void SkiaTextRenderer::SetTextSize(SkScalar size) {
213 paint_.setTextSize(size);
216 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family,
218 DCHECK(!family.empty());
220 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style);
221 skia::RefPtr<SkTypeface> typeface =
222 skia::AdoptRef(SkTypeface::CreateFromName(family.c_str(), skia_style));
224 // |paint_| adds its own ref. So don't |release()| it from the ref ptr here.
225 SetTypeface(typeface.get());
227 // Enable fake bold text if bold style is needed but new typeface does not
229 paint_.setFakeBoldText((skia_style & SkTypeface::kBold) &&
230 !typeface->isBold());
234 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) {
235 paint_.setColor(foreground);
238 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) {
239 bounds_ = RectToSkRect(bounds);
240 paint_.setShader(shader);
243 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness,
245 underline_thickness_ = thickness;
246 underline_position_ = position;
249 void SkiaTextRenderer::DrawPosText(const SkPoint* pos,
250 const uint16* glyphs,
251 size_t glyph_count) {
252 if (!started_drawing_) {
253 started_drawing_ = true;
254 // Work-around for http://crbug.com/122743, where non-ClearType text is
255 // rendered with incorrect gamma when using the fade shader. Draw the text
256 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode.
258 // Skip this when there is a looper which seems not working well with
259 // deferred paint. Currently a looper is only used for text shadows.
261 // TODO(asvitkine): Remove this work-around once the Skia bug is fixed.
262 // http://code.google.com/p/skia/issues/detail?id=590
263 if (!paint_.isLCDRenderText() &&
264 paint_.getShader() &&
265 !paint_.getLooper()) {
266 deferred_fade_shader_ = skia::SharePtr(paint_.getShader());
267 paint_.setShader(NULL);
268 canvas_skia_->saveLayer(&bounds_, NULL);
272 const size_t byte_length = glyph_count * sizeof(glyphs[0]);
273 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_);
276 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline,
277 bool strike, bool diagonal_strike) {
279 DrawUnderline(x, y, width);
281 DrawStrike(x, y, width);
283 DrawDiagonalStrike(x, y, width);
286 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) {
287 SkRect r = SkRect::MakeLTRB(x, y + underline_position_, x + width,
288 y + underline_position_ + underline_thickness_);
289 if (underline_thickness_ == kUnderlineMetricsNotSet) {
290 const SkScalar text_size = paint_.getTextSize();
291 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y);
292 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness);
294 canvas_skia_->drawRect(r, paint_);
297 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const {
298 const SkScalar text_size = paint_.getTextSize();
299 const SkScalar height = SkScalarMul(text_size, kLineThickness);
300 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y);
301 const SkRect r = SkRect::MakeLTRB(x, offset, x + width, offset + height);
302 canvas_skia_->drawRect(r, paint_);
305 void SkiaTextRenderer::DrawDiagonalStrike(int x, int y, int width) const {
306 const SkScalar text_size = paint_.getTextSize();
307 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset);
309 SkPaint paint(paint_);
310 paint.setAntiAlias(true);
311 paint.setStyle(SkPaint::kFill_Style);
312 paint.setStrokeWidth(SkScalarMul(text_size, kLineThickness) * 2);
313 canvas_skia_->drawLine(x, y, x + width, y - text_size + offset, paint);
316 StyleIterator::StyleIterator(const BreakList<SkColor>& colors,
317 const std::vector<BreakList<bool> >& styles)
320 color_ = colors_.breaks().begin();
321 for (size_t i = 0; i < styles_.size(); ++i)
322 style_.push_back(styles_[i].breaks().begin());
325 StyleIterator::~StyleIterator() {}
327 Range StyleIterator::GetRange() const {
328 Range range(colors_.GetRange(color_));
329 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i)
330 range = range.Intersect(styles_[i].GetRange(style_[i]));
334 void StyleIterator::UpdatePosition(size_t position) {
335 color_ = colors_.GetBreak(position);
336 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i)
337 style_[i] = styles_[i].GetBreak(position);
340 LineSegment::LineSegment() : run(0) {}
342 LineSegment::~LineSegment() {}
344 Line::Line() : preceding_heights(0), baseline(0) {}
348 } // namespace internal
350 RenderText::~RenderText() {
353 void RenderText::SetText(const base::string16& text) {
354 DCHECK(!composition_range_.IsValid());
359 // Adjust ranged styles and colors to accommodate a new text length.
360 const size_t text_length = text_.length();
361 colors_.SetMax(text_length);
362 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
363 styles_[style].SetMax(text_length);
364 cached_bounds_and_offset_valid_ = false;
366 // Reset selection model. SetText should always followed by SetSelectionModel
367 // or SetCursorPosition in upper layer.
368 SetSelectionModel(SelectionModel());
370 // Invalidate the cached text direction if it depends on the text contents.
371 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT)
372 text_direction_ = base::i18n::UNKNOWN_DIRECTION;
374 obscured_reveal_index_ = -1;
378 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
379 if (horizontal_alignment_ != alignment) {
380 horizontal_alignment_ = alignment;
381 display_offset_ = Vector2d();
382 cached_bounds_and_offset_valid_ = false;
386 void RenderText::SetFontList(const FontList& font_list) {
387 font_list_ = font_list;
388 baseline_ = kInvalidBaseline;
389 cached_bounds_and_offset_valid_ = false;
393 void RenderText::SetCursorEnabled(bool cursor_enabled) {
394 cursor_enabled_ = cursor_enabled;
395 cached_bounds_and_offset_valid_ = false;
398 void RenderText::ToggleInsertMode() {
399 insert_mode_ = !insert_mode_;
400 cached_bounds_and_offset_valid_ = false;
403 void RenderText::SetObscured(bool obscured) {
404 if (obscured != obscured_) {
405 obscured_ = obscured;
406 obscured_reveal_index_ = -1;
407 cached_bounds_and_offset_valid_ = false;
412 void RenderText::SetObscuredRevealIndex(int index) {
413 if (obscured_reveal_index_ == index)
416 obscured_reveal_index_ = index;
417 cached_bounds_and_offset_valid_ = false;
421 void RenderText::SetMultiline(bool multiline) {
422 if (multiline != multiline_) {
423 multiline_ = multiline;
424 cached_bounds_and_offset_valid_ = false;
429 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) {
430 // TODO(skanuj) : Add a test for triggering layout change.
431 if (elide_behavior_ != elide_behavior) {
432 elide_behavior_ = elide_behavior;
437 void RenderText::SetDisplayRect(const Rect& r) {
438 if (r != display_rect_) {
440 baseline_ = kInvalidBaseline;
441 cached_bounds_and_offset_valid_ = false;
443 if (elide_behavior_ != gfx::NO_ELIDE)
448 void RenderText::SetCursorPosition(size_t position) {
449 MoveCursorTo(position, false);
452 void RenderText::MoveCursor(BreakType break_type,
453 VisualCursorDirection direction,
455 SelectionModel position(cursor_position(), selection_model_.caret_affinity());
456 // Cancelling a selection moves to the edge of the selection.
457 if (break_type != LINE_BREAK && !selection().is_empty() && !select) {
458 SelectionModel selection_start = GetSelectionModelForSelectionStart();
459 int start_x = GetCursorBounds(selection_start, true).x();
460 int cursor_x = GetCursorBounds(position, true).x();
461 // Use the selection start if it is left (when |direction| is CURSOR_LEFT)
462 // or right (when |direction| is CURSOR_RIGHT) of the selection end.
463 if (direction == CURSOR_RIGHT ? start_x > cursor_x : start_x < cursor_x)
464 position = selection_start;
465 // For word breaks, use the nearest word boundary in the appropriate
467 if (break_type == WORD_BREAK)
468 position = GetAdjacentSelectionModel(position, break_type, direction);
470 position = GetAdjacentSelectionModel(position, break_type, direction);
473 position.set_selection_start(selection().start());
474 MoveCursorTo(position);
477 bool RenderText::MoveCursorTo(const SelectionModel& model) {
478 // Enforce valid selection model components.
479 size_t text_length = text().length();
480 Range range(std::min(model.selection().start(), text_length),
481 std::min(model.caret_pos(), text_length));
482 // The current model only supports caret positions at valid character indices.
483 if (!IsCursorablePosition(range.start()) ||
484 !IsCursorablePosition(range.end()))
486 SelectionModel sel(range, model.caret_affinity());
487 bool changed = sel != selection_model_;
488 SetSelectionModel(sel);
492 bool RenderText::MoveCursorTo(const Point& point, bool select) {
493 SelectionModel position = FindCursorPosition(point);
495 position.set_selection_start(selection().start());
496 return MoveCursorTo(position);
499 bool RenderText::SelectRange(const Range& range) {
500 Range sel(std::min(range.start(), text().length()),
501 std::min(range.end(), text().length()));
502 if (!IsCursorablePosition(sel.start()) || !IsCursorablePosition(sel.end()))
504 LogicalCursorDirection affinity =
505 (sel.is_reversed() || sel.is_empty()) ? CURSOR_FORWARD : CURSOR_BACKWARD;
506 SetSelectionModel(SelectionModel(sel, affinity));
510 bool RenderText::IsPointInSelection(const Point& point) {
511 if (selection().is_empty())
513 SelectionModel cursor = FindCursorPosition(point);
514 return RangeContainsCaret(
515 selection(), cursor.caret_pos(), cursor.caret_affinity());
518 void RenderText::ClearSelection() {
519 SetSelectionModel(SelectionModel(cursor_position(),
520 selection_model_.caret_affinity()));
523 void RenderText::SelectAll(bool reversed) {
524 const size_t length = text().length();
525 const Range all = reversed ? Range(length, 0) : Range(0, length);
526 const bool success = SelectRange(all);
530 void RenderText::SelectWord() {
536 size_t selection_max = selection().GetMax();
538 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD);
539 bool success = iter.Init();
544 size_t selection_min = selection().GetMin();
545 if (selection_min == text().length() && selection_min != 0)
548 for (; selection_min != 0; --selection_min) {
549 if (iter.IsStartOfWord(selection_min) ||
550 iter.IsEndOfWord(selection_min))
554 if (selection_min == selection_max && selection_max != text().length())
557 for (; selection_max < text().length(); ++selection_max)
558 if (iter.IsEndOfWord(selection_max) || iter.IsStartOfWord(selection_max))
561 const bool reversed = selection().is_reversed();
562 MoveCursorTo(reversed ? selection_max : selection_min, false);
563 MoveCursorTo(reversed ? selection_min : selection_max, true);
566 const Range& RenderText::GetCompositionRange() const {
567 return composition_range_;
570 void RenderText::SetCompositionRange(const Range& composition_range) {
571 CHECK(!composition_range.IsValid() ||
572 Range(0, text_.length()).Contains(composition_range));
573 composition_range_.set_end(composition_range.end());
574 composition_range_.set_start(composition_range.start());
578 void RenderText::SetColor(SkColor value) {
579 colors_.SetValue(value);
582 // TODO(msw): Windows applies colors and decorations in the layout process.
583 cached_bounds_and_offset_valid_ = false;
588 void RenderText::ApplyColor(SkColor value, const Range& range) {
589 colors_.ApplyValue(value, range);
592 // TODO(msw): Windows applies colors and decorations in the layout process.
593 cached_bounds_and_offset_valid_ = false;
598 void RenderText::SetStyle(TextStyle style, bool value) {
599 styles_[style].SetValue(value);
601 // Only invalidate the layout on font changes; not for colors or decorations.
602 bool invalidate = (style == BOLD) || (style == ITALIC);
604 // TODO(msw): Windows applies colors and decorations in the layout process.
608 cached_bounds_and_offset_valid_ = false;
613 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) {
614 styles_[style].ApplyValue(value, range);
616 // Only invalidate the layout on font changes; not for colors or decorations.
617 bool invalidate = (style == BOLD) || (style == ITALIC);
619 // TODO(msw): Windows applies colors and decorations in the layout process.
623 cached_bounds_and_offset_valid_ = false;
628 bool RenderText::GetStyle(TextStyle style) const {
629 return (styles_[style].breaks().size() == 1) &&
630 styles_[style].breaks().front().second;
633 void RenderText::SetDirectionalityMode(DirectionalityMode mode) {
634 if (mode == directionality_mode_)
637 directionality_mode_ = mode;
638 text_direction_ = base::i18n::UNKNOWN_DIRECTION;
639 cached_bounds_and_offset_valid_ = false;
643 base::i18n::TextDirection RenderText::GetTextDirection() {
644 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) {
645 switch (directionality_mode_) {
646 case DIRECTIONALITY_FROM_TEXT:
647 // Derive the direction from the display text, which differs from text()
648 // in the case of obscured (password) textfields.
650 base::i18n::GetFirstStrongCharacterDirection(GetLayoutText());
652 case DIRECTIONALITY_FROM_UI:
653 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT :
654 base::i18n::LEFT_TO_RIGHT;
656 case DIRECTIONALITY_FORCE_LTR:
657 text_direction_ = base::i18n::LEFT_TO_RIGHT;
659 case DIRECTIONALITY_FORCE_RTL:
660 text_direction_ = base::i18n::RIGHT_TO_LEFT;
667 return text_direction_;
670 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() {
671 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ?
672 CURSOR_RIGHT : CURSOR_LEFT;
675 SizeF RenderText::GetStringSizeF() {
676 const Size size = GetStringSize();
677 return SizeF(size.width(), size.height());
680 int RenderText::GetContentWidth() {
681 return GetStringSize().width() + (cursor_enabled_ ? 1 : 0);
684 int RenderText::GetBaseline() {
685 if (baseline_ == kInvalidBaseline)
686 baseline_ = DetermineBaselineCenteringText(display_rect(), font_list());
687 DCHECK_NE(kInvalidBaseline, baseline_);
691 void RenderText::Draw(Canvas* canvas) {
694 if (clip_to_display_rect()) {
695 Rect clip_rect(display_rect());
696 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_));
699 canvas->ClipRect(clip_rect);
702 if (!text().empty() && focused())
703 DrawSelection(canvas);
705 if (cursor_enabled() && cursor_visible() && focused())
706 DrawCursor(canvas, selection_model_);
709 DrawVisualText(canvas);
711 if (clip_to_display_rect())
715 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) {
716 // Paint cursor. Replace cursor is drawn as rectangle for now.
717 // TODO(msw): Draw a better cursor with a better indication of association.
718 canvas->FillRect(GetCursorBounds(position, true), cursor_color_);
721 void RenderText::DrawSelectedTextForDrag(Canvas* canvas) {
723 const std::vector<Rect> sel = GetSubstringBounds(selection());
725 // Override the selection color with black, and force the background to be
726 // transparent so that it's rendered without subpixel antialiasing.
727 const bool saved_background_is_transparent = background_is_transparent();
728 const SkColor saved_selection_color = selection_color();
729 set_background_is_transparent(true);
730 set_selection_color(SK_ColorBLACK);
732 for (size_t i = 0; i < sel.size(); ++i) {
734 canvas->ClipRect(sel[i]);
735 DrawVisualText(canvas);
739 // Restore saved transparency and selection color.
740 set_selection_color(saved_selection_color);
741 set_background_is_transparent(saved_background_is_transparent);
744 Rect RenderText::GetCursorBounds(const SelectionModel& caret,
746 // TODO(ckocagil): Support multiline. This function should return the height
747 // of the line the cursor is on. |GetStringSize()| now returns
748 // the multiline size, eliminate its use here.
752 size_t caret_pos = caret.caret_pos();
753 DCHECK(IsCursorablePosition(caret_pos));
754 // In overtype mode, ignore the affinity and always indicate that we will
755 // overtype the next character.
756 LogicalCursorDirection caret_affinity =
757 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD;
758 int x = 0, width = 1;
759 Size size = GetStringSize();
760 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) {
761 // The caret is attached to the boundary. Always return a 1-dip width caret,
762 // since there is nothing to overtype.
763 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0))
766 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ?
767 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD);
768 Range xspan(GetGlyphBounds(grapheme_start));
770 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start();
771 } else { // overtype mode
773 width = xspan.length();
776 return Rect(ToViewPoint(Point(x, 0)), Size(width, size.height()));
779 const Rect& RenderText::GetUpdatedCursorBounds() {
780 UpdateCachedBoundsAndOffset();
781 return cursor_bounds_;
784 size_t RenderText::IndexOfAdjacentGrapheme(size_t index,
785 LogicalCursorDirection direction) {
786 if (index > text().length())
787 return text().length();
791 if (direction == CURSOR_FORWARD) {
792 while (index < text().length()) {
794 if (IsCursorablePosition(index))
797 return text().length();
802 if (IsCursorablePosition(index))
808 SelectionModel RenderText::GetSelectionModelForSelectionStart() {
809 const Range& sel = selection();
811 return selection_model_;
812 return SelectionModel(sel.start(),
813 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD);
816 void RenderText::SetTextShadows(const ShadowValues& shadows) {
817 text_shadows_ = shadows;
820 RenderText::RenderText()
821 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT),
822 directionality_mode_(DIRECTIONALITY_FROM_TEXT),
823 text_direction_(base::i18n::UNKNOWN_DIRECTION),
824 cursor_enabled_(true),
825 cursor_visible_(false),
827 cursor_color_(kDefaultColor),
828 selection_color_(kDefaultColor),
829 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
831 composition_range_(Range::InvalidRange()),
832 colors_(kDefaultColor),
833 styles_(NUM_TEXT_STYLES),
834 composition_and_selection_styles_applied_(false),
836 obscured_reveal_index_(-1),
838 elide_behavior_(NO_ELIDE),
842 background_is_transparent_(false),
843 clip_to_display_rect_(true),
844 baseline_(kInvalidBaseline),
845 cached_bounds_and_offset_valid_(false) {
848 const Vector2d& RenderText::GetUpdatedDisplayOffset() {
849 UpdateCachedBoundsAndOffset();
850 return display_offset_;
853 SelectionModel RenderText::GetAdjacentSelectionModel(
854 const SelectionModel& current,
855 BreakType break_type,
856 VisualCursorDirection direction) {
859 if (break_type == LINE_BREAK || text().empty())
860 return EdgeSelectionModel(direction);
861 if (break_type == CHARACTER_BREAK)
862 return AdjacentCharSelectionModel(current, direction);
863 DCHECK(break_type == WORD_BREAK);
864 return AdjacentWordSelectionModel(current, direction);
867 SelectionModel RenderText::EdgeSelectionModel(
868 VisualCursorDirection direction) {
869 if (direction == GetVisualDirectionOfLogicalEnd())
870 return SelectionModel(text().length(), CURSOR_FORWARD);
871 return SelectionModel(0, CURSOR_BACKWARD);
874 void RenderText::SetSelectionModel(const SelectionModel& model) {
875 DCHECK_LE(model.selection().GetMax(), text().length());
876 selection_model_ = model;
877 cached_bounds_and_offset_valid_ = false;
880 const base::string16& RenderText::GetLayoutText() const {
881 return layout_text_.empty() ? text_ : layout_text_;
884 const BreakList<size_t>& RenderText::GetLineBreaks() {
885 if (line_breaks_.max() != 0)
888 const base::string16& layout_text = GetLayoutText();
889 const size_t text_length = layout_text.length();
890 line_breaks_.SetValue(0);
891 line_breaks_.SetMax(text_length);
892 base::i18n::BreakIterator iter(layout_text,
893 base::i18n::BreakIterator::BREAK_LINE);
894 const bool success = iter.Init();
898 line_breaks_.ApplyValue(iter.pos(), Range(iter.pos(), text_length));
899 } while (iter.Advance());
904 void RenderText::ApplyCompositionAndSelectionStyles() {
905 // Save the underline and color breaks to undo the temporary styles later.
906 DCHECK(!composition_and_selection_styles_applied_);
907 saved_colors_ = colors_;
908 saved_underlines_ = styles_[UNDERLINE];
910 // Apply an underline to the composition range in |underlines|.
911 if (composition_range_.IsValid() && !composition_range_.is_empty())
912 styles_[UNDERLINE].ApplyValue(true, composition_range_);
914 // Apply the selected text color to the [un-reversed] selection range.
915 if (!selection().is_empty() && focused()) {
916 const Range range(selection().GetMin(), selection().GetMax());
917 colors_.ApplyValue(selection_color_, range);
919 composition_and_selection_styles_applied_ = true;
922 void RenderText::UndoCompositionAndSelectionStyles() {
923 // Restore the underline and color breaks to undo the temporary styles.
924 DCHECK(composition_and_selection_styles_applied_);
925 colors_ = saved_colors_;
926 styles_[UNDERLINE] = saved_underlines_;
927 composition_and_selection_styles_applied_ = false;
930 Vector2d RenderText::GetLineOffset(size_t line_number) {
931 Vector2d offset = display_rect().OffsetFromOrigin();
932 // TODO(ckocagil): Apply the display offset for multiline scrolling.
934 offset.Add(GetUpdatedDisplayOffset());
936 offset.Add(Vector2d(0, lines_[line_number].preceding_heights));
937 offset.Add(GetAlignmentOffset(line_number));
941 Point RenderText::ToTextPoint(const Point& point) {
942 return point - GetLineOffset(0);
943 // TODO(ckocagil): Convert multiline view space points to text space.
946 Point RenderText::ToViewPoint(const Point& point) {
948 return point + GetLineOffset(0);
950 // TODO(ckocagil): Traverse individual line segments for RTL support.
951 DCHECK(!lines_.empty());
954 for (; line < lines_.size() && x > lines_[line].size.width(); ++line)
955 x -= lines_[line].size.width();
956 return Point(x, point.y()) + GetLineOffset(line);
959 std::vector<Rect> RenderText::TextBoundsToViewBounds(const Range& x) {
960 std::vector<Rect> rects;
963 rects.push_back(Rect(ToViewPoint(Point(x.GetMin(), 0)),
964 Size(x.length(), GetStringSize().height())));
970 // Each line segment keeps its position in text coordinates. Traverse all line
971 // segments and if the segment intersects with the given range, add the view
972 // rect corresponding to the intersection to |rects|.
973 for (size_t line = 0; line < lines_.size(); ++line) {
975 const Vector2d offset = GetLineOffset(line);
976 for (size_t i = 0; i < lines_[line].segments.size(); ++i) {
977 const internal::LineSegment* segment = &lines_[line].segments[i];
978 const Range intersection = segment->x_range.Intersect(x);
979 if (!intersection.is_empty()) {
980 Rect rect(line_x + intersection.start() - segment->x_range.start(),
981 0, intersection.length(), lines_[line].size.height());
982 rects.push_back(rect + offset);
984 line_x += segment->x_range.length();
991 Vector2d RenderText::GetAlignmentOffset(size_t line_number) {
992 // TODO(ckocagil): Enable |lines_| usage in other platforms.
994 DCHECK_LT(line_number, lines_.size());
997 if (horizontal_alignment_ != ALIGN_LEFT) {
999 const int width = lines_[line_number].size.width() +
1000 (cursor_enabled_ ? 1 : 0);
1002 const int width = GetContentWidth();
1004 offset.set_x(display_rect().width() - width);
1005 if (horizontal_alignment_ == ALIGN_CENTER)
1006 offset.set_x(offset.x() / 2);
1009 // Vertically center the text.
1011 const int text_height = lines_.back().preceding_heights +
1012 lines_.back().size.height();
1013 offset.set_y((display_rect_.height() - text_height) / 2);
1015 offset.set_y(GetBaseline() - GetLayoutTextBaseline());
1021 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
1022 if (multiline() || (!fade_head() && !fade_tail()))
1025 const int display_width = display_rect().width();
1027 // If the text fits as-is, no need to fade.
1028 if (GetStringSize().width() <= display_width)
1031 int gradient_width = CalculateFadeGradientWidth(font_list(), display_width);
1032 if (gradient_width == 0)
1035 bool fade_left = fade_head();
1036 bool fade_right = fade_tail();
1037 // Under RTL, |fade_right| == |fade_head|.
1038 // TODO(asvitkine): This is currently not based on GetTextDirection() because
1039 // RenderTextWin does not return a direction that's based on
1040 // the text content.
1041 if (horizontal_alignment_ == ALIGN_RIGHT)
1042 std::swap(fade_left, fade_right);
1044 Rect solid_part = display_rect();
1048 left_part = solid_part;
1049 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0);
1050 solid_part.Inset(gradient_width, 0, 0, 0);
1053 right_part = solid_part;
1054 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
1055 solid_part.Inset(0, 0, gradient_width, 0);
1058 Rect text_rect = display_rect();
1059 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0);
1061 // TODO(msw): Use the actual text colors corresponding to each faded part.
1062 skia::RefPtr<SkShader> shader = CreateFadeShader(
1063 text_rect, left_part, right_part, colors_.breaks().front().second);
1065 renderer->SetShader(shader.get(), display_rect());
1068 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
1069 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_);
1070 renderer->SetDrawLooper(looper.get());
1074 bool RenderText::RangeContainsCaret(const Range& range,
1076 LogicalCursorDirection caret_affinity) {
1077 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9).
1078 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ?
1079 caret_pos - 1 : caret_pos + 1;
1080 return range.Contains(Range(caret_pos, adjacent));
1083 void RenderText::MoveCursorTo(size_t position, bool select) {
1084 size_t cursor = std::min(position, text().length());
1085 if (IsCursorablePosition(cursor))
1086 SetSelectionModel(SelectionModel(
1087 Range(select ? selection().start() : cursor, cursor),
1088 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD));
1091 void RenderText::UpdateLayoutText() {
1092 layout_text_.clear();
1093 line_breaks_.SetMax(0);
1096 size_t obscured_text_length =
1097 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length()));
1098 layout_text_.assign(obscured_text_length, kPasswordReplacementChar);
1100 if (obscured_reveal_index_ >= 0 &&
1101 obscured_reveal_index_ < static_cast<int>(text_.length())) {
1102 // Gets the index range in |text_| to be revealed.
1103 size_t start = obscured_reveal_index_;
1104 U16_SET_CP_START(text_.data(), 0, start);
1106 UChar32 unused_char;
1107 U16_NEXT(text_.data(), end, text_.length(), unused_char);
1109 // Gets the index in |layout_text_| to be replaced.
1110 const size_t cp_start =
1111 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, start));
1112 if (layout_text_.length() > cp_start)
1113 layout_text_.replace(cp_start, 1, text_.substr(start, end - start));
1117 const base::string16& text = GetLayoutText();
1118 if (truncate_length_ > 0 && truncate_length_ < text.length()) {
1119 // Truncate the text at a valid character break and append an ellipsis.
1120 icu::StringCharacterIterator iter(text.c_str());
1121 iter.setIndex32(truncate_length_ - 1);
1122 layout_text_.assign(text.substr(0, iter.getIndex()) + gfx::kEllipsisUTF16);
1125 if (elide_behavior_ != NO_ELIDE && display_rect_.width() > 0 &&
1126 !GetLayoutText().empty() && GetContentWidth() > display_rect_.width()) {
1127 base::string16 elided_text = ElideText(GetLayoutText());
1129 // This doesn't trim styles so ellipsis may get rendered as a different
1130 // style than the preceding text. See crbug.com/327850.
1131 layout_text_.assign(elided_text);
1136 // TODO(skanuj): Fix code duplication with ElideText in ui/gfx/text_elider.cc
1137 // See crbug.com/327846
1138 base::string16 RenderText::ElideText(const base::string16& text) {
1139 const bool insert_ellipsis = (elide_behavior_ != TRUNCATE_AT_END);
1140 // Create a RenderText copy with attributes that affect the rendering width.
1141 scoped_ptr<RenderText> render_text(CreateInstance());
1142 render_text->SetFontList(font_list_);
1143 render_text->SetDirectionalityMode(directionality_mode_);
1144 render_text->SetCursorEnabled(cursor_enabled_);
1146 render_text->styles_ = styles_;
1147 render_text->colors_ = colors_;
1148 render_text->SetText(text);
1149 const int current_text_pixel_width = render_text->GetContentWidth();
1151 const base::string16 ellipsis = base::string16(gfx::kEllipsisUTF16);
1152 const bool elide_in_middle = false;
1153 const bool elide_at_beginning = false;
1154 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning);
1156 // Pango will return 0 width for absurdly long strings. Cut the string in
1157 // half and try again.
1158 // This is caused by an int overflow in Pango (specifically, in
1159 // pango_glyph_string_extents_range). It's actually more subtle than just
1160 // returning 0, since on super absurdly long strings, the int can wrap and
1161 // return positive numbers again. Detecting that is probably not worth it
1162 // (eliding way too much from a ridiculous string is probably still
1163 // ridiculous), but we should check other widths for bogus values as well.
1164 if (current_text_pixel_width <= 0 && !text.empty())
1165 return ElideText(slicer.CutString(text.length() / 2, insert_ellipsis));
1167 if (current_text_pixel_width <= display_rect_.width())
1170 render_text->SetText(base::string16());
1171 render_text->SetText(ellipsis);
1172 const int ellipsis_width = render_text->GetContentWidth();
1174 if (insert_ellipsis && (ellipsis_width >= display_rect_.width()))
1175 return base::string16();
1177 // Use binary search to compute the elided text.
1179 size_t hi = text.length() - 1;
1180 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) {
1181 // Restore styles and colors. They will be truncated to size by SetText.
1182 render_text->styles_ = styles_;
1183 render_text->colors_ = colors_;
1184 base::string16 new_text = slicer.CutString(guess, false);
1185 render_text->SetText(new_text);
1187 // This has to be an additional step so that the ellipsis is rendered with
1188 // same style as trailing part of the text.
1189 if (insert_ellipsis) {
1190 // When ellipsis follows text whose directionality is not the same as that
1191 // of the whole text, it will be rendered with the directionality of the
1192 // whole text. Since we want ellipsis to indicate continuation of the
1193 // preceding text, we force the directionality of ellipsis to be same as
1194 // the preceding text using LTR or RTL markers.
1195 base::i18n::TextDirection leading_text_direction =
1196 base::i18n::GetFirstStrongCharacterDirection(new_text);
1197 base::i18n::TextDirection trailing_text_direction =
1198 base::i18n::GetLastStrongCharacterDirection(new_text);
1199 new_text.append(ellipsis);
1200 if (trailing_text_direction != leading_text_direction) {
1201 if (trailing_text_direction == base::i18n::LEFT_TO_RIGHT)
1202 new_text += base::i18n::kLeftToRightMark;
1204 new_text += base::i18n::kRightToLeftMark;
1206 render_text->SetText(new_text);
1209 // We check the width of the whole desired string at once to ensure we
1210 // handle kerning/ligatures/etc. correctly.
1211 const int guess_width = render_text->GetContentWidth();
1212 if (guess_width == display_rect_.width())
1214 if (guess_width > display_rect_.width()) {
1216 // Move back if we are on loop terminating condition, and guess is wider
1225 return render_text->text();
1228 void RenderText::UpdateCachedBoundsAndOffset() {
1229 if (cached_bounds_and_offset_valid_)
1232 // TODO(ckocagil): Add support for scrolling multiline text.
1234 // First, set the valid flag true to calculate the current cursor bounds using
1235 // the stale |display_offset_|. Applying |delta_offset| at the end of this
1236 // function will set |cursor_bounds_| and |display_offset_| to correct values.
1237 cached_bounds_and_offset_valid_ = true;
1238 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_);
1240 // Update |display_offset_| to ensure the current cursor is visible.
1241 const int display_width = display_rect_.width();
1242 const int content_width = GetContentWidth();
1245 if (content_width <= display_width || !cursor_enabled()) {
1246 // Don't pan if the text fits in the display width or when the cursor is
1248 delta_x = -display_offset_.x();
1249 } else if (cursor_bounds_.right() > display_rect_.right()) {
1250 // TODO(xji): when the character overflow is a RTL character, currently, if
1251 // we pan cursor at the rightmost position, the entered RTL character is not
1252 // displayed. Should pan cursor to show the last logical characters.
1254 // Pan to show the cursor when it overflows to the right.
1255 delta_x = display_rect_.right() - cursor_bounds_.right();
1256 } else if (cursor_bounds_.x() < display_rect_.x()) {
1257 // TODO(xji): have similar problem as above when overflow character is a
1260 // Pan to show the cursor when it overflows to the left.
1261 delta_x = display_rect_.x() - cursor_bounds_.x();
1262 } else if (display_offset_.x() != 0) {
1263 // Reduce the pan offset to show additional overflow text when the display
1265 const int negate_rtl = horizontal_alignment_ == ALIGN_RIGHT ? -1 : 1;
1266 const int offset = negate_rtl * display_offset_.x();
1267 if (display_width > (content_width + offset)) {
1268 delta_x = negate_rtl * (display_width - (content_width + offset));
1272 Vector2d delta_offset(delta_x, 0);
1273 display_offset_ += delta_offset;
1274 cursor_bounds_ += delta_offset;
1277 void RenderText::DrawSelection(Canvas* canvas) {
1278 const std::vector<Rect> sel = GetSubstringBounds(selection());
1279 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
1280 canvas->FillRect(*i, selection_background_focused_color_);