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) |
Alexei Svitkine (slow)
2013/01/22 19:20:21
The comment isn't clear about what the last entry
msw
2013/01/22 22:27:24
Done.
| |
83 StyleRange(); | 89 typedef std::pair<size_t, SkColor> ColorBreak; |
84 | 90 typedef std::vector<ColorBreak> ColorBreaks; |
85 SkColor foreground; | 91 typedef std::pair<size_t, bool> StyleBreak; |
86 // A gfx::Font::FontStyle flag to specify bold and italic styles. | 92 typedef std::vector<StyleBreak> StyleBreaks; |
Alexei Svitkine (slow)
2013/01/22 19:20:21
Do we really need StyleBreaks and ColorBreaks type
msw
2013/01/22 22:27:24
Done.
| |
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 | 93 |
96 // RenderText represents an abstract model of styled text and its corresponding | 94 // 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, | 95 // visual layout. Support is built in for a cursor, a selection, simple styling, |
98 // complex scripts, and bi-directional text. Implementations provide mechanisms | 96 // complex scripts, and bi-directional text. Implementations provide mechanisms |
99 // for rendering and translation between logical and visual data. | 97 // for rendering and translation between logical and visual data. |
100 class UI_EXPORT RenderText { | 98 class UI_EXPORT RenderText { |
101 public: | 99 public: |
102 virtual ~RenderText(); | 100 virtual ~RenderText(); |
103 | 101 |
104 // Creates a platform-specific RenderText instance. | 102 // 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) { | 148 void set_selection_background_unfocused_color(SkColor color) { |
151 selection_background_unfocused_color_ = color; | 149 selection_background_unfocused_color_ = color; |
152 } | 150 } |
153 | 151 |
154 bool focused() const { return focused_; } | 152 bool focused() const { return focused_; } |
155 void set_focused(bool focused) { focused_ = focused; } | 153 void set_focused(bool focused) { focused_ = focused; } |
156 | 154 |
157 bool clip_to_display_rect() const { return clip_to_display_rect_; } | 155 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; } | 156 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; } |
159 | 157 |
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. | 158 // In an obscured (password) field, all text is drawn as asterisks or bullets. |
164 bool obscured() const { return obscured_; } | 159 bool obscured() const { return obscured_; } |
165 void SetObscured(bool obscured); | 160 void SetObscured(bool obscured); |
166 | 161 |
167 const Rect& display_rect() const { return display_rect_; } | 162 const Rect& display_rect() const { return display_rect_; } |
168 void SetDisplayRect(const Rect& r); | 163 void SetDisplayRect(const Rect& r); |
169 | 164 |
170 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } | 165 void set_fade_head(bool fade_head) { fade_head_ = fade_head; } |
171 bool fade_head() const { return fade_head_; } | 166 bool fade_head() const { return fade_head_; } |
172 void set_fade_tail(bool fade_tail) { fade_tail_ = fade_tail; } | 167 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 | 215 // the logical beginning of the text; this generally shows the leading portion |
221 // of text that overflows its display area. | 216 // of text that overflows its display area. |
222 void SelectAll(bool reversed); | 217 void SelectAll(bool reversed); |
223 | 218 |
224 // Selects the word at the current cursor position. | 219 // Selects the word at the current cursor position. |
225 void SelectWord(); | 220 void SelectWord(); |
226 | 221 |
227 const ui::Range& GetCompositionRange() const; | 222 const ui::Range& GetCompositionRange() const; |
228 void SetCompositionRange(const ui::Range& composition_range); | 223 void SetCompositionRange(const ui::Range& composition_range); |
229 | 224 |
230 // Apply |style_range| to the internal style model. | 225 // Set the text color over the entire text or a logical character range. |
231 void ApplyStyleRange(const StyleRange& style_range); | 226 void SetColor(SkColor value); |
227 void ApplyColor(SkColor value, const ui::Range& range); | |
232 | 228 |
233 // Apply |default_style_| over the entire text range. | 229 // Set various text styles over the entire text or a logical character range. |
Alexei Svitkine (slow)
2013/01/22 19:20:21
Same comment as textfield, please document |value|
msw
2013/01/22 22:27:24
Done; but I hope usage is obvious, even ApplyStyle
| |
234 void ApplyDefaultStyle(); | 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 ColorBreaks& colors() { return colors_; } | |
296 const StyleBreaks& styles(TextStyle style) { return styles_[style]; } | |
297 | |
298 // Get the end of the supplied break (the next break's start or text length). | |
Alexei Svitkine (slow)
2013/01/22 19:20:21
From reading the comment, I still have no idea wha
msw
2013/01/22 22:27:24
Done.
| |
299 template <class T> | |
300 size_t GetBreakEnd(const std::vector<std::pair<size_t, T> >& breaks, | |
Alexei Svitkine (slow)
2013/01/22 19:20:21
Can you make the template param the vector itself?
msw
2013/01/22 22:27:24
Done.
| |
301 const typename std::vector<std::pair<size_t, T> >:: | |
302 const_iterator& i) const { | |
303 return (i + 1) == breaks.end() ? text().length() : (i + 1)->first; | |
304 } | |
305 | |
298 const Vector2d& GetUpdatedDisplayOffset(); | 306 const Vector2d& GetUpdatedDisplayOffset(); |
299 | 307 |
300 void set_cached_bounds_and_offset_valid(bool valid) { | 308 void set_cached_bounds_and_offset_valid(bool valid) { |
301 cached_bounds_and_offset_valid_ = valid; | 309 cached_bounds_and_offset_valid_ = valid; |
302 } | 310 } |
303 | 311 |
304 const StyleRanges& style_ranges() const { return style_ranges_; } | |
305 | |
306 // Get the selection model that visually neighbors |position| by |break_type|. | 312 // Get the selection model that visually neighbors |position| by |break_type|. |
307 // The returned value represents a cursor/caret position without a selection. | 313 // The returned value represents a cursor/caret position without a selection. |
308 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, | 314 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current, |
309 BreakType break_type, | 315 BreakType break_type, |
310 VisualCursorDirection direction); | 316 VisualCursorDirection direction); |
311 | 317 |
312 // Get the selection model visually left/right of |selection| by one grapheme. | 318 // Get the selection model visually left/right of |selection| by one grapheme. |
313 // The returned value represents a cursor/caret position without a selection. | 319 // The returned value represents a cursor/caret position without a selection. |
314 virtual SelectionModel AdjacentCharSelectionModel( | 320 virtual SelectionModel AdjacentCharSelectionModel( |
315 const SelectionModel& selection, | 321 const SelectionModel& selection, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 | 363 |
358 // Ensure the text is laid out. | 364 // Ensure the text is laid out. |
359 virtual void EnsureLayout() = 0; | 365 virtual void EnsureLayout() = 0; |
360 | 366 |
361 // Draw the text. | 367 // Draw the text. |
362 virtual void DrawVisualText(Canvas* canvas) = 0; | 368 virtual void DrawVisualText(Canvas* canvas) = 0; |
363 | 369 |
364 // Returns the text used for layout, which may be |obscured_text_|. | 370 // Returns the text used for layout, which may be |obscured_text_|. |
365 const string16& GetLayoutText() const; | 371 const string16& GetLayoutText() const; |
366 | 372 |
367 // Apply composition style (underline) to composition range and selection | 373 // Apply underline styling to mark the composition range(s). |
368 // style (foreground) to selection range. | 374 void ApplyCompositionStyle(StyleBreaks* underlines); |
Alexei Svitkine (slow)
2013/01/22 19:20:21
Is this specific to underline? If not, change the
msw
2013/01/22 22:27:24
It is specific to underlines, left as-is.
| |
369 void ApplyCompositionAndSelectionStyles(StyleRanges* style_ranges); | 375 // Apply the selection text color to mark the selected text range. |
376 void ApplySelectionColor(ColorBreaks* colors); | |
370 | 377 |
371 // Returns the text offset from the origin after applying text alignment and | 378 // Returns the text offset from the origin after applying text alignment and |
372 // display offset. | 379 // display offset. |
373 Vector2d GetTextOffset(); | 380 Vector2d GetTextOffset(); |
374 | 381 |
375 // Convert points from the text space to the view space and back. | 382 // 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. | 383 // Handles the display area, display offset, and the application LTR/RTL mode. |
377 Point ToTextPoint(const Point& point); | 384 Point ToTextPoint(const Point& point); |
378 Point ToViewPoint(const Point& point); | 385 Point ToViewPoint(const Point& point); |
379 | 386 |
(...skipping 17 matching lines...) Expand all Loading... | |
397 // A convenience function to check whether the glyph attached to the caret | 404 // A convenience function to check whether the glyph attached to the caret |
398 // is within the given range. | 405 // is within the given range. |
399 static bool RangeContainsCaret(const ui::Range& range, | 406 static bool RangeContainsCaret(const ui::Range& range, |
400 size_t caret_pos, | 407 size_t caret_pos, |
401 LogicalCursorDirection caret_affinity); | 408 LogicalCursorDirection caret_affinity); |
402 | 409 |
403 private: | 410 private: |
404 friend class RenderTextTest; | 411 friend class RenderTextTest; |
405 | 412 |
406 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); | 413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
407 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); | 414 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetColorAndStyle); |
408 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); | 415 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyInvalidOrEmptyRange); |
409 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); | 416 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyle); |
417 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ResizeStyle); | |
418 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyColor); | |
410 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); | 419 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText); |
411 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); | 420 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions); |
412 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); | 421 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels); |
413 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); | 422 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing); |
414 | 423 |
424 // Adjust the existing |breaks| to apply |value| over the supplied |range|. | |
425 template <class T> | |
426 void SetBreaks(std::vector<std::pair<size_t, T> >* breaks, | |
Alexei Svitkine (slow)
2013/01/22 19:20:21
Is there a better name for this than SetBreaks()?
msw
2013/01/22 22:27:24
Done.
| |
427 T value, | |
428 const ui::Range& range); | |
429 | |
415 // Set the cursor to |position|, with the caret trailing the previous | 430 // Set the cursor to |position|, with the caret trailing the previous |
416 // grapheme, or if there is no previous grapheme, leading the cursor position. | 431 // 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. | 432 // 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), | 433 // If the |position| is not a cursorable position (not on grapheme boundary), |
419 // it is a NO-OP. | 434 // it is a NO-OP. |
420 void MoveCursorTo(size_t position, bool select); | 435 void MoveCursorTo(size_t position, bool select); |
421 | 436 |
422 // Updates |obscured_text_| if the text is obscured. | 437 // Updates |obscured_text_| if the text is obscured. |
423 void UpdateObscuredText(); | 438 void UpdateObscuredText(); |
424 | 439 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 | 486 |
472 // The background color used for drawing the selection when not focused. | 487 // The background color used for drawing the selection when not focused. |
473 SkColor selection_background_unfocused_color_; | 488 SkColor selection_background_unfocused_color_; |
474 | 489 |
475 // The focus state of the text. | 490 // The focus state of the text. |
476 bool focused_; | 491 bool focused_; |
477 | 492 |
478 // Composition text range. | 493 // Composition text range. |
479 ui::Range composition_range_; | 494 ui::Range composition_range_; |
480 | 495 |
481 // List of style ranges. Elements in the list never overlap each other. | 496 // Color and style breaks, used to color and stylize ranges of text. |
482 StyleRanges style_ranges_; | 497 // TODO(msw): Expand to support cursor, selection, background, etc. colors. |
483 // The default text style. | 498 ColorBreaks colors_; |
484 StyleRange default_style_; | 499 StyleBreaks styles_[NUM_TEXT_STYLES]; |
485 | 500 |
486 // A flag and the text to display for obscured (password) fields. | 501 // A flag and the text to display for obscured (password) fields. |
487 // Asterisks are used instead of the actual text glyphs when true. | 502 // Asterisks are used instead of the actual text glyphs when true. |
488 bool obscured_; | 503 bool obscured_; |
489 string16 obscured_text_; | 504 string16 obscured_text_; |
490 | 505 |
491 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. | 506 // Fade text head and/or tail, if text doesn't fit into |display_rect_|. |
492 bool fade_head_; | 507 bool fade_head_; |
493 bool fade_tail_; | 508 bool fade_tail_; |
494 | 509 |
(...skipping 19 matching lines...) Expand all Loading... | |
514 | 529 |
515 // Text shadows to be drawn. | 530 // Text shadows to be drawn. |
516 ShadowValues text_shadows_; | 531 ShadowValues text_shadows_; |
517 | 532 |
518 DISALLOW_COPY_AND_ASSIGN(RenderText); | 533 DISALLOW_COPY_AND_ASSIGN(RenderText); |
519 }; | 534 }; |
520 | 535 |
521 } // namespace gfx | 536 } // namespace gfx |
522 | 537 |
523 #endif // UI_GFX_RENDER_TEXT_H_ | 538 #endif // UI_GFX_RENDER_TEXT_H_ |
OLD | NEW |