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