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