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> |
(...skipping 19 matching lines...) Expand all Loading... | |
30 class SkDrawLooper; | 30 class SkDrawLooper; |
31 struct SkPoint; | 31 struct SkPoint; |
32 class SkShader; | 32 class SkShader; |
33 class SkTypeface; | 33 class SkTypeface; |
34 | 34 |
35 namespace gfx { | 35 namespace gfx { |
36 | 36 |
37 class Canvas; | 37 class Canvas; |
38 class Font; | 38 class Font; |
39 class RenderTextTest; | 39 class RenderTextTest; |
40 struct StyleRange; | |
41 | 40 |
42 namespace internal { | 41 namespace internal { |
43 | 42 |
44 // Internal helper class used by derived classes to draw text through Skia. | 43 // Internal helper class used by derived classes to draw text through Skia. |
45 class SkiaTextRenderer { | 44 class SkiaTextRenderer { |
46 public: | 45 public: |
47 explicit SkiaTextRenderer(Canvas* canvas); | 46 explicit SkiaTextRenderer(Canvas* canvas); |
48 ~SkiaTextRenderer(); | 47 ~SkiaTextRenderer(); |
49 | 48 |
50 void SetDrawLooper(SkDrawLooper* draw_looper); | 49 void SetDrawLooper(SkDrawLooper* draw_looper); |
51 void SetFontSmoothingSettings(bool enable_smoothing, bool enable_lcd_text); | 50 void SetFontSmoothingSettings(bool enable_smoothing, bool enable_lcd_text); |
52 void SetTypeface(SkTypeface* typeface); | 51 void SetTypeface(SkTypeface* typeface); |
53 void SetTextSize(SkScalar size); | 52 void SetTextSize(SkScalar size); |
54 void SetFontFamilyWithStyle(const std::string& family, int font_style); | 53 void SetFontFamilyWithStyle(const std::string& family, int font_style); |
55 void SetForegroundColor(SkColor foreground); | 54 void SetForegroundColor(SkColor foreground); |
56 void SetShader(SkShader* shader, const Rect& bounds); | 55 void SetShader(SkShader* shader, const Rect& bounds); |
57 // Sets underline metrics to use if the text will be drawn with an underline. | 56 // 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 | 57 // If not set, default values based on the size of the text will be used. The |
59 // two metrics must be set together. | 58 // two metrics must be set together. |
60 void SetUnderlineMetrics(SkScalar thickness, SkScalar position); | 59 void SetUnderlineMetrics(SkScalar thickness, SkScalar position); |
61 void DrawSelection(const std::vector<Rect>& selection, SkColor color); | 60 void DrawSelection(const std::vector<Rect>& selection, SkColor color); |
62 void DrawPosText(const SkPoint* pos, | 61 void DrawPosText(const SkPoint* pos, |
63 const uint16* glyphs, | 62 const uint16* glyphs, |
64 size_t glyph_count); | 63 size_t glyph_count); |
65 void DrawDecorations(int x, int y, int width, const StyleRange& style); | 64 // Draw underline and strike-through text decorations. |
65 // Based on |SkCanvas::DrawTextDecorations()| and constants from: | |
66 // third_party/skia/src/core/SkTextFormatParams.h | |
67 void DrawDecorations(int x, int y, int width, bool underline, bool strike, | |
68 bool diagonal_strike); | |
69 void DrawUnderline(int x, int y, int width); | |
70 void DrawStrike(int x, int y, int width) const; | |
71 void DrawDiagonalStrike(int x, int y, int width) const; | |
66 | 72 |
67 private: | 73 private: |
68 SkCanvas* canvas_skia_; | 74 SkCanvas* canvas_skia_; |
69 bool started_drawing_; | 75 bool started_drawing_; |
70 SkPaint paint_; | 76 SkPaint paint_; |
71 SkRect bounds_; | 77 SkRect bounds_; |
72 SkRefPtr<SkShader> deferred_fade_shader_; | 78 SkRefPtr<SkShader> deferred_fade_shader_; |
73 SkScalar underline_thickness_; | 79 SkScalar underline_thickness_; |
74 SkScalar underline_position_; | 80 SkScalar underline_position_; |
75 | 81 |
76 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer); | 82 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer); |
77 }; | 83 }; |
78 | 84 |
79 } // namespace internal | 85 } // namespace internal |
80 | 86 |
81 // A visual style applicable to a range of text. | 87 // Ordered ColorBreak/StyleBreak lists split text into ranged colors and styles. |
82 struct UI_EXPORT StyleRange { | 88 // Each |break| applies to logical text range: [break.first, (break+1).first) |
83 StyleRange(); | 89 // The first break is always at 0; the last break applies through the text end. |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Maybe move this part of the comment to the actual
msw
2013/01/25 09:10:02
Moot.
| |
84 | 90 typedef std::pair<size_t, SkColor> ColorBreak; |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Can these be inside RenderText (and possibly priva
msw
2013/01/25 09:10:02
Moot.
| |
85 SkColor foreground; | 91 typedef std::pair<size_t, bool> StyleBreak; |
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 | 92 |
96 // RenderText represents an abstract model of styled text and its corresponding | 93 // 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, | 94 // visual layout. Support is built in for a cursor, a selection, simple styling, |
98 // complex scripts, and bi-directional text. Implementations provide mechanisms | 95 // complex scripts, and bi-directional text. Implementations provide mechanisms |
99 // for rendering and translation between logical and visual data. | 96 // for rendering and translation between logical and visual data. |
100 class UI_EXPORT RenderText { | 97 class UI_EXPORT RenderText { |
101 public: | 98 public: |
102 virtual ~RenderText(); | 99 virtual ~RenderText(); |
103 | 100 |
104 // Creates a platform-specific RenderText instance. | 101 // Creates a platform-specific RenderText instance. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 void set_selection_background_unfocused_color(SkColor color) { | 147 void set_selection_background_unfocused_color(SkColor color) { |
151 selection_background_unfocused_color_ = color; | 148 selection_background_unfocused_color_ = color; |
152 } | 149 } |
153 | 150 |
154 bool focused() const { return focused_; } | 151 bool focused() const { return focused_; } |
155 void set_focused(bool focused) { focused_ = focused; } | 152 void set_focused(bool focused) { focused_ = focused; } |
156 | 153 |
157 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 154 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; } | 155 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
159 | 156 |
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. | 157 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
164 bool obscured() const { return obscured_; } | 158 bool obscured() const { return obscured_; } |
165 void SetObscured(bool obscured); | 159 void SetObscured(bool obscured); |
166 | 160 |
167 const Rect& display_rect() const { return display_rect_; } | 161 const Rect& display_rect() const { return display_rect_; } |
168 void SetDisplayRect(const Rect& r); | 162 void SetDisplayRect(const Rect& r); |
169 | 163 |
170 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } | 164 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } |
171 bool fade_head() const { return fade_head_; } | 165 bool fade_head() const { return fade_head_; } |
172 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } | 166 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 | 214 // the logical beginning of the text; this generally shows the leading portion |
221 // of text that overflows its display area. | 215 // of text that overflows its display area. |
222 void SelectAll(bool reversed); | 216 void SelectAll(bool reversed); |
223 | 217 |
224 // Selects the word at the current cursor position. | 218 // Selects the word at the current cursor position. |
225 void SelectWord(); | 219 void SelectWord(); |
226 | 220 |
227 const ui::Range& GetCompositionRange() const; | 221 const ui::Range& GetCompositionRange() const; |
228 void SetCompositionRange(const ui::Range& composition_range); | 222 void SetCompositionRange(const ui::Range& composition_range); |
229 | 223 |
230 // Apply |style_range| to the internal style model. | 224 // Set the text color over the entire text or a logical character range. |
231 void ApplyStyleRange(const StyleRange& style_range); | 225 void SetColor(SkColor value); |
226 void ApplyColor(SkColor value, const ui::Range& range); | |
232 | 227 |
233 // Apply |default_style_| over the entire text range. | 228 // Set various text styles over the entire text or a logical character range. |
234 void ApplyDefaultStyle(); | 229 // The respective |style| is applied if |value| is true, or removed if false. |
230 void SetStyle(TextStyle style, bool value); | |
231 void ApplyStyle(TextStyle style, bool value, const ui::Range& range); | |
235 | 232 |
236 // Set the text directionality mode and get the text direction yielded. | 233 // Set the text directionality mode and get the text direction yielded. |
237 void SetDirectionalityMode(DirectionalityMode mode); | 234 void SetDirectionalityMode(DirectionalityMode mode); |
238 base::i18n::TextDirection GetTextDirection(); | 235 base::i18n::TextDirection GetTextDirection(); |
239 | 236 |
240 // Returns the visual movement direction corresponding to the logical end | 237 // Returns the visual movement direction corresponding to the logical end |
241 // of the text, considering only the dominant direction returned by | 238 // of the text, considering only the dominant direction returned by |
242 // |GetTextDirection()|, not the direction of a particular run. | 239 // |GetTextDirection()|, not the direction of a particular run. |
243 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); | 240 VisualCursorDirection GetVisualDirectionOfLogicalEnd(); |
244 | 241 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 typedef std::pair<Font, ui::Range> FontSpan; | 285 typedef std::pair<Font, ui::Range> FontSpan; |
289 // For testing purposes, returns which fonts were chosen for which parts of | 286 // 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 | 287 // 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 | 288 // specifies the character range for which the corresponding font has been |
292 // chosen. | 289 // chosen. |
293 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; | 290 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; |
294 | 291 |
295 protected: | 292 protected: |
296 RenderText(); | 293 RenderText(); |
297 | 294 |
295 const std::vector<ColorBreak>& colors() { return colors_; } | |
296 const std::vector<StyleBreak>& styles(TextStyle s) { return styles_[s]; } | |
297 | |
298 // Get the end of the supplied break's range. Returns the logical index of the | |
299 // first character to which |i| does not apply; the next break or text length. | |
300 template <class T> | |
301 size_t GetBreakEnd(const T& list, const typename T::const_iterator& i) const { | |
302 return (i + 1) == list.end() ? text().length() : (i + 1)->first; | |
303 } | |
304 | |
298 const Vector2d& GetUpdatedDisplayOffset(); | 305 const Vector2d& GetUpdatedDisplayOffset(); |
299 | 306 |
300 void set_cached_bounds_and_offset_valid(bool valid) { | 307 void set_cached_bounds_and_offset_valid(bool valid) { |
301 cached_bounds_and_offset_valid_ = valid; | 308 cached_bounds_and_offset_valid_ = valid; |
302 } | 309 } |
303 | 310 |
304 const StyleRanges& style_ranges() const { return style_ranges_; } | |
305 | |
306 // Get the selection model that visually neighbors |position| by |break_type|. | 311 // Get the selection model that visually neighbors |position| by |break_type|. |
307 // The returned value represents a cursor/caret position without a selection. | 312 // The returned value represents a cursor/caret position without a selection. |
308 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 313 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
309 BreakType break_type, | 314 BreakType break_type, |
310 VisualCursorDirection direction); | 315 VisualCursorDirection direction); |
311 | 316 |
312 // Get the selection model visually left/right of |selection| by one grapheme. | 317 // Get the selection model visually left/right of |selection| by one grapheme. |
313 // The returned value represents a cursor/caret position without a selection. | 318 // The returned value represents a cursor/caret position without a selection. |
314 virtual SelectionModel AdjacentCharSelectionModel( | 319 virtual SelectionModel AdjacentCharSelectionModel( |
315 const SelectionModel& selection, | 320 const SelectionModel& selection, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 | 362 |
358 // Ensure the text is laid out. | 363 // Ensure the text is laid out. |
359 virtual void EnsureLayout() = 0; | 364 virtual void EnsureLayout() = 0; |
360 | 365 |
361 // Draw the text. | 366 // Draw the text. |
362 virtual void DrawVisualText(Canvas* canvas) = 0; | 367 virtual void DrawVisualText(Canvas* canvas) = 0; |
363 | 368 |
364 // Returns the text used for layout, which may be |obscured_text_|. | 369 // Returns the text used for layout, which may be |obscured_text_|. |
365 const string16& GetLayoutText() const; | 370 const string16& GetLayoutText() const; |
366 | 371 |
367 // Apply composition style (underline) to composition range and selection | 372 // Apply underline styling to mark the composition range(s). |
368 // style (foreground) to selection range. | 373 void ApplyCompositionStyle(std::vector<StyleBreak>* underlines); |
Alexei Svitkine (slow)
2013/01/23 16:52:54
Please rename the method if this is specific to un
msw
2013/01/25 09:10:02
Moot.
| |
369 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges); | 374 // Apply the selection text color to mark the selected text range. |
375 void ApplySelectionColor(std::vector<ColorBreak>* colors); | |
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 17 matching lines...) Expand all Loading... | |
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 | 411 |
406 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
407 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); | 413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
408 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); | 414 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyInvalidOrEmptyRange); |
409 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); | 415 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyle); |
416 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ResizeStyle); | |
417 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColor); | |
410 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); | 418 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
411 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); | 419 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); | 420 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); | 421 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); |
414 | 422 |
423 // Adjust the existing |breaks| to apply |value| over the supplied |range|. | |
424 template <class T> | |
425 void ApplyBreaks(T value, | |
426 const ui::Range& range, | |
427 std::vector<std::pair<size_t, T> >* breaks); | |
428 | |
415 // Set the cursor to |position|, with the caret trailing the previous | 429 // Set the cursor to |position|, with the caret trailing the previous |
416 // grapheme, or if there is no previous grapheme, leading the cursor position. | 430 // 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. | 431 // 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), | 432 // If the |position| is not a cursorable position (not on grapheme boundary), |
419 // it is a NO-OP. | 433 // it is a NO-OP. |
420 void MoveCursorTo(size_t position, bool select); | 434 void MoveCursorTo(size_t position, bool select); |
421 | 435 |
422 // Updates |obscured_text_| if the text is obscured. | 436 // Updates |obscured_text_| if the text is obscured. |
423 void UpdateObscuredText(); | 437 void UpdateObscuredText(); |
424 | 438 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 | 485 |
472 // The background color used for drawing the selection when not focused. | 486 // The background color used for drawing the selection when not focused. |
473 SkColor selection_background_unfocused_color_; | 487 SkColor selection_background_unfocused_color_; |
474 | 488 |
475 // The focus state of the text. | 489 // The focus state of the text. |
476 bool focused_; | 490 bool focused_; |
477 | 491 |
478 // Composition text range. | 492 // Composition text range. |
479 ui::Range composition_range_; | 493 ui::Range composition_range_; |
480 | 494 |
481 // List of style ranges. Elements in the list never overlap each other. | 495 // Color and style breaks, used to color and stylize ranges of text. |
482 StyleRanges style_ranges_; | 496 // TODO(msw): Expand to support cursor, selection, background, etc. colors. |
483 // The default text style. | 497 std::vector<ColorBreak> colors_; |
484 StyleRange default_style_; | 498 std::vector<StyleBreak> styles_[NUM_TEXT_STYLES]; |
485 | 499 |
486 // A flag and the text to display for obscured (password) fields. | 500 // A flag and the text to display for obscured (password) fields. |
487 // Asterisks are used instead of the actual text glyphs when true. | 501 // Asterisks are used instead of the actual text glyphs when true. |
488 bool obscured_; | 502 bool obscured_; |
489 string16 obscured_text_; | 503 string16 obscured_text_; |
490 | 504 |
491 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 505 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
492 bool fade_head_; | 506 bool fade_head_; |
493 bool fade_tail_; | 507 bool fade_tail_; |
494 | 508 |
(...skipping 19 matching lines...) Expand all Loading... | |
514 | 528 |
515 // Text shadows to be drawn. | 529 // Text shadows to be drawn. |
516 ShadowValues text_shadows_; | 530 ShadowValues text_shadows_; |
517 | 531 |
518 DISALLOW_COPY_AND_ASSIGN(RenderText); | 532 DISALLOW_COPY_AND_ASSIGN(RenderText); |
519 }; | 533 }; |
520 | 534 |
521 } // namespace gfx | 535 } // namespace gfx |
522 | 536 |
523 #endif // UI_GFX_RENDER_TEXT_H_ | 537 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |