Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef UI_GFX_RENDER_TEXT_H_ | |
| 6 #define UI_GFX_RENDER_TEXT_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/string16.h" | |
| 13 #include "third_party/skia/include/core/SkColor.h" | |
| 14 #include "ui/base/range/range.h" | |
| 15 #include "ui/gfx/font.h" | |
| 16 #include "ui/gfx/rect.h" | |
| 17 #include "ui/gfx/point.h" | |
| 18 | |
| 19 namespace gfx { | |
| 20 | |
| 21 struct StyleRange { | |
| 22 Font font; | |
| 23 SkColor foreground; | |
| 24 bool strike; | |
| 25 bool underline; | |
| 26 ui::Range range; | |
|
xji
2011/07/01 18:29:44
also
SkColor background;
for selection background
msw
2011/07/01 21:52:15
Interesting, we can add a background color if we w
| |
| 27 }; | |
| 28 | |
| 29 typedef std::vector<StyleRange*> StyleRanges; | |
| 30 | |
| 31 // RenderText represents an abstract model of styled text and its corresponding | |
| 32 // visual layout. Support is built in for a cursor, a selection, simple styling, | |
| 33 // complex scripts, and bi-directional text. Implementations provide mechanisms | |
| 34 // for rendering and translation between logical and visual data. | |
| 35 class RenderText { | |
| 36 public: | |
| 37 virtual ~RenderText(); | |
| 38 | |
| 39 // Creates a platform-specific RenderText instance. | |
| 40 static RenderText* CreateRenderText(); | |
| 41 static RenderText* CreateRenderText(const string16& text, | |
| 42 const gfx::Font& font, | |
| 43 const SkColor& color, | |
| 44 const gfx::Rect& display_rect, | |
| 45 int flags); | |
| 46 | |
| 47 const string16& text() const { return text_; } | |
| 48 virtual void SetText(const string16& text); | |
| 49 | |
| 50 int get_flags() const { return flags_; } | |
| 51 void set_flags(int flags) { flags_ = flags; } | |
| 52 | |
| 53 const gfx::Rect& get_display_rect() const { return display_rect_; } | |
| 54 | |
| 55 size_t GetCursor() const; | |
| 56 void SetCursor(const size_t position); | |
| 57 | |
| 58 // TODO(msw): The current composition text will be confirmed??? | |
| 59 | |
| 60 // Moves the cursor left or right. Cursor movement is visual, meaning that | |
| 61 // left and right are relative to screen, not the directionality of the text. | |
| 62 // If |select| is false, the selection range is emptied at the new position. | |
| 63 // If |move_by_word| is true, the cursor moves to the nearest word boundary. | |
| 64 // The current composition text will be confirmed. | |
| 65 void MoveCursorLeft(bool select, bool move_by_word); | |
| 66 void MoveCursorRight(bool select, bool move_by_word); | |
| 67 | |
| 68 // Moves the cursor to left or right end of the text as shown on screen. | |
| 69 // If |select| is false, the selection range is emptied at the new position. | |
| 70 // The current composition text will be confirmed. | |
| 71 void MoveCursorToLeftEnd(bool select); | |
| 72 void MoveCursorToRightEnd(bool select); | |
| 73 | |
| 74 // Moves the cursor to the specified logical |position|. | |
| 75 // If |select| is false, the selection range is emptied at the new position. | |
| 76 // The current composition text will be confirmed. | |
| 77 void MoveCursorTo(size_t position, bool select); | |
| 78 | |
| 79 const ui::Range& GetSelection() const; | |
| 80 void SetSelection(const ui::Range& selection_range); | |
| 81 | |
| 82 // Selects no text, all text, or the word at the current cursor position. | |
| 83 // The current composition text will be confirmed. | |
| 84 void ClearSelection(); | |
| 85 void SelectAll(); | |
| 86 void SelectWord(); | |
| 87 | |
| 88 const ui::Range& GetComposition() const; | |
| 89 void SetComposition(const ui::Range& composition_range); | |
| 90 | |
| 91 const StyleRanges& GetStyleRanges() const; | |
| 92 // Takes ownership of |style_range| and updates the internal style model. | |
| 93 void ApplyStyleRange(StyleRange* style_range); | |
| 94 | |
| 95 virtual void Draw(gfx::NativeDrawingContext drawing_context) const = 0; | |
| 96 | |
| 97 // Get the logical cursor position from a visual point in local coordinates. | |
| 98 virtual size_t FindCursorPosition(const gfx::Point& point) const = 0; | |
| 99 | |
| 100 // Get the visual bounds containing the logical substring within |range|. | |
| 101 // These bounds could be visually discontiguous if the logical selection range | |
| 102 // is split by a single LTR/RTL level change. | |
| 103 virtual std::vector<gfx::Rect> GetSubstringBounds( | |
| 104 const ui::Range& range) const = 0; | |
| 105 | |
| 106 // Get the cursor position that visually neighbors |position|. | |
| 107 // If |move_by_word| is true, return the neighboring word delimiter position. | |
| 108 virtual size_t GetLeftCursorPosition(size_t position, | |
| 109 bool move_by_word) const = 0; | |
| 110 virtual size_t GetRightCursorPosition(size_t position, | |
| 111 bool move_by_word) const = 0; | |
|
xji
2011/07/01 18:29:44
if we have "MoveCursorLeft() and MoveCursorRight()
msw
2011/07/01 21:52:15
I agree. I'm just now integrating this new API int
| |
| 112 | |
| 113 // Get the visual bounds describing the cursor at |position|. These bounds | |
| 114 // typically represent a vertical line, but if |insert_mode| is true they | |
| 115 // contain the bounds of the associated glyph. | |
| 116 virtual gfx::Rect GetCursorBounds(size_t position, | |
| 117 bool insert_mode) const = 0; | |
| 118 | |
| 119 protected: | |
| 120 RenderText(); | |
| 121 RenderText(const string16& text, | |
| 122 const gfx::Font& font, | |
| 123 const SkColor& color, | |
| 124 const gfx::Rect& display_rect, | |
| 125 int flags); | |
| 126 | |
| 127 private: | |
| 128 // TODO(msw): Revise SelectWord to eliminate this? | |
| 129 bool IsPositionAtWordSelectionBoundary(size_t pos); | |
| 130 | |
| 131 // Logical UTF-16 string data to be drawn. | |
| 132 string16 text_; | |
|
xji
2011/07/01 18:29:44
are we going to remove those duplicated fields fro
msw
2011/07/01 21:52:15
I'm not sure; currently the TextfieldViewsModel op
| |
| 133 | |
| 134 // Logical selection range; the range end is also the logical cursor position. | |
| 135 ui::Range selection_range_; | |
| 136 | |
| 137 // The cursor visibility. | |
| 138 bool is_cursor_visible_; | |
| 139 | |
| 140 // Composition text range. | |
| 141 ui::Range composition_range_; | |
| 142 | |
| 143 // List of style ranges. Elements in the list never overlap each other. | |
| 144 StyleRanges style_ranges_; | |
| 145 | |
| 146 // The local display area for rendering the text. | |
| 147 gfx::Rect display_rect_; | |
| 148 // The x offset for the text to be drawn, without insets. | |
| 149 gfx::Point render_offset_; | |
|
xji
2011/07/01 18:29:44
what is the usage of this field?
msw
2011/07/01 21:52:15
This is for offsetting the origin of the text as r
| |
| 150 | |
| 151 gfx::Font default_font_; | |
| 152 SkColor default_color_; | |
| 153 | |
| 154 // TODO(msw): Needed? | |
| 155 // The visual cursor bounds. These bounds typically represent a vertical line, | |
| 156 // but in insert mode they contain the bounds of the associated glyph. | |
| 157 //gfx::Rect cursor_bounds_; | |
| 158 // The visual selection bounds. These bounds could be visually discontiguous | |
| 159 // if the logical selection range is split by a single LTR/RTL level change. | |
| 160 //std::vector<gfx::Rect> selection_bounds_; | |
| 161 | |
| 162 // Rendering flags, defined in Canvas. | |
| 163 int flags_; | |
| 164 | |
|
xji
2011/07/01 18:29:44
I do not have clear idea on what (how much) stuff
msw
2011/07/01 21:52:15
Unfortunately I haven't solved that dilemma. I tho
| |
| 165 DISALLOW_COPY_AND_ASSIGN(RenderText); | |
| 166 }; | |
| 167 | |
| 168 } // namespace gfx; | |
| 169 | |
| 170 #endif // UI_GFX_RENDER_TEXT_H_ | |
| OLD | NEW |