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