Chromium Code Reviews| 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 { | |
|
Alexei Svitkine (slow)
2013/08/08 15:01:03
Add a brief comment explaining this class.
ckocagil
2013/08/10 13:39:23
Done.
| |
| 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) {} | |
|
Alexei Svitkine (slow)
2013/08/08 15:01:03
Please define the bodies of these in the .cc file.
ckocagil
2013/08/10 13:39:23
Done.
| |
| 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. | |
|
Alexei Svitkine (slow)
2013/08/08 15:01:03
Please move the comments on their own line before
ckocagil
2013/08/10 13:39:23
Done.
| |
| 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 SetMultiline(bool multiline); | |
|
Alexei Svitkine (slow)
2013/08/08 15:01:03
Add a TODO comment here that this currently only s
ckocagil
2013/08/10 13:39:23
Done.
| |
| 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 std::vector<internal::Line>& lines() const { return lines_; } | |
| 387 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } | |
| 388 | |
| 350 const Vector2d& GetUpdatedDisplayOffset(); | 389 const Vector2d& GetUpdatedDisplayOffset(); |
| 351 | 390 |
| 352 void set_cached_bounds_and_offset_valid(bool valid) { | 391 void set_cached_bounds_and_offset_valid(bool valid) { |
| 353 cached_bounds_and_offset_valid_ = valid; | 392 cached_bounds_and_offset_valid_ = valid; |
| 354 } | 393 } |
| 355 | 394 |
| 356 // Get the selection model that visually neighbors |position| by |break_type|. | 395 // Get the selection model that visually neighbors |position| by |break_type|. |
| 357 // The returned value represents a cursor/caret position without a selection. | 396 // The returned value represents a cursor/caret position without a selection. |
| 358 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 397 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
| 359 BreakType break_type, | 398 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; | 437 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; |
| 399 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; | 438 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; |
| 400 | 439 |
| 401 // Return true if cursor can appear in front of the character at |position|, | 440 // 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. | 441 // which means it is a grapheme boundary or the first character in the text. |
| 403 virtual bool IsCursorablePosition(size_t position) = 0; | 442 virtual bool IsCursorablePosition(size_t position) = 0; |
| 404 | 443 |
| 405 // Reset the layout to be invalid. | 444 // Reset the layout to be invalid. |
| 406 virtual void ResetLayout() = 0; | 445 virtual void ResetLayout() = 0; |
| 407 | 446 |
| 408 // Ensure the text is laid out. | 447 // Ensure the text is laid out, lines are computed, and |lines_| is valid. |
| 409 virtual void EnsureLayout() = 0; | 448 virtual void EnsureLayout() = 0; |
| 410 | 449 |
| 411 // Draw the text. | 450 // Draw the text. |
| 412 virtual void DrawVisualText(Canvas* canvas) = 0; | 451 virtual void DrawVisualText(Canvas* canvas) = 0; |
| 413 | 452 |
| 414 // Returns the text used for layout, which may be obscured or truncated. | 453 // Returns the text used for layout, which may be obscured or truncated. |
| 415 const base::string16& GetLayoutText() const; | 454 const base::string16& GetLayoutText() const; |
| 416 | 455 |
| 456 // Returns layout text positions that are suitable for breaking lines at. | |
| 457 const BreakList<size_t>& GetLineBreaks(); | |
| 458 | |
| 417 // Apply (and undo) temporary composition underlines and selection colors. | 459 // Apply (and undo) temporary composition underlines and selection colors. |
| 418 void ApplyCompositionAndSelectionStyles(); | 460 void ApplyCompositionAndSelectionStyles(); |
| 419 void UndoCompositionAndSelectionStyles(); | 461 void UndoCompositionAndSelectionStyles(); |
| 420 | 462 |
| 421 // Returns the text offset from the origin after applying text alignment and | 463 // Returns the line offset from the origin after applying text alignment and |
| 422 // display offset. | 464 // display offset. |
| 423 Vector2d GetTextOffset(); | 465 Vector2d GetLineOffset(size_t line_number); |
| 424 | 466 |
| 425 // Convert points from the text space to the view space and back. | 467 // 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. | 468 // Handles the display area, display offset, application LTR/RTL mode and |
| 469 // multi-line. | |
| 427 Point ToTextPoint(const Point& point); | 470 Point ToTextPoint(const Point& point); |
| 428 Point ToViewPoint(const Point& point); | 471 Point ToViewPoint(const Point& point); |
| 429 | 472 |
| 430 // Returns the text offset from the origin, taking into account text alignment | 473 // Convert the range in text coordinates to rects in view coordinates that |
| 474 // cover the given range. | |
| 475 std::vector<Rect> TextBoundsToViewBounds(const ui::Range& x); | |
| 476 | |
| 477 // Returns the line offset from the origin, taking into account text alignment | |
| 431 // only. | 478 // only. |
| 432 Vector2d GetAlignmentOffset(); | 479 Vector2d GetAlignmentOffset(size_t line_number); |
| 433 | 480 |
| 434 // Applies fade effects to |renderer|. | 481 // Applies fade effects to |renderer|. |
| 435 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 482 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
| 436 | 483 |
| 437 // Applies text shadows to |renderer|. | 484 // Applies text shadows to |renderer|. |
| 438 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 485 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
| 439 | 486 |
| 440 // A convenience function to check whether the glyph attached to the caret | 487 // A convenience function to check whether the glyph attached to the caret |
| 441 // is within the given range. | 488 // is within the given range. |
| 442 static bool RangeContainsCaret(const ui::Range& range, | 489 static bool RangeContainsCaret(const ui::Range& range, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 bool obscured_; | 586 bool obscured_; |
| 540 // The index at which the char should be revealed in the obscured text. | 587 // The index at which the char should be revealed in the obscured text. |
| 541 int obscured_reveal_index_; | 588 int obscured_reveal_index_; |
| 542 | 589 |
| 543 // The maximum length of text to display, 0 forgoes a hard limit. | 590 // The maximum length of text to display, 0 forgoes a hard limit. |
| 544 size_t truncate_length_; | 591 size_t truncate_length_; |
| 545 | 592 |
| 546 // The obscured and/or truncated text that will be displayed. | 593 // The obscured and/or truncated text that will be displayed. |
| 547 base::string16 layout_text_; | 594 base::string16 layout_text_; |
| 548 | 595 |
| 596 // Whether the text should be broken into multiple lines. Uses the width of | |
| 597 // |display_rect_| as the width cap. | |
| 598 bool multiline_; | |
| 599 | |
| 549 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 600 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
| 550 bool fade_head_; | 601 bool fade_head_; |
| 551 bool fade_tail_; | 602 bool fade_tail_; |
| 552 | 603 |
| 553 // Is the background transparent (either partially or fully)? | 604 // Is the background transparent (either partially or fully)? |
| 554 bool background_is_transparent_; | 605 bool background_is_transparent_; |
| 555 | 606 |
| 556 // The local display area for rendering the text. | 607 // The local display area for rendering the text. |
| 557 Rect display_rect_; | 608 Rect display_rect_; |
| 558 | 609 |
| 559 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) | 610 // 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. | 611 // that results in incorrect clipping when drawing to the document margins. |
| 561 // This field allows disabling clipping to work around the issue. | 612 // This field allows disabling clipping to work around the issue. |
| 562 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 613 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
| 563 bool clip_to_display_rect_; | 614 bool clip_to_display_rect_; |
| 564 | 615 |
| 565 // The offset for the text to be drawn, relative to the display area. | 616 // 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). | 617 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
| 567 Vector2d display_offset_; | 618 Vector2d display_offset_; |
| 568 | 619 |
| 569 // The cached bounds and offset are invalidated by changes to the cursor, | 620 // The cached bounds and offset are invalidated by changes to the cursor, |
| 570 // selection, font, and other operations that adjust the visible text bounds. | 621 // selection, font, and other operations that adjust the visible text bounds. |
| 571 bool cached_bounds_and_offset_valid_; | 622 bool cached_bounds_and_offset_valid_; |
| 572 | 623 |
| 573 // Text shadows to be drawn. | 624 // Text shadows to be drawn. |
| 574 ShadowValues text_shadows_; | 625 ShadowValues text_shadows_; |
| 575 | 626 |
| 627 // BreakList to find valid positions to break the line at. | |
| 628 BreakList<size_t> line_breaks_; | |
| 629 | |
| 630 // Lines computed by EnsureLayout. Should be emptied whenever the text, styles | |
| 631 // or |display_rect_| changes. | |
| 632 std::vector<internal::Line> lines_; | |
| 633 | |
| 576 DISALLOW_COPY_AND_ASSIGN(RenderText); | 634 DISALLOW_COPY_AND_ASSIGN(RenderText); |
| 577 }; | 635 }; |
| 578 | 636 |
| 579 } // namespace gfx | 637 } // namespace gfx |
| 580 | 638 |
| 581 #endif // UI_GFX_RENDER_TEXT_H_ | 639 #endif // UI_GFX_RENDER_TEXT_H_ |
| OLD | NEW |