OLD | NEW |
---|---|
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/memory/scoped_vector.h" | |
Alexei Svitkine (slow)
2013/08/16 18:32:34
No longer needed, please remove.
ckocagil
2013/08/20 00:09:06
Done.
| |
16 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
17 #include "skia/ext/refptr.h" | 18 #include "skia/ext/refptr.h" |
18 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
19 #include "third_party/skia/include/core/SkPaint.h" | 20 #include "third_party/skia/include/core/SkPaint.h" |
20 #include "third_party/skia/include/core/SkRect.h" | 21 #include "third_party/skia/include/core/SkRect.h" |
21 #include "ui/base/range/range.h" | 22 #include "ui/base/range/range.h" |
22 #include "ui/gfx/break_list.h" | 23 #include "ui/gfx/break_list.h" |
23 #include "ui/gfx/font_list.h" | 24 #include "ui/gfx/font_list.h" |
24 #include "ui/gfx/point.h" | 25 #include "ui/gfx/point.h" |
25 #include "ui/gfx/rect.h" | 26 #include "ui/gfx/rect.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 private: | 105 private: |
105 BreakList<SkColor> colors_; | 106 BreakList<SkColor> colors_; |
106 std::vector<BreakList<bool> > styles_; | 107 std::vector<BreakList<bool> > styles_; |
107 | 108 |
108 BreakList<SkColor>::const_iterator color_; | 109 BreakList<SkColor>::const_iterator color_; |
109 std::vector<BreakList<bool>::const_iterator> style_; | 110 std::vector<BreakList<bool>::const_iterator> style_; |
110 | 111 |
111 DISALLOW_COPY_AND_ASSIGN(StyleIterator); | 112 DISALLOW_COPY_AND_ASSIGN(StyleIterator); |
112 }; | 113 }; |
113 | 114 |
115 // Line segments are slices of the layout text to be rendered on a single line. | |
116 // This structure holds the logical and visual position information for a line | |
117 // segment. | |
118 struct LineSegment { | |
119 // Platform-specific type to hold a handle to an internal text run structure. | |
120 typedef int NativeRunData; | |
121 | |
122 // X coordinates of this line segment in text coordinate space. | |
123 ui::Range x_pos; | |
Alexei Svitkine (slow)
2013/08/16 18:32:34
Nit: Rename to x_range.
ckocagil
2013/08/20 00:09:06
Done.
| |
124 | |
125 // The character range this segment corresponds to. | |
126 ui::Range char_pos; | |
Alexei Svitkine (slow)
2013/08/16 18:32:34
Nit: Rename to char_range.
ckocagil
2013/08/20 00:09:06
Done.
| |
127 | |
128 // Platform-specific handle to the text run that generated this segment. | |
129 NativeRunData run; | |
Alexei Svitkine (slow)
2013/08/16 18:32:34
Just make it a size_t run_index; for this change a
ckocagil
2013/08/20 00:09:06
Done.
| |
130 }; | |
131 | |
132 // Holds a list of line segments and metrics for a line. | |
133 struct Line { | |
134 Line(); | |
135 | |
136 // Destructive. Empties the |segments| field of the given Line. | |
137 Line(Line& other); | |
138 | |
139 // Destructive. Empties the |segments| field of the Line on the rhs. | |
140 Line& operator=(Line& other); | |
Alexei Svitkine (slow)
2013/08/16 18:32:34
You can get rid of this and the copy ctor now that
ckocagil
2013/08/20 00:09:06
Done.
| |
141 | |
142 // Segments that make up this line in visual order. | |
143 std::vector<LineSegment> segments; | |
144 | |
145 // Sum of the widths of all segments on this line. | |
146 int width; | |
147 | |
148 // Maximum text height of all segments on this line. | |
149 int height; | |
150 | |
151 // Sum of the |height| fields of preceding lines. | |
152 int preceding_heights; | |
153 | |
154 // Maximum baseline of all runs on this line. | |
155 int baseline; | |
156 }; | |
157 | |
114 } // namespace internal | 158 } // namespace internal |
115 | 159 |
116 // RenderText represents an abstract model of styled text and its corresponding | 160 // RenderText represents an abstract model of styled text and its corresponding |
117 // visual layout. Support is built in for a cursor, a selection, simple styling, | 161 // visual layout. Support is built in for a cursor, a selection, simple styling, |
118 // complex scripts, and bi-directional text. Implementations provide mechanisms | 162 // complex scripts, and bi-directional text. Implementations provide mechanisms |
119 // for rendering and translation between logical and visual data. | 163 // for rendering and translation between logical and visual data. |
120 class UI_EXPORT RenderText { | 164 class UI_EXPORT RenderText { |
121 public: | 165 public: |
122 virtual ~RenderText(); | 166 virtual ~RenderText(); |
123 | 167 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 bool focused() const { return focused_; } | 216 bool focused() const { return focused_; } |
173 void set_focused(bool focused) { focused_ = focused; } | 217 void set_focused(bool focused) { focused_ = focused; } |
174 | 218 |
175 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 219 bool clip_to_display_rect() const { return clip_to_display_rect_; } |
176 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } | 220 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
177 | 221 |
178 // In an obscured (password) field, all text is drawn as asterisks or bullets. | 222 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
179 bool obscured() const { return obscured_; } | 223 bool obscured() const { return obscured_; } |
180 void SetObscured(bool obscured); | 224 void SetObscured(bool obscured); |
181 | 225 |
226 // TODO(ckocagil): Multiline text rendering is currently only supported on | |
227 // Windows. Support other platforms. | |
228 bool multiline() const { return multiline_; } | |
229 void SetMultiline(bool multiline); | |
230 | |
182 // Makes a char in obscured text at |index| to be revealed. |index| should be | 231 // Makes a char in obscured text at |index| to be revealed. |index| should be |
183 // a UTF16 text index. If there is a previous revealed index, the previous one | 232 // a UTF16 text index. If there is a previous revealed index, the previous one |
184 // is cleared and only the last set index will be revealed. If |index| is -1 | 233 // is cleared and only the last set index will be revealed. If |index| is -1 |
185 // or out of range, no char will be revealed. The revealed index is also | 234 // or out of range, no char will be revealed. The revealed index is also |
186 // cleared when SetText or SetObscured is called. | 235 // cleared when SetText or SetObscured is called. |
187 void SetObscuredRevealIndex(int index); | 236 void SetObscuredRevealIndex(int index); |
188 | 237 |
189 // Set the maximum length of the displayed layout text, not the actual text. | 238 // Set the maximum length of the displayed layout text, not the actual text. |
190 // A |length| of 0 forgoes a hard limit, but does not guarantee proper | 239 // A |length| of 0 forgoes a hard limit, but does not guarantee proper |
191 // functionality of very long strings. Applies to subsequent SetText calls. | 240 // functionality of very long strings. Applies to subsequent SetText calls. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 // of the text, considering only the dominant direction returned by | 329 // of the text, considering only the dominant direction returned by |
281 // |GetTextDirection()|, not the direction of a particular run. | 330 // |GetTextDirection()|, not the direction of a particular run. |
282 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 331 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
283 | 332 |
284 // Returns the size in pixels of the entire string. For the height, this will | 333 // Returns the size in pixels of the entire string. For the height, this will |
285 // return the maximum height among the different fonts in the text runs. | 334 // return the maximum height among the different fonts in the text runs. |
286 // Note that this returns the raw size of the string, which does not include | 335 // Note that this returns the raw size of the string, which does not include |
287 // the margin area of text shadows. | 336 // the margin area of text shadows. |
288 virtual Size GetStringSize() = 0; | 337 virtual Size GetStringSize() = 0; |
289 | 338 |
339 // Returns the size required to display the current multiline text, which is | |
340 // horizontally capped by the width of |display_rect()|. | |
341 // TODO(ckocagil): Merge with GetContentWidth. | |
342 virtual Size GetMultilineTextSize() = 0; | |
343 | |
290 // Returns the width of content, which reserves room for the cursor if | 344 // Returns the width of content, which reserves room for the cursor if |
291 // |cursor_enabled_| is true. | 345 // |cursor_enabled_| is true. |
292 int GetContentWidth(); | 346 int GetContentWidth(); |
293 | 347 |
294 // Returns the common baseline of the text. The returned value is the vertical | 348 // Returns the common baseline of the text. The returned value is the vertical |
295 // offset from the top of |display_rect| to the text baseline, in pixels. | 349 // offset from the top of |display_rect| to the text baseline, in pixels. |
296 virtual int GetBaseline() = 0; | 350 virtual int GetBaseline() = 0; |
297 | 351 |
298 void Draw(Canvas* canvas); | 352 void Draw(Canvas* canvas); |
299 | 353 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 // specifies the character range for which the corresponding font has been | 395 // specifies the character range for which the corresponding font has been |
342 // chosen. | 396 // chosen. |
343 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 397 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
344 | 398 |
345 protected: | 399 protected: |
346 RenderText(); | 400 RenderText(); |
347 | 401 |
348 const BreakList<SkColor>& colors() const { return colors_; } | 402 const BreakList<SkColor>& colors() const { return colors_; } |
349 const std::vector<BreakList<bool> >& styles() const { return styles_; } | 403 const std::vector<BreakList<bool> >& styles() const { return styles_; } |
350 | 404 |
405 const std::vector<internal::Line>& lines() const { return lines_; } | |
406 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } | |
407 | |
351 const Vector2d& GetUpdatedDisplayOffset(); | 408 const Vector2d& GetUpdatedDisplayOffset(); |
352 | 409 |
353 void set_cached_bounds_and_offset_valid(bool valid) { | 410 void set_cached_bounds_and_offset_valid(bool valid) { |
354 cached_bounds_and_offset_valid_ = valid; | 411 cached_bounds_and_offset_valid_ = valid; |
355 } | 412 } |
356 | 413 |
357 // Get the selection model that visually neighbors |position| by |break_type|. | 414 // Get the selection model that visually neighbors |position| by |break_type|. |
358 // The returned value represents a cursor/caret position without a selection. | 415 // The returned value represents a cursor/caret position without a selection. |
359 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 416 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
360 BreakType break_type, | 417 BreakType break_type, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; | 456 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; |
400 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; | 457 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; |
401 | 458 |
402 // Return true if cursor can appear in front of the character at |position|, | 459 // 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. | 460 // which means it is a grapheme boundary or the first character in the text. |
404 virtual bool IsCursorablePosition(size_t position) = 0; | 461 virtual bool IsCursorablePosition(size_t position) = 0; |
405 | 462 |
406 // Reset the layout to be invalid. | 463 // Reset the layout to be invalid. |
407 virtual void ResetLayout() = 0; | 464 virtual void ResetLayout() = 0; |
408 | 465 |
409 // Ensure the text is laid out. | 466 // Ensure the text is laid out, lines are computed, and |lines_| is valid. |
410 virtual void EnsureLayout() = 0; | 467 virtual void EnsureLayout() = 0; |
411 | 468 |
412 // Draw the text. | 469 // Draw the text. |
413 virtual void DrawVisualText(Canvas* canvas) = 0; | 470 virtual void DrawVisualText(Canvas* canvas) = 0; |
414 | 471 |
415 // Returns the text used for layout, which may be obscured or truncated. | 472 // Returns the text used for layout, which may be obscured or truncated. |
416 const base::string16& GetLayoutText() const; | 473 const base::string16& GetLayoutText() const; |
417 | 474 |
475 // Returns layout text positions that are suitable for breaking lines at. | |
476 const BreakList<size_t>& GetLineBreaks(); | |
477 | |
418 // Apply (and undo) temporary composition underlines and selection colors. | 478 // Apply (and undo) temporary composition underlines and selection colors. |
419 void ApplyCompositionAndSelectionStyles(); | 479 void ApplyCompositionAndSelectionStyles(); |
420 void UndoCompositionAndSelectionStyles(); | 480 void UndoCompositionAndSelectionStyles(); |
421 | 481 |
422 // Returns the text offset from the origin after applying text alignment and | 482 // Returns the line offset from the origin after applying text alignment and |
423 // display offset. | 483 // display offset. |
424 Vector2d GetTextOffset(); | 484 Vector2d GetLineOffset(size_t line_number); |
425 | 485 |
426 // Convert points from the text space to the view space and back. | 486 // Convert points from the text space to the view space and back. |
427 // Handles the display area, display offset, and the application LTR/RTL mode. | 487 // Handles the display area, display offset, application LTR/RTL mode and |
488 // multi-line. | |
428 Point ToTextPoint(const Point& point); | 489 Point ToTextPoint(const Point& point); |
429 Point ToViewPoint(const Point& point); | 490 Point ToViewPoint(const Point& point); |
430 | 491 |
431 // Returns the text offset from the origin, taking into account text alignment | 492 // Convert the range in text coordinates to rects in view coordinates that |
493 // cover the given range. | |
494 std::vector<Rect> TextBoundsToViewBounds(const ui::Range& x); | |
495 | |
496 // Returns the line offset from the origin, taking into account text alignment | |
432 // only. | 497 // only. |
433 Vector2d GetAlignmentOffset(); | 498 Vector2d GetAlignmentOffset(size_t line_number); |
434 | 499 |
435 // Applies fade effects to |renderer|. | 500 // Applies fade effects to |renderer|. |
436 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 501 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
437 | 502 |
438 // Applies text shadows to |renderer|. | 503 // Applies text shadows to |renderer|. |
439 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 504 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
440 | 505 |
441 // A convenience function to check whether the glyph attached to the caret | 506 // A convenience function to check whether the glyph attached to the caret |
442 // is within the given range. | 507 // is within the given range. |
443 static bool RangeContainsCaret(const ui::Range& range, | 508 static bool RangeContainsCaret(const ui::Range& range, |
444 size_t caret_pos, | 509 size_t caret_pos, |
445 LogicalCursorDirection caret_affinity); | 510 LogicalCursorDirection caret_affinity); |
446 | 511 |
447 private: | 512 private: |
448 friend class RenderTextTest; | 513 friend class RenderTextTest; |
449 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 514 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
450 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); | 515 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
451 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); | 516 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); |
452 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); | 517 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
453 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText); | 518 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText); |
454 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText); | 519 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText); |
455 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText); | 520 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText); |
456 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); | 521 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
457 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); | 522 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
458 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset); | 523 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset); |
459 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL); | 524 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL); |
525 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline); | |
460 | 526 |
461 // Set the cursor to |position|, with the caret trailing the previous | 527 // Set the cursor to |position|, with the caret trailing the previous |
462 // grapheme, or if there is no previous grapheme, leading the cursor position. | 528 // grapheme, or if there is no previous grapheme, leading the cursor position. |
463 // If |select| is false, the selection start is moved to the same position. | 529 // If |select| is false, the selection start is moved to the same position. |
464 // If the |position| is not a cursorable position (not on grapheme boundary), | 530 // If the |position| is not a cursorable position (not on grapheme boundary), |
465 // it is a NO-OP. | 531 // it is a NO-OP. |
466 void MoveCursorTo(size_t position, bool select); | 532 void MoveCursorTo(size_t position, bool select); |
467 | 533 |
468 // Updates |layout_text_| if the text is obscured or truncated. | 534 // Updates |layout_text_| if the text is obscured or truncated. |
469 void UpdateLayoutText(); | 535 void UpdateLayoutText(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
540 bool obscured_; | 606 bool obscured_; |
541 // The index at which the char should be revealed in the obscured text. | 607 // The index at which the char should be revealed in the obscured text. |
542 int obscured_reveal_index_; | 608 int obscured_reveal_index_; |
543 | 609 |
544 // The maximum length of text to display, 0 forgoes a hard limit. | 610 // The maximum length of text to display, 0 forgoes a hard limit. |
545 size_t truncate_length_; | 611 size_t truncate_length_; |
546 | 612 |
547 // The obscured and/or truncated text that will be displayed. | 613 // The obscured and/or truncated text that will be displayed. |
548 base::string16 layout_text_; | 614 base::string16 layout_text_; |
549 | 615 |
616 // Whether the text should be broken into multiple lines. Uses the width of | |
617 // |display_rect_| as the width cap. | |
618 bool multiline_; | |
619 | |
550 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 620 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
551 bool fade_head_; | 621 bool fade_head_; |
552 bool fade_tail_; | 622 bool fade_tail_; |
553 | 623 |
554 // Is the background transparent (either partially or fully)? | 624 // Is the background transparent (either partially or fully)? |
555 bool background_is_transparent_; | 625 bool background_is_transparent_; |
556 | 626 |
557 // The local display area for rendering the text. | 627 // The local display area for rendering the text. |
558 Rect display_rect_; | 628 Rect display_rect_; |
559 | 629 |
560 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) | 630 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) |
561 // that results in incorrect clipping when drawing to the document margins. | 631 // that results in incorrect clipping when drawing to the document margins. |
562 // This field allows disabling clipping to work around the issue. | 632 // This field allows disabling clipping to work around the issue. |
563 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 633 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
564 bool clip_to_display_rect_; | 634 bool clip_to_display_rect_; |
565 | 635 |
566 // The offset for the text to be drawn, relative to the display area. | 636 // The offset for the text to be drawn, relative to the display area. |
567 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). | 637 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
568 Vector2d display_offset_; | 638 Vector2d display_offset_; |
569 | 639 |
570 // The cached bounds and offset are invalidated by changes to the cursor, | 640 // The cached bounds and offset are invalidated by changes to the cursor, |
571 // selection, font, and other operations that adjust the visible text bounds. | 641 // selection, font, and other operations that adjust the visible text bounds. |
572 bool cached_bounds_and_offset_valid_; | 642 bool cached_bounds_and_offset_valid_; |
573 | 643 |
574 // Text shadows to be drawn. | 644 // Text shadows to be drawn. |
575 ShadowValues text_shadows_; | 645 ShadowValues text_shadows_; |
576 | 646 |
647 // BreakList to find valid positions to break the line at. | |
648 BreakList<size_t> line_breaks_; | |
649 | |
650 // Lines computed by EnsureLayout. Should be emptied whenever the text, styles | |
651 // or |display_rect_| changes. | |
652 std::vector<internal::Line> lines_; | |
653 | |
577 DISALLOW_COPY_AND_ASSIGN(RenderText); | 654 DISALLOW_COPY_AND_ASSIGN(RenderText); |
578 }; | 655 }; |
579 | 656 |
580 } // namespace gfx | 657 } // namespace gfx |
581 | 658 |
582 #endif // UI_GFX_RENDER_TEXT_H_ | 659 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |