Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: ui/gfx/render_text.h

Issue 11535014: Replace StyleRange with BreakList; update RenderText, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update iteration and helpers; add tests; etc. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef UI_GFX_RENDER_TEXT_H_ 5 #ifndef UI_GFX_RENDER_TEXT_H_
6 #define UI_GFX_RENDER_TEXT_H_ 6 #define UI_GFX_RENDER_TEXT_H_
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <cstring> 9 #include <cstring>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/i18n/rtl.h" 15 #include "base/i18n/rtl.h"
16 #include "base/string16.h" 16 #include "base/string16.h"
17 #include "third_party/skia/include/core/SkColor.h" 17 #include "third_party/skia/include/core/SkColor.h"
18 #include "third_party/skia/include/core/SkPaint.h" 18 #include "third_party/skia/include/core/SkPaint.h"
19 #include "third_party/skia/include/core/SkRect.h" 19 #include "third_party/skia/include/core/SkRect.h"
20 #include "ui/base/range/range.h" 20 #include "ui/base/range/range.h"
21 #include "ui/gfx/break_list.h"
21 #include "ui/gfx/font_list.h" 22 #include "ui/gfx/font_list.h"
22 #include "ui/gfx/point.h" 23 #include "ui/gfx/point.h"
23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect.h"
24 #include "ui/gfx/selection_model.h" 25 #include "ui/gfx/selection_model.h"
25 #include "ui/gfx/shadow_value.h" 26 #include "ui/gfx/shadow_value.h"
26 #include "ui/gfx/text_constants.h" 27 #include "ui/gfx/text_constants.h"
27 #include "ui/gfx/vector2d.h" 28 #include "ui/gfx/vector2d.h"
28 29
29 class SkCanvas; 30 class SkCanvas;
30 class SkDrawLooper; 31 class SkDrawLooper;
31 struct SkPoint; 32 struct SkPoint;
32 class SkShader; 33 class SkShader;
33 class SkTypeface; 34 class SkTypeface;
34 35
35 namespace gfx { 36 namespace gfx {
36 37
37 class Canvas; 38 class Canvas;
38 class Font; 39 class Font;
39 class RenderTextTest; 40 class RenderTextTest;
40 struct StyleRange;
41 41
42 namespace internal { 42 namespace internal {
43 43
44 // Internal helper class used by derived classes to draw text through Skia. 44 // Internal helper class used by derived classes to draw text through Skia.
45 class SkiaTextRenderer { 45 class SkiaTextRenderer {
46 public: 46 public:
47 explicit SkiaTextRenderer(Canvas* canvas); 47 explicit SkiaTextRenderer(Canvas* canvas);
48 ~SkiaTextRenderer(); 48 ~SkiaTextRenderer();
49 49
50 void SetDrawLooper(SkDrawLooper* draw_looper); 50 void SetDrawLooper(SkDrawLooper* draw_looper);
51 void SetFontSmoothingSettings(bool enable_smoothing, bool enable_lcd_text); 51 void SetFontSmoothingSettings(bool enable_smoothing, bool enable_lcd_text);
52 void SetTypeface(SkTypeface* typeface); 52 void SetTypeface(SkTypeface* typeface);
53 void SetTextSize(SkScalar size); 53 void SetTextSize(SkScalar size);
54 void SetFontFamilyWithStyle(const std::string& family, int font_style); 54 void SetFontFamilyWithStyle(const std::string& family, int font_style);
55 void SetForegroundColor(SkColor foreground); 55 void SetForegroundColor(SkColor foreground);
56 void SetShader(SkShader* shader, const Rect& bounds); 56 void SetShader(SkShader* shader, const Rect& bounds);
57 // Sets underline metrics to use if the text will be drawn with an underline. 57 // Sets underline metrics to use if the text will be drawn with an underline.
58 // If not set, default values based on the size of the text will be used. The 58 // If not set, default values based on the size of the text will be used. The
59 // two metrics must be set together. 59 // two metrics must be set together.
60 void SetUnderlineMetrics(SkScalar thickness, SkScalar position); 60 void SetUnderlineMetrics(SkScalar thickness, SkScalar position);
61 void DrawSelection(const std::vector<Rect>& selection, SkColor color); 61 void DrawSelection(const std::vector<Rect>& selection, SkColor color);
62 void DrawPosText(const SkPoint* pos, 62 void DrawPosText(const SkPoint* pos,
63 const uint16* glyphs, 63 const uint16* glyphs,
64 size_t glyph_count); 64 size_t glyph_count);
65 void DrawDecorations(int x, int y, int width, const StyleRange& style); 65 // Draw underline and strike-through text decorations.
66 // Based on |SkCanvas::DrawTextDecorations()| and constants from:
67 // third_party/skia/src/core/SkTextFormatParams.h
68 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
69 bool diagonal_strike);
70 void DrawUnderline(int x, int y, int width);
71 void DrawStrike(int x, int y, int width) const;
72 void DrawDiagonalStrike(int x, int y, int width) const;
66 73
67 private: 74 private:
68 SkCanvas* canvas_skia_; 75 SkCanvas* canvas_skia_;
69 bool started_drawing_; 76 bool started_drawing_;
70 SkPaint paint_; 77 SkPaint paint_;
71 SkRect bounds_; 78 SkRect bounds_;
72 SkRefPtr<SkShader> deferred_fade_shader_; 79 SkRefPtr<SkShader> deferred_fade_shader_;
73 SkScalar underline_thickness_; 80 SkScalar underline_thickness_;
74 SkScalar underline_position_; 81 SkScalar underline_position_;
75 82
76 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer); 83 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer);
77 }; 84 };
78 85
86 // Internal helper class used by derived classes to iterate colors and styles.
87 class StyleIterator {
88 public:
89 StyleIterator(const BreakList<SkColor>& colors,
90 const std::vector<BreakList<bool> >& styles);
91 ~StyleIterator();
92
93 // Get the colors and styles at the current iterator position.
94 SkColor color() const { return color_->second; }
95 bool style(TextStyle s) const { return style_[s]->second; }
96
97 // Get the intersecting range of the current iterator set.
98 ui::Range GetRange() const;
99
100 // Update the iterator to point to colors and styles applicable at |position|.
101 void UpdatePosition(size_t position);
102
103 private:
104 BreakList<SkColor> colors_;
105 std::vector<BreakList<bool> > styles_;
106
107 BreakList<SkColor>::const_iterator color_;
108 std::vector<BreakList<bool>::const_iterator> style_;
109
110 DISALLOW_COPY_AND_ASSIGN(StyleIterator);
111 };
112
79 } // namespace internal 113 } // namespace internal
80 114
81 // A visual style applicable to a range of text.
82 struct UI_EXPORT StyleRange {
83 StyleRange();
84
85 SkColor foreground;
86 // A gfx::Font::FontStyle flag to specify bold and italic styles.
87 int font_style;
88 bool strike;
89 bool diagonal_strike;
90 bool underline;
91 ui::Range range;
92 };
93
94 typedef std::vector<StyleRange> StyleRanges;
95
96 // RenderText represents an abstract model of styled text and its corresponding 115 // RenderText represents an abstract model of styled text and its corresponding
97 // visual layout. Support is built in for a cursor, a selection, simple styling, 116 // visual layout. Support is built in for a cursor, a selection, simple styling,
98 // complex scripts, and bi-directional text. Implementations provide mechanisms 117 // complex scripts, and bi-directional text. Implementations provide mechanisms
99 // for rendering and translation between logical and visual data. 118 // for rendering and translation between logical and visual data.
100 class UI_EXPORT RenderText { 119 class UI_EXPORT RenderText {
101 public: 120 public:
102 virtual ~RenderText(); 121 virtual ~RenderText();
103 122
104 // Creates a platform-specific RenderText instance. 123 // Creates a platform-specific RenderText instance.
105 static RenderText* CreateInstance(); 124 static RenderText* CreateInstance();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 void set_selection_background_unfocused_color(SkColor color) { 169 void set_selection_background_unfocused_color(SkColor color) {
151 selection_background_unfocused_color_ = color; 170 selection_background_unfocused_color_ = color;
152 } 171 }
153 172
154 bool focused() const { return focused_; } 173 bool focused() const { return focused_; }
155 void set_focused(bool focused) { focused_ = focused; } 174 void set_focused(bool focused) { focused_ = focused; }
156 175
157 bool clip_to_display_rect() const { return clip_to_display_rect_; } 176 bool clip_to_display_rect() const { return clip_to_display_rect_; }
158 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } 177 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; }
159 178
160 const StyleRange& default_style() const { return default_style_; }
161 void set_default_style(const StyleRange& style) { default_style_ = style; }
162
163 // In an obscured (password) field, all text is drawn as asterisks or bullets. 179 // In an obscured (password) field, all text is drawn as asterisks or bullets.
164 bool obscured() const { return obscured_; } 180 bool obscured() const { return obscured_; }
165 void SetObscured(bool obscured); 181 void SetObscured(bool obscured);
166 182
167 const Rect& display_rect() const { return display_rect_; } 183 const Rect& display_rect() const { return display_rect_; }
168 void SetDisplayRect(const Rect& r); 184 void SetDisplayRect(const Rect& r);
169 185
170 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } 186 void set_fade_head(bool fade_head) { fade_head_ = fade_head; }
171 bool fade_head() const { return fade_head_; } 187 bool fade_head() const { return fade_head_; }
172 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } 188 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // the logical beginning of the text; this generally shows the leading portion 236 // the logical beginning of the text; this generally shows the leading portion
221 // of text that overflows its display area. 237 // of text that overflows its display area.
222 void SelectAll(bool reversed); 238 void SelectAll(bool reversed);
223 239
224 // Selects the word at the current cursor position. 240 // Selects the word at the current cursor position.
225 void SelectWord(); 241 void SelectWord();
226 242
227 const ui::Range& GetCompositionRange() const; 243 const ui::Range& GetCompositionRange() const;
228 void SetCompositionRange(const ui::Range& composition_range); 244 void SetCompositionRange(const ui::Range& composition_range);
229 245
230 // Apply |style_range| to the internal style model. 246 // Set the text color over the entire text or a logical character range.
231 void ApplyStyleRange(const StyleRange& style_range); 247 // The |range| should be valid, non-reversed, and within [0, text().length()].
248 void SetColor(SkColor value);
249 void ApplyColor(SkColor value, const ui::Range& range);
232 250
233 // Apply |default_style_| over the entire text range. 251 // Set various text styles over the entire text or a logical character range.
234 void ApplyDefaultStyle(); 252 // The respective |style| is applied if |value| is true, or removed if false.
253 // The |range| should be valid, non-reversed, and within [0, text().length()].
254 void SetStyle(TextStyle style, bool value);
255 void ApplyStyle(TextStyle style, bool value, const ui::Range& range);
235 256
236 // Set the text directionality mode and get the text direction yielded. 257 // Set the text directionality mode and get the text direction yielded.
237 void SetDirectionalityMode(DirectionalityMode mode); 258 void SetDirectionalityMode(DirectionalityMode mode);
238 base::i18n::TextDirection GetTextDirection(); 259 base::i18n::TextDirection GetTextDirection();
239 260
240 // Returns the visual movement direction corresponding to the logical end 261 // Returns the visual movement direction corresponding to the logical end
241 // of the text, considering only the dominant direction returned by 262 // of the text, considering only the dominant direction returned by
242 // |GetTextDirection()|, not the direction of a particular run. 263 // |GetTextDirection()|, not the direction of a particular run.
243 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); 264 VisualCursorDirection GetVisualDirectionOfLogicalEnd();
244 265
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 typedef std::pair<Font, ui::Range> FontSpan; 309 typedef std::pair<Font, ui::Range> FontSpan;
289 // For testing purposes, returns which fonts were chosen for which parts of 310 // For testing purposes, returns which fonts were chosen for which parts of
290 // the text by returning a vector of Font and Range pairs, where each range 311 // the text by returning a vector of Font and Range pairs, where each range
291 // specifies the character range for which the corresponding font has been 312 // specifies the character range for which the corresponding font has been
292 // chosen. 313 // chosen.
293 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; 314 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
294 315
295 protected: 316 protected:
296 RenderText(); 317 RenderText();
297 318
319 const BreakList<SkColor>& colors() const { return colors_; }
320 const std::vector<BreakList<bool> >& styles() const { return styles_; }
321
298 const Vector2d& GetUpdatedDisplayOffset(); 322 const Vector2d& GetUpdatedDisplayOffset();
299 323
300 void set_cached_bounds_and_offset_valid(bool valid) { 324 void set_cached_bounds_and_offset_valid(bool valid) {
301 cached_bounds_and_offset_valid_ = valid; 325 cached_bounds_and_offset_valid_ = valid;
302 } 326 }
303 327
304 const StyleRanges& style_ranges() const { return style_ranges_; }
305
306 // Get the selection model that visually neighbors |position| by |break_type|. 328 // Get the selection model that visually neighbors |position| by |break_type|.
307 // The returned value represents a cursor/caret position without a selection. 329 // The returned value represents a cursor/caret position without a selection.
308 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, 330 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current,
309 BreakType break_type, 331 BreakType break_type,
310 VisualCursorDirection direction); 332 VisualCursorDirection direction);
311 333
312 // Get the selection model visually left/right of |selection| by one grapheme. 334 // Get the selection model visually left/right of |selection| by one grapheme.
313 // The returned value represents a cursor/caret position without a selection. 335 // The returned value represents a cursor/caret position without a selection.
314 virtual SelectionModel AdjacentCharSelectionModel( 336 virtual SelectionModel AdjacentCharSelectionModel(
315 const SelectionModel& selection, 337 const SelectionModel& selection,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 379
358 // Ensure the text is laid out. 380 // Ensure the text is laid out.
359 virtual void EnsureLayout() = 0; 381 virtual void EnsureLayout() = 0;
360 382
361 // Draw the text. 383 // Draw the text.
362 virtual void DrawVisualText(Canvas* canvas) = 0; 384 virtual void DrawVisualText(Canvas* canvas) = 0;
363 385
364 // Returns the text used for layout, which may be |obscured_text_|. 386 // Returns the text used for layout, which may be |obscured_text_|.
365 const string16& GetLayoutText() const; 387 const string16& GetLayoutText() const;
366 388
367 // Apply composition style (underline) to composition range and selection 389 // Apply (and undo) temporary composition underlines and selection colors.
368 // style (foreground) to selection range. 390 void ApplyCompositionAndSelectionStyles();
369 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges); 391 void UndoCompositionAndSelectionStyles();
370 392
371 // Returns the text offset from the origin after applying text alignment and 393 // Returns the text offset from the origin after applying text alignment and
372 // display offset. 394 // display offset.
373 Vector2d GetTextOffset(); 395 Vector2d GetTextOffset();
374 396
375 // Convert points from the text space to the view space and back. 397 // Convert points from the text space to the view space and back.
376 // Handles the display area, display offset, and the application LTR/RTL mode. 398 // Handles the display area, display offset, and the application LTR/RTL mode.
377 Point ToTextPoint(const Point& point); 399 Point ToTextPoint(const Point& point);
378 Point ToViewPoint(const Point& point); 400 Point ToViewPoint(const Point& point);
379 401
(...skipping 15 matching lines...) Expand all
395 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); 417 void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
396 418
397 // A convenience function to check whether the glyph attached to the caret 419 // A convenience function to check whether the glyph attached to the caret
398 // is within the given range. 420 // is within the given range.
399 static bool RangeContainsCaret(const ui::Range& range, 421 static bool RangeContainsCaret(const ui::Range& range,
400 size_t caret_pos, 422 size_t caret_pos,
401 LogicalCursorDirection caret_affinity); 423 LogicalCursorDirection caret_affinity);
402 424
403 private: 425 private:
404 friend class RenderTextTest; 426 friend class RenderTextTest;
405
406 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); 427 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle);
407 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); 428 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle);
408 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); 429 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle);
409 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust);
410 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); 430 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
411 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); 431 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); 432 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); 433 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing);
414 434
415 // Set the cursor to |position|, with the caret trailing the previous 435 // Set the cursor to |position|, with the caret trailing the previous
416 // grapheme, or if there is no previous grapheme, leading the cursor position. 436 // grapheme, or if there is no previous grapheme, leading the cursor position.
417 // If |select| is false, the selection start is moved to the same position. 437 // If |select| is false, the selection start is moved to the same position.
418 // If the |position| is not a cursorable position (not on grapheme boundary), 438 // If the |position| is not a cursorable position (not on grapheme boundary),
419 // it is a NO-OP. 439 // it is a NO-OP.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 491
472 // The background color used for drawing the selection when not focused. 492 // The background color used for drawing the selection when not focused.
473 SkColor selection_background_unfocused_color_; 493 SkColor selection_background_unfocused_color_;
474 494
475 // The focus state of the text. 495 // The focus state of the text.
476 bool focused_; 496 bool focused_;
477 497
478 // Composition text range. 498 // Composition text range.
479 ui::Range composition_range_; 499 ui::Range composition_range_;
480 500
481 // List of style ranges. Elements in the list never overlap each other. 501 // Color and style breaks, used to color and stylize ranges of text.
Alexei Svitkine (slow) 2013/01/31 16:33:34 Mention that these BreakLists use text indexes (an
msw 2013/01/31 20:27:39 Done.
482 StyleRanges style_ranges_; 502 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
483 // The default text style. 503 BreakList<SkColor> colors_;
484 StyleRange default_style_; 504 std::vector<BreakList<bool> > styles_;
505
506 // Breaks saved without temporary composition and selection styling.
507 BreakList<SkColor> saved_colors_;
508 BreakList<bool> saved_underlines_;
509 bool composition_and_selection_styles_applied_;
485 510
486 // A flag and the text to display for obscured (password) fields. 511 // A flag and the text to display for obscured (password) fields.
487 // Asterisks are used instead of the actual text glyphs when true. 512 // Asterisks are used instead of the actual text glyphs when true.
488 bool obscured_; 513 bool obscured_;
489 string16 obscured_text_; 514 string16 obscured_text_;
490 515
491 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. 516 // Fade text head and/or tail, if text doesn't fit into |display_rect_|.
492 bool fade_head_; 517 bool fade_head_;
493 bool fade_tail_; 518 bool fade_tail_;
494 519
(...skipping 19 matching lines...) Expand all
514 539
515 // Text shadows to be drawn. 540 // Text shadows to be drawn.
516 ShadowValues text_shadows_; 541 ShadowValues text_shadows_;
517 542
518 DISALLOW_COPY_AND_ASSIGN(RenderText); 543 DISALLOW_COPY_AND_ASSIGN(RenderText);
519 }; 544 };
520 545
521 } // namespace gfx 546 } // namespace gfx
522 547
523 #endif // UI_GFX_RENDER_TEXT_H_ 548 #endif // UI_GFX_RENDER_TEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698