OLD | NEW |
---|---|
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 |
79 } // namespace internal | 86 } // namespace internal |
80 | 87 |
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 | 88 // 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, | 89 // visual layout. Support is built in for a cursor, a selection, simple styling, |
98 // complex scripts, and bi-directional text. Implementations provide mechanisms | 90 // complex scripts, and bi-directional text. Implementations provide mechanisms |
99 // for rendering and translation between logical and visual data. | 91 // for rendering and translation between logical and visual data. |
100 class UI_EXPORT RenderText { | 92 class UI_EXPORT RenderText { |
101 public: | 93 public: |
102 virtual ~RenderText(); | 94 virtual ~RenderText(); |
103 | 95 |
104 // Creates a platform-specific RenderText instance. | 96 // Creates a platform-specific RenderText instance. |
105 static RenderText* CreateInstance(); | 97 static RenderText* CreateInstance(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 void set_selection_background_unfocused_color(SkColor color) { | 142 void set_selection_background_unfocused_color(SkColor color) { |
151 selection_background_unfocused_color_ = color; | 143 selection_background_unfocused_color_ = color; |
152 } | 144 } |
153 | 145 |
154 bool focused() const { return focused_; } | 146 bool focused() const { return focused_; } |
155 void set_focused(bool focused) { focused_ = focused; } | 147 void set_focused(bool focused) { focused_ = focused; } |
156 | 148 |
157 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 149 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; } | 150 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
159 | 151 |
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. | 152 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
164 bool obscured() const { return obscured_; } | 153 bool obscured() const { return obscured_; } |
165 void SetObscured(bool obscured); | 154 void SetObscured(bool obscured); |
166 | 155 |
167 const Rect& display_rect() const { return display_rect_; } | 156 const Rect& display_rect() const { return display_rect_; } |
168 void SetDisplayRect(const Rect& r); | 157 void SetDisplayRect(const Rect& r); |
169 | 158 |
170 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } | 159 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } |
171 bool fade_head() const { return fade_head_; } | 160 bool fade_head() const { return fade_head_; } |
172 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } | 161 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 // the logical beginning of the text; this generally shows the leading portion | 209 // the logical beginning of the text; this generally shows the leading portion |
221 // of text that overflows its display area. | 210 // of text that overflows its display area. |
222 void SelectAll(bool reversed); | 211 void SelectAll(bool reversed); |
223 | 212 |
224 // Selects the word at the current cursor position. | 213 // Selects the word at the current cursor position. |
225 void SelectWord(); | 214 void SelectWord(); |
226 | 215 |
227 const ui::Range& GetCompositionRange() const; | 216 const ui::Range& GetCompositionRange() const; |
228 void SetCompositionRange(const ui::Range& composition_range); | 217 void SetCompositionRange(const ui::Range& composition_range); |
229 | 218 |
230 // Apply |style_range| to the internal style model. | 219 // Set the text color over the entire text or a logical character range. |
231 void ApplyStyleRange(const StyleRange& style_range); | 220 // The |range| should be valid, non-reversed, and within [0, text().length()]. |
221 void SetColor(SkColor value); | |
222 void ApplyColor(SkColor value, const ui::Range& range); | |
232 | 223 |
233 // Apply |default_style_| over the entire text range. | 224 // Set various text styles over the entire text or a logical character range. |
234 void ApplyDefaultStyle(); | 225 // The respective |style| is applied if |value| is true, or removed if false. |
226 // The |range| should be valid, non-reversed, and within [0, text().length()]. | |
227 void SetStyle(TextStyle style, bool value); | |
228 void ApplyStyle(TextStyle style, bool value, const ui::Range& range); | |
235 | 229 |
236 // Set the text directionality mode and get the text direction yielded. | 230 // Set the text directionality mode and get the text direction yielded. |
237 void SetDirectionalityMode(DirectionalityMode mode); | 231 void SetDirectionalityMode(DirectionalityMode mode); |
238 base::i18n::TextDirection GetTextDirection(); | 232 base::i18n::TextDirection GetTextDirection(); |
239 | 233 |
240 // Returns the visual movement direction corresponding to the logical end | 234 // Returns the visual movement direction corresponding to the logical end |
241 // of the text, considering only the dominant direction returned by | 235 // of the text, considering only the dominant direction returned by |
242 // |GetTextDirection()|, not the direction of a particular run. | 236 // |GetTextDirection()|, not the direction of a particular run. |
243 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 237 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
244 | 238 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 typedef std::pair<Font, ui::Range> FontSpan; | 282 typedef std::pair<Font, ui::Range> FontSpan; |
289 // For testing purposes, returns which fonts were chosen for which parts of | 283 // 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 | 284 // 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 | 285 // specifies the character range for which the corresponding font has been |
292 // chosen. | 286 // chosen. |
293 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 287 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
294 | 288 |
295 protected: | 289 protected: |
296 RenderText(); | 290 RenderText(); |
297 | 291 |
292 const BreakList<SkColor>& colors() const { return colors_; } | |
293 const BreakList<bool>& styles(TextStyle s) const { return styles_[s]; } | |
294 | |
298 const Vector2d& GetUpdatedDisplayOffset(); | 295 const Vector2d& GetUpdatedDisplayOffset(); |
299 | 296 |
300 void set_cached_bounds_and_offset_valid(bool valid) { | 297 void set_cached_bounds_and_offset_valid(bool valid) { |
301 cached_bounds_and_offset_valid_ = valid; | 298 cached_bounds_and_offset_valid_ = valid; |
302 } | 299 } |
303 | 300 |
304 const StyleRanges& style_ranges() const { return style_ranges_; } | |
305 | |
306 // Get the selection model that visually neighbors |position| by |break_type|. | 301 // Get the selection model that visually neighbors |position| by |break_type|. |
307 // The returned value represents a cursor/caret position without a selection. | 302 // The returned value represents a cursor/caret position without a selection. |
308 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 303 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
309 BreakType break_type, | 304 BreakType break_type, |
310 VisualCursorDirection direction); | 305 VisualCursorDirection direction); |
311 | 306 |
312 // Get the selection model visually left/right of |selection| by one grapheme. | 307 // Get the selection model visually left/right of |selection| by one grapheme. |
313 // The returned value represents a cursor/caret position without a selection. | 308 // The returned value represents a cursor/caret position without a selection. |
314 virtual SelectionModel AdjacentCharSelectionModel( | 309 virtual SelectionModel AdjacentCharSelectionModel( |
315 const SelectionModel& selection, | 310 const SelectionModel& selection, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 | 352 |
358 // Ensure the text is laid out. | 353 // Ensure the text is laid out. |
359 virtual void EnsureLayout() = 0; | 354 virtual void EnsureLayout() = 0; |
360 | 355 |
361 // Draw the text. | 356 // Draw the text. |
362 virtual void DrawVisualText(Canvas* canvas) = 0; | 357 virtual void DrawVisualText(Canvas* canvas) = 0; |
363 | 358 |
364 // Returns the text used for layout, which may be |obscured_text_|. | 359 // Returns the text used for layout, which may be |obscured_text_|. |
365 const string16& GetLayoutText() const; | 360 const string16& GetLayoutText() const; |
366 | 361 |
367 // Apply composition style (underline) to composition range and selection | 362 // Apply (and undo) temporary composition underlines and selection colors. |
368 // style (foreground) to selection range. | 363 void ApplyCompositionAndSelectionStyles(); |
369 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges); | 364 void UndoCompositionAndSelectionStyles(); |
365 | |
366 // Get the layout index of the next break from the given iterators. | |
367 size_t GetNextBreak( | |
368 const BreakList<SkColor>::const_iterator& color, | |
369 const BreakList<bool>::const_iterator style[NUM_TEXT_STYLES]) const; | |
Alexei Svitkine (slow)
2013/01/25 13:06:56
Can you make an inner class called StyleIterator t
msw
2013/01/25 13:33:32
I'm going to pass for now, but will consider your
| |
370 | |
371 // Advance the given iterators (by one) that end at the supplied layout index. | |
Alexei Svitkine (slow)
2013/01/28 20:52:30
I find this wording a bit confusing - I see what y
msw
2013/01/29 17:17:25
Changed the comment, name, and impl in the spirit
| |
372 void AdvanceIterators( | |
373 size_t layout_index, | |
374 BreakList<SkColor>::const_iterator* color, | |
375 BreakList<bool>::const_iterator style[NUM_TEXT_STYLES]) const; | |
Alexei Svitkine (slow)
2013/01/28 20:52:30
I find the syntax of the last parameter confusing
msw
2013/01/29 17:17:25
Done; I dislike wasting effort polishing a tempora
| |
370 | 376 |
371 // Returns the text offset from the origin after applying text alignment and | 377 // Returns the text offset from the origin after applying text alignment and |
372 // display offset. | 378 // display offset. |
373 Vector2d GetTextOffset(); | 379 Vector2d GetTextOffset(); |
374 | 380 |
375 // Convert points from the text space to the view space and back. | 381 // 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. | 382 // Handles the display area, display offset, and the application LTR/RTL mode. |
377 Point ToTextPoint(const Point& point); | 383 Point ToTextPoint(const Point& point); |
378 Point ToViewPoint(const Point& point); | 384 Point ToViewPoint(const Point& point); |
379 | 385 |
(...skipping 15 matching lines...) Expand all Loading... | |
395 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); | 401 void ApplyTextShadows(internal::SkiaTextRenderer* renderer); |
396 | 402 |
397 // A convenience function to check whether the glyph attached to the caret | 403 // A convenience function to check whether the glyph attached to the caret |
398 // is within the given range. | 404 // is within the given range. |
399 static bool RangeContainsCaret(const ui::Range& range, | 405 static bool RangeContainsCaret(const ui::Range& range, |
400 size_t caret_pos, | 406 size_t caret_pos, |
401 LogicalCursorDirection caret_affinity); | 407 LogicalCursorDirection caret_affinity); |
402 | 408 |
403 private: | 409 private: |
404 friend class RenderTextTest; | 410 friend class RenderTextTest; |
405 | |
406 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 411 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
407 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); | 412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
408 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); | 413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColorAndStyle); |
409 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); | |
410 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); | 414 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
411 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); | 415 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); | 416 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); | 417 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); |
414 | 418 |
415 // Set the cursor to |position|, with the caret trailing the previous | 419 // Set the cursor to |position|, with the caret trailing the previous |
416 // grapheme, or if there is no previous grapheme, leading the cursor position. | 420 // 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. | 421 // 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), | 422 // If the |position| is not a cursorable position (not on grapheme boundary), |
419 // it is a NO-OP. | 423 // it is a NO-OP. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 | 475 |
472 // The background color used for drawing the selection when not focused. | 476 // The background color used for drawing the selection when not focused. |
473 SkColor selection_background_unfocused_color_; | 477 SkColor selection_background_unfocused_color_; |
474 | 478 |
475 // The focus state of the text. | 479 // The focus state of the text. |
476 bool focused_; | 480 bool focused_; |
477 | 481 |
478 // Composition text range. | 482 // Composition text range. |
479 ui::Range composition_range_; | 483 ui::Range composition_range_; |
480 | 484 |
481 // List of style ranges. Elements in the list never overlap each other. | 485 // Color and style breaks, used to color and stylize ranges of text. |
482 StyleRanges style_ranges_; | 486 // TODO(msw): Expand to support cursor, selection, background, etc. colors. |
483 // The default text style. | 487 BreakList<SkColor> colors_; |
484 StyleRange default_style_; | 488 BreakList<bool> styles_[NUM_TEXT_STYLES]; |
485 | 489 |
486 // A flag and the text to display for obscured (password) fields. | 490 // A flag and the text to display for obscured (password) fields. |
487 // Asterisks are used instead of the actual text glyphs when true. | 491 // Asterisks are used instead of the actual text glyphs when true. |
488 bool obscured_; | 492 bool obscured_; |
489 string16 obscured_text_; | 493 string16 obscured_text_; |
490 | 494 |
491 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 495 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
492 bool fade_head_; | 496 bool fade_head_; |
493 bool fade_tail_; | 497 bool fade_tail_; |
494 | 498 |
(...skipping 19 matching lines...) Expand all Loading... | |
514 | 518 |
515 // Text shadows to be drawn. | 519 // Text shadows to be drawn. |
516 ShadowValues text_shadows_; | 520 ShadowValues text_shadows_; |
517 | 521 |
518 DISALLOW_COPY_AND_ASSIGN(RenderText); | 522 DISALLOW_COPY_AND_ASSIGN(RenderText); |
519 }; | 523 }; |
520 | 524 |
521 } // namespace gfx | 525 } // namespace gfx |
522 | 526 |
523 #endif // UI_GFX_RENDER_TEXT_H_ | 527 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |