Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: ui/gfx/render_text.h

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

Powered by Google App Engine
This is Rietveld 408576698