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" | |
| 17 #include "base/stl_util.h" | |
| 16 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 17 #include "skia/ext/refptr.h" | 19 #include "skia/ext/refptr.h" |
| 18 #include "third_party/skia/include/core/SkColor.h" | 20 #include "third_party/skia/include/core/SkColor.h" |
| 19 #include "third_party/skia/include/core/SkPaint.h" | 21 #include "third_party/skia/include/core/SkPaint.h" |
| 20 #include "third_party/skia/include/core/SkRect.h" | 22 #include "third_party/skia/include/core/SkRect.h" |
| 21 #include "ui/base/range/range.h" | 23 #include "ui/base/range/range.h" |
| 22 #include "ui/gfx/break_list.h" | 24 #include "ui/gfx/break_list.h" |
| 23 #include "ui/gfx/font_list.h" | 25 #include "ui/gfx/font_list.h" |
| 24 #include "ui/gfx/point.h" | 26 #include "ui/gfx/point.h" |
| 25 #include "ui/gfx/rect.h" | 27 #include "ui/gfx/rect.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 private: | 106 private: |
| 105 BreakList<SkColor> colors_; | 107 BreakList<SkColor> colors_; |
| 106 std::vector<BreakList<bool> > styles_; | 108 std::vector<BreakList<bool> > styles_; |
| 107 | 109 |
| 108 BreakList<SkColor>::const_iterator color_; | 110 BreakList<SkColor>::const_iterator color_; |
| 109 std::vector<BreakList<bool>::const_iterator> style_; | 111 std::vector<BreakList<bool>::const_iterator> style_; |
| 110 | 112 |
| 111 DISALLOW_COPY_AND_ASSIGN(StyleIterator); | 113 DISALLOW_COPY_AND_ASSIGN(StyleIterator); |
| 112 }; | 114 }; |
| 113 | 115 |
| 116 struct LineSegment { | |
| 117 ui::Range x_pos; // x coord, in text coordinates. | |
| 118 ui::Range char_pos; | |
| 119 }; | |
| 120 | |
| 121 struct Line { | |
| 122 Line() : width(0), | |
|
msw
2013/07/17 06:47:18
nit: make this all one line or indent intitializer
ckocagil
2013/07/19 19:40:50
Done.
| |
| 123 height(0), | |
| 124 preceding_heights(0), | |
| 125 baseline(0) { | |
| 126 } | |
| 127 Line(Line& other) { | |
| 128 *this = other; | |
| 129 } | |
| 130 Line& operator=(Line& other) { | |
| 131 width = other.width; | |
| 132 height = other.height; | |
| 133 preceding_heights = other.preceding_heights; | |
| 134 baseline = other.baseline; | |
| 135 segments.swap(other.segments); | |
|
msw
2013/07/17 06:47:18
Swap makes this destructive to the right hand side
ckocagil
2013/07/19 19:40:50
Yes, these are for STL containers only. I'm adding
msw
2013/08/15 02:44:17
Use ScopedVector and avoid copy/assign if possible
| |
| 136 return *this; | |
| 137 } | |
| 138 ScopedVector<LineSegment> segments; | |
| 139 int width; | |
| 140 int height; // Maximum text height of all runs on this line. | |
|
msw
2013/07/17 06:47:18
See Yuki's ongoing work to find common heights and
| |
| 141 int preceding_heights; // Sum of |height| fields of preceding lines. | |
| 142 int baseline; // Maximum baseline of all runs on this line. | |
| 143 }; | |
| 144 | |
| 114 } // namespace internal | 145 } // namespace internal |
| 115 | 146 |
| 116 // RenderText represents an abstract model of styled text and its corresponding | 147 // 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, | 148 // visual layout. Support is built in for a cursor, a selection, simple styling, |
| 118 // complex scripts, and bi-directional text. Implementations provide mechanisms | 149 // complex scripts, and bi-directional text. Implementations provide mechanisms |
| 119 // for rendering and translation between logical and visual data. | 150 // for rendering and translation between logical and visual data. |
| 120 class UI_EXPORT RenderText { | 151 class UI_EXPORT RenderText { |
| 121 public: | 152 public: |
| 122 virtual ~RenderText(); | 153 virtual ~RenderText(); |
| 123 | 154 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 bool focused() const { return focused_; } | 203 bool focused() const { return focused_; } |
| 173 void set_focused(bool focused) { focused_ = focused; } | 204 void set_focused(bool focused) { focused_ = focused; } |
| 174 | 205 |
| 175 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 206 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; } | 207 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
| 177 | 208 |
| 178 // In an obscured (password) field, all text is drawn as asterisks or bullets. | 209 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
| 179 bool obscured() const { return obscured_; } | 210 bool obscured() const { return obscured_; } |
| 180 void SetObscured(bool obscured); | 211 void SetObscured(bool obscured); |
| 181 | 212 |
| 213 bool multiline() const { return multiline_; } | |
| 214 void set_multiline(bool multiline) { multiline_ = multiline; } | |
| 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 |
| 189 // Set the maximum length of the displayed layout text, not the actual text. | 223 // 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 | 224 // A |length| of 0 forgoes a hard limit, but does not guarantee proper |
| 191 // functionality of very long strings. Applies to subsequent SetText calls. | 225 // 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 | 314 // of the text, considering only the dominant direction returned by |
| 281 // |GetTextDirection()|, not the direction of a particular run. | 315 // |GetTextDirection()|, not the direction of a particular run. |
| 282 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 316 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
| 283 | 317 |
| 284 // Returns the size in pixels of the entire string. For the height, this will | 318 // 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. | 319 // 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 | 320 // Note that this returns the raw size of the string, which does not include |
| 287 // the margin area of text shadows. | 321 // the margin area of text shadows. |
| 288 virtual Size GetStringSize() = 0; | 322 virtual Size GetStringSize() = 0; |
| 289 | 323 |
| 324 // Returns the size required to display the current multiline text, which is | |
| 325 // horizontally capped by the width of |display_rect()|. | |
| 326 // TODO(ckocagil): Merge with GetContentWidth. | |
| 327 virtual Size GetMultilineTextSize() = 0; | |
| 328 | |
| 290 // Returns the width of content, which reserves room for the cursor if | 329 // Returns the width of content, which reserves room for the cursor if |
| 291 // |cursor_enabled_| is true. | 330 // |cursor_enabled_| is true. |
| 292 int GetContentWidth(); | 331 int GetContentWidth(); |
| 293 | 332 |
| 294 // Returns the common baseline of the text. The returned value is the vertical | 333 // 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. | 334 // offset from the top of |display_rect| to the text baseline, in pixels. |
| 296 virtual int GetBaseline() = 0; | 335 virtual int GetBaseline() = 0; |
| 297 | 336 |
| 298 void Draw(Canvas* canvas); | 337 void Draw(Canvas* canvas); |
| 299 | 338 |
| (...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 | 379 // specifies the character range for which the corresponding font has been |
| 341 // chosen. | 380 // chosen. |
| 342 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 381 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
| 343 | 382 |
| 344 protected: | 383 protected: |
| 345 RenderText(); | 384 RenderText(); |
| 346 | 385 |
| 347 const BreakList<SkColor>& colors() const { return colors_; } | 386 const BreakList<SkColor>& colors() const { return colors_; } |
| 348 const std::vector<BreakList<bool> >& styles() const { return styles_; } | 387 const std::vector<BreakList<bool> >& styles() const { return styles_; } |
| 349 | 388 |
| 389 const std::vector<size_t>& line_breaks() const { return line_breaks_; } | |
| 390 | |
| 391 const std::vector<internal::Line>& lines() const { return lines_; } | |
| 392 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); } | |
| 393 | |
| 394 bool lines_valid() const { return lines_valid_; } | |
| 395 void set_lines_valid(bool lines_valid) { lines_valid_ = lines_valid; } | |
| 396 | |
| 350 const Vector2d& GetUpdatedDisplayOffset(); | 397 const Vector2d& GetUpdatedDisplayOffset(); |
| 351 | 398 |
| 352 void set_cached_bounds_and_offset_valid(bool valid) { | 399 void set_cached_bounds_and_offset_valid(bool valid) { |
| 353 cached_bounds_and_offset_valid_ = valid; | 400 cached_bounds_and_offset_valid_ = valid; |
| 354 } | 401 } |
| 355 | 402 |
| 356 // Get the selection model that visually neighbors |position| by |break_type|. | 403 // Get the selection model that visually neighbors |position| by |break_type|. |
| 357 // The returned value represents a cursor/caret position without a selection. | 404 // The returned value represents a cursor/caret position without a selection. |
| 358 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 405 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
| 359 BreakType break_type, | 406 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; | 445 virtual size_t TextIndexToLayoutIndex(size_t index) const = 0; |
| 399 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; | 446 virtual size_t LayoutIndexToTextIndex(size_t index) const = 0; |
| 400 | 447 |
| 401 // Return true if cursor can appear in front of the character at |position|, | 448 // 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. | 449 // which means it is a grapheme boundary or the first character in the text. |
| 403 virtual bool IsCursorablePosition(size_t position) = 0; | 450 virtual bool IsCursorablePosition(size_t position) = 0; |
| 404 | 451 |
| 405 // Reset the layout to be invalid. | 452 // Reset the layout to be invalid. |
| 406 virtual void ResetLayout() = 0; | 453 virtual void ResetLayout() = 0; |
| 407 | 454 |
| 408 // Ensure the text is laid out. | 455 // Ensure the text is laid out, lines are computed, and |lines_| is valid. |
| 409 virtual void EnsureLayout() = 0; | 456 virtual void EnsureLayout() = 0; |
| 410 | 457 |
| 411 // Draw the text. | 458 // Draw the text. |
| 412 virtual void DrawVisualText(Canvas* canvas) = 0; | 459 virtual void DrawVisualText(Canvas* canvas) = 0; |
| 413 | 460 |
| 414 // Returns the text used for layout, which may be obscured or truncated. | 461 // Returns the text used for layout, which may be obscured or truncated. |
| 415 const base::string16& GetLayoutText() const; | 462 const base::string16& GetLayoutText() const; |
| 416 | 463 |
| 417 // Apply (and undo) temporary composition underlines and selection colors. | 464 // Apply (and undo) temporary composition underlines and selection colors. |
| 418 void ApplyCompositionAndSelectionStyles(); | 465 void ApplyCompositionAndSelectionStyles(); |
| 419 void UndoCompositionAndSelectionStyles(); | 466 void UndoCompositionAndSelectionStyles(); |
| 420 | 467 |
| 421 // Returns the text offset from the origin after applying text alignment and | 468 // Returns the line offset from the origin after applying text alignment and |
| 422 // display offset. | 469 // display offset. |
| 423 Vector2d GetTextOffset(); | 470 Vector2d GetLineOffset(int line); |
|
msw
2013/07/17 06:47:18
Rename to |line_number| to clarify the arg; and us
ckocagil
2013/07/19 19:40:50
Done.
| |
| 424 | 471 |
| 425 // Convert points from the text space to the view space and back. | 472 // 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. | 473 // Handles the display area, display offset, and the application LTR/RTL mode. |
|
msw
2013/07/17 06:47:18
Update the comment, to reflect the new multi-line
ckocagil
2013/07/19 19:40:50
Done.
| |
| 427 Point ToTextPoint(const Point& point); | 474 Point ToTextPoint(const Point& point); |
| 428 Point ToViewPoint(const Point& point); | 475 Point ToViewPoint(const Point& point); |
| 429 | 476 |
| 430 // Returns the text offset from the origin, taking into account text alignment | 477 // Convert the range in text coordinates to rects in view coordinates that |
| 478 // cover the given range. | |
| 479 std::vector<Rect> RangeToViewRects(const ui::Range& x); | |
|
msw
2013/07/17 06:47:18
I'm trying to think of a better name, maybe: TextB
ckocagil
2013/07/19 19:40:50
Changed to TextBoundsToViewBounds.
| |
| 480 | |
| 481 // Returns the line offset from the origin, taking into account text alignment | |
| 431 // only. | 482 // only. |
| 432 Vector2d GetAlignmentOffset(); | 483 Vector2d GetAlignmentOffset(int line); |
|
msw
2013/07/17 06:47:18
Ditto: Rename to |line_number| to clarify the arg;
ckocagil
2013/07/19 19:40:50
Done.
| |
| 433 | 484 |
| 434 // Applies fade effects to |renderer|. | 485 // Applies fade effects to |renderer|. |
| 435 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 486 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
| 436 | 487 |
| 437 // Applies text shadows to |renderer|. | 488 // Applies text shadows to |renderer|. |
| 438 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 489 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
| 439 | 490 |
| 440 // A convenience function to check whether the glyph attached to the caret | 491 // A convenience function to check whether the glyph attached to the caret |
| 441 // is within the given range. | 492 // is within the given range. |
| 442 static bool RangeContainsCaret(const ui::Range& range, | 493 static bool RangeContainsCaret(const ui::Range& range, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 bool obscured_; | 590 bool obscured_; |
| 540 // 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. |
| 541 int obscured_reveal_index_; | 592 int obscured_reveal_index_; |
| 542 | 593 |
| 543 // 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. |
| 544 size_t truncate_length_; | 595 size_t truncate_length_; |
| 545 | 596 |
| 546 // The obscured and/or truncated text that will be displayed. | 597 // The obscured and/or truncated text that will be displayed. |
| 547 base::string16 layout_text_; | 598 base::string16 layout_text_; |
| 548 | 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 | |
| 549 // 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_|. |
| 550 bool fade_head_; | 605 bool fade_head_; |
| 551 bool fade_tail_; | 606 bool fade_tail_; |
| 552 | 607 |
| 553 // Is the background transparent (either partially or fully)? | 608 // Is the background transparent (either partially or fully)? |
| 554 bool background_is_transparent_; | 609 bool background_is_transparent_; |
| 555 | 610 |
| 556 // The local display area for rendering the text. | 611 // The local display area for rendering the text. |
| 557 Rect display_rect_; | 612 Rect display_rect_; |
| 558 | 613 |
| 559 // 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) |
| 560 // that results in incorrect clipping when drawing to the document margins. | 615 // that results in incorrect clipping when drawing to the document margins. |
| 561 // This field allows disabling clipping to work around the issue. | 616 // This field allows disabling clipping to work around the issue. |
| 562 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 617 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
| 563 bool clip_to_display_rect_; | 618 bool clip_to_display_rect_; |
| 564 | 619 |
| 565 // 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. |
| 566 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). | 621 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
| 567 Vector2d display_offset_; | 622 Vector2d display_offset_; |
| 568 | 623 |
| 569 // 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, |
| 570 // selection, font, and other operations that adjust the visible text bounds. | 625 // selection, font, and other operations that adjust the visible text bounds. |
| 571 bool cached_bounds_and_offset_valid_; | 626 bool cached_bounds_and_offset_valid_; |
| 572 | 627 |
| 573 // Text shadows to be drawn. | 628 // Text shadows to be drawn. |
| 574 ShadowValues text_shadows_; | 629 ShadowValues text_shadows_; |
| 575 | 630 |
| 631 // Positions at which we can break the line. | |
| 632 std::vector<size_t> line_breaks_; | |
| 633 | |
| 634 // Lines computed by EnsureLayout. Valid when |lines_valid_| is true. | |
| 635 std::vector<internal::Line> lines_; | |
| 636 | |
| 637 // Whether the current |lines_| is valid. |lines_| is invalidated whenever | |
|
msw
2013/07/17 06:47:18
Since we reconstruct |lines_| on ComputeLines, can
ckocagil
2013/07/19 19:40:50
Done.
| |
| 638 // the text, styles or |display_rect_| changes. | |
| 639 bool lines_valid_; | |
| 640 | |
| 576 DISALLOW_COPY_AND_ASSIGN(RenderText); | 641 DISALLOW_COPY_AND_ASSIGN(RenderText); |
| 577 }; | 642 }; |
| 578 | 643 |
| 579 } // namespace gfx | 644 } // namespace gfx |
| 580 | 645 |
| 581 #endif // UI_GFX_RENDER_TEXT_H_ | 646 #endif // UI_GFX_RENDER_TEXT_H_ |
| OLD | NEW |