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