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> |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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_; |
110 | 110 |
111 DISALLOW_COPY_AND_ASSIGN(StyleIterator); | 111 DISALLOW_COPY_AND_ASSIGN(StyleIterator); |
112 }; | 112 }; |
113 | 113 |
114 // Line segments are slices of the layout text to be rendered on a single line. | |
115 // This structure holds the logical and visual position information for a line | |
msw
2013/09/06 23:47:45
nit: remove this second sentence of the comment; i
ckocagil
2013/09/11 14:59:49
Done.
| |
116 // segment. | |
117 struct LineSegment { | |
118 LineSegment(); | |
119 | |
120 // X coordinates of this line segment in text coordinate space. | |
msw
2013/09/06 23:47:45
nit: "text space"
ckocagil
2013/09/11 14:59:49
Done.
| |
121 ui::Range x_range; | |
122 | |
123 // The character range this segment corresponds to. | |
124 ui::Range char_range; | |
125 | |
126 // Index of the text run that generated this segment. | |
127 size_t run; | |
128 }; | |
129 | |
130 // Holds a list of line segments and metrics for a line. | |
msw
2013/09/06 23:47:45
nit: consider: // A line of layout text, comprised
ckocagil
2013/09/11 14:59:49
Done.
| |
131 struct Line { | |
132 Line(); | |
133 | |
134 // Segments that make up this line in visual order. | |
135 std::vector<LineSegment> segments; | |
136 | |
137 // Total size of this line. Width is the sum of |segment| widths, height is | |
msw
2013/09/06 23:47:45
nit: consider: // A line size is the sum of segmen
ckocagil
2013/09/11 14:59:49
Done.
| |
138 // the maximum height among |segments|. | |
139 Size size; | |
140 | |
141 // Sum of the |height| fields of preceding lines. | |
msw
2013/09/06 23:47:45
nit: |height| doesn't exist anymore; use "Sum of t
ckocagil
2013/09/11 14:59:49
Done.
| |
142 int preceding_heights; | |
143 | |
144 // Maximum baseline of all runs on this line. | |
msw
2013/09/06 23:47:45
nit: s/runs/segments/
ckocagil
2013/09/11 14:59:49
Done.
| |
145 int baseline; | |
146 }; | |
147 | |
114 } // namespace internal | 148 } // namespace internal |
115 | 149 |
116 // RenderText represents an abstract model of styled text and its corresponding | 150 // 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, | 151 // visual layout. Support is built in for a cursor, a selection, simple styling, |
118 // complex scripts, and bi-directional text. Implementations provide mechanisms | 152 // complex scripts, and bi-directional text. Implementations provide mechanisms |
119 // for rendering and translation between logical and visual data. | 153 // for rendering and translation between logical and visual data. |
120 class UI_EXPORT RenderText { | 154 class UI_EXPORT RenderText { |
121 public: | 155 public: |
122 virtual ~RenderText(); | 156 virtual ~RenderText(); |
123 | 157 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 bool obscured() const { return obscured_; } | 213 bool obscured() const { return obscured_; } |
180 void SetObscured(bool obscured); | 214 void SetObscured(bool obscured); |
181 | 215 |
182 // Makes a char in obscured text at |index| to be revealed. |index| should be | 216 // 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 | 217 // 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 | 218 // 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 | 219 // or out of range, no char will be revealed. The revealed index is also |
186 // cleared when SetText or SetObscured is called. | 220 // cleared when SetText or SetObscured is called. |
187 void SetObscuredRevealIndex(int index); | 221 void SetObscuredRevealIndex(int index); |
188 | 222 |
223 // TODO(ckocagil): Multiline text rendering is currently only supported on | |
msw
2013/09/06 23:47:45
nit: try to be consistent about using either "mult
ckocagil
2013/09/11 14:59:49
Done, used "multiline".
| |
224 // Windows. Support other platforms. | |
225 bool multiline() const { return multiline_; } | |
226 void SetMultiline(bool multiline); | |
227 | |
189 // Set the maximum length of the displayed layout text, not the actual text. | 228 // 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 | 229 // A |length| of 0 forgoes a hard limit, but does not guarantee proper |
191 // functionality of very long strings. Applies to subsequent SetText calls. | 230 // functionality of very long strings. Applies to subsequent SetText calls. |
192 // WARNING: Only use this for system limits, it lacks complex text support. | 231 // WARNING: Only use this for system limits, it lacks complex text support. |
193 void set_truncate_length(size_t length) { truncate_length_ = length; } | 232 void set_truncate_length(size_t length) { truncate_length_ = length; } |
194 | 233 |
195 const Rect& display_rect() const { return display_rect_; } | 234 const Rect& display_rect() const { return display_rect_; } |
196 void SetDisplayRect(const Rect& r); | 235 void SetDisplayRect(const Rect& r); |
197 | 236 |
198 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } | 237 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 | 313 |
275 // Set the text directionality mode and get the text direction yielded. | 314 // Set the text directionality mode and get the text direction yielded. |
276 void SetDirectionalityMode(DirectionalityMode mode); | 315 void SetDirectionalityMode(DirectionalityMode mode); |
277 base::i18n::TextDirection GetTextDirection(); | 316 base::i18n::TextDirection GetTextDirection(); |
278 | 317 |
279 // Returns the visual movement direction corresponding to the logical end | 318 // Returns the visual movement direction corresponding to the logical end |
280 // of the text, considering only the dominant direction returned by | 319 // of the text, considering only the dominant direction returned by |
281 // |GetTextDirection()|, not the direction of a particular run. | 320 // |GetTextDirection()|, not the direction of a particular run. |
282 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 321 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
283 | 322 |
284 // Returns the size in pixels of the entire string. For the height, this will | 323 // Returns the size required to display the current multi-line text. Note that |
285 // return the maximum height among the different fonts in the text runs. | 324 // this returns the raw size of the string, which does not include the cursor |
286 // Note that this returns the raw size of the string, which does not include | 325 // or the margin area of text shadows. |
287 // the margin area of text shadows. | |
288 virtual Size GetStringSize() = 0; | 326 virtual Size GetStringSize() = 0; |
289 | 327 |
290 // Returns the width of content, which reserves room for the cursor if | 328 // Returns the width of multi-line content. Reserves room for the cursor if |
Alexei Svitkine (slow)
2013/08/30 18:45:25
I think "of the multi-line content" is misleading
msw
2013/09/06 23:47:45
Agreed, and ditto for the GetStringSize() comment.
ckocagil
2013/09/11 14:59:49
Done.
ckocagil
2013/09/11 14:59:49
Done.
| |
291 // |cursor_enabled_| is true. | 329 // |cursor_enabled_| is true. |
292 int GetContentWidth(); | 330 int GetContentWidth(); |
293 | 331 |
294 // Returns the common baseline of the text. The returned value is the vertical | 332 // 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. | 333 // offset from the top of |display_rect| to the text baseline, in pixels. |
296 virtual int GetBaseline() = 0; | 334 virtual int GetBaseline() = 0; |
297 | 335 |
298 void Draw(Canvas* canvas); | 336 void Draw(Canvas* canvas); |
299 | 337 |
300 // Draws a cursor at |position|. | 338 // Draws a cursor at |position|. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 // specifies the character range for which the corresponding font has been | 379 // specifies the character range for which the corresponding font has been |
342 // chosen. | 380 // chosen. |
343 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 381 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
344 | 382 |
345 protected: | 383 protected: |
346 RenderText(); | 384 RenderText(); |
347 | 385 |
348 const BreakList<SkColor>& colors() const { return colors_; } | 386 const BreakList<SkColor>& colors() const { return colors_; } |
349 const std::vector<BreakList<bool> >& styles() const { return styles_; } | 387 const std::vector<BreakList<bool> >& styles() const { return styles_; } |
350 | 388 |
389 const std::vector<internal::Line>& lines() const { return lines_; } | |
390 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } | |
391 | |
351 const Vector2d& GetUpdatedDisplayOffset(); | 392 const Vector2d& GetUpdatedDisplayOffset(); |
352 | 393 |
353 void set_cached_bounds_and_offset_valid(bool valid) { | 394 void set_cached_bounds_and_offset_valid(bool valid) { |
354 cached_bounds_and_offset_valid_ = valid; | 395 cached_bounds_and_offset_valid_ = valid; |
355 } | 396 } |
356 | 397 |
357 // Get the selection model that visually neighbors |position| by |break_type|. | 398 // Get the selection model that visually neighbors |position| by |break_type|. |
358 // The returned value represents a cursor/caret position without a selection. | 399 // The returned value represents a cursor/caret position without a selection. |
359 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 400 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
360 BreakType break_type, | 401 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; | 440 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; |
400 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; | 441 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; |
401 | 442 |
402 // Return true if cursor can appear in front of the character at |position|, | 443 // 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. | 444 // which means it is a grapheme boundary or the first character in the text. |
404 virtual bool IsCursorablePosition(size_t position) = 0; | 445 virtual bool IsCursorablePosition(size_t position) = 0; |
405 | 446 |
406 // Reset the layout to be invalid. | 447 // Reset the layout to be invalid. |
407 virtual void ResetLayout() = 0; | 448 virtual void ResetLayout() = 0; |
408 | 449 |
409 // Ensure the text is laid out. | 450 // Ensure the text is laid out, lines are computed, and |lines_| is valid. |
410 virtual void EnsureLayout() = 0; | 451 virtual void EnsureLayout() = 0; |
411 | 452 |
412 // Draw the text. | 453 // Draw the text. |
413 virtual void DrawVisualText(Canvas* canvas) = 0; | 454 virtual void DrawVisualText(Canvas* canvas) = 0; |
414 | 455 |
415 // Returns the text used for layout, which may be obscured or truncated. | 456 // Returns the text used for layout, which may be obscured or truncated. |
416 const base::string16& GetLayoutText() const; | 457 const base::string16& GetLayoutText() const; |
417 | 458 |
459 // Returns layout text positions that are suitable for breaking lines at. | |
msw
2013/09/06 23:47:45
nit: remove trailing "at".
ckocagil
2013/09/11 14:59:49
Done.
| |
460 const BreakList<size_t>& GetLineBreaks(); | |
461 | |
418 // Apply (and undo) temporary composition underlines and selection colors. | 462 // Apply (and undo) temporary composition underlines and selection colors. |
419 void ApplyCompositionAndSelectionStyles(); | 463 void ApplyCompositionAndSelectionStyles(); |
420 void UndoCompositionAndSelectionStyles(); | 464 void UndoCompositionAndSelectionStyles(); |
421 | 465 |
422 // Returns the text offset from the origin after applying text alignment and | 466 // Returns the line offset from the origin after applying text alignment and |
msw
2013/09/06 23:47:45
nit: please fix this to "the text alignment and th
ckocagil
2013/09/11 14:59:49
Done.
| |
423 // display offset. | 467 // display offset. |
424 Vector2d GetTextOffset(); | 468 Vector2d GetLineOffset(size_t line_number); |
425 | 469 |
426 // Convert points from the text space to the view space and back. | 470 // Convert points from the text space to the view space and back. Handles the |
427 // Handles the display area, display offset, and the application LTR/RTL mode. | 471 // display area, display offset, application LTR/RTL mode and multi-line. |
428 Point ToTextPoint(const Point& point); | 472 Point ToTextPoint(const Point& point); |
429 Point ToViewPoint(const Point& point); | 473 Point ToViewPoint(const Point& point); |
430 | 474 |
431 // Returns the text offset from the origin, taking into account text alignment | 475 // Convert the range in text coordinates to rects in view coordinates that |
msw
2013/09/06 23:47:45
nit: s/coordinates/space/ (times two) to match the
ckocagil
2013/09/11 14:59:49
Done.
| |
476 // cover the given range. | |
477 std::vector<Rect> TextBoundsToViewBounds(const ui::Range& x); | |
478 | |
479 // Returns the line offset from the origin, taking into account text alignment | |
msw
2013/09/06 23:47:45
nit: // Returns the line offset from the origin, a
ckocagil
2013/09/11 14:59:49
Done.
| |
432 // only. | 480 // only. |
433 Vector2d GetAlignmentOffset(); | 481 Vector2d GetAlignmentOffset(size_t line_number); |
434 | 482 |
435 // Applies fade effects to |renderer|. | 483 // Applies fade effects to |renderer|. |
436 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 484 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
437 | 485 |
438 // Applies text shadows to |renderer|. | 486 // Applies text shadows to |renderer|. |
439 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 487 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
440 | 488 |
441 // A convenience function to check whether the glyph attached to the caret | 489 // A convenience function to check whether the glyph attached to the caret |
442 // is within the given range. | 490 // is within the given range. |
443 static bool RangeContainsCaret(const ui::Range& range, | 491 static bool RangeContainsCaret(const ui::Range& range, |
444 size_t caret_pos, | 492 size_t caret_pos, |
445 LogicalCursorDirection caret_affinity); | 493 LogicalCursorDirection caret_affinity); |
446 | 494 |
447 private: | 495 private: |
448 friend class RenderTextTest; | 496 friend class RenderTextTest; |
449 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 497 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
450 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); | 498 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
451 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); | 499 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); |
452 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); | 500 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
453 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText); | 501 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText); |
454 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText); | 502 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText); |
455 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText); | 503 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText); |
456 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); | 504 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
457 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); | 505 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
458 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset); | 506 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset); |
459 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL); | 507 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL); |
508 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth); | |
509 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth); | |
460 | 510 |
461 // Set the cursor to |position|, with the caret trailing the previous | 511 // Set the cursor to |position|, with the caret trailing the previous |
462 // grapheme, or if there is no previous grapheme, leading the cursor position. | 512 // 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. | 513 // 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), | 514 // If the |position| is not a cursorable position (not on grapheme boundary), |
465 // it is a NO-OP. | 515 // it is a NO-OP. |
466 void MoveCursorTo(size_t position, bool select); | 516 void MoveCursorTo(size_t position, bool select); |
467 | 517 |
468 // Updates |layout_text_| if the text is obscured or truncated. | 518 // Updates |layout_text_| if the text is obscured or truncated. |
469 void UpdateLayoutText(); | 519 void UpdateLayoutText(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
540 bool obscured_; | 590 bool obscured_; |
541 // The index at which the char should be revealed in the obscured text. | 591 // The index at which the char should be revealed in the obscured text. |
542 int obscured_reveal_index_; | 592 int obscured_reveal_index_; |
543 | 593 |
544 // The maximum length of text to display, 0 forgoes a hard limit. | 594 // The maximum length of text to display, 0 forgoes a hard limit. |
545 size_t truncate_length_; | 595 size_t truncate_length_; |
546 | 596 |
547 // The obscured and/or truncated text that will be displayed. | 597 // The obscured and/or truncated text that will be displayed. |
548 base::string16 layout_text_; | 598 base::string16 layout_text_; |
549 | 599 |
600 // Whether the text should be broken into multiple lines. Uses the width of | |
601 // |display_rect_| as the width cap. | |
602 bool multiline_; | |
603 | |
550 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 604 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
551 bool fade_head_; | 605 bool fade_head_; |
552 bool fade_tail_; | 606 bool fade_tail_; |
553 | 607 |
554 // Is the background transparent (either partially or fully)? | 608 // Is the background transparent (either partially or fully)? |
555 bool background_is_transparent_; | 609 bool background_is_transparent_; |
556 | 610 |
557 // The local display area for rendering the text. | 611 // The local display area for rendering the text. |
558 Rect display_rect_; | 612 Rect display_rect_; |
559 | 613 |
560 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) | 614 // 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. | 615 // that results in incorrect clipping when drawing to the document margins. |
562 // This field allows disabling clipping to work around the issue. | 616 // This field allows disabling clipping to work around the issue. |
563 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 617 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
564 bool clip_to_display_rect_; | 618 bool clip_to_display_rect_; |
565 | 619 |
566 // The offset for the text to be drawn, relative to the display area. | 620 // 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). | 621 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
568 Vector2d display_offset_; | 622 Vector2d display_offset_; |
569 | 623 |
570 // The cached bounds and offset are invalidated by changes to the cursor, | 624 // The cached bounds and offset are invalidated by changes to the cursor, |
571 // selection, font, and other operations that adjust the visible text bounds. | 625 // selection, font, and other operations that adjust the visible text bounds. |
572 bool cached_bounds_and_offset_valid_; | 626 bool cached_bounds_and_offset_valid_; |
573 | 627 |
574 // Text shadows to be drawn. | 628 // Text shadows to be drawn. |
575 ShadowValues text_shadows_; | 629 ShadowValues text_shadows_; |
576 | 630 |
631 // BreakList to find valid positions to break the line at. | |
msw
2013/09/06 23:47:45
nit: consider: // A list of valid layout text line
ckocagil
2013/09/11 14:59:49
Done.
| |
632 BreakList<size_t> line_breaks_; | |
633 | |
634 // Lines computed by EnsureLayout. These should be invalidated with | |
635 // ResetLayout and on |display_rect_| changes. | |
636 std::vector<internal::Line> lines_; | |
637 | |
577 DISALLOW_COPY_AND_ASSIGN(RenderText); | 638 DISALLOW_COPY_AND_ASSIGN(RenderText); |
578 }; | 639 }; |
579 | 640 |
580 } // namespace gfx | 641 } // namespace gfx |
581 | 642 |
582 #endif // UI_GFX_RENDER_TEXT_H_ | 643 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |