diff options
Diffstat (limited to 'chromium/ui/gfx/render_text.h')
-rw-r--r-- | chromium/ui/gfx/render_text.h | 117 |
1 files changed, 90 insertions, 27 deletions
diff --git a/chromium/ui/gfx/render_text.h b/chromium/ui/gfx/render_text.h index 34277400b73..598f4e1d181 100644 --- a/chromium/ui/gfx/render_text.h +++ b/chromium/ui/gfx/render_text.h @@ -18,10 +18,10 @@ #include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkRect.h" -#include "ui/base/range/range.h" #include "ui/gfx/break_list.h" #include "ui/gfx/font_list.h" #include "ui/gfx/point.h" +#include "ui/gfx/range/range.h" #include "ui/gfx/rect.h" #include "ui/gfx/selection_model.h" #include "ui/gfx/shadow_value.h" @@ -96,7 +96,7 @@ class StyleIterator { bool style(TextStyle s) const { return style_[s]->second; } // Get the intersecting range of the current iterator set. - ui::Range GetRange() const; + Range GetRange() const; // Update the iterator to point to colors and styles applicable at |position|. void UpdatePosition(size_t position); @@ -111,6 +111,40 @@ class StyleIterator { DISALLOW_COPY_AND_ASSIGN(StyleIterator); }; +// Line segments are slices of the layout text to be rendered on a single line. +struct LineSegment { + LineSegment(); + ~LineSegment(); + + // X coordinates of this line segment in text space. + Range x_range; + + // The character range this segment corresponds to. + Range char_range; + + // Index of the text run that generated this segment. + size_t run; +}; + +// A line of layout text, comprised of a line segment list and some metrics. +struct Line { + Line(); + ~Line(); + + // Segments that make up this line in visual order. + std::vector<LineSegment> segments; + + // A line size is the sum of segment widths and the maximum of segment + // heights. + Size size; + + // Sum of preceding lines' heights. + int preceding_heights; + + // Maximum baseline of all segments on this line. + int baseline; +}; + } // namespace internal // RenderText represents an abstract model of styled text and its corresponding @@ -186,6 +220,11 @@ class UI_EXPORT RenderText { // cleared when SetText or SetObscured is called. void SetObscuredRevealIndex(int index); + // TODO(ckocagil): Multiline text rendering is currently only supported on + // Windows. Support other platforms. + bool multiline() const { return multiline_; } + void SetMultiline(bool multiline); + // Set the maximum length of the displayed layout text, not the actual text. // A |length| of 0 forgoes a hard limit, but does not guarantee proper // functionality of very long strings. Applies to subsequent SetText calls. @@ -207,7 +246,7 @@ class UI_EXPORT RenderText { const SelectionModel& selection_model() const { return selection_model_; } - const ui::Range& selection() const { return selection_model_.selection(); } + const Range& selection() const { return selection_model_.selection(); } size_t cursor_position() const { return selection_model_.caret_pos(); } void SetCursorPosition(size_t position); @@ -236,7 +275,7 @@ class UI_EXPORT RenderText { // to be the text length. // If the |range| start or end is not a cursorable position (not on grapheme // boundary), it is a NO-OP and returns false. Otherwise, returns true. - bool SelectRange(const ui::Range& range); + bool SelectRange(const Range& range); // Returns true if the local point is over selected text. bool IsPointInSelection(const Point& point); @@ -254,19 +293,19 @@ class UI_EXPORT RenderText { // boundaries. void SelectWord(); - const ui::Range& GetCompositionRange() const; - void SetCompositionRange(const ui::Range& composition_range); + const Range& GetCompositionRange() const; + void SetCompositionRange(const Range& composition_range); // Set the text color over the entire text or a logical character range. // The |range| should be valid, non-reversed, and within [0, text().length()]. void SetColor(SkColor value); - void ApplyColor(SkColor value, const ui::Range& range); + void ApplyColor(SkColor value, const Range& range); // Set various text styles over the entire text or a logical character range. // The respective |style| is applied if |value| is true, or removed if false. // The |range| should be valid, non-reversed, and within [0, text().length()]. void SetStyle(TextStyle style, bool value); - void ApplyStyle(TextStyle style, bool value, const ui::Range& range); + void ApplyStyle(TextStyle style, bool value, const Range& range); // Returns whether this style is enabled consistently across the entire // RenderText. @@ -281,14 +320,14 @@ class UI_EXPORT RenderText { // |GetTextDirection()|, not the direction of a particular run. VisualCursorDirection GetVisualDirectionOfLogicalEnd(); - // Returns the size in pixels of the entire string. For the height, this will - // return the maximum height among the different fonts in the text runs. - // Note that this returns the raw size of the string, which does not include - // the margin area of text shadows. + // Returns the size required to display the current string (which is the + // wrapped size in multiline mode). Note that this returns the raw size of the + // string, which does not include the cursor or the margin area of text + // shadows. virtual Size GetStringSize() = 0; - // Returns the width of content, which reserves room for the cursor if - // |cursor_enabled_| is true. + // Returns the width of the content (which is the wrapped width in multiline + // mode). Reserves room for the cursor if |cursor_enabled_| is true. int GetContentWidth(); // Returns the common baseline of the text. The returned value is the vertical @@ -335,7 +374,7 @@ class UI_EXPORT RenderText { // Sets shadows to drawn with text. void SetTextShadows(const ShadowValues& shadows); - typedef std::pair<Font, ui::Range> FontSpan; + typedef std::pair<Font, Range> FontSpan; // For testing purposes, returns which fonts were chosen for which parts of // the text by returning a vector of Font and Range pairs, where each range // specifies the character range for which the corresponding font has been @@ -348,6 +387,9 @@ class UI_EXPORT RenderText { const BreakList<SkColor>& colors() const { return colors_; } const std::vector<BreakList<bool> >& styles() const { return styles_; } + const std::vector<internal::Line>& lines() const { return lines_; } + void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } + const Vector2d& GetUpdatedDisplayOffset(); void set_cached_bounds_and_offset_valid(bool valid) { @@ -383,7 +425,7 @@ class UI_EXPORT RenderText { // of the glyph starting at |index|. If the glyph is RTL then the returned // Range will have is_reversed() true. (This does not return a Rect because a // Rect can't have a negative width.) - virtual ui::Range GetGlyphBounds(size_t index) = 0; + virtual Range GetGlyphBounds(size_t index) = 0; // Get the visual bounds containing the logical substring within the |range|. // If |range| is empty, the result is empty. These bounds could be visually @@ -391,7 +433,7 @@ class UI_EXPORT RenderText { // These bounds are in local coordinates, but may be outside the visible // region if the text is longer than the textfield. Subsequent text, cursor, // or bounds changes may invalidate returned values. - virtual std::vector<Rect> GetSubstringBounds(const ui::Range& range) = 0; + virtual std::vector<Rect> GetSubstringBounds(const Range& range) = 0; // Convert between indices into |text_| and indices into |obscured_text_|, // which differ when the text is obscured. Regardless of whether or not the @@ -406,7 +448,7 @@ class UI_EXPORT RenderText { // Reset the layout to be invalid. virtual void ResetLayout() = 0; - // Ensure the text is laid out. + // Ensure the text is laid out, lines are computed, and |lines_| is valid. virtual void EnsureLayout() = 0; // Draw the text. @@ -415,22 +457,29 @@ class UI_EXPORT RenderText { // Returns the text used for layout, which may be obscured or truncated. const base::string16& GetLayoutText() const; + // Returns layout text positions that are suitable for breaking lines. + const BreakList<size_t>& GetLineBreaks(); + // Apply (and undo) temporary composition underlines and selection colors. void ApplyCompositionAndSelectionStyles(); void UndoCompositionAndSelectionStyles(); - // Returns the text offset from the origin after applying text alignment and - // display offset. - Vector2d GetTextOffset(); + // Returns the line offset from the origin after applying the text alignment + // and the display offset. + Vector2d GetLineOffset(size_t line_number); - // Convert points from the text space to the view space and back. - // Handles the display area, display offset, and the application LTR/RTL mode. + // Convert points from the text space to the view space and back. Handles the + // display area, display offset, application LTR/RTL mode and multiline. Point ToTextPoint(const Point& point); Point ToViewPoint(const Point& point); - // Returns the text offset from the origin, taking into account text alignment + // Convert a text space x-coordinate range to corresponding rects in view + // space. + std::vector<Rect> TextBoundsToViewBounds(const Range& x); + + // Returns the line offset from the origin, accounting for text alignment // only. - Vector2d GetAlignmentOffset(); + Vector2d GetAlignmentOffset(size_t line_number); // Applies fade effects to |renderer|. void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); @@ -440,7 +489,7 @@ class UI_EXPORT RenderText { // A convenience function to check whether the glyph attached to the caret // is within the given range. - static bool RangeContainsCaret(const ui::Range& range, + static bool RangeContainsCaret(const Range& range, size_t caret_pos, LogicalCursorDirection caret_affinity); @@ -457,6 +506,9 @@ class UI_EXPORT RenderText { FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset); FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL); + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth); + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth); + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_SufficientWidth); // Set the cursor to |position|, with the caret trailing the previous // grapheme, or if there is no previous grapheme, leading the cursor position. @@ -523,7 +575,7 @@ class UI_EXPORT RenderText { bool focused_; // Composition text range. - ui::Range composition_range_; + Range composition_range_; // Color and style breaks, used to color and stylize ranges of text. // BreakList positions are stored with text indices, not layout indices. @@ -547,6 +599,10 @@ class UI_EXPORT RenderText { // The obscured and/or truncated text that will be displayed. base::string16 layout_text_; + // Whether the text should be broken into multiple lines. Uses the width of + // |display_rect_| as the width cap. + bool multiline_; + // Fade text head and/or tail, if text doesn't fit into |display_rect_|. bool fade_head_; bool fade_tail_; @@ -574,6 +630,13 @@ class UI_EXPORT RenderText { // Text shadows to be drawn. ShadowValues text_shadows_; + // A list of valid layout text line break positions. + BreakList<size_t> line_breaks_; + + // Lines computed by EnsureLayout. These should be invalidated with + // ResetLayout and on |display_rect_| changes. + std::vector<internal::Line> lines_; + DISALLOW_COPY_AND_ASSIGN(RenderText); }; |