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" |
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 // X coordinates of this line segment in text coordinate space. |
| 120 ui::Range x_pos; |
| 121 |
| 122 // The character range this segment corresponds to. |
| 123 ui::Range char_pos; |
| 124 }; |
| 125 |
| 126 // Holds a list of line segments and metrics for a line. |
| 127 struct Line { |
| 128 Line(); |
| 129 |
| 130 // Destructive. Empties the |segments| field of the given Line. |
| 131 Line(Line& other); |
| 132 |
| 133 // Destructive. Empties the |segments| field of the Line on the rhs. |
| 134 Line& operator=(Line& other); |
| 135 |
| 136 // Segments that make up this line in visual order. |
| 137 ScopedVector<LineSegment> segments; |
| 138 |
| 139 // Sum of the widths of all segments on this line. |
| 140 int width; |
| 141 |
| 142 // Maximum text height of all segments on this line. |
| 143 int height; |
| 144 |
| 145 // Sum of the |height| fields of preceding lines. |
| 146 int preceding_heights; |
| 147 |
| 148 // Maximum baseline of all runs on this line. |
| 149 int baseline; |
| 150 }; |
| 151 |
114 } // namespace internal | 152 } // namespace internal |
115 | 153 |
116 // RenderText represents an abstract model of styled text and its corresponding | 154 // 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, | 155 // visual layout. Support is built in for a cursor, a selection, simple styling, |
118 // complex scripts, and bi-directional text. Implementations provide mechanisms | 156 // complex scripts, and bi-directional text. Implementations provide mechanisms |
119 // for rendering and translation between logical and visual data. | 157 // for rendering and translation between logical and visual data. |
120 class UI_EXPORT RenderText { | 158 class UI_EXPORT RenderText { |
121 public: | 159 public: |
122 virtual ~RenderText(); | 160 virtual ~RenderText(); |
123 | 161 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 bool focused() const { return focused_; } | 210 bool focused() const { return focused_; } |
173 void set_focused(bool focused) { focused_ = focused; } | 211 void set_focused(bool focused) { focused_ = focused; } |
174 | 212 |
175 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 213 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; } | 214 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
177 | 215 |
178 // In an obscured (password) field, all text is drawn as asterisks or bullets. | 216 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
179 bool obscured() const { return obscured_; } | 217 bool obscured() const { return obscured_; } |
180 void SetObscured(bool obscured); | 218 void SetObscured(bool obscured); |
181 | 219 |
| 220 // TODO(ckocagil): Multiline text rendering is currently only supported on |
| 221 // Windows. Support other platforms. |
| 222 bool multiline() const { return multiline_; } |
| 223 void SetMultiline(bool multiline); |
| 224 |
182 // Makes a char in obscured text at |index| to be revealed. |index| should be | 225 // 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 | 226 // 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 | 227 // 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 | 228 // or out of range, no char will be revealed. The revealed index is also |
186 // cleared when SetText or SetObscured is called. | 229 // cleared when SetText or SetObscured is called. |
187 void SetObscuredRevealIndex(int index); | 230 void SetObscuredRevealIndex(int index); |
188 | 231 |
189 // Set the maximum length of the displayed layout text, not the actual text. | 232 // 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 | 233 // A |length| of 0 forgoes a hard limit, but does not guarantee proper |
191 // functionality of very long strings. Applies to subsequent SetText calls. | 234 // 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 | 323 // of the text, considering only the dominant direction returned by |
281 // |GetTextDirection()|, not the direction of a particular run. | 324 // |GetTextDirection()|, not the direction of a particular run. |
282 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 325 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
283 | 326 |
284 // Returns the size in pixels of the entire string. For the height, this will | 327 // 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. | 328 // 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 | 329 // Note that this returns the raw size of the string, which does not include |
287 // the margin area of text shadows. | 330 // the margin area of text shadows. |
288 virtual Size GetStringSize() = 0; | 331 virtual Size GetStringSize() = 0; |
289 | 332 |
| 333 // Returns the size required to display the current multiline text, which is |
| 334 // horizontally capped by the width of |display_rect()|. |
| 335 // TODO(ckocagil): Merge with GetContentWidth. |
| 336 virtual Size GetMultilineTextSize() = 0; |
| 337 |
290 // Returns the width of content, which reserves room for the cursor if | 338 // Returns the width of content, which reserves room for the cursor if |
291 // |cursor_enabled_| is true. | 339 // |cursor_enabled_| is true. |
292 int GetContentWidth(); | 340 int GetContentWidth(); |
293 | 341 |
294 // Returns the common baseline of the text. The returned value is the vertical | 342 // 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. | 343 // offset from the top of |display_rect| to the text baseline, in pixels. |
296 virtual int GetBaseline() = 0; | 344 virtual int GetBaseline() = 0; |
297 | 345 |
298 void Draw(Canvas* canvas); | 346 void Draw(Canvas* canvas); |
299 | 347 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 // specifies the character range for which the corresponding font has been | 388 // specifies the character range for which the corresponding font has been |
341 // chosen. | 389 // chosen. |
342 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 390 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
343 | 391 |
344 protected: | 392 protected: |
345 RenderText(); | 393 RenderText(); |
346 | 394 |
347 const BreakList<SkColor>& colors() const { return colors_; } | 395 const BreakList<SkColor>& colors() const { return colors_; } |
348 const std::vector<BreakList<bool> >& styles() const { return styles_; } | 396 const std::vector<BreakList<bool> >& styles() const { return styles_; } |
349 | 397 |
| 398 const std::vector<internal::Line>& lines() const { return lines_; } |
| 399 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } |
| 400 |
350 const Vector2d& GetUpdatedDisplayOffset(); | 401 const Vector2d& GetUpdatedDisplayOffset(); |
351 | 402 |
352 void set_cached_bounds_and_offset_valid(bool valid) { | 403 void set_cached_bounds_and_offset_valid(bool valid) { |
353 cached_bounds_and_offset_valid_ = valid; | 404 cached_bounds_and_offset_valid_ = valid; |
354 } | 405 } |
355 | 406 |
356 // Get the selection model that visually neighbors |position| by |break_type|. | 407 // Get the selection model that visually neighbors |position| by |break_type|. |
357 // The returned value represents a cursor/caret position without a selection. | 408 // The returned value represents a cursor/caret position without a selection. |
358 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 409 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
359 BreakType break_type, | 410 BreakType break_type, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; | 449 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; |
399 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; | 450 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; |
400 | 451 |
401 // Return true if cursor can appear in front of the character at |position|, | 452 // Return true if cursor can appear in front of the character at |position|, |
402 // which means it is a grapheme boundary or the first character in the text. | 453 // which means it is a grapheme boundary or the first character in the text. |
403 virtual bool IsCursorablePosition(size_t position) = 0; | 454 virtual bool IsCursorablePosition(size_t position) = 0; |
404 | 455 |
405 // Reset the layout to be invalid. | 456 // Reset the layout to be invalid. |
406 virtual void ResetLayout() = 0; | 457 virtual void ResetLayout() = 0; |
407 | 458 |
408 // Ensure the text is laid out. | 459 // Ensure the text is laid out, lines are computed, and |lines_| is valid. |
409 virtual void EnsureLayout() = 0; | 460 virtual void EnsureLayout() = 0; |
410 | 461 |
411 // Draw the text. | 462 // Draw the text. |
412 virtual void DrawVisualText(Canvas* canvas) = 0; | 463 virtual void DrawVisualText(Canvas* canvas) = 0; |
413 | 464 |
414 // Returns the text used for layout, which may be obscured or truncated. | 465 // Returns the text used for layout, which may be obscured or truncated. |
415 const base::string16& GetLayoutText() const; | 466 const base::string16& GetLayoutText() const; |
416 | 467 |
| 468 // Returns layout text positions that are suitable for breaking lines at. |
| 469 const BreakList<size_t>& GetLineBreaks(); |
| 470 |
417 // Apply (and undo) temporary composition underlines and selection colors. | 471 // Apply (and undo) temporary composition underlines and selection colors. |
418 void ApplyCompositionAndSelectionStyles(); | 472 void ApplyCompositionAndSelectionStyles(); |
419 void UndoCompositionAndSelectionStyles(); | 473 void UndoCompositionAndSelectionStyles(); |
420 | 474 |
421 // Returns the text offset from the origin after applying text alignment and | 475 // Returns the line offset from the origin after applying text alignment and |
422 // display offset. | 476 // display offset. |
423 Vector2d GetTextOffset(); | 477 Vector2d GetLineOffset(size_t line_number); |
424 | 478 |
425 // Convert points from the text space to the view space and back. | 479 // Convert points from the text space to the view space and back. |
426 // Handles the display area, display offset, and the application LTR/RTL mode. | 480 // Handles the display area, display offset, application LTR/RTL mode and |
| 481 // multi-line. |
427 Point ToTextPoint(const Point& point); | 482 Point ToTextPoint(const Point& point); |
428 Point ToViewPoint(const Point& point); | 483 Point ToViewPoint(const Point& point); |
429 | 484 |
430 // Returns the text offset from the origin, taking into account text alignment | 485 // Convert the range in text coordinates to rects in view coordinates that |
| 486 // cover the given range. |
| 487 std::vector<Rect> TextBoundsToViewBounds(const ui::Range& x); |
| 488 |
| 489 // Returns the line offset from the origin, taking into account text alignment |
431 // only. | 490 // only. |
432 Vector2d GetAlignmentOffset(); | 491 Vector2d GetAlignmentOffset(size_t line_number); |
433 | 492 |
434 // Applies fade effects to |renderer|. | 493 // Applies fade effects to |renderer|. |
435 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 494 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
436 | 495 |
437 // Applies text shadows to |renderer|. | 496 // Applies text shadows to |renderer|. |
438 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 497 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
439 | 498 |
440 // A convenience function to check whether the glyph attached to the caret | 499 // A convenience function to check whether the glyph attached to the caret |
441 // is within the given range. | 500 // is within the given range. |
442 static bool RangeContainsCaret(const ui::Range& range, | 501 static bool RangeContainsCaret(const ui::Range& range, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 bool obscured_; | 598 bool obscured_; |
540 // The index at which the char should be revealed in the obscured text. | 599 // The index at which the char should be revealed in the obscured text. |
541 int obscured_reveal_index_; | 600 int obscured_reveal_index_; |
542 | 601 |
543 // The maximum length of text to display, 0 forgoes a hard limit. | 602 // The maximum length of text to display, 0 forgoes a hard limit. |
544 size_t truncate_length_; | 603 size_t truncate_length_; |
545 | 604 |
546 // The obscured and/or truncated text that will be displayed. | 605 // The obscured and/or truncated text that will be displayed. |
547 base::string16 layout_text_; | 606 base::string16 layout_text_; |
548 | 607 |
| 608 // Whether the text should be broken into multiple lines. Uses the width of |
| 609 // |display_rect_| as the width cap. |
| 610 bool multiline_; |
| 611 |
549 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 612 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
550 bool fade_head_; | 613 bool fade_head_; |
551 bool fade_tail_; | 614 bool fade_tail_; |
552 | 615 |
553 // Is the background transparent (either partially or fully)? | 616 // Is the background transparent (either partially or fully)? |
554 bool background_is_transparent_; | 617 bool background_is_transparent_; |
555 | 618 |
556 // The local display area for rendering the text. | 619 // The local display area for rendering the text. |
557 Rect display_rect_; | 620 Rect display_rect_; |
558 | 621 |
559 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) | 622 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) |
560 // that results in incorrect clipping when drawing to the document margins. | 623 // that results in incorrect clipping when drawing to the document margins. |
561 // This field allows disabling clipping to work around the issue. | 624 // This field allows disabling clipping to work around the issue. |
562 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 625 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
563 bool clip_to_display_rect_; | 626 bool clip_to_display_rect_; |
564 | 627 |
565 // The offset for the text to be drawn, relative to the display area. | 628 // The offset for the text to be drawn, relative to the display area. |
566 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). | 629 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
567 Vector2d display_offset_; | 630 Vector2d display_offset_; |
568 | 631 |
569 // The cached bounds and offset are invalidated by changes to the cursor, | 632 // The cached bounds and offset are invalidated by changes to the cursor, |
570 // selection, font, and other operations that adjust the visible text bounds. | 633 // selection, font, and other operations that adjust the visible text bounds. |
571 bool cached_bounds_and_offset_valid_; | 634 bool cached_bounds_and_offset_valid_; |
572 | 635 |
573 // Text shadows to be drawn. | 636 // Text shadows to be drawn. |
574 ShadowValues text_shadows_; | 637 ShadowValues text_shadows_; |
575 | 638 |
| 639 // BreakList to find valid positions to break the line at. |
| 640 BreakList<size_t> line_breaks_; |
| 641 |
| 642 // Lines computed by EnsureLayout. Should be emptied whenever the text, styles |
| 643 // or |display_rect_| changes. |
| 644 std::vector<internal::Line> lines_; |
| 645 |
576 DISALLOW_COPY_AND_ASSIGN(RenderText); | 646 DISALLOW_COPY_AND_ASSIGN(RenderText); |
577 }; | 647 }; |
578 | 648 |
579 } // namespace gfx | 649 } // namespace gfx |
580 | 650 |
581 #endif // UI_GFX_RENDER_TEXT_H_ | 651 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |