Index: ui/gfx/render_text.h |
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h |
index dd86fc53dc40bf0d94621bbfe37adcb3b1371045..5fe6d204614519cfbaf1981bcd169bfcd6d9e60f 100644 |
--- a/ui/gfx/render_text.h |
+++ b/ui/gfx/render_text.h |
@@ -37,7 +37,6 @@ namespace gfx { |
class Canvas; |
class Font; |
class RenderTextTest; |
-struct StyleRange; |
namespace internal { |
@@ -62,7 +61,14 @@ class SkiaTextRenderer { |
void DrawPosText(const SkPoint* pos, |
const uint16* glyphs, |
size_t glyph_count); |
- void DrawDecorations(int x, int y, int width, const StyleRange& style); |
+ // Draw underline and strike-through text decorations. |
+ // Based on |SkCanvas::DrawTextDecorations()| and constants from: |
+ // third_party/skia/src/core/SkTextFormatParams.h |
+ void DrawDecorations(int x, int y, int width, bool underline, bool strike, |
+ bool diagonal_strike); |
+ void DrawUnderline(int x, int y, int width); |
+ void DrawStrike(int x, int y, int width) const; |
+ void DrawDiagonalStrike(int x, int y, int width) const; |
private: |
SkCanvas* canvas_skia_; |
@@ -78,20 +84,11 @@ class SkiaTextRenderer { |
} // namespace internal |
-// A visual style applicable to a range of text. |
-struct UI_EXPORT StyleRange { |
- StyleRange(); |
- |
- SkColor foreground; |
- // A gfx::Font::FontStyle flag to specify bold and italic styles. |
- int font_style; |
- bool strike; |
- bool diagonal_strike; |
- bool underline; |
- ui::Range range; |
-}; |
- |
-typedef std::vector<StyleRange> StyleRanges; |
+// Ordered ColorBreak/StyleBreak lists split text into ranged colors and styles. |
+// Each |break| applies to logical text range: [break.first, (break+1).first) |
+// The first break is always at 0; the last break applies through the text end. |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Maybe move this part of the comment to the actual
msw
2013/01/25 09:10:02
Moot.
|
+typedef std::pair<size_t, SkColor> ColorBreak; |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Can these be inside RenderText (and possibly priva
msw
2013/01/25 09:10:02
Moot.
|
+typedef std::pair<size_t, bool> StyleBreak; |
// RenderText represents an abstract model of styled text and its corresponding |
// visual layout. Support is built in for a cursor, a selection, simple styling, |
@@ -157,9 +154,6 @@ class UI_EXPORT RenderText { |
bool clip_to_display_rect() const { return clip_to_display_rect_; } |
void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
- const StyleRange& default_style() const { return default_style_; } |
- void set_default_style(const StyleRange& style) { default_style_ = style; } |
- |
// In an obscured (password) field, all text is drawn as asterisks or bullets. |
bool obscured() const { return obscured_; } |
void SetObscured(bool obscured); |
@@ -227,11 +221,14 @@ class UI_EXPORT RenderText { |
const ui::Range& GetCompositionRange() const; |
void SetCompositionRange(const ui::Range& composition_range); |
- // Apply |style_range| to the internal style model. |
- void ApplyStyleRange(const StyleRange& style_range); |
+ // Set the text color over the entire text or a logical character range. |
+ void SetColor(SkColor value); |
+ void ApplyColor(SkColor value, const ui::Range& range); |
- // Apply |default_style_| over the entire text range. |
- void ApplyDefaultStyle(); |
+ // 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. |
+ void SetStyle(TextStyle style, bool value); |
+ void ApplyStyle(TextStyle style, bool value, const ui::Range& range); |
// Set the text directionality mode and get the text direction yielded. |
void SetDirectionalityMode(DirectionalityMode mode); |
@@ -295,14 +292,22 @@ class UI_EXPORT RenderText { |
protected: |
RenderText(); |
+ const std::vector<ColorBreak>& colors() { return colors_; } |
+ const std::vector<StyleBreak>& styles(TextStyle s) { return styles_[s]; } |
+ |
+ // Get the end of the supplied break's range. Returns the logical index of the |
+ // first character to which |i| does not apply; the next break or text length. |
+ template <class T> |
+ size_t GetBreakEnd(const T& list, const typename T::const_iterator& i) const { |
+ return (i + 1) == list.end() ? text().length() : (i + 1)->first; |
+ } |
+ |
const Vector2d& GetUpdatedDisplayOffset(); |
void set_cached_bounds_and_offset_valid(bool valid) { |
cached_bounds_and_offset_valid_ = valid; |
} |
- const StyleRanges& style_ranges() const { return style_ranges_; } |
- |
// Get the selection model that visually neighbors |position| by |break_type|. |
// The returned value represents a cursor/caret position without a selection. |
SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
@@ -364,9 +369,10 @@ class UI_EXPORT RenderText { |
// Returns the text used for layout, which may be |obscured_text_|. |
const string16& GetLayoutText() const; |
- // Apply composition style (underline) to composition range and selection |
- // style (foreground) to selection range. |
- void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges); |
+ // Apply underline styling to mark the composition range(s). |
+ void ApplyCompositionStyle(std::vector<StyleBreak>* underlines); |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Please rename the method if this is specific to un
msw
2013/01/25 09:10:02
Moot.
|
+ // Apply the selection text color to mark the selected text range. |
+ void ApplySelectionColor(std::vector<ColorBreak>* colors); |
// Returns the text offset from the origin after applying text alignment and |
// display offset. |
@@ -404,14 +410,22 @@ class UI_EXPORT RenderText { |
friend class RenderTextTest; |
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
- FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); |
- FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); |
- FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); |
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyInvalidOrEmptyRange); |
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyle); |
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ResizeStyle); |
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColor); |
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); |
+ // Adjust the existing |breaks| to apply |value| over the supplied |range|. |
+ template <class T> |
+ void ApplyBreaks(T value, |
+ const ui::Range& range, |
+ std::vector<std::pair<size_t, T> >* breaks); |
+ |
// Set the cursor to |position|, with the caret trailing the previous |
// grapheme, or if there is no previous grapheme, leading the cursor position. |
// If |select| is false, the selection start is moved to the same position. |
@@ -478,10 +492,10 @@ class UI_EXPORT RenderText { |
// Composition text range. |
ui::Range composition_range_; |
- // List of style ranges. Elements in the list never overlap each other. |
- StyleRanges style_ranges_; |
- // The default text style. |
- StyleRange default_style_; |
+ // Color and style breaks, used to color and stylize ranges of text. |
+ // TODO(msw): Expand to support cursor, selection, background, etc. colors. |
+ std::vector<ColorBreak> colors_; |
+ std::vector<StyleBreak> styles_[NUM_TEXT_STYLES]; |
// A flag and the text to display for obscured (password) fields. |
// Asterisks are used instead of the actual text glyphs when true. |