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

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

Issue 24012002: Move Range code to gfx. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: d Created 7 years, 3 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
« no previous file with comments | « ui/gfx/range/range_win_unittest.cc ('k') | ui/gfx/render_text.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <cstring> 9 #include <cstring>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/i18n/rtl.h" 15 #include "base/i18n/rtl.h"
16 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
17 #include "skia/ext/refptr.h" 17 #include "skia/ext/refptr.h"
18 #include "third_party/skia/include/core/SkColor.h" 18 #include "third_party/skia/include/core/SkColor.h"
19 #include "third_party/skia/include/core/SkPaint.h" 19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "third_party/skia/include/core/SkRect.h" 20 #include "third_party/skia/include/core/SkRect.h"
21 #include "ui/base/range/range.h"
22 #include "ui/gfx/break_list.h" 21 #include "ui/gfx/break_list.h"
23 #include "ui/gfx/font_list.h" 22 #include "ui/gfx/font_list.h"
24 #include "ui/gfx/point.h" 23 #include "ui/gfx/point.h"
24 #include "ui/gfx/range/range.h"
25 #include "ui/gfx/rect.h" 25 #include "ui/gfx/rect.h"
26 #include "ui/gfx/selection_model.h" 26 #include "ui/gfx/selection_model.h"
27 #include "ui/gfx/shadow_value.h" 27 #include "ui/gfx/shadow_value.h"
28 #include "ui/gfx/text_constants.h" 28 #include "ui/gfx/text_constants.h"
29 #include "ui/gfx/vector2d.h" 29 #include "ui/gfx/vector2d.h"
30 30
31 class SkCanvas; 31 class SkCanvas;
32 class SkDrawLooper; 32 class SkDrawLooper;
33 struct SkPoint; 33 struct SkPoint;
34 class SkShader; 34 class SkShader;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 public: 89 public:
90 StyleIterator(const BreakList<SkColor>& colors, 90 StyleIterator(const BreakList<SkColor>& colors,
91 const std::vector<BreakList<bool> >& styles); 91 const std::vector<BreakList<bool> >& styles);
92 ~StyleIterator(); 92 ~StyleIterator();
93 93
94 // Get the colors and styles at the current iterator position. 94 // Get the colors and styles at the current iterator position.
95 SkColor color() const { return color_->second; } 95 SkColor color() const { return color_->second; }
96 bool style(TextStyle s) const { return style_[s]->second; } 96 bool style(TextStyle s) const { return style_[s]->second; }
97 97
98 // Get the intersecting range of the current iterator set. 98 // Get the intersecting range of the current iterator set.
99 ui::Range GetRange() const; 99 gfx::Range GetRange() const;
100 100
101 // Update the iterator to point to colors and styles applicable at |position|. 101 // Update the iterator to point to colors and styles applicable at |position|.
102 void UpdatePosition(size_t position); 102 void UpdatePosition(size_t position);
103 103
104 private: 104 private:
105 BreakList<SkColor> colors_; 105 BreakList<SkColor> colors_;
106 std::vector<BreakList<bool> > styles_; 106 std::vector<BreakList<bool> > styles_;
107 107
108 BreakList<SkColor>::const_iterator color_; 108 BreakList<SkColor>::const_iterator color_;
109 std::vector<BreakList<bool>::const_iterator> style_; 109 std::vector<BreakList<bool>::const_iterator> style_;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } 200 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; }
201 bool fade_tail() const { return fade_tail_; } 201 bool fade_tail() const { return fade_tail_; }
202 202
203 bool background_is_transparent() const { return background_is_transparent_; } 203 bool background_is_transparent() const { return background_is_transparent_; }
204 void set_background_is_transparent(bool transparent) { 204 void set_background_is_transparent(bool transparent) {
205 background_is_transparent_ = transparent; 205 background_is_transparent_ = transparent;
206 } 206 }
207 207
208 const SelectionModel& selection_model() const { return selection_model_; } 208 const SelectionModel& selection_model() const { return selection_model_; }
209 209
210 const ui::Range& selection() const { return selection_model_.selection(); } 210 const gfx::Range& selection() const { return selection_model_.selection(); }
211 211
212 size_t cursor_position() const { return selection_model_.caret_pos(); } 212 size_t cursor_position() const { return selection_model_.caret_pos(); }
213 void SetCursorPosition(size_t position); 213 void SetCursorPosition(size_t position);
214 214
215 // Moves the cursor left or right. Cursor movement is visual, meaning that 215 // Moves the cursor left or right. Cursor movement is visual, meaning that
216 // left and right are relative to screen, not the directionality of the text. 216 // left and right are relative to screen, not the directionality of the text.
217 // If |select| is false, the selection start is moved to the same position. 217 // If |select| is false, the selection start is moved to the same position.
218 void MoveCursor(BreakType break_type, 218 void MoveCursor(BreakType break_type,
219 VisualCursorDirection direction, 219 VisualCursorDirection direction,
220 bool select); 220 bool select);
221 221
222 // Set the selection_model_ to the value of |selection|. 222 // Set the selection_model_ to the value of |selection|.
223 // The selection range is clamped to text().length() if out of range. 223 // The selection range is clamped to text().length() if out of range.
224 // Returns true if the cursor position or selection range changed. 224 // Returns true if the cursor position or selection range changed.
225 // If any index in |selection_model| is not a cursorable position (not on a 225 // If any index in |selection_model| is not a cursorable position (not on a
226 // grapheme boundary), it is a no-op and returns false. 226 // grapheme boundary), it is a no-op and returns false.
227 bool MoveCursorTo(const SelectionModel& selection_model); 227 bool MoveCursorTo(const SelectionModel& selection_model);
228 228
229 // Move the cursor to the position associated with the clicked point. 229 // Move the cursor to the position associated with the clicked point.
230 // If |select| is false, the selection start is moved to the same position. 230 // If |select| is false, the selection start is moved to the same position.
231 // Returns true if the cursor position or selection range changed. 231 // Returns true if the cursor position or selection range changed.
232 bool MoveCursorTo(const Point& point, bool select); 232 bool MoveCursorTo(const Point& point, bool select);
233 233
234 // Set the selection_model_ based on |range|. 234 // Set the selection_model_ based on |range|.
235 // If the |range| start or end is greater than text length, it is modified 235 // If the |range| start or end is greater than text length, it is modified
236 // to be the text length. 236 // to be the text length.
237 // If the |range| start or end is not a cursorable position (not on grapheme 237 // If the |range| start or end is not a cursorable position (not on grapheme
238 // boundary), it is a NO-OP and returns false. Otherwise, returns true. 238 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
239 bool SelectRange(const ui::Range& range); 239 bool SelectRange(const gfx::Range& range);
240 240
241 // Returns true if the local point is over selected text. 241 // Returns true if the local point is over selected text.
242 bool IsPointInSelection(const Point& point); 242 bool IsPointInSelection(const Point& point);
243 243
244 // Selects no text, keeping the current cursor position and caret affinity. 244 // Selects no text, keeping the current cursor position and caret affinity.
245 void ClearSelection(); 245 void ClearSelection();
246 246
247 // Select the entire text range. If |reversed| is true, the range will end at 247 // Select the entire text range. If |reversed| is true, the range will end at
248 // the logical beginning of the text; this generally shows the leading portion 248 // the logical beginning of the text; this generally shows the leading portion
249 // of text that overflows its display area. 249 // of text that overflows its display area.
250 void SelectAll(bool reversed); 250 void SelectAll(bool reversed);
251 251
252 // Selects the word at the current cursor position. If there is a non-empty 252 // Selects the word at the current cursor position. If there is a non-empty
253 // selection, the selection bounds are extended to their nearest word 253 // selection, the selection bounds are extended to their nearest word
254 // boundaries. 254 // boundaries.
255 void SelectWord(); 255 void SelectWord();
256 256
257 const ui::Range& GetCompositionRange() const; 257 const gfx::Range& GetCompositionRange() const;
258 void SetCompositionRange(const ui::Range& composition_range); 258 void SetCompositionRange(const gfx::Range& composition_range);
259 259
260 // Set the text color over the entire text or a logical character range. 260 // Set the text color over the entire text or a logical character range.
261 // The |range| should be valid, non-reversed, and within [0, text().length()]. 261 // The |range| should be valid, non-reversed, and within [0, text().length()].
262 void SetColor(SkColor value); 262 void SetColor(SkColor value);
263 void ApplyColor(SkColor value, const ui::Range& range); 263 void ApplyColor(SkColor value, const gfx::Range& range);
264 264
265 // Set various text styles over the entire text or a logical character range. 265 // Set various text styles over the entire text or a logical character range.
266 // The respective |style| is applied if |value| is true, or removed if false. 266 // The respective |style| is applied if |value| is true, or removed if false.
267 // The |range| should be valid, non-reversed, and within [0, text().length()]. 267 // The |range| should be valid, non-reversed, and within [0, text().length()].
268 void SetStyle(TextStyle style, bool value); 268 void SetStyle(TextStyle style, bool value);
269 void ApplyStyle(TextStyle style, bool value, const ui::Range& range); 269 void ApplyStyle(TextStyle style, bool value, const gfx::Range& range);
270 270
271 // Returns whether this style is enabled consistently across the entire 271 // Returns whether this style is enabled consistently across the entire
272 // RenderText. 272 // RenderText.
273 bool GetStyle(TextStyle style) const; 273 bool GetStyle(TextStyle style) const;
274 274
275 // Set the text directionality mode and get the text direction yielded. 275 // Set the text directionality mode and get the text direction yielded.
276 void SetDirectionalityMode(DirectionalityMode mode); 276 void SetDirectionalityMode(DirectionalityMode mode);
277 base::i18n::TextDirection GetTextDirection(); 277 base::i18n::TextDirection GetTextDirection();
278 278
279 // Returns the visual movement direction corresponding to the logical end 279 // Returns the visual movement direction corresponding to the logical end
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 size_t IndexOfAdjacentGrapheme(size_t index, 328 size_t IndexOfAdjacentGrapheme(size_t index,
329 LogicalCursorDirection direction); 329 LogicalCursorDirection direction);
330 330
331 // Return a SelectionModel with the cursor at the current selection's start. 331 // Return a SelectionModel with the cursor at the current selection's start.
332 // The returned value represents a cursor/caret position without a selection. 332 // The returned value represents a cursor/caret position without a selection.
333 SelectionModel GetSelectionModelForSelectionStart(); 333 SelectionModel GetSelectionModelForSelectionStart();
334 334
335 // Sets shadows to drawn with text. 335 // Sets shadows to drawn with text.
336 void SetTextShadows(const ShadowValues& shadows); 336 void SetTextShadows(const ShadowValues& shadows);
337 337
338 typedef std::pair<Font, ui::Range> FontSpan; 338 typedef std::pair<Font, gfx::Range> FontSpan;
339 // For testing purposes, returns which fonts were chosen for which parts of 339 // For testing purposes, returns which fonts were chosen for which parts of
340 // the text by returning a vector of Font and Range pairs, where each range 340 // the text by returning a vector of Font and Range pairs, where each range
341 // specifies the character range for which the corresponding font has been 341 // specifies the character range for which the corresponding font has been
342 // chosen. 342 // chosen.
343 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; 343 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
344 344
345 protected: 345 protected:
346 RenderText(); 346 RenderText();
347 347
348 const BreakList<SkColor>& colors() const { return colors_; } 348 const BreakList<SkColor>& colors() const { return colors_; }
(...skipping 27 matching lines...) Expand all
376 // The returned value represents a cursor/caret position without a selection. 376 // The returned value represents a cursor/caret position without a selection.
377 SelectionModel EdgeSelectionModel(VisualCursorDirection direction); 377 SelectionModel EdgeSelectionModel(VisualCursorDirection direction);
378 378
379 // Sets the selection model, the argument is assumed to be valid. 379 // Sets the selection model, the argument is assumed to be valid.
380 virtual void SetSelectionModel(const SelectionModel& model); 380 virtual void SetSelectionModel(const SelectionModel& model);
381 381
382 // Get the horizontal bounds (relative to the left of the text, not the view) 382 // Get the horizontal bounds (relative to the left of the text, not the view)
383 // of the glyph starting at |index|. If the glyph is RTL then the returned 383 // of the glyph starting at |index|. If the glyph is RTL then the returned
384 // Range will have is_reversed() true. (This does not return a Rect because a 384 // Range will have is_reversed() true. (This does not return a Rect because a
385 // Rect can't have a negative width.) 385 // Rect can't have a negative width.)
386 virtual ui::Range GetGlyphBounds(size_t index) = 0; 386 virtual gfx::Range GetGlyphBounds(size_t index) = 0;
387 387
388 // Get the visual bounds containing the logical substring within the |range|. 388 // Get the visual bounds containing the logical substring within the |range|.
389 // If |range| is empty, the result is empty. These bounds could be visually 389 // If |range| is empty, the result is empty. These bounds could be visually
390 // discontinuous if the substring is split by a LTR/RTL level change. 390 // discontinuous if the substring is split by a LTR/RTL level change.
391 // These bounds are in local coordinates, but may be outside the visible 391 // These bounds are in local coordinates, but may be outside the visible
392 // region if the text is longer than the textfield. Subsequent text, cursor, 392 // region if the text is longer than the textfield. Subsequent text, cursor,
393 // or bounds changes may invalidate returned values. 393 // or bounds changes may invalidate returned values.
394 virtual std::vector<Rect> GetSubstringBounds(const ui::Range& range) = 0; 394 virtual std::vector<Rect> GetSubstringBounds(const gfx::Range& range) = 0;
395 395
396 // Convert between indices into |text_| and indices into |obscured_text_|, 396 // Convert between indices into |text_| and indices into |obscured_text_|,
397 // which differ when the text is obscured. Regardless of whether or not the 397 // which differ when the text is obscured. Regardless of whether or not the
398 // text is obscured, the character (code point) offsets always match. 398 // text is obscured, the character (code point) offsets always match.
399 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; 399 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0;
400 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; 400 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0;
401 401
402 // Return true if cursor can appear in front of the character at |position|, 402 // Return true if cursor can appear in front of the character at |position|,
403 // which means it is a grapheme boundary or the first character in the text. 403 // which means it is a grapheme boundary or the first character in the text.
404 virtual bool IsCursorablePosition(size_t position) = 0; 404 virtual bool IsCursorablePosition(size_t position) = 0;
(...skipping 28 matching lines...) Expand all
433 Vector2d GetAlignmentOffset(); 433 Vector2d GetAlignmentOffset();
434 434
435 // Applies fade effects to |renderer|. 435 // Applies fade effects to |renderer|.
436 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); 436 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
437 437
438 // Applies text shadows to |renderer|. 438 // Applies text shadows to |renderer|.
439 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); 439 void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
440 440
441 // A convenience function to check whether the glyph attached to the caret 441 // A convenience function to check whether the glyph attached to the caret
442 // is within the given range. 442 // is within the given range.
443 static bool RangeContainsCaret(const ui::Range& range, 443 static bool RangeContainsCaret(const gfx::Range& range,
444 size_t caret_pos, 444 size_t caret_pos,
445 LogicalCursorDirection caret_affinity); 445 LogicalCursorDirection caret_affinity);
446 446
447 private: 447 private:
448 friend class RenderTextTest; 448 friend class RenderTextTest;
449 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); 449 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle);
450 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); 450 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle);
451 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); 451 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle);
452 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); 452 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
453 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText); 453 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 // The color used for drawing selected text. 516 // The color used for drawing selected text.
517 SkColor selection_color_; 517 SkColor selection_color_;
518 518
519 // The background color used for drawing the selection when focused. 519 // The background color used for drawing the selection when focused.
520 SkColor selection_background_focused_color_; 520 SkColor selection_background_focused_color_;
521 521
522 // The focus state of the text. 522 // The focus state of the text.
523 bool focused_; 523 bool focused_;
524 524
525 // Composition text range. 525 // Composition text range.
526 ui::Range composition_range_; 526 gfx::Range composition_range_;
527 527
528 // Color and style breaks, used to color and stylize ranges of text. 528 // Color and style breaks, used to color and stylize ranges of text.
529 // BreakList positions are stored with text indices, not layout indices. 529 // BreakList positions are stored with text indices, not layout indices.
530 // TODO(msw): Expand to support cursor, selection, background, etc. colors. 530 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
531 BreakList<SkColor> colors_; 531 BreakList<SkColor> colors_;
532 std::vector<BreakList<bool> > styles_; 532 std::vector<BreakList<bool> > styles_;
533 533
534 // Breaks saved without temporary composition and selection styling. 534 // Breaks saved without temporary composition and selection styling.
535 BreakList<SkColor> saved_colors_; 535 BreakList<SkColor> saved_colors_;
536 BreakList<bool> saved_underlines_; 536 BreakList<bool> saved_underlines_;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 573
574 // Text shadows to be drawn. 574 // Text shadows to be drawn.
575 ShadowValues text_shadows_; 575 ShadowValues text_shadows_;
576 576
577 DISALLOW_COPY_AND_ASSIGN(RenderText); 577 DISALLOW_COPY_AND_ASSIGN(RenderText);
578 }; 578 };
579 579
580 } // namespace gfx 580 } // namespace gfx
581 581
582 #endif // UI_GFX_RENDER_TEXT_H_ 582 #endif // UI_GFX_RENDER_TEXT_H_
OLDNEW
« no previous file with comments | « ui/gfx/range/range_win_unittest.cc ('k') | ui/gfx/render_text.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698