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/07/09 15:35:13
This should live in the class unless you're planni
msw
2013/07/10 04:01:56
Add a struct comment to describe what this is.
(a
msw
2013/07/10 04:01:56
I disagree; SkiaTextRenderer and StyleIterator are
ckocagil
2013/07/13 16:05:10
Keeping it in internal namespace for now.
| |
| 116 ui::Range x_pos; // x coord, in text coordinates | |
| 117 ui::Range char_pos; | |
| 118 }; | |
| 119 | |
| 114 } // namespace internal | 120 } // namespace internal |
| 115 | 121 |
| 116 // RenderText represents an abstract model of styled text and its corresponding | 122 // 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, | 123 // visual layout. Support is built in for a cursor, a selection, simple styling, |
| 118 // complex scripts, and bi-directional text. Implementations provide mechanisms | 124 // complex scripts, and bi-directional text. Implementations provide mechanisms |
| 119 // for rendering and translation between logical and visual data. | 125 // for rendering and translation between logical and visual data. |
| 120 class UI_EXPORT RenderText { | 126 class UI_EXPORT RenderText { |
| 121 public: | 127 public: |
| 122 virtual ~RenderText(); | 128 virtual ~RenderText(); |
| 123 | 129 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 bool focused() const { return focused_; } | 185 bool focused() const { return focused_; } |
| 180 void set_focused(bool focused) { focused_ = focused; } | 186 void set_focused(bool focused) { focused_ = focused; } |
| 181 | 187 |
| 182 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 188 bool clip_to_display_rect() const { return clip_to_display_rect_; } |
| 183 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } | 189 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
| 184 | 190 |
| 185 // In an obscured (password) field, all text is drawn as asterisks or bullets. | 191 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
| 186 bool obscured() const { return obscured_; } | 192 bool obscured() const { return obscured_; } |
| 187 void SetObscured(bool obscured); | 193 void SetObscured(bool obscured); |
| 188 | 194 |
| 195 bool multiline() const { return multiline_; } | |
| 196 void set_multiline(bool multiline) { multiline_ = multiline; } | |
| 197 | |
| 189 // Makes a char in obscured text at |index| to be revealed. |index| should be | 198 // Makes a char in obscured text at |index| to be revealed. |index| should be |
| 190 // a UTF16 text index. If there is a previous revealed index, the previous one | 199 // a UTF16 text index. If there is a previous revealed index, the previous one |
| 191 // is cleared and only the last set index will be revealed. If |index| is -1 | 200 // is cleared and only the last set index will be revealed. If |index| is -1 |
| 192 // or out of range, no char will be revealed. The revealed index is also | 201 // or out of range, no char will be revealed. The revealed index is also |
| 193 // cleared when SetText or SetObscured is called. | 202 // cleared when SetText or SetObscured is called. |
| 194 void SetObscuredRevealIndex(int index); | 203 void SetObscuredRevealIndex(int index); |
| 195 | 204 |
| 196 const Rect& display_rect() const { return display_rect_; } | 205 const Rect& display_rect() const { return display_rect_; } |
| 197 void SetDisplayRect(const Rect& r); | 206 void SetDisplayRect(const Rect& r); |
| 198 | 207 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 // of the text, considering only the dominant direction returned by | 290 // of the text, considering only the dominant direction returned by |
| 282 // |GetTextDirection()|, not the direction of a particular run. | 291 // |GetTextDirection()|, not the direction of a particular run. |
| 283 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 292 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
| 284 | 293 |
| 285 // Returns the size in pixels of the entire string. For the height, this will | 294 // Returns the size in pixels of the entire string. For the height, this will |
| 286 // return the maximum height among the different fonts in the text runs. | 295 // return the maximum height among the different fonts in the text runs. |
| 287 // Note that this returns the raw size of the string, which does not include | 296 // Note that this returns the raw size of the string, which does not include |
| 288 // the margin area of text shadows. | 297 // the margin area of text shadows. |
| 289 virtual Size GetStringSize() = 0; | 298 virtual Size GetStringSize() = 0; |
| 290 | 299 |
| 300 virtual Size RenderText::GetMultilineTextSize() = 0; | |
|
msw
2013/07/10 04:01:56
Remove "RenderText::" and add a comment (explain t
ckocagil
2013/07/13 16:05:10
Done.
| |
| 301 | |
| 291 // Returns the width of content, which reserves room for the cursor if | 302 // Returns the width of content, which reserves room for the cursor if |
| 292 // |cursor_enabled_| is true. | 303 // |cursor_enabled_| is true. |
| 293 int GetContentWidth(); | 304 int GetContentWidth(); |
| 294 | 305 |
| 295 // Returns the common baseline of the text. The returned value is the vertical | 306 // Returns the common baseline of the text. The returned value is the vertical |
| 296 // offset from the top of |display_rect| to the text baseline, in pixels. | 307 // offset from the top of |display_rect| to the text baseline, in pixels. |
| 297 virtual int GetBaseline() = 0; | 308 virtual int GetBaseline() = 0; |
| 298 | 309 |
| 299 void Draw(Canvas* canvas); | 310 void Draw(Canvas* canvas); |
| 300 | 311 |
| (...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 | 352 // specifies the character range for which the corresponding font has been |
| 342 // chosen. | 353 // chosen. |
| 343 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 354 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
| 344 | 355 |
| 345 protected: | 356 protected: |
| 346 RenderText(); | 357 RenderText(); |
| 347 | 358 |
| 348 const BreakList<SkColor>& colors() const { return colors_; } | 359 const BreakList<SkColor>& colors() const { return colors_; } |
| 349 const std::vector<BreakList<bool> >& styles() const { return styles_; } | 360 const std::vector<BreakList<bool> >& styles() const { return styles_; } |
| 350 | 361 |
| 362 const std::vector<size_t>& line_breaks() const { return line_breaks_; } | |
| 363 | |
| 364 const std::vector<ScopedVector<internal::LineSegment> >& lines() const { | |
| 365 return lines_; | |
| 366 } | |
| 367 void set_lines(std::vector<ScopedVector<internal::LineSegment> >* lines) { | |
| 368 lines_.swap(*lines); | |
| 369 } | |
| 370 | |
| 371 bool lines_valid() const { return lines_valid_; } | |
| 372 void set_lines_valid(bool lines_valid) { | |
|
msw
2013/07/10 04:01:56
nit: rename the param to |valid| to fit this on on
ckocagil
2013/07/13 16:05:10
Actually it fits without renaming.
| |
| 373 lines_valid_ = lines_valid; | |
| 374 } | |
| 375 | |
| 376 int LineWidth(int line) { | |
|
Alexei Svitkine (slow)
2013/07/09 15:35:13
This is not a trivial method so the implementation
ckocagil
2013/07/13 16:05:10
Removed since we now have a Line struct that cache
| |
| 377 int width = 0; | |
| 378 for (size_t i = 0; i < lines()[line].size(); ++i) | |
|
Alexei Svitkine (slow)
2013/07/09 15:35:13
Add a DCHECK() that |line| is within a valid range
ckocagil
2013/07/13 16:05:10
This method no longer exists.
| |
| 379 width += lines()[line][i]->x_pos.length(); | |
| 380 return width; | |
| 381 } | |
| 382 | |
| 351 const Vector2d& GetUpdatedDisplayOffset(); | 383 const Vector2d& GetUpdatedDisplayOffset(); |
| 352 | 384 |
| 353 void set_cached_bounds_and_offset_valid(bool valid) { | 385 void set_cached_bounds_and_offset_valid(bool valid) { |
| 354 cached_bounds_and_offset_valid_ = valid; | 386 cached_bounds_and_offset_valid_ = valid; |
| 355 } | 387 } |
| 356 | 388 |
| 357 // Get the selection model that visually neighbors |position| by |break_type|. | 389 // Get the selection model that visually neighbors |position| by |break_type|. |
| 358 // The returned value represents a cursor/caret position without a selection. | 390 // The returned value represents a cursor/caret position without a selection. |
| 359 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 391 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
| 360 BreakType break_type, | 392 BreakType break_type, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 | 437 |
| 406 // Reset the layout to be invalid. | 438 // Reset the layout to be invalid. |
| 407 virtual void ResetLayout() = 0; | 439 virtual void ResetLayout() = 0; |
| 408 | 440 |
| 409 // Ensure the text is laid out. | 441 // Ensure the text is laid out. |
| 410 virtual void EnsureLayout() = 0; | 442 virtual void EnsureLayout() = 0; |
| 411 | 443 |
| 412 // Draw the text. | 444 // Draw the text. |
| 413 virtual void DrawVisualText(Canvas* canvas) = 0; | 445 virtual void DrawVisualText(Canvas* canvas) = 0; |
| 414 | 446 |
| 447 virtual void ComputeLines() = 0; | |
|
msw
2013/07/10 04:01:56
Add a comment, move this above DrawVisualText (it'
ckocagil
2013/07/13 16:05:10
I moved this to render_text_win since we now call
| |
| 448 | |
| 415 // Returns the text used for layout, which may be |obscured_text_|. | 449 // Returns the text used for layout, which may be |obscured_text_|. |
| 416 const base::string16& GetLayoutText() const; | 450 const base::string16& GetLayoutText() const; |
| 417 | 451 |
| 418 // Apply (and undo) temporary composition underlines and selection colors. | 452 // Apply (and undo) temporary composition underlines and selection colors. |
| 419 void ApplyCompositionAndSelectionStyles(); | 453 void ApplyCompositionAndSelectionStyles(); |
| 420 void UndoCompositionAndSelectionStyles(); | 454 void UndoCompositionAndSelectionStyles(); |
| 421 | 455 |
| 422 // Returns the text offset from the origin after applying text alignment and | 456 // Returns the text offset from the origin after applying text alignment and |
| 423 // display offset. | 457 // display offset. |
| 424 Vector2d GetTextOffset(); | 458 Vector2d GetTextOffset(int line_width); |
|
msw
2013/07/10 04:01:56
Comment on what |line_width| is used for here. It'
ckocagil
2013/07/13 16:05:10
I renamed this to |GetLineOffset| and it now takes
msw
2013/07/17 06:47:18
Sounds good.
| |
| 425 | 459 |
| 426 // Convert points from the text space to the view space and back. | 460 // Convert points from the text space to the view space and back. |
| 427 // Handles the display area, display offset, and the application LTR/RTL mode. | 461 // Handles the display area, display offset, and the application LTR/RTL mode. |
| 428 Point ToTextPoint(const Point& point); | 462 Point ToTextPoint(const Point& point); |
| 429 Point ToViewPoint(const Point& point); | 463 Point ToViewPoint(const Point& point); |
| 430 | 464 |
| 465 std::vector<Rect> RangeToViewRects(const ui::Range& x, int y); | |
|
msw
2013/07/10 04:01:56
nit: Comment on what this does and what the args a
ckocagil
2013/07/13 16:05:10
Comment added, |y| removed.
| |
| 466 | |
| 431 // Returns the text offset from the origin, taking into account text alignment | 467 // Returns the text offset from the origin, taking into account text alignment |
| 432 // only. | 468 // only. |
| 433 Vector2d GetAlignmentOffset(); | 469 Vector2d GetAlignmentOffset(int line_width); |
|
msw
2013/07/10 04:01:56
Ditto comment from GetTextOffset: Comment and rena
ckocagil
2013/07/13 16:05:10
This method now takes a line number.
| |
| 434 | 470 |
| 435 // Applies fade effects to |renderer|. | 471 // Applies fade effects to |renderer|. |
| 436 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); | 472 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer); |
| 437 | 473 |
| 438 // Applies text shadows to |renderer|. | 474 // Applies text shadows to |renderer|. |
| 439 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 475 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
| 440 | 476 |
| 441 // A convenience function to check whether the glyph attached to the caret | 477 // A convenience function to check whether the glyph attached to the caret |
| 442 // is within the given range. | 478 // is within the given range. |
| 443 static bool RangeContainsCaret(const ui::Range& range, | 479 static bool RangeContainsCaret(const ui::Range& range, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 bool composition_and_selection_styles_applied_; | 574 bool composition_and_selection_styles_applied_; |
| 539 | 575 |
| 540 // A flag and the text to display for obscured (password) fields. | 576 // A flag and the text to display for obscured (password) fields. |
| 541 // Asterisks are used instead of the actual text glyphs when true. | 577 // Asterisks are used instead of the actual text glyphs when true. |
| 542 bool obscured_; | 578 bool obscured_; |
| 543 base::string16 obscured_text_; | 579 base::string16 obscured_text_; |
| 544 | 580 |
| 545 // The index at which the char should be revealed in the obscured text. | 581 // The index at which the char should be revealed in the obscured text. |
| 546 int obscured_reveal_index_; | 582 int obscured_reveal_index_; |
| 547 | 583 |
| 584 // Whether we should break the text to multiple lines. | |
|
msw
2013/07/10 04:01:56
optional nit: I prefer avoiding "we" in comments.
ckocagil
2013/07/13 16:05:10
Done.
| |
| 585 bool multiline_; | |
| 586 | |
| 548 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 587 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
| 549 bool fade_head_; | 588 bool fade_head_; |
| 550 bool fade_tail_; | 589 bool fade_tail_; |
| 551 | 590 |
| 552 // Is the background transparent (either partially or fully)? | 591 // Is the background transparent (either partially or fully)? |
| 553 bool background_is_transparent_; | 592 bool background_is_transparent_; |
| 554 | 593 |
| 555 // The local display area for rendering the text. | 594 // The local display area for rendering the text. |
| 556 Rect display_rect_; | 595 Rect display_rect_; |
| 557 | 596 |
| 558 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) | 597 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548) |
| 559 // that results in incorrect clipping when drawing to the document margins. | 598 // that results in incorrect clipping when drawing to the document margins. |
| 560 // This field allows disabling clipping to work around the issue. | 599 // This field allows disabling clipping to work around the issue. |
| 561 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. | 600 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed. |
| 562 bool clip_to_display_rect_; | 601 bool clip_to_display_rect_; |
| 563 | 602 |
| 564 // The offset for the text to be drawn, relative to the display area. | 603 // The offset for the text to be drawn, relative to the display area. |
| 565 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). | 604 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value). |
| 566 Vector2d display_offset_; | 605 Vector2d display_offset_; |
| 567 | 606 |
| 568 // The cached bounds and offset are invalidated by changes to the cursor, | 607 // The cached bounds and offset are invalidated by changes to the cursor, |
| 569 // selection, font, and other operations that adjust the visible text bounds. | 608 // selection, font, and other operations that adjust the visible text bounds. |
| 570 bool cached_bounds_and_offset_valid_; | 609 bool cached_bounds_and_offset_valid_; |
| 571 | 610 |
| 572 // Text shadows to be drawn. | 611 // Text shadows to be drawn. |
| 573 ShadowValues text_shadows_; | 612 ShadowValues text_shadows_; |
| 574 | 613 |
| 614 // Positions at which we can break the line. | |
| 615 std::vector<size_t> line_breaks_; | |
| 616 | |
| 617 // Lines computed by ComputeLines(). | |
| 618 std::vector<ScopedVector<internal::LineSegment> > lines_; | |
|
Alexei Svitkine (slow)
2013/07/09 15:35:13
Can you use a regular vector instead of a ScopedVe
msw
2013/07/10 04:01:56
I'm wondering if it might help in the long run to
ckocagil
2013/07/13 16:05:10
I can't, we need pointers as long as we have LineS
ckocagil
2013/07/13 16:05:10
I'm not sure how this would work but I'm adding a
| |
| 619 | |
| 620 bool lines_valid_; | |
|
Alexei Svitkine (slow)
2013/07/09 15:35:13
Add a comment.
ckocagil
2013/07/13 16:05:10
Done.
| |
| 621 | |
| 575 DISALLOW_COPY_AND_ASSIGN(RenderText); | 622 DISALLOW_COPY_AND_ASSIGN(RenderText); |
| 576 }; | 623 }; |
| 577 | 624 |
| 578 } // namespace gfx | 625 } // namespace gfx |
| 579 | 626 |
| 580 #endif // UI_GFX_RENDER_TEXT_H_ | 627 #endif // UI_GFX_RENDER_TEXT_H_ |
| OLD | NEW |