Chromium Code Reviews| Index: ui/gfx/render_text.h |
| =================================================================== |
| --- ui/gfx/render_text.h (revision 95171) |
| +++ ui/gfx/render_text.h (working copy) |
| @@ -6,6 +6,7 @@ |
| #define UI_GFX_RENDER_TEXT_H_ |
| #pragma once |
| +#include <algorithm> |
| #include <vector> |
| #include "base/gtest_prod_util.h" |
| @@ -17,7 +18,7 @@ |
| #include "ui/gfx/rect.h" |
| #include "ui/gfx/point.h" |
| -namespace { |
| +namespace gfx { |
| // Strike line width. |
| const int kStrikeWidth = 2; |
| @@ -32,10 +33,6 @@ |
| const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY; |
| const SkColor kCursorColor = SK_ColorBLACK; |
| -} // namespace |
| - |
| -namespace gfx { |
| - |
| class Canvas; |
| class RenderTextTest; |
| @@ -59,6 +56,104 @@ |
| LINE_BREAK, |
| }; |
| +// TODO(xji): publish bidi-editing guide line and replace the place holder. |
| +// SelectionModel is used to represent the logical selection and visual |
| +// position of cursor. |
| +// |
| +// For bi-directional text, the mapping between visual position and logical |
| +// position is not one-to-one. For example, logical text "abcDEF" where capital |
| +// letters stand for Hebrew, the visual display is "abcFED". According to the |
| +// bidi editing guide (http://bidi-editing-guideline): |
| +// 1. If pointing to the right half of the cell of a LTR character, the current |
| +// position must be set after this character and the caret must be displayed |
| +// after this character. |
| +// 2. If pointing to the right half of the cell of a RTL character, the current |
| +// position must be set before this character and the caret must be displayed |
| +// before this character. |
| +// |
| +// Pointing to the right half of 'c' and pointing to the right half of 'D' both |
| +// set the logical cursor position to 3. But the cursor displayed visually at |
| +// different places: |
| +// Pointing to the right half of 'c' displays the cursor right of 'c' as |
| +// "abc|FED". |
| +// Pointing to the right half of 'D' displays the cursor right of 'D' as |
| +// "abcFED|". |
| +// So, besides the logical selection start point and end point, we need extra |
| +// information to specify to which character and on which edge of the character |
| +// the visual cursor is bound to. For example, the visual cursor is bound to |
| +// the trailing side of the 2nd character 'c' when pointing to right half of |
| +// 'c'. And it is bound to the leading edge of the 3rd character 'D' when |
| +// pointing to right of 'D'. |
| +class UI_API SelectionModel { |
| + public: |
| + enum LeadingTrailingStatus { |
|
msw
2011/08/03 02:13:06
Optional: rename CaretPlacement / CursorPlacement.
xji
2011/08/03 18:47:40
changed to CaretPlacement
|
| + // PREVIOUS_GRAPHEME_TRAILING means cursor is visually attached to the |
| + // trailing edge of previous grapheme. |
| + PREVIOUS_GRAPHEME_TRAILING, |
| + LEADING, |
| + TRAILING, |
| + }; |
| + |
| + SelectionModel(); |
| + explicit SelectionModel(size_t pos); |
| + SelectionModel(size_t end, size_t pos, LeadingTrailingStatus status); |
| + SelectionModel(size_t start, size_t end, |
| + size_t pos, LeadingTrailingStatus status); |
| + |
| + virtual ~SelectionModel(); |
| + |
| + size_t selection_start() const { return selection_start_; } |
| + void set_selection_start(size_t start) { selection_start_ = start; } |
|
msw
2011/08/03 02:13:06
Nit: use consistent argument names for these three
xji
2011/08/03 18:47:40
used |pos| for all.
|
| + |
| + size_t selection_end() const { return selection_end_; } |
| + void set_selection_end(size_t pos) { selection_end_ = pos; } |
| + |
| + size_t cursor_pos() const { return cursor_pos_; } |
| + void set_cursor_pos(size_t index) { cursor_pos_ = index; } |
| + |
| + LeadingTrailingStatus cursor_placement() const { return cursor_placement_; } |
| + void set_cursor_placement(LeadingTrailingStatus placement) { |
| + cursor_placement_ = placement; |
| + } |
| + |
| + // If |select| is true, set selection_start_ to |selection_start|, otherwise, |
| + // empty the selection. |
| + void SetSelectionStart(size_t selection_start, bool select) { |
|
msw
2011/08/03 02:13:06
Remove this; it's unused and isn't needed.
xji
2011/08/03 18:47:40
Done.
|
| + if (select) |
| + selection_start_ = selection_start; |
| + else |
| + selection_start_ = selection_end_; |
| + } |
| + |
| + void SetSelectionEmpty() { selection_start_ = selection_end_; } |
|
msw
2011/08/03 02:13:06
I'd prefer that we remove this; fewer mutators is
xji
2011/08/03 18:47:40
removed
|
| + |
| + bool operator!=(const SelectionModel& sel) const { |
|
msw
2011/08/03 02:13:06
Sorry if I told you to do operator overloading, ma
xji
2011/08/03 18:47:40
Done.
|
| + return selection_start_ != sel.selection_start_ |
| + || selection_end_ != sel.selection_end() |
| + || cursor_pos_ != sel.cursor_pos() |
| + || cursor_placement_ != sel.cursor_placement(); |
| + } |
| + |
| + private: |
| + void Init(size_t start, size_t end, size_t pos, |
| + LeadingTrailingStatus status); |
| + |
| + // Logical selection start. If there is non-empty selection, the selection |
| + // always starts visually at the leading edge of the selection_start. So, we |
| + // do not need extra information for visual cursor bounding. |
|
msw
2011/08/03 02:13:06
Optional: "visual selection bounding".
xji
2011/08/03 18:47:40
Done.
|
| + size_t selection_start_; |
| + |
| + // The logical cursor position that next character will be inserted into. |
| + // It is also the end of the selection. |
| + size_t selection_end_; |
| + |
| + // The following two fields are used to guide cursor visual position. |
| + // The index of the character that cursor is visually attached to. |
| + size_t cursor_pos_; |
|
msw
2011/08/03 02:13:06
I'm really sorry, but upon further reflection, I t
xji
2011/08/03 18:47:40
Done and it looks much better.
|
| + // The visual placement of the cursor, relative to its associated character. |
| + LeadingTrailingStatus cursor_placement_; |
|
msw
2011/08/03 02:13:06
If we rename cursor_pos_ to caret_pos_ this ought
xji
2011/08/03 18:47:40
Done.
|
| +}; |
| + |
| // TODO(msw): Implement RenderText[Win|Linux] for Uniscribe/Pango BiDi... |
| // RenderText represents an abstract model of styled text and its corresponding |
| @@ -66,7 +161,6 @@ |
| // complex scripts, and bi-directional text. Implementations provide mechanisms |
| // for rendering and translation between logical and visual data. |
| class UI_API RenderText { |
| - |
| public: |
| virtual ~RenderText(); |
| @@ -76,6 +170,9 @@ |
| const string16& text() const { return text_; } |
| virtual void SetText(const string16& text); |
| + SelectionModel selection_model() const { return selection_model_; } |
|
msw
2011/08/03 02:13:06
Return a const reference.
xji
2011/08/03 18:47:40
Done.
|
| + void SetSelectionModel(const SelectionModel& sel); |
| + |
| bool cursor_visible() const { return cursor_visible_; } |
| void set_cursor_visible(bool visible) { cursor_visible_ = visible; } |
| @@ -89,11 +186,24 @@ |
| void set_default_style(StyleRange style) { default_style_ = style; } |
| const Rect& display_rect() const { return display_rect_; } |
| - void set_display_rect(const Rect& r) { display_rect_ = r; } |
| + virtual void set_display_rect(const Rect& r) { display_rect_ = r; } |
| size_t GetCursorPosition() const; |
| void SetCursorPosition(const size_t position); |
| + SelectionModel::LeadingTrailingStatus GetCursorPlacement() const { |
|
msw
2011/08/03 02:13:06
Remove this, see my comment for its only use in
Te
xji
2011/08/03 18:47:40
Done.
|
| + return selection_model_.cursor_placement(); |
| + } |
| + void SetCursorPlacement(SelectionModel::LeadingTrailingStatus placement) { |
|
msw
2011/08/03 02:13:06
Remove this, it's unused.
xji
2011/08/03 18:47:40
This is used in RenderTextLinux. caret_placement i
msw
2011/08/03 19:46:38
OK
|
| + selection_model_.set_cursor_placement(placement); |
| + } |
| + |
| + // TODO(xji): a better name? GetCursorPos() is too similar to |
| + // GetCursorPosition. |
| + size_t GetCursorBoundingCharIndex() const { |
|
msw
2011/08/03 02:13:06
Remove this, see my comment for its only use in Te
xji
2011/08/03 18:47:40
Done.
|
| + return selection_model_.cursor_pos(); |
| + } |
| + |
| // Moves the cursor left or right. Cursor movement is visual, meaning that |
| // left and right are relative to screen, not the directionality of the text. |
| // If |select| is false, the selection range is emptied at the new position. |
| @@ -103,20 +213,29 @@ |
| void MoveCursorLeft(BreakType break_type, bool select); |
| void MoveCursorRight(BreakType break_type, bool select); |
| - // Moves the cursor to the specified logical |position|. |
| - // If |select| is false, the selection range is emptied at the new position. |
| + // Set the selection_model_ to the value of |selection|. |
| // Returns true if the cursor position or selection range changed. |
| - bool MoveCursorTo(size_t position, bool select); |
| + bool MoveCursorTo(const SelectionModel& selection); |
| // Move the cursor to the position associated with the clicked point. |
| // If |select| is false, the selection range is emptied at the new position. |
| bool MoveCursorTo(const Point& point, bool select); |
| - const ui::Range& GetSelection() const; |
| - void SetSelection(const ui::Range& range); |
| + size_t GetSelectionStart() const { |
| + return selection_model_.selection_start(); |
| + } |
| + size_t MinOfSelection() const { |
| + return std::min(GetSelectionStart(), GetCursorPosition()); |
| + } |
| + size_t MaxOfSelection() const { |
| + return std::max(GetSelectionStart(), GetCursorPosition()); |
| + } |
| + bool EmptySelection() const { |
| + return GetSelectionStart() == GetCursorPosition(); |
| + } |
| // Returns true if the local point is over selected text. |
| - bool IsPointInSelection(const Point& point) const; |
| + bool IsPointInSelection(const Point& point); |
| // Selects no text, all text, or the word at the current cursor position. |
| void ClearSelection(); |
| @@ -135,40 +254,49 @@ |
| base::i18n::TextDirection GetTextDirection() const; |
| // Get the width of the entire string. |
| - int GetStringWidth() const; |
| + virtual int GetStringWidth(); |
| virtual void Draw(Canvas* canvas); |
| - // TODO(msw): Deprecate this function. Logical and visual cursors are not |
| - // mapped one-to-one. See the selection_range_ TODO for more information. |
| - // Get the logical cursor position from a visual point in local coordinates. |
| - virtual size_t FindCursorPosition(const Point& point) const; |
| + // Gets the SelectionModel from a visual point in local coordinates. |
| + virtual SelectionModel FindCursorPosition(const Point& point); |
| - // Get the visual bounds containing the logical substring within |range|. |
| - // These bounds could be visually discontiguous if the logical selection range |
| - // is split by an odd number of LTR/RTL level change. |
| + // Get the visual bounds containing the logical substring within |from| to |
| + // |to|. These bounds could be visually discontinuous if the logical |
| + // selection range is split by an odd number of LTR/RTL level change. |
| virtual std::vector<Rect> GetSubstringBounds( |
| - const ui::Range& range) const; |
| + size_t from, size_t to) const; |
| - // Get the visual bounds describing the cursor at |position|. These bounds |
| + // Get the visual bounds describing the cursor at |selection|. These bounds |
| // typically represent a vertical line, but if |insert_mode| is true they |
| // contain the bounds of the associated glyph. |
| - virtual Rect GetCursorBounds(size_t position, bool insert_mode) const; |
| + virtual Rect GetCursorBounds(const SelectionModel& selection, |
| + bool insert_mode); |
| + // Compute cursor_bounds_ and update display_offset_ when necessary. Cache |
| + // the values for later use and return cursor_bounds_. |
| + Rect CursorBounds(); |
| + |
| protected: |
| RenderText(); |
| + void set_cursor_bounds_valid(bool valid) { cursor_bounds_valid_ = valid; } |
| + |
| const StyleRanges& style_ranges() const { return style_ranges_; } |
| const Point& display_offset() const { return display_offset_; } |
| // Get the cursor position that visually neighbors |position|. |
| // If |move_by_word| is true, return the neighboring word delimiter position. |
| - virtual size_t GetLeftCursorPosition(size_t position, |
| - bool move_by_word) const; |
| - virtual size_t GetRightCursorPosition(size_t position, |
| - bool move_by_word) const; |
| + virtual SelectionModel GetLeftCursorPosition(const SelectionModel& current, |
| + bool move_by_word); |
| + virtual SelectionModel GetRightCursorPosition(const SelectionModel& current, |
| + bool move_by_word); |
| + // Apply composition style (underline) to composition range and selection |
| + // style (foreground) to selection range. |
| + void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges) const; |
| + |
| private: |
| friend class RenderTextTest; |
| @@ -182,20 +310,25 @@ |
| bool IsPositionAtWordSelectionBoundary(size_t pos); |
| + void UpdateCursorBoundsAndDisplayOffset(); |
| + |
| + // Set the selection logical start and end in selection_model_. |
| + // And invalidate cursor_bounds_. |
| + void SetSelection(size_t start, size_t end); |
|
msw
2011/08/03 02:13:06
We should eliminate this function and use SetSelec
xji
2011/08/03 18:47:40
removed.
|
| + |
| // Logical UTF-16 string data to be drawn. |
| string16 text_; |
| - // TODO(msw): A single logical cursor position doesn't support two potential |
| - // visual cursor positions. For example, clicking right of 'c' & 'D' yeilds: |
| - // (visually: 'abc|FEDghi' and 'abcFED|ghi', both logically: 'abc|DEFghi'). |
| - // Similarly, one visual position may have two associated logical positions. |
| - // For example, clicking the right side of 'D' and left side of 'g' yields: |
| - // (both visually: 'abcFED|ghi', logically: 'abc|DEFghi' and 'abcDEF|ghi'). |
| - // Update the cursor model with a leading/trailing flag, a level association, |
| - // or a disjoint visual position to satisfy the proposed visual behavior. |
| - // Logical selection range; the range end is also the logical cursor position. |
| - ui::Range selection_range_; |
| + // Logical selection range and visual cursor position. |
| + SelectionModel selection_model_; |
| + // The cached cursor bounds. |
| + Rect cursor_bounds_; |
| + // cursor_bounds_ is computed when needed and cached afterwards. And it is |
| + // invalidated in operations such as SetCursorPosition, SetSelection, Font |
| + // related style change, and other operations that trigger re-layout. |
| + bool cursor_bounds_valid_; |
| + |
| // The cursor visibility and insert mode. |
| bool cursor_visible_; |
| bool insert_mode_; |