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/gtest_prod_util.h" | |
| 13 #include "base/i18n/rtl.h" | |
| 14 #include "base/string16.h" | |
| 15 #include "third_party/skia/include/core/SkColor.h" | |
| 16 #include "ui/base/range/range.h" | |
| 17 #include "ui/gfx/font.h" | |
| 18 #include "ui/gfx/rect.h" | |
| 19 #include "ui/gfx/point.h" | |
| 20 | |
| 21 namespace gfx { | |
| 22 | |
| 23 class Canvas; | |
| 24 class RenderTextTest; | |
| 25 | |
| 26 struct StyleRange { | |
| 27 StyleRange() | |
| 28 : font(), | |
| 29 foreground(SK_ColorBLACK), | |
| 30 strike(false), | |
| 31 underline(false), | |
| 32 range() { | |
| 33 } | |
| 34 | |
|
oshima
2011/07/23 09:51:12
move constructor to .cc as this has non trivial ob
| |
| 35 Font font; | |
| 36 SkColor foreground; | |
| 37 bool strike; | |
| 38 bool underline; | |
| 39 ui::Range range; | |
| 40 }; | |
| 41 | |
| 42 typedef std::vector<StyleRange> StyleRanges; | |
| 43 | |
| 44 // TODO(msw): Implement RenderText[Win|Linux] for Uniscribe/Pango BiDi... | |
| 45 | |
| 46 // RenderText represents an abstract model of styled text and its corresponding | |
| 47 // visual layout. Support is built in for a cursor, a selection, simple styling, | |
| 48 // complex scripts, and bi-directional text. Implementations provide mechanisms | |
| 49 // for rendering and translation between logical and visual data. | |
| 50 class RenderText { | |
| 51 public: | |
| 52 virtual ~RenderText(); | |
| 53 | |
| 54 // Creates a platform-specific RenderText instance. | |
| 55 static RenderText* CreateRenderText(); | |
| 56 | |
| 57 const string16& text() const { return text_; } | |
| 58 virtual void SetText(const string16& text); | |
| 59 | |
| 60 void set_cursor_visible(bool visible) { is_cursor_visible_ = visible; } | |
| 61 | |
| 62 bool get_insert_mode() const { return insert_mode_; } | |
| 63 void toggle_insert_mode() { insert_mode_ = !insert_mode_; } | |
| 64 | |
| 65 void set_focused(bool focused) { is_focused_ = focused; } | |
| 66 | |
| 67 const StyleRange& get_default_style() { return default_style_; } | |
| 68 void set_default_style(StyleRange style) { default_style_ = style; } | |
| 69 | |
| 70 const gfx::Rect& get_display_rect() const { return display_rect_; } | |
| 71 void set_display_rect(const gfx::Rect& r) { display_rect_ = r; } | |
| 72 | |
| 73 size_t GetCursor() const; | |
| 74 void SetCursor(const size_t position); | |
| 75 | |
| 76 // Moves the cursor left or right. Cursor movement is visual, meaning that | |
| 77 // left and right are relative to screen, not the directionality of the text. | |
| 78 // If |select| is false, the selection range is emptied at the new position. | |
| 79 // If |move_by_word| is true, the cursor moves to the nearest word boundary. | |
| 80 void MoveCursorLeft(bool select, bool move_by_word); | |
| 81 void MoveCursorRight(bool select, bool move_by_word); | |
| 82 | |
| 83 // Moves the cursor to left or right end of the text as shown on screen. | |
| 84 // If |select| is false, the selection range is emptied at the new position. | |
| 85 void MoveCursorToLeftEnd(bool select); | |
| 86 void MoveCursorToRightEnd(bool select); | |
| 87 | |
| 88 // Moves the cursor to the specified logical |position|. | |
| 89 // If |select| is false, the selection range is emptied at the new position. | |
| 90 // Returns true if the cursor position or selection range changed. | |
| 91 bool MoveCursorTo(size_t position, bool select); | |
| 92 | |
| 93 const ui::Range& GetSelection() const; | |
| 94 void SetSelection(const ui::Range& range); | |
| 95 | |
| 96 // Selects no text, all text, or the word at the current cursor position. | |
| 97 void ClearSelection(); | |
| 98 void SelectAll(); | |
| 99 void SelectWord(); | |
| 100 | |
| 101 const ui::Range& GetCompositionRange() const; | |
| 102 void SetCompositionRange(const ui::Range& composition_range); | |
| 103 | |
| 104 // Apply |style_range| to the internal style model. | |
| 105 void ApplyStyleRange(StyleRange style_range); | |
| 106 // Apply |default_style_| over the entire text range. | |
| 107 void ApplyDefaultStyle(); | |
| 108 | |
| 109 base::i18n::TextDirection GetTextDirection() const; | |
| 110 | |
| 111 // Get the width of the entire string. | |
| 112 int GetStringWidth() const; | |
| 113 | |
| 114 virtual void Draw(gfx::Canvas* canvas) const; | |
| 115 | |
| 116 // Get the logical cursor position from a visual point in local coordinates. | |
| 117 virtual size_t FindCursorPosition(const gfx::Point& point) const; | |
| 118 | |
| 119 // Get the visual bounds containing the logical substring within |range|. | |
| 120 // These bounds could be visually discontiguous if the logical selection range | |
| 121 // is split by a single LTR/RTL level change. | |
| 122 virtual std::vector<gfx::Rect> GetSubstringBounds( | |
| 123 const ui::Range& range) const; | |
| 124 | |
| 125 // Get the visual bounds describing the cursor at |position|. These bounds | |
| 126 // typically represent a vertical line, but if |insert_mode| is true they | |
| 127 // contain the bounds of the associated glyph. | |
| 128 virtual gfx::Rect GetCursorBounds(size_t position, bool insert_mode) const; | |
| 129 | |
| 130 protected: | |
| 131 RenderText(); | |
| 132 | |
| 133 // Get the cursor position that visually neighbors |position|. | |
| 134 // If |move_by_word| is true, return the neighboring word delimiter position. | |
| 135 virtual size_t GetLeftCursorPosition(size_t position, | |
| 136 bool move_by_word) const; | |
| 137 virtual size_t GetRightCursorPosition(size_t position, | |
| 138 bool move_by_word) const; | |
| 139 | |
| 140 private: | |
| 141 friend class RenderTextTest; | |
| 142 | |
| 143 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | |
| 144 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); | |
| 145 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); | |
| 146 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); | |
| 147 | |
| 148 const StyleRanges& get_style_ranges() const { return style_ranges_; } | |
| 149 | |
| 150 // Clear out |style_ranges_|. | |
| 151 void ClearStyleRanges(); | |
| 152 | |
| 153 bool IsPositionAtWordSelectionBoundary(size_t pos); | |
| 154 | |
| 155 // Logical UTF-16 string data to be drawn. | |
| 156 string16 text_; | |
| 157 | |
| 158 // Logical selection range; the range end is also the logical cursor position. | |
| 159 ui::Range selection_range_; | |
|
xji
2011/07/20 01:33:33
I think we will need the leading or trailing infor
msw
2011/07/20 09:22:46
You're right, this model doesn't currently support
xji
2011/07/20 18:39:51
in bidi text, the mapping between logical and visu
xji
2011/07/20 19:59:36
I'am resending/republishing my comments since it w
msw
2011/07/21 00:19:55
Yes, the non insert-mode cursor is essentially a m
| |
| 160 | |
| 161 // The cursor visibility and insert mode. | |
| 162 bool is_cursor_visible_; | |
| 163 bool insert_mode_; | |
| 164 | |
| 165 // The focus state of the text. | |
| 166 bool is_focused_; | |
| 167 | |
| 168 // Composition text range. | |
| 169 ui::Range composition_range_; | |
| 170 | |
| 171 // List of style ranges. Elements in the list never overlap each other. | |
| 172 StyleRanges style_ranges_; | |
| 173 // The default text style. | |
| 174 StyleRange default_style_; | |
| 175 | |
| 176 // The local display area for rendering the text. | |
| 177 gfx::Rect display_rect_; | |
| 178 // The offset for the text to be drawn, relative to the display area. | |
| 179 gfx::Point display_offset_; | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(RenderText); | |
| 182 }; | |
| 183 | |
| 184 } // namespace gfx | |
| 185 | |
| 186 #endif // UI_GFX_RENDER_TEXT_H_ | |
| OLD | NEW |