- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / render_text.h
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 the text directionality mode and get the text direction yielded.
311   void SetDirectionalityMode(DirectionalityMode mode);
312   base::i18n::TextDirection GetTextDirection();
313
314   // Returns the visual movement direction corresponding to the logical end
315   // of the text, considering only the dominant direction returned by
316   // |GetTextDirection()|, not the direction of a particular run.
317   VisualCursorDirection GetVisualDirectionOfLogicalEnd();
318
319   // Returns the size required to display the current string (which is the
320   // wrapped size in multiline mode). Note that this returns the raw size of the
321   // string, which does not include the cursor or the margin area of text
322   // shadows.
323   virtual Size GetStringSize() = 0;
324
325   // This is same as GetStringSize except that fractional size is returned.
326   // The default implementation is same as GetStringSize. Certain platforms that
327   // compute the text size as floating-point values, like Mac, will override
328   // this method.
329   // See comment in Canvas::GetStringWidthF for its usage.
330   virtual SizeF GetStringSizeF();
331
332   // Returns the width of the content (which is the wrapped width in multiline
333   // mode). Reserves room for the cursor if |cursor_enabled_| is true.
334   int GetContentWidth();
335
336   // Returns the common baseline of the text. The return value is the vertical
337   // offset from the top of |display_rect_| to the text baseline, in pixels.
338   // The baseline is determined from the font list and display rect, and does
339   // not depend on the text.
340   int GetBaseline();
341
342   void Draw(Canvas* canvas);
343
344   // Draws a cursor at |position|.
345   void DrawCursor(Canvas* canvas, const SelectionModel& position);
346
347   // Draw the selected text without a cursor or selection highlight. Subpixel
348   // antialiasing is disabled and foreground color is forced to black.
349   void DrawSelectedTextForDrag(Canvas* canvas);
350
351   // Gets the SelectionModel from a visual point in local coordinates.
352   virtual SelectionModel FindCursorPosition(const Point& point) = 0;
353
354   // Get the visual bounds of a cursor at |selection|. These bounds typically
355   // represent a vertical line, but if |insert_mode| is true they contain the
356   // bounds of the associated glyph. These bounds are in local coordinates, but
357   // may be outside the visible region if the text is longer than the textfield.
358   // Subsequent text, cursor, or bounds changes may invalidate returned values.
359   Rect GetCursorBounds(const SelectionModel& selection, bool insert_mode);
360
361   // Compute the current cursor bounds, panning the text to show the cursor in
362   // the display rect if necessary. These bounds are in local coordinates.
363   // Subsequent text, cursor, or bounds changes may invalidate returned values.
364   const Rect& GetUpdatedCursorBounds();
365
366   // Given an |index| in text(), return the next or previous grapheme boundary
367   // in logical order (that is, the nearest index for which
368   // |IsCursorablePosition(index)| returns true). The return value is in the
369   // range 0 to text().length() inclusive (the input is clamped if it is out of
370   // that range). Always moves by at least one character index unless the
371   // supplied index is already at the boundary of the string.
372   size_t IndexOfAdjacentGrapheme(size_t index,
373                                  LogicalCursorDirection direction);
374
375   // Return a SelectionModel with the cursor at the current selection's start.
376   // The returned value represents a cursor/caret position without a selection.
377   SelectionModel GetSelectionModelForSelectionStart();
378
379   // Sets shadows to drawn with text.
380   void SetTextShadows(const ShadowValues& shadows);
381
382   typedef std::pair<Font, Range> FontSpan;
383   // For testing purposes, returns which fonts were chosen for which parts of
384   // the text by returning a vector of Font and Range pairs, where each range
385   // specifies the character range for which the corresponding font has been
386   // chosen.
387   virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
388
389  protected:
390   RenderText();
391
392   const BreakList<SkColor>& colors() const { return colors_; }
393   const std::vector<BreakList<bool> >& styles() const { return styles_; }
394
395   const std::vector<internal::Line>& lines() const { return lines_; }
396   void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); }
397
398   // Returns the baseline of the current text.  The return value depends on
399   // the text and its layout while the return value of GetBaseline() doesn't.
400   // GetAlignmentOffset() takes into account the difference between them.
401   //
402   // We'd like a RenderText to show the text always on the same baseline
403   // regardless of the text, so the text does not jump up or down depending
404   // on the text.  However, underlying layout engines return different baselines
405   // depending on the text.  In general, layout engines determine the minimum
406   // bounding box for the text and return the baseline from the top of the
407   // bounding box.  So the baseline changes depending on font metrics used to
408   // layout the text.
409   //
410   // For example, suppose there are FontA and FontB and the baseline of FontA
411   // is smaller than the one of FontB.  If the text is laid out only with FontA,
412   // then the baseline of FontA may be returned.  If the text includes some
413   // characters which are laid out with FontB, then the baseline of FontB may
414   // be returned.
415   //
416   // GetBaseline() returns the fixed baseline regardless of the text.
417   // GetLayoutTextBaseline() returns the baseline determined by the underlying
418   // layout engine, and it changes depending on the text.  GetAlignmentOffset()
419   // returns the difference between them.
420   virtual int GetLayoutTextBaseline() = 0;
421
422   const Vector2d& GetUpdatedDisplayOffset();
423
424   void set_cached_bounds_and_offset_valid(bool valid) {
425     cached_bounds_and_offset_valid_ = valid;
426   }
427
428   // Get the selection model that visually neighbors |position| by |break_type|.
429   // The returned value represents a cursor/caret position without a selection.
430   SelectionModel GetAdjacentSelectionModel(const SelectionModel& current,
431                                            BreakType break_type,
432                                            VisualCursorDirection direction);
433
434   // Get the selection model visually left/right of |selection| by one grapheme.
435   // The returned value represents a cursor/caret position without a selection.
436   virtual SelectionModel AdjacentCharSelectionModel(
437       const SelectionModel& selection,
438       VisualCursorDirection direction) = 0;
439
440   // Get the selection model visually left/right of |selection| by one word.
441   // The returned value represents a cursor/caret position without a selection.
442   virtual SelectionModel AdjacentWordSelectionModel(
443       const SelectionModel& selection,
444       VisualCursorDirection direction) = 0;
445
446   // Get the SelectionModels corresponding to visual text ends.
447   // The returned value represents a cursor/caret position without a selection.
448   SelectionModel EdgeSelectionModel(VisualCursorDirection direction);
449
450   // Sets the selection model, the argument is assumed to be valid.
451   virtual void SetSelectionModel(const SelectionModel& model);
452
453   // Get the horizontal bounds (relative to the left of the text, not the view)
454   // of the glyph starting at |index|. If the glyph is RTL then the returned
455   // Range will have is_reversed() true.  (This does not return a Rect because a
456   // Rect can't have a negative width.)
457   virtual Range GetGlyphBounds(size_t index) = 0;
458
459   // Get the visual bounds containing the logical substring within the |range|.
460   // If |range| is empty, the result is empty. These bounds could be visually
461   // discontinuous if the substring is split by a LTR/RTL level change.
462   // These bounds are in local coordinates, but may be outside the visible
463   // region if the text is longer than the textfield. Subsequent text, cursor,
464   // or bounds changes may invalidate returned values.
465   virtual std::vector<Rect> GetSubstringBounds(const Range& range) = 0;
466
467   // Convert between indices into |text_| and indices into |obscured_text_|,
468   // which differ when the text is obscured. Regardless of whether or not the
469   // text is obscured, the character (code point) offsets always match.
470   virtual size_t TextIndexToLayoutIndex(size_t index) const = 0;
471   virtual size_t LayoutIndexToTextIndex(size_t index) const = 0;
472
473   // Return true if cursor can appear in front of the character at |position|,
474   // which means it is a grapheme boundary or the first character in the text.
475   virtual bool IsCursorablePosition(size_t position) = 0;
476
477   // Reset the layout to be invalid.
478   virtual void ResetLayout() = 0;
479
480   // Ensure the text is laid out, lines are computed, and |lines_| is valid.
481   virtual void EnsureLayout() = 0;
482
483   // Draw the text.
484   virtual void DrawVisualText(Canvas* canvas) = 0;
485
486   // Returns the text used for layout, which may be obscured or truncated.
487   const base::string16& GetLayoutText() const;
488
489   // Returns layout text positions that are suitable for breaking lines.
490   const BreakList<size_t>& GetLineBreaks();
491
492   // Apply (and undo) temporary composition underlines and selection colors.
493   void ApplyCompositionAndSelectionStyles();
494   void UndoCompositionAndSelectionStyles();
495
496   // Returns the line offset from the origin after applying the text alignment
497   // and the display offset.
498   Vector2d GetLineOffset(size_t line_number);
499
500   // Convert points from the text space to the view space and back. Handles the
501   // display area, display offset, application LTR/RTL mode and multiline.
502   Point ToTextPoint(const Point& point);
503   Point ToViewPoint(const Point& point);
504
505   // Convert a text space x-coordinate range to corresponding rects in view
506   // space.
507   std::vector<Rect> TextBoundsToViewBounds(const Range& x);
508
509   // Returns the line offset from the origin, accounting for text alignment
510   // only.
511   Vector2d GetAlignmentOffset(size_t line_number);
512
513   // Applies fade effects to |renderer|.
514   void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
515
516   // Applies text shadows to |renderer|.
517   void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
518
519   // A convenience function to check whether the glyph attached to the caret
520   // is within the given range.
521   static bool RangeContainsCaret(const Range& range,
522                                  size_t caret_pos,
523                                  LogicalCursorDirection caret_affinity);
524
525  private:
526   friend class RenderTextTest;
527   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle);
528   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle);
529   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle);
530   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
531   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText);
532   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText);
533   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText);
534   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
535   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
536   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset);
537   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL);
538   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth);
539   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
540   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_SufficientWidth);
541
542   // Set the cursor to |position|, with the caret trailing the previous
543   // grapheme, or if there is no previous grapheme, leading the cursor position.
544   // If |select| is false, the selection start is moved to the same position.
545   // If the |position| is not a cursorable position (not on grapheme boundary),
546   // it is a NO-OP.
547   void MoveCursorTo(size_t position, bool select);
548
549   // Updates |layout_text_| if the text is obscured or truncated.
550   void UpdateLayoutText();
551
552   // Update the cached bounds and display offset to ensure that the current
553   // cursor is within the visible display area.
554   void UpdateCachedBoundsAndOffset();
555
556   // Draw the selection.
557   void DrawSelection(Canvas* canvas);
558
559   // Logical UTF-16 string data to be drawn.
560   base::string16 text_;
561
562   // Horizontal alignment of the text with respect to |display_rect_|.  The
563   // default is to align left if the application UI is LTR and right if RTL.
564   HorizontalAlignment horizontal_alignment_;
565
566   // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
567   DirectionalityMode directionality_mode_;
568
569   // The cached text direction, potentially computed from the text or UI locale.
570   // Use GetTextDirection(), do not use this potentially invalid value directly!
571   base::i18n::TextDirection text_direction_;
572
573   // A list of fonts used to render |text_|.
574   FontList font_list_;
575
576   // Logical selection range and visual cursor position.
577   SelectionModel selection_model_;
578
579   // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
580   Rect cursor_bounds_;
581
582   // Specifies whether the cursor is enabled. If disabled, no space is reserved
583   // for the cursor when positioning text.
584   bool cursor_enabled_;
585
586   // The cursor visibility and insert mode.
587   bool cursor_visible_;
588   bool insert_mode_;
589
590   // The color used for the cursor.
591   SkColor cursor_color_;
592
593   // The color used for drawing selected text.
594   SkColor selection_color_;
595
596   // The background color used for drawing the selection when focused.
597   SkColor selection_background_focused_color_;
598
599   // The focus state of the text.
600   bool focused_;
601
602   // Composition text range.
603   Range composition_range_;
604
605   // Color and style breaks, used to color and stylize ranges of text.
606   // BreakList positions are stored with text indices, not layout indices.
607   // TODO(msw): Expand to support cursor, selection, background, etc. colors.
608   BreakList<SkColor> colors_;
609   std::vector<BreakList<bool> > styles_;
610
611   // Breaks saved without temporary composition and selection styling.
612   BreakList<SkColor> saved_colors_;
613   BreakList<bool> saved_underlines_;
614   bool composition_and_selection_styles_applied_;
615
616   // A flag to obscure actual text with asterisks for password fields.
617   bool obscured_;
618   // The index at which the char should be revealed in the obscured text.
619   int obscured_reveal_index_;
620
621   // The maximum length of text to display, 0 forgoes a hard limit.
622   size_t truncate_length_;
623
624   // The obscured and/or truncated text that will be displayed.
625   base::string16 layout_text_;
626
627   // Whether the text should be broken into multiple lines. Uses the width of
628   // |display_rect_| as the width cap.
629   bool multiline_;
630
631   // Fade text head and/or tail, if text doesn't fit into |display_rect_|.
632   bool fade_head_;
633   bool fade_tail_;
634
635   // Is the background transparent (either partially or fully)?
636   bool background_is_transparent_;
637
638   // The local display area for rendering the text.
639   Rect display_rect_;
640
641   // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
642   // that results in incorrect clipping when drawing to the document margins.
643   // This field allows disabling clipping to work around the issue.
644   // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
645   bool clip_to_display_rect_;
646
647   // The offset for the text to be drawn, relative to the display area.
648   // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
649   Vector2d display_offset_;
650
651   // The baseline of the text.  This is determined from the height of the
652   // display area and the cap height of the font list so the text is vertically
653   // centered.
654   int baseline_;
655
656   // The cached bounds and offset are invalidated by changes to the cursor,
657   // selection, font, and other operations that adjust the visible text bounds.
658   bool cached_bounds_and_offset_valid_;
659
660   // Text shadows to be drawn.
661   ShadowValues text_shadows_;
662
663   // A list of valid layout text line break positions.
664   BreakList<size_t> line_breaks_;
665
666   // Lines computed by EnsureLayout. These should be invalidated with
667   // ResetLayout and on |display_rect_| changes.
668   std::vector<internal::Line> lines_;
669
670   DISALLOW_COPY_AND_ASSIGN(RenderText);
671 };
672
673 }  // namespace gfx
674
675 #endif  // UI_GFX_RENDER_TEXT_H_