summaryrefslogtreecommitdiffstats
path: root/ui/gfx/render_text.h
blob: 99b4033f03f483d275d5f9a0bff674a6c06015f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_RENDER_TEXT_H_
#define UI_GFX_RENDER_TEXT_H_
#pragma once

#include <algorithm>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/i18n/rtl.h"
#include "base/string16.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/range/range.h"
#include "ui/gfx/font.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/point.h"

namespace gfx {

// Strike line width.
const int kStrikeWidth = 2;

// Color settings for text, backgrounds and cursor.
// These are tentative, and should be derived from theme, system
// settings and current settings.
// TODO(oshima): Change this to match the standard chrome
// before dogfooding textfield views.
const SkColor kSelectedTextColor = SK_ColorWHITE;
const SkColor kFocusedSelectionColor = SK_ColorCYAN;
const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY;
const SkColor kCursorColor = SK_ColorBLACK;

class Canvas;
class RenderTextTest;

// A visual style applicable to a range of text.
struct UI_API StyleRange {
  StyleRange();

  Font font;
  SkColor foreground;
  bool strike;
  bool underline;
  ui::Range range;
};

typedef std::vector<StyleRange> StyleRanges;

// TODO(msw): Distinguish between logical character and glyph?
enum BreakType {
  CHARACTER_BREAK,
  WORD_BREAK,
  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 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, CaretPlacement status);
  SelectionModel(size_t start, size_t end, size_t pos, CaretPlacement status);

  virtual ~SelectionModel();

  size_t selection_start() const { return selection_start_; }
  void set_selection_start(size_t pos) { selection_start_ = pos; }

  size_t selection_end() const { return selection_end_; }
  void set_selection_end(size_t pos) { selection_end_ = pos; }

  size_t caret_pos() const { return caret_pos_; }
  void set_caret_pos(size_t pos) { caret_pos_ = pos; }

  CaretPlacement caret_placement() const { return caret_placement_; }
  void set_caret_placement(CaretPlacement placement) {
    caret_placement_ = placement;
  }

  bool Equals(const SelectionModel& sel) const;

 private:
  void Init(size_t start, size_t end, size_t pos, CaretPlacement 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 selection bounding.
  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 caret_pos_;
  // The visual placement of the cursor, relative to its associated character.
  CaretPlacement caret_placement_;
};

// TODO(msw): Implement RenderText[Win|Linux] for Uniscribe/Pango BiDi...

// RenderText represents an abstract model of styled text and its corresponding
// visual layout. Support is built in for a cursor, a selection, simple styling,
// 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();

  // Creates a platform-specific RenderText instance.
  static RenderText* CreateRenderText();

  const string16& text() const { return text_; }
  virtual void SetText(const string16& text);

  const SelectionModel& selection_model() const { return selection_model_; }
  void SetSelectionModel(const SelectionModel& sel);

  bool cursor_visible() const { return cursor_visible_; }
  void set_cursor_visible(bool visible) { cursor_visible_ = visible; }

  bool insert_mode() const { return insert_mode_; }
  void toggle_insert_mode() { insert_mode_ = !insert_mode_; }

  bool focused() const { return focused_; }
  void set_focused(bool focused) { focused_ = focused; }

  const StyleRange& default_style() const { return default_style_; }
  void set_default_style(StyleRange style) { default_style_ = style; }

  const Rect& display_rect() const { return display_rect_; }
  virtual void set_display_rect(const Rect& r);

  const Point& display_offset() const { return display_offset_; }

  // This cursor position corresponds to SelectionModel::selection_end. In
  // addition to representing the selection end, it's also where logical text
  // edits take place, and doesn't necessarily correspond to
  // SelectionModel::caret_pos.
  size_t GetCursorPosition() const;
  void SetCursorPosition(const size_t position);

  void SetCaretPlacement(SelectionModel::CaretPlacement placement) {
      selection_model_.set_caret_placement(placement);
  }

  // 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.
  // If |break_type| is CHARACTER_BREAK, move to the neighboring character.
  // If |break_type| is WORD_BREAK, move to the nearest word boundary.
  // If |break_type| is LINE_BREAK, move to text edge as shown on screen.
  void MoveCursorLeft(BreakType break_type, bool select);
  void MoveCursorRight(BreakType break_type, bool select);

  // Set the selection_model_ to the value of |selection|.
  // Returns true if the cursor position or selection range changed.
  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);

  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);

  // Selects no text, all text, or the word at the current cursor position.
  void ClearSelection();
  void SelectAll();
  void SelectWord();

  const ui::Range& GetCompositionRange() const;
  void SetCompositionRange(const ui::Range& composition_range);

  // Apply |style_range| to the internal style model.
  virtual void ApplyStyleRange(StyleRange style_range);

  // Apply |default_style_| over the entire text range.
  virtual void ApplyDefaultStyle();

  base::i18n::TextDirection GetTextDirection() const;

  // Get the width of the entire string.
  virtual int GetStringWidth();

  virtual void Draw(Canvas* canvas);

  // 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 |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(
      size_t from, size_t to) const;

  // 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(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_.
  const Rect& CursorBounds();

 protected:
  RenderText();

  void set_cursor_bounds_valid(bool valid) { cursor_bounds_valid_ = valid; }

  const StyleRanges& style_ranges() const { return style_ranges_; }

  // Get the cursor position that visually neighbors |position|.
  // If |move_by_word| is true, return the neighboring word delimiter position.
  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;

  FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle);
  FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle);
  FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange);
  FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust);

  // Clear out |style_ranges_|.
  void ClearStyleRanges();

  bool IsPositionAtWordSelectionBoundary(size_t pos);

  void UpdateCursorBoundsAndDisplayOffset();

  // Logical UTF-16 string data to be drawn.
  string16 text_;

  // 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_;

  // The focus state of the text.
  bool focused_;

  // Composition text range.
  ui::Range composition_range_;

  // List of style ranges. Elements in the list never overlap each other.
  StyleRanges style_ranges_;
  // The default text style.
  StyleRange default_style_;

  // The local display area for rendering the text.
  Rect display_rect_;
  // The offset for the text to be drawn, relative to the display area.
  Point display_offset_;

  DISALLOW_COPY_AND_ASSIGN(RenderText);
};

}  // namespace gfx

#endif  // UI_GFX_RENDER_TEXT_H_