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