Support float widths in RenderTextHarfBuzz.
[chromium-blink-merge.git] / ui / gfx / render_text.h
blobe5a17a1d1189a4d72876b1189141e83f21756abc
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 #ifndef UI_GFX_RENDER_TEXT_H_
6 #define UI_GFX_RENDER_TEXT_H_
8 #include <algorithm>
9 #include <cstring>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/gtest_prod_util.h"
15 #include "base/i18n/rtl.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string16.h"
18 #include "skia/ext/refptr.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "third_party/skia/include/core/SkPaint.h"
21 #include "third_party/skia/include/core/SkRect.h"
22 #include "ui/gfx/break_list.h"
23 #include "ui/gfx/font_list.h"
24 #include "ui/gfx/font_render_params.h"
25 #include "ui/gfx/point.h"
26 #include "ui/gfx/range/range.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/gfx/selection_model.h"
29 #include "ui/gfx/shadow_value.h"
30 #include "ui/gfx/size_f.h"
31 #include "ui/gfx/text_constants.h"
32 #include "ui/gfx/vector2d.h"
34 class SkCanvas;
35 class SkDrawLooper;
36 struct SkPoint;
37 class SkShader;
38 class SkTypeface;
40 namespace gfx {
42 class Canvas;
43 class Font;
44 class RenderTextTest;
46 namespace internal {
48 // Internal helper class used by derived classes to draw text through Skia.
49 class SkiaTextRenderer {
50 public:
51 explicit SkiaTextRenderer(Canvas* canvas);
52 ~SkiaTextRenderer();
54 void SetDrawLooper(SkDrawLooper* draw_looper);
55 void SetFontRenderParams(const FontRenderParams& params,
56 bool background_is_transparent);
57 void SetTypeface(SkTypeface* typeface);
58 void SetTextSize(SkScalar size);
59 void SetFontFamilyWithStyle(const std::string& family, int font_style);
60 void SetForegroundColor(SkColor foreground);
61 void SetShader(SkShader* shader, const Rect& bounds);
62 // Sets underline metrics to use if the text will be drawn with an underline.
63 // If not set, default values based on the size of the text will be used. The
64 // two metrics must be set together.
65 void SetUnderlineMetrics(SkScalar thickness, SkScalar position);
66 void DrawSelection(const std::vector<Rect>& selection, SkColor color);
67 void DrawPosText(const SkPoint* pos,
68 const uint16* glyphs,
69 size_t glyph_count);
70 // Draw underline and strike-through text decorations.
71 // Based on |SkCanvas::DrawTextDecorations()| and constants from:
72 // third_party/skia/src/core/SkTextFormatParams.h
73 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
74 bool diagonal_strike);
75 // Finishes any ongoing diagonal strike run.
76 void EndDiagonalStrike();
77 void DrawUnderline(int x, int y, int width);
78 void DrawStrike(int x, int y, int width) const;
80 private:
81 // Helper class to draw a diagonal line with multiple pieces of different
82 // lengths and colors; to support text selection appearances.
83 class DiagonalStrike {
84 public:
85 DiagonalStrike(Canvas* canvas, Point start, const SkPaint& paint);
86 ~DiagonalStrike();
88 void AddPiece(int length, SkColor color);
89 void Draw();
91 private:
92 typedef std::pair<int, SkColor> Piece;
94 Canvas* canvas_;
95 const Point start_;
96 SkPaint paint_;
97 int total_length_;
98 std::vector<Piece> pieces_;
100 DISALLOW_COPY_AND_ASSIGN(DiagonalStrike);
103 Canvas* canvas_;
104 SkCanvas* canvas_skia_;
105 bool started_drawing_;
106 SkPaint paint_;
107 SkRect bounds_;
108 skia::RefPtr<SkShader> deferred_fade_shader_;
109 SkScalar underline_thickness_;
110 SkScalar underline_position_;
111 scoped_ptr<DiagonalStrike> diagonal_;
113 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer);
116 // Internal helper class used by derived classes to iterate colors and styles.
117 class StyleIterator {
118 public:
119 StyleIterator(const BreakList<SkColor>& colors,
120 const std::vector<BreakList<bool> >& styles);
121 ~StyleIterator();
123 // Get the colors and styles at the current iterator position.
124 SkColor color() const { return color_->second; }
125 bool style(TextStyle s) const { return style_[s]->second; }
127 // Get the intersecting range of the current iterator set.
128 Range GetRange() const;
130 // Update the iterator to point to colors and styles applicable at |position|.
131 void UpdatePosition(size_t position);
133 private:
134 BreakList<SkColor> colors_;
135 std::vector<BreakList<bool> > styles_;
137 BreakList<SkColor>::const_iterator color_;
138 std::vector<BreakList<bool>::const_iterator> style_;
140 DISALLOW_COPY_AND_ASSIGN(StyleIterator);
143 // Line segments are slices of the layout text to be rendered on a single line.
144 struct LineSegment {
145 LineSegment();
146 ~LineSegment();
148 // X coordinates of this line segment in text space.
149 Range x_range;
151 // The character range this segment corresponds to.
152 Range char_range;
154 // Index of the text run that generated this segment.
155 size_t run;
158 // A line of layout text, comprised of a line segment list and some metrics.
159 struct Line {
160 Line();
161 ~Line();
163 // Segments that make up this line in visual order.
164 std::vector<LineSegment> segments;
166 // The sum of segment widths and the maximum of segment heights.
167 SizeF size;
169 // Sum of preceding lines' heights.
170 int preceding_heights;
172 // Maximum baseline of all segments on this line.
173 int baseline;
176 // Creates an SkTypeface from a font |family| name and a |gfx::Font::FontStyle|.
177 // May return NULL.
178 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const std::string& family,
179 int style);
181 } // namespace internal
183 // RenderText represents an abstract model of styled text and its corresponding
184 // visual layout. Support is built in for a cursor, a selection, simple styling,
185 // complex scripts, and bi-directional text. Implementations provide mechanisms
186 // for rendering and translation between logical and visual data.
187 class GFX_EXPORT RenderText {
188 public:
189 virtual ~RenderText();
191 // Creates a platform-specific or cross-platform RenderText instance.
192 static RenderText* CreateInstance();
194 const base::string16& text() const { return text_; }
195 void SetText(const base::string16& text);
197 HorizontalAlignment horizontal_alignment() const {
198 return horizontal_alignment_;
200 void SetHorizontalAlignment(HorizontalAlignment alignment);
202 const FontList& font_list() const { return font_list_; }
203 void SetFontList(const FontList& font_list);
205 bool cursor_enabled() const { return cursor_enabled_; }
206 void SetCursorEnabled(bool cursor_enabled);
208 bool cursor_visible() const { return cursor_visible_; }
209 void set_cursor_visible(bool visible) { cursor_visible_ = visible; }
211 bool insert_mode() const { return insert_mode_; }
212 void ToggleInsertMode();
214 SkColor cursor_color() const { return cursor_color_; }
215 void set_cursor_color(SkColor color) { cursor_color_ = color; }
217 SkColor selection_color() const { return selection_color_; }
218 void set_selection_color(SkColor color) { selection_color_ = color; }
220 SkColor selection_background_focused_color() const {
221 return selection_background_focused_color_;
223 void set_selection_background_focused_color(SkColor color) {
224 selection_background_focused_color_ = color;
227 bool focused() const { return focused_; }
228 void set_focused(bool focused) { focused_ = focused; }
230 bool clip_to_display_rect() const { return clip_to_display_rect_; }
231 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; }
233 // In an obscured (password) field, all text is drawn as asterisks or bullets.
234 bool obscured() const { return obscured_; }
235 void SetObscured(bool obscured);
237 // Makes a char in obscured text at |index| to be revealed. |index| should be
238 // a UTF16 text index. If there is a previous revealed index, the previous one
239 // is cleared and only the last set index will be revealed. If |index| is -1
240 // or out of range, no char will be revealed. The revealed index is also
241 // cleared when SetText or SetObscured is called.
242 void SetObscuredRevealIndex(int index);
244 // Set whether newline characters should be replaced with newline symbols.
245 void SetReplaceNewlineCharsWithSymbols(bool replace);
247 // TODO(ckocagil): Multiline text rendering is currently only supported on
248 // Windows. Support other platforms.
249 bool multiline() const { return multiline_; }
250 void SetMultiline(bool multiline);
252 // Set the maximum length of the displayed layout text, not the actual text.
253 // A |length| of 0 forgoes a hard limit, but does not guarantee proper
254 // functionality of very long strings. Applies to subsequent SetText calls.
255 // WARNING: Only use this for system limits, it lacks complex text support.
256 void set_truncate_length(size_t length) { truncate_length_ = length; }
258 // The layout text will be elided to fit |display_rect| using this behavior.
259 // The layout text may be shortened further by the truncate length.
260 void SetElideBehavior(ElideBehavior elide_behavior);
261 ElideBehavior elide_behavior() const { return elide_behavior_; }
263 const base::string16& layout_text() const { return layout_text_; }
265 const Rect& display_rect() const { return display_rect_; }
266 void SetDisplayRect(const Rect& r);
268 bool background_is_transparent() const { return background_is_transparent_; }
269 void set_background_is_transparent(bool transparent) {
270 background_is_transparent_ = transparent;
273 const SelectionModel& selection_model() const { return selection_model_; }
275 const Range& selection() const { return selection_model_.selection(); }
277 size_t cursor_position() const { return selection_model_.caret_pos(); }
278 void SetCursorPosition(size_t position);
280 // Moves the cursor left or right. Cursor movement is visual, meaning that
281 // left and right are relative to screen, not the directionality of the text.
282 // If |select| is false, the selection start is moved to the same position.
283 void MoveCursor(BreakType break_type,
284 VisualCursorDirection direction,
285 bool select);
287 // Set the selection_model_ to the value of |selection|.
288 // The selection range is clamped to text().length() if out of range.
289 // Returns true if the cursor position or selection range changed.
290 // If any index in |selection_model| is not a cursorable position (not on a
291 // grapheme boundary), it is a no-op and returns false.
292 bool MoveCursorTo(const SelectionModel& selection_model);
294 // Set the selection_model_ based on |range|.
295 // If the |range| start or end is greater than text length, it is modified
296 // to be the text length.
297 // If the |range| start or end is not a cursorable position (not on grapheme
298 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
299 bool SelectRange(const Range& range);
301 // Returns true if the local point is over selected text.
302 bool IsPointInSelection(const Point& point);
304 // Selects no text, keeping the current cursor position and caret affinity.
305 void ClearSelection();
307 // Select the entire text range. If |reversed| is true, the range will end at
308 // the logical beginning of the text; this generally shows the leading portion
309 // of text that overflows its display area.
310 void SelectAll(bool reversed);
312 // Selects the word at the current cursor position. If there is a non-empty
313 // selection, the selection bounds are extended to their nearest word
314 // boundaries.
315 void SelectWord();
317 const Range& GetCompositionRange() const;
318 void SetCompositionRange(const Range& composition_range);
320 // Set the text color over the entire text or a logical character range.
321 // The |range| should be valid, non-reversed, and within [0, text().length()].
322 void SetColor(SkColor value);
323 void ApplyColor(SkColor value, const Range& range);
325 // Set various text styles over the entire text or a logical character range.
326 // The respective |style| is applied if |value| is true, or removed if false.
327 // The |range| should be valid, non-reversed, and within [0, text().length()].
328 void SetStyle(TextStyle style, bool value);
329 void ApplyStyle(TextStyle style, bool value, const Range& range);
331 // Returns whether this style is enabled consistently across the entire
332 // RenderText.
333 bool GetStyle(TextStyle style) const;
335 // Set or get the text directionality mode and get the text direction yielded.
336 void SetDirectionalityMode(DirectionalityMode mode);
337 DirectionalityMode directionality_mode() const {
338 return directionality_mode_;
340 base::i18n::TextDirection GetTextDirection();
342 // Returns the visual movement direction corresponding to the logical end
343 // of the text, considering only the dominant direction returned by
344 // |GetTextDirection()|, not the direction of a particular run.
345 VisualCursorDirection GetVisualDirectionOfLogicalEnd();
347 // Returns the size required to display the current string (which is the
348 // wrapped size in multiline mode). The returned size does not include space
349 // reserved for the cursor or the offset text shadows.
350 virtual Size GetStringSize() = 0;
352 // This is same as GetStringSize except that fractional size is returned.
353 // The default implementation is same as GetStringSize. Certain platforms that
354 // compute the text size as floating-point values, like Mac, will override
355 // this method.
356 // See comment in Canvas::GetStringWidthF for its usage.
357 virtual SizeF GetStringSizeF();
359 // Returns the width of the content (which is the wrapped width in multiline
360 // mode). Reserves room for the cursor if |cursor_enabled_| is true.
361 float GetContentWidth();
363 // Returns the common baseline of the text. The return value is the vertical
364 // offset from the top of |display_rect_| to the text baseline, in pixels.
365 // The baseline is determined from the font list and display rect, and does
366 // not depend on the text.
367 int GetBaseline();
369 void Draw(Canvas* canvas);
371 // Draws a cursor at |position|.
372 void DrawCursor(Canvas* canvas, const SelectionModel& position);
374 // Gets the SelectionModel from a visual point in local coordinates.
375 virtual SelectionModel FindCursorPosition(const Point& point) = 0;
377 // Returns true if the position is a valid logical index into text(), and is
378 // also a valid grapheme boundary, which may be used as a cursor position.
379 virtual bool IsValidCursorIndex(size_t index) = 0;
381 // Returns true if the position is a valid logical index into text(). Indices
382 // amid multi-character graphemes are allowed here, unlike IsValidCursorIndex.
383 virtual bool IsValidLogicalIndex(size_t index);
385 // Get the visual bounds of a cursor at |caret|. These bounds typically
386 // represent a vertical line if |insert_mode| is true. Pass false for
387 // |insert_mode| to retrieve the bounds of the associated glyph. These bounds
388 // are in local coordinates, but may be outside the visible region if the text
389 // is longer than the textfield. Subsequent text, cursor, or bounds changes
390 // may invalidate returned values. Note that |caret| must be placed at
391 // grapheme boundary, i.e. caret.caret_pos() must be a cursorable position.
392 Rect GetCursorBounds(const SelectionModel& caret, bool insert_mode);
394 // Compute the current cursor bounds, panning the text to show the cursor in
395 // the display rect if necessary. These bounds are in local coordinates.
396 // Subsequent text, cursor, or bounds changes may invalidate returned values.
397 const Rect& GetUpdatedCursorBounds();
399 // Given an |index| in text(), return the next or previous grapheme boundary
400 // in logical order (i.e. the nearest cursorable index). The return value is
401 // in the range 0 to text().length() inclusive (the input is clamped if it is
402 // out of that range). Always moves by at least one character index unless the
403 // supplied index is already at the boundary of the string.
404 size_t IndexOfAdjacentGrapheme(size_t index,
405 LogicalCursorDirection direction);
407 // Return a SelectionModel with the cursor at the current selection's start.
408 // The returned value represents a cursor/caret position without a selection.
409 SelectionModel GetSelectionModelForSelectionStart();
411 // Sets shadows to drawn with text.
412 void set_shadows(const ShadowValues& shadows) { shadows_ = shadows; }
413 const ShadowValues& shadows() { return shadows_; }
415 typedef std::pair<Font, Range> FontSpan;
416 // For testing purposes, returns which fonts were chosen for which parts of
417 // the text by returning a vector of Font and Range pairs, where each range
418 // specifies the character range for which the corresponding font has been
419 // chosen.
420 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
422 // Gets the horizontal bounds (relative to the left of the text, not the view)
423 // of the glyph starting at |index|. If the glyph is RTL then the returned
424 // Range will have is_reversed() true. (This does not return a Rect because a
425 // Rect can't have a negative width.)
426 virtual Range GetGlyphBounds(size_t index) = 0;
428 const Vector2d& GetUpdatedDisplayOffset();
429 void SetDisplayOffset(int horizontal_offset);
431 protected:
432 RenderText();
434 const BreakList<SkColor>& colors() const { return colors_; }
435 const std::vector<BreakList<bool> >& styles() const { return styles_; }
437 const std::vector<internal::Line>& lines() const { return lines_; }
438 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); }
440 // Returns the baseline of the current text. The return value depends on
441 // the text and its layout while the return value of GetBaseline() doesn't.
442 // GetAlignmentOffset() takes into account the difference between them.
444 // We'd like a RenderText to show the text always on the same baseline
445 // regardless of the text, so the text does not jump up or down depending
446 // on the text. However, underlying layout engines return different baselines
447 // depending on the text. In general, layout engines determine the minimum
448 // bounding box for the text and return the baseline from the top of the
449 // bounding box. So the baseline changes depending on font metrics used to
450 // layout the text.
452 // For example, suppose there are FontA and FontB and the baseline of FontA
453 // is smaller than the one of FontB. If the text is laid out only with FontA,
454 // then the baseline of FontA may be returned. If the text includes some
455 // characters which are laid out with FontB, then the baseline of FontB may
456 // be returned.
458 // GetBaseline() returns the fixed baseline regardless of the text.
459 // GetLayoutTextBaseline() returns the baseline determined by the underlying
460 // layout engine, and it changes depending on the text. GetAlignmentOffset()
461 // returns the difference between them.
462 virtual int GetLayoutTextBaseline() = 0;
464 void set_cached_bounds_and_offset_valid(bool valid) {
465 cached_bounds_and_offset_valid_ = valid;
468 // Get the selection model that visually neighbors |position| by |break_type|.
469 // The returned value represents a cursor/caret position without a selection.
470 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current,
471 BreakType break_type,
472 VisualCursorDirection direction);
474 // Get the selection model visually left/right of |selection| by one grapheme.
475 // The returned value represents a cursor/caret position without a selection.
476 virtual SelectionModel AdjacentCharSelectionModel(
477 const SelectionModel& selection,
478 VisualCursorDirection direction) = 0;
480 // Get the selection model visually left/right of |selection| by one word.
481 // The returned value represents a cursor/caret position without a selection.
482 virtual SelectionModel AdjacentWordSelectionModel(
483 const SelectionModel& selection,
484 VisualCursorDirection direction) = 0;
486 // Get the SelectionModels corresponding to visual text ends.
487 // The returned value represents a cursor/caret position without a selection.
488 SelectionModel EdgeSelectionModel(VisualCursorDirection direction);
490 // Sets the selection model, the argument is assumed to be valid.
491 virtual void SetSelectionModel(const SelectionModel& model);
493 // Get the visual bounds containing the logical substring within the |range|.
494 // If |range| is empty, the result is empty. These bounds could be visually
495 // discontinuous if the substring is split by a LTR/RTL level change.
496 // These bounds are in local coordinates, but may be outside the visible
497 // region if the text is longer than the textfield. Subsequent text, cursor,
498 // or bounds changes may invalidate returned values.
499 virtual std::vector<Rect> GetSubstringBounds(const Range& range) = 0;
501 // Convert between indices into |text_| and indices into |obscured_text_|,
502 // which differ when the text is obscured. Regardless of whether or not the
503 // text is obscured, the character (code point) offsets always match.
504 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0;
505 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0;
507 // Reset the layout to be invalid.
508 virtual void ResetLayout() = 0;
510 // Ensure the text is laid out, lines are computed, and |lines_| is valid.
511 virtual void EnsureLayout() = 0;
513 // Draw the text.
514 virtual void DrawVisualText(Canvas* canvas) = 0;
516 // Returns the text used for layout, which may be obscured or truncated.
517 const base::string16& GetLayoutText() const;
519 // Returns layout text positions that are suitable for breaking lines.
520 const BreakList<size_t>& GetLineBreaks();
522 // Apply (and undo) temporary composition underlines and selection colors.
523 void ApplyCompositionAndSelectionStyles();
524 void UndoCompositionAndSelectionStyles();
526 // Returns the line offset from the origin after applying the text alignment
527 // and the display offset.
528 Vector2d GetLineOffset(size_t line_number);
530 // Convert points from the text space to the view space and back. Handles the
531 // display area, display offset, application LTR/RTL mode and multiline.
532 Point ToTextPoint(const Point& point);
533 Point ToViewPoint(const Point& point);
535 // Convert a text space x-coordinate range to rects in view space.
536 std::vector<Rect> TextBoundsToViewBounds(const Range& x);
538 // Get the alignment, resolving ALIGN_TO_HEAD with the current text direction.
539 HorizontalAlignment GetCurrentHorizontalAlignment();
541 // Returns the line offset from the origin, accounts for text alignment only.
542 Vector2d GetAlignmentOffset(size_t line_number);
544 // Applies fade effects to |renderer|.
545 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
547 // Applies text shadows to |renderer|.
548 void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
550 // A convenience function to check whether the glyph attached to the caret
551 // is within the given range.
552 static bool RangeContainsCaret(const Range& range,
553 size_t caret_pos,
554 LogicalCursorDirection caret_affinity);
556 private:
557 friend class RenderTextTest;
558 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle);
559 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle);
560 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle);
561 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
562 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText);
563 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedText);
564 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedObscuredText);
565 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText);
566 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText);
567 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
568 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
569 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset);
570 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL);
571 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth);
572 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
573 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_SufficientWidth);
574 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_Newline);
575 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GlyphBounds);
576 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_GlyphBounds);
577 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
578 MoveCursorLeftRight_MeiryoUILigatures);
579 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Win_LogicalClusters);
580 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SameFontForParentheses);
581 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, BreakRunsByUnicodeBlocks);
583 // Creates a platform-specific RenderText instance.
584 static RenderText* CreateNativeInstance();
586 // Set the cursor to |position|, with the caret trailing the previous
587 // grapheme, or if there is no previous grapheme, leading the cursor position.
588 // If |select| is false, the selection start is moved to the same position.
589 // If the |position| is not a cursorable position (not on grapheme boundary),
590 // it is a NO-OP.
591 void MoveCursorTo(size_t position, bool select);
593 // Updates |layout_text_| if the text is obscured or truncated.
594 void UpdateLayoutText();
596 // Elides |text| as needed to fit in the |available_width| using |behavior|.
597 base::string16 Elide(const base::string16& text,
598 float available_width,
599 ElideBehavior behavior);
601 // Elides |email| as needed to fit the |available_width|.
602 base::string16 ElideEmail(const base::string16& email, float available_width);
604 // Update the cached bounds and display offset to ensure that the current
605 // cursor is within the visible display area.
606 void UpdateCachedBoundsAndOffset();
608 // Draw the selection.
609 void DrawSelection(Canvas* canvas);
611 // Logical UTF-16 string data to be drawn.
612 base::string16 text_;
614 // Horizontal alignment of the text with respect to |display_rect_|. The
615 // default is to align left if the application UI is LTR and right if RTL.
616 HorizontalAlignment horizontal_alignment_;
618 // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
619 DirectionalityMode directionality_mode_;
621 // The cached text direction, potentially computed from the text or UI locale.
622 // Use GetTextDirection(), do not use this potentially invalid value directly!
623 base::i18n::TextDirection text_direction_;
625 // A list of fonts used to render |text_|.
626 FontList font_list_;
628 // Logical selection range and visual cursor position.
629 SelectionModel selection_model_;
631 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
632 Rect cursor_bounds_;
634 // Specifies whether the cursor is enabled. If disabled, no space is reserved
635 // for the cursor when positioning text.
636 bool cursor_enabled_;
638 // The cursor visibility and insert mode.
639 bool cursor_visible_;
640 bool insert_mode_;
642 // The color used for the cursor.
643 SkColor cursor_color_;
645 // The color used for drawing selected text.
646 SkColor selection_color_;
648 // The background color used for drawing the selection when focused.
649 SkColor selection_background_focused_color_;
651 // The focus state of the text.
652 bool focused_;
654 // Composition text range.
655 Range composition_range_;
657 // Color and style breaks, used to color and stylize ranges of text.
658 // BreakList positions are stored with text indices, not layout indices.
659 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
660 BreakList<SkColor> colors_;
661 std::vector<BreakList<bool> > styles_;
663 // Breaks saved without temporary composition and selection styling.
664 BreakList<SkColor> saved_colors_;
665 BreakList<bool> saved_underlines_;
666 bool composition_and_selection_styles_applied_;
668 // A flag to obscure actual text with asterisks for password fields.
669 bool obscured_;
670 // The index at which the char should be revealed in the obscured text.
671 int obscured_reveal_index_;
673 // The maximum length of text to display, 0 forgoes a hard limit.
674 size_t truncate_length_;
676 // The behavior for eliding, fading, or truncating.
677 ElideBehavior elide_behavior_;
679 // The obscured and/or truncated text that will be displayed.
680 base::string16 layout_text_;
682 // Whether newline characters should be replaced with newline symbols.
683 bool replace_newline_chars_with_symbols_;
685 // Whether the text should be broken into multiple lines. Uses the width of
686 // |display_rect_| as the width cap.
687 bool multiline_;
689 // Is the background transparent (either partially or fully)?
690 bool background_is_transparent_;
692 // The local display area for rendering the text.
693 Rect display_rect_;
695 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
696 // that results in incorrect clipping when drawing to the document margins.
697 // This field allows disabling clipping to work around the issue.
698 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
699 bool clip_to_display_rect_;
701 // The offset for the text to be drawn, relative to the display area.
702 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
703 Vector2d display_offset_;
705 // The baseline of the text. This is determined from the height of the
706 // display area and the cap height of the font list so the text is vertically
707 // centered.
708 int baseline_;
710 // The cached bounds and offset are invalidated by changes to the cursor,
711 // selection, font, and other operations that adjust the visible text bounds.
712 bool cached_bounds_and_offset_valid_;
714 // Text shadows to be drawn.
715 ShadowValues shadows_;
717 // A list of valid layout text line break positions.
718 BreakList<size_t> line_breaks_;
720 // Lines computed by EnsureLayout. These should be invalidated with
721 // ResetLayout and on |display_rect_| changes.
722 std::vector<internal::Line> lines_;
724 DISALLOW_COPY_AND_ASSIGN(RenderText);
727 } // namespace gfx
729 #endif // UI_GFX_RENDER_TEXT_H_