OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/i18n/rtl.h" |
| 13 #include "base/string16.h" |
| 14 #include "third_party/skia/include/core/SkColor.h" |
| 15 #include "ui/base/range/range.h" |
| 16 #include "ui/gfx/font.h" |
| 17 #include "ui/gfx/rect.h" |
| 18 #include "ui/gfx/point.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Strike line width. |
| 23 const int kStrikeWidth = 2; |
| 24 |
| 25 // Color settings for text, backgrounds and cursor. |
| 26 // These are tentative, and should be derived from theme, system |
| 27 // settings and current settings. |
| 28 // TODO(oshima): Change this to match the standard chrome |
| 29 // before dogfooding textfield views. |
| 30 const SkColor kSelectedTextColor = SK_ColorWHITE; |
| 31 const SkColor kFocusedSelectionColor = SK_ColorCYAN; |
| 32 const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY; |
| 33 const SkColor kCursorColor = SK_ColorBLACK; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 namespace gfx { |
| 38 |
| 39 class Canvas; |
| 40 class RenderTextTest; |
| 41 |
| 42 // A visual style applicable to a range of text. |
| 43 struct UI_API StyleRange { |
| 44 StyleRange(); |
| 45 |
| 46 Font font; |
| 47 SkColor foreground; |
| 48 bool strike; |
| 49 bool underline; |
| 50 ui::Range range; |
| 51 }; |
| 52 |
| 53 typedef std::vector<StyleRange> StyleRanges; |
| 54 |
| 55 // TODO(msw): Distinguish between logical character and glyph? |
| 56 enum BreakType { |
| 57 CHARACTER_BREAK, |
| 58 WORD_BREAK, |
| 59 LINE_BREAK, |
| 60 }; |
| 61 |
| 62 // TODO(msw): Implement RenderText[Win|Linux] for Uniscribe/Pango BiDi... |
| 63 |
| 64 // RenderText represents an abstract model of styled text and its corresponding |
| 65 // visual layout. Support is built in for a cursor, a selection, simple styling, |
| 66 // complex scripts, and bi-directional text. Implementations provide mechanisms |
| 67 // for rendering and translation between logical and visual data. |
| 68 class UI_API RenderText { |
| 69 |
| 70 public: |
| 71 virtual ~RenderText(); |
| 72 |
| 73 // Creates a platform-specific RenderText instance. |
| 74 static RenderText* CreateRenderText(); |
| 75 |
| 76 const string16& text() const { return text_; } |
| 77 virtual void SetText(const string16& text); |
| 78 |
| 79 bool cursor_visible() const { return cursor_visible_; } |
| 80 void set_cursor_visible(bool visible) { cursor_visible_ = visible; } |
| 81 |
| 82 bool insert_mode() const { return insert_mode_; } |
| 83 void toggle_insert_mode() { insert_mode_ = !insert_mode_; } |
| 84 |
| 85 bool focused() const { return focused_; } |
| 86 void set_focused(bool focused) { focused_ = focused; } |
| 87 |
| 88 const StyleRange& default_style() const { return default_style_; } |
| 89 void set_default_style(StyleRange style) { default_style_ = style; } |
| 90 |
| 91 const gfx::Rect& display_rect() const { return display_rect_; } |
| 92 void set_display_rect(const gfx::Rect& r) { display_rect_ = r; } |
| 93 |
| 94 size_t GetCursorPosition() const; |
| 95 void SetCursorPosition(const size_t position); |
| 96 |
| 97 // Moves the cursor left or right. Cursor movement is visual, meaning that |
| 98 // left and right are relative to screen, not the directionality of the text. |
| 99 // If |select| is false, the selection range is emptied at the new position. |
| 100 // If |break_type| is CHARACTER_BREAK, move to the neighboring character. |
| 101 // If |break_type| is WORD_BREAK, move to the nearest word boundary. |
| 102 // If |break_type| is LINE_BREAK, move to text edge as shown on screen. |
| 103 void MoveCursorLeft(BreakType break_type, bool select); |
| 104 void MoveCursorRight(BreakType break_type, bool select); |
| 105 |
| 106 // Moves the cursor to the specified logical |position|. |
| 107 // If |select| is false, the selection range is emptied at the new position. |
| 108 // Returns true if the cursor position or selection range changed. |
| 109 bool MoveCursorTo(size_t position, bool select); |
| 110 |
| 111 // Move the cursor to the position associated with the clicked point. |
| 112 // If |select| is false, the selection range is emptied at the new position. |
| 113 bool MoveCursorTo(const gfx::Point& point, bool select); |
| 114 |
| 115 const ui::Range& GetSelection() const; |
| 116 void SetSelection(const ui::Range& range); |
| 117 |
| 118 // Returns true if the local point is over selected text. |
| 119 bool IsPointInSelection(const gfx::Point& point) const; |
| 120 |
| 121 // Selects no text, all text, or the word at the current cursor position. |
| 122 void ClearSelection(); |
| 123 void SelectAll(); |
| 124 void SelectWord(); |
| 125 |
| 126 const ui::Range& GetCompositionRange() const; |
| 127 void SetCompositionRange(const ui::Range& composition_range); |
| 128 |
| 129 // Apply |style_range| to the internal style model. |
| 130 virtual void ApplyStyleRange(StyleRange style_range); |
| 131 |
| 132 // Apply |default_style_| over the entire text range. |
| 133 virtual void ApplyDefaultStyle(); |
| 134 |
| 135 base::i18n::TextDirection GetTextDirection() const; |
| 136 |
| 137 // Get the width of the entire string. |
| 138 int GetStringWidth() const; |
| 139 |
| 140 virtual void Draw(gfx::Canvas* canvas); |
| 141 |
| 142 // TODO(msw): Deprecate this function. Logical and visual cursors are not |
| 143 // mapped one-to-one. See the selection_range_ TODO for more information. |
| 144 // Get the logical cursor position from a visual point in local coordinates. |
| 145 virtual size_t FindCursorPosition(const gfx::Point& point) const; |
| 146 |
| 147 // Get the visual bounds containing the logical substring within |range|. |
| 148 // These bounds could be visually discontiguous if the logical selection range |
| 149 // is split by an odd number of LTR/RTL level change. |
| 150 virtual std::vector<gfx::Rect> GetSubstringBounds( |
| 151 const ui::Range& range) const; |
| 152 |
| 153 // Get the visual bounds describing the cursor at |position|. These bounds |
| 154 // typically represent a vertical line, but if |insert_mode| is true they |
| 155 // contain the bounds of the associated glyph. |
| 156 virtual gfx::Rect GetCursorBounds(size_t position, bool insert_mode) const; |
| 157 |
| 158 protected: |
| 159 RenderText(); |
| 160 |
| 161 const StyleRanges& style_ranges() const { return style_ranges_; } |
| 162 |
| 163 const gfx::Point& display_offset() const { return display_offset_; } |
| 164 |
| 165 // Get the cursor position that visually neighbors |position|. |
| 166 // If |move_by_word| is true, return the neighboring word delimiter position. |
| 167 virtual size_t GetLeftCursorPosition(size_t position, |
| 168 bool move_by_word) const; |
| 169 virtual size_t GetRightCursorPosition(size_t position, |
| 170 bool move_by_word) const; |
| 171 |
| 172 private: |
| 173 friend class RenderTextTest; |
| 174 |
| 175 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
| 176 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); |
| 177 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); |
| 178 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); |
| 179 |
| 180 // Clear out |style_ranges_|. |
| 181 void ClearStyleRanges(); |
| 182 |
| 183 bool IsPositionAtWordSelectionBoundary(size_t pos); |
| 184 |
| 185 // Logical UTF-16 string data to be drawn. |
| 186 string16 text_; |
| 187 |
| 188 // TODO(msw): A single logical cursor position doesn't support two potential |
| 189 // visual cursor positions. For example, clicking right of 'c' & 'D' yeilds: |
| 190 // (visually: 'abc|FEDghi' and 'abcFED|ghi', both logically: 'abc|DEFghi'). |
| 191 // Similarly, one visual position may have two associated logical positions. |
| 192 // For example, clicking the right side of 'D' and left side of 'g' yields: |
| 193 // (both visually: 'abcFED|ghi', logically: 'abc|DEFghi' and 'abcDEF|ghi'). |
| 194 // Update the cursor model with a leading/trailing flag, a level association, |
| 195 // or a disjoint visual position to satisfy the proposed visual behavior. |
| 196 // Logical selection range; the range end is also the logical cursor position. |
| 197 ui::Range selection_range_; |
| 198 |
| 199 // The cursor visibility and insert mode. |
| 200 bool cursor_visible_; |
| 201 bool insert_mode_; |
| 202 |
| 203 // The focus state of the text. |
| 204 bool focused_; |
| 205 |
| 206 // Composition text range. |
| 207 ui::Range composition_range_; |
| 208 |
| 209 // List of style ranges. Elements in the list never overlap each other. |
| 210 StyleRanges style_ranges_; |
| 211 // The default text style. |
| 212 StyleRange default_style_; |
| 213 |
| 214 // The local display area for rendering the text. |
| 215 gfx::Rect display_rect_; |
| 216 // The offset for the text to be drawn, relative to the display area. |
| 217 gfx::Point display_offset_; |
| 218 |
| 219 DISALLOW_COPY_AND_ASSIGN(RenderText); |
| 220 }; |
| 221 |
| 222 } // namespace gfx |
| 223 |
| 224 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |