OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_RENDER_TEXT_H_ | 5 #ifndef UI_GFX_RENDER_TEXT_H_ |
6 #define UI_GFX_RENDER_TEXT_H_ | 6 #define UI_GFX_RENDER_TEXT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
16 #include "ui/base/range/range.h" | 16 #include "ui/base/range/range.h" |
17 #include "ui/gfx/font.h" | 17 #include "ui/gfx/font.h" |
18 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
19 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
20 #include "ui/gfx/selection_model.h" | 20 #include "ui/gfx/selection_model.h" |
21 | 21 |
22 namespace gfx { | 22 namespace gfx { |
23 | 23 |
24 // Strike line width. | |
25 const int kStrikeWidth = 2; | |
26 | |
27 // Color settings for text, backgrounds and cursor. | 24 // Color settings for text, backgrounds and cursor. |
28 // These are tentative, and should be derived from theme, system | 25 // These are tentative, and should be derived from theme, system |
29 // settings and current settings. | 26 // settings and current settings. |
30 // TODO(oshima): Change this to match the standard chrome | 27 // TODO(oshima): Change this to match the standard chrome |
31 // before dogfooding textfield views. | 28 // before dogfooding textfield views. |
32 const SkColor kSelectedTextColor = SK_ColorWHITE; | 29 const SkColor kSelectedTextColor = SK_ColorWHITE; |
33 const SkColor kFocusedSelectionColor = SkColorSetRGB(30, 144, 255); | 30 const SkColor kFocusedSelectionColor = SkColorSetRGB(30, 144, 255); |
34 const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY; | 31 const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY; |
35 const SkColor kCursorColor = SK_ColorBLACK; | 32 const SkColor kCursorColor = SK_ColorBLACK; |
36 | 33 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 162 |
166 // Gets the SelectionModel from a visual point in local coordinates. | 163 // Gets the SelectionModel from a visual point in local coordinates. |
167 virtual SelectionModel FindCursorPosition(const Point& point); | 164 virtual SelectionModel FindCursorPosition(const Point& point); |
168 | 165 |
169 // Get the visual bounds of a cursor at |selection|. These bounds typically | 166 // Get the visual bounds of a cursor at |selection|. These bounds typically |
170 // represent a vertical line, but if |insert_mode| is true they contain the | 167 // represent a vertical line, but if |insert_mode| is true they contain the |
171 // bounds of the associated glyph. These bounds are in local coordinates, but | 168 // bounds of the associated glyph. These bounds are in local coordinates, but |
172 // may be outside the visible region if the text is longer than the textfield. | 169 // may be outside the visible region if the text is longer than the textfield. |
173 // Subsequent text, cursor, or bounds changes may invalidate returned values. | 170 // Subsequent text, cursor, or bounds changes may invalidate returned values. |
174 virtual Rect GetCursorBounds(const SelectionModel& selection, | 171 virtual Rect GetCursorBounds(const SelectionModel& selection, |
175 bool insert_mode); | 172 bool insert_mode) = 0; |
176 | 173 |
177 // Compute the current cursor bounds, panning the text to show the cursor in | 174 // Compute the current cursor bounds, panning the text to show the cursor in |
178 // the display rect if necessary. These bounds are in local coordinates. | 175 // the display rect if necessary. These bounds are in local coordinates. |
179 // Subsequent text, cursor, or bounds changes may invalidate returned values. | 176 // Subsequent text, cursor, or bounds changes may invalidate returned values. |
180 const Rect& GetUpdatedCursorBounds(); | 177 const Rect& GetUpdatedCursorBounds(); |
181 | 178 |
182 // Get the logical index of the grapheme following the argument |position|. | 179 // Get the logical index of the grapheme following the argument |position|. |
183 size_t GetIndexOfNextGrapheme(size_t position); | 180 size_t GetIndexOfNextGrapheme(size_t position); |
184 | 181 |
185 // Return a SelectionModel with the cursor at the current selection's start. | 182 // Return a SelectionModel with the cursor at the current selection's start. |
(...skipping 16 matching lines...) Expand all Loading... |
202 virtual SelectionModel GetLeftSelectionModel(const SelectionModel& current, | 199 virtual SelectionModel GetLeftSelectionModel(const SelectionModel& current, |
203 BreakType break_type); | 200 BreakType break_type); |
204 virtual SelectionModel GetRightSelectionModel(const SelectionModel& current, | 201 virtual SelectionModel GetRightSelectionModel(const SelectionModel& current, |
205 BreakType break_type); | 202 BreakType break_type); |
206 | 203 |
207 // Get the SelectionModels corresponding to visual text ends. | 204 // Get the SelectionModels corresponding to visual text ends. |
208 // The returned value represents a cursor/caret position without a selection. | 205 // The returned value represents a cursor/caret position without a selection. |
209 virtual SelectionModel LeftEndSelectionModel(); | 206 virtual SelectionModel LeftEndSelectionModel(); |
210 virtual SelectionModel RightEndSelectionModel(); | 207 virtual SelectionModel RightEndSelectionModel(); |
211 | 208 |
212 // Get the logical index of the grapheme preceeding the argument |position|. | 209 // Sets the selection model, the argument is assumed to be valid. |
213 virtual size_t GetIndexOfPreviousGrapheme(size_t position); | 210 virtual void SetSelectionModel(const SelectionModel& model); |
214 | 211 |
215 // Get the visual bounds containing the logical substring within |from| to | 212 // Get the visual bounds containing the logical substring within |from| to |
216 // |to|. These bounds could be visually discontinuous if the substring is | 213 // |to| into |bounds|. If |from| equals to |to|, |bounds| is set as empty. |
217 // split by a LTR/RTL level change. These bounds are in local coordinates, but | 214 // These bounds could be visually discontinuous if the substring is split by a |
218 // may be outside the visible region if the text is longer than the textfield. | 215 // LTR/RTL level change. These bounds are in local coordinates, but may be |
| 216 // outside the visible region if the text is longer than the textfield. |
219 // Subsequent text, cursor, or bounds changes may invalidate returned values. | 217 // Subsequent text, cursor, or bounds changes may invalidate returned values. |
220 // TODO(msw) Re-evaluate this function's necessity and signature. | 218 virtual void GetSubstringBounds(size_t from, |
221 virtual std::vector<Rect> GetSubstringBounds(size_t from, size_t to); | 219 size_t to, |
| 220 std::vector<Rect>* bounds) = 0; |
222 | 221 |
223 // Return true if cursor can appear in front of the character at |position|, | 222 // Return true if cursor can appear in front of the character at |position|, |
224 // which means it is a grapheme boundary or the first character in the text. | 223 // which means it is a grapheme boundary or the first character in the text. |
225 virtual bool IsCursorablePosition(size_t position) = 0; | 224 virtual bool IsCursorablePosition(size_t position) = 0; |
226 | 225 |
227 // Updates the layout so that the next draw request can correctly | 226 // Update the layout so that the next draw request can correctly |
228 // render the text and its attributes. | 227 // render the text and its attributes. |
229 virtual void UpdateLayout() = 0; | 228 virtual void UpdateLayout() = 0; |
230 | 229 |
| 230 // Ensure the text is laid out. |
| 231 virtual void EnsureLayout() = 0; |
| 232 |
| 233 // Draw the text. |
| 234 virtual void DrawVisualText(Canvas* canvas) = 0; |
| 235 |
| 236 // Get the logical index of the grapheme preceding the argument |position|. |
| 237 size_t GetIndexOfPreviousGrapheme(size_t position); |
| 238 |
231 // Apply composition style (underline) to composition range and selection | 239 // Apply composition style (underline) to composition range and selection |
232 // style (foreground) to selection range. | 240 // style (foreground) to selection range. |
233 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges) const; | 241 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges) const; |
234 | 242 |
235 // Convert points from the text space to the view space and back. | 243 // Convert points from the text space to the view space and back. |
236 // Handles the display area, display offset, and the application LTR/RTL mode. | 244 // Handles the display area, display offset, and the application LTR/RTL mode. |
237 Point ToTextPoint(const Point& point); | 245 Point ToTextPoint(const Point& point); |
238 Point ToViewPoint(const Point& point); | 246 Point ToViewPoint(const Point& point); |
239 | 247 |
240 private: | 248 private: |
241 friend class RenderTextTest; | 249 friend class RenderTextTest; |
242 | 250 |
243 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 251 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
244 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); | 252 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); |
245 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); | 253 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); |
246 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); | 254 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); |
247 | 255 |
248 // Return an index belonging to the |next| or previous logical grapheme. | 256 // Return an index belonging to the |next| or previous logical grapheme. |
249 // The return value is bounded by 0 and the text length, inclusive. | 257 // The return value is bounded by 0 and the text length, inclusive. |
250 virtual size_t IndexOfAdjacentGrapheme(size_t index, bool next) = 0; | 258 virtual size_t IndexOfAdjacentGrapheme(size_t index, bool next) = 0; |
251 | 259 |
252 // Sets the selection model, the argument is assumed to be valid. | |
253 void SetSelectionModel(const SelectionModel& selection_model); | |
254 | |
255 // Set the cursor to |position|, with the caret trailing the previous | 260 // Set the cursor to |position|, with the caret trailing the previous |
256 // grapheme, or if there is no previous grapheme, leading the cursor position. | 261 // grapheme, or if there is no previous grapheme, leading the cursor position. |
257 // If |select| is false, the selection start is moved to the same position. | 262 // If |select| is false, the selection start is moved to the same position. |
258 // If the |position| is not a cursorable position (not on grapheme boundary), | 263 // If the |position| is not a cursorable position (not on grapheme boundary), |
259 // it is a NO-OP. | 264 // it is a NO-OP. |
260 void MoveCursorTo(size_t position, bool select); | 265 void MoveCursorTo(size_t position, bool select); |
261 | 266 |
262 // Update the cached bounds and display offset to ensure that the current | 267 // Update the cached bounds and display offset to ensure that the current |
263 // cursor is within the visible display area. | 268 // cursor is within the visible display area. |
264 void UpdateCachedBoundsAndOffset(); | 269 void UpdateCachedBoundsAndOffset(); |
265 | 270 |
| 271 // Draw the selection and cursor. |
| 272 void DrawSelection(Canvas* canvas); |
| 273 void DrawCursor(Canvas* canvas); |
| 274 |
266 // Logical UTF-16 string data to be drawn. | 275 // Logical UTF-16 string data to be drawn. |
267 string16 text_; | 276 string16 text_; |
268 | 277 |
269 // Logical selection range and visual cursor position. | 278 // Logical selection range and visual cursor position. |
270 SelectionModel selection_model_; | 279 SelectionModel selection_model_; |
271 | 280 |
272 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds. | 281 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds. |
273 Rect cursor_bounds_; | 282 Rect cursor_bounds_; |
274 | 283 |
275 // The cursor visibility and insert mode. | 284 // The cursor visibility and insert mode. |
(...skipping 20 matching lines...) Expand all Loading... |
296 // The cached bounds and offset are invalidated by changes to the cursor, | 305 // The cached bounds and offset are invalidated by changes to the cursor, |
297 // selection, font, and other operations that adjust the visible text bounds. | 306 // selection, font, and other operations that adjust the visible text bounds. |
298 bool cached_bounds_and_offset_valid_; | 307 bool cached_bounds_and_offset_valid_; |
299 | 308 |
300 DISALLOW_COPY_AND_ASSIGN(RenderText); | 309 DISALLOW_COPY_AND_ASSIGN(RenderText); |
301 }; | 310 }; |
302 | 311 |
303 } // namespace gfx | 312 } // namespace gfx |
304 | 313 |
305 #endif // UI_GFX_RENDER_TEXT_H_ | 314 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |