OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file defines utility functions for working with text in views. | 5 // This file defines utility functions for working with text in views. |
6 | 6 |
7 #ifndef VIEWS_VIEW_TEXT_UTILS_H_ | 7 #ifndef VIEWS_VIEW_TEXT_UTILS_H_ |
8 #define VIEWS_VIEW_TEXT_UTILS_H_ | 8 #define VIEWS_VIEW_TEXT_UTILS_H_ |
9 #pragma once | 9 #pragma once |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "ui/gfx/font.h" | 13 #include "ui/gfx/font.h" |
14 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
15 #include "views/views_api.h" | 15 #include "views/views_export.h" |
16 | 16 |
17 namespace gfx { | 17 namespace gfx { |
18 class Canvas; | 18 class Canvas; |
19 class Size; | 19 class Size; |
20 } | 20 } |
21 | 21 |
22 namespace views { | 22 namespace views { |
23 class Label; | 23 class Label; |
24 class Link; | 24 class Link; |
25 } | 25 } |
26 | 26 |
27 namespace view_text_utils { | 27 namespace view_text_utils { |
28 | 28 |
29 // Draws a string onto the canvas (wrapping if needed) while also keeping | 29 // Draws a string onto the canvas (wrapping if needed) while also keeping |
30 // track of where it ends so we can position a URL after the text. The | 30 // track of where it ends so we can position a URL after the text. The |
31 // parameter |bounds| represents the boundary we have to work with, |position| | 31 // parameter |bounds| represents the boundary we have to work with, |position| |
32 // specifies where to draw the string (relative to the top left corner of the | 32 // specifies where to draw the string (relative to the top left corner of the |
33 // |bounds| rectangle and |font| specifies the font to use when drawing. When | 33 // |bounds| rectangle and |font| specifies the font to use when drawing. When |
34 // the function returns, the parameter |rect| contains where to draw the URL | 34 // the function returns, the parameter |rect| contains where to draw the URL |
35 // (to the right of where we just drew the text) and |position| is updated to | 35 // (to the right of where we just drew the text) and |position| is updated to |
36 // reflect where to draw the next string after the URL. |label| is a dummy | 36 // reflect where to draw the next string after the URL. |label| is a dummy |
37 // label with the correct width and origin for the text to be written; it's | 37 // label with the correct width and origin for the text to be written; it's |
38 // used so that the x position can be correctly mirrored in RTL languages. | 38 // used so that the x position can be correctly mirrored in RTL languages. |
39 // |text_direction_is_rtl| is true if an RTL language is being used. | 39 // |text_direction_is_rtl| is true if an RTL language is being used. |
40 // NOTE: The reason why we need this function is because while Skia knows how | 40 // NOTE: The reason why we need this function is because while Skia knows how |
41 // to wrap text appropriately, it doesn't tell us where it drew the last | 41 // to wrap text appropriately, it doesn't tell us where it drew the last |
42 // character, which we need to position the URLs within the text. | 42 // character, which we need to position the URLs within the text. |
43 VIEWS_API void DrawTextAndPositionUrl(gfx::Canvas* canvas, | 43 VIEWS_EXPORT void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
44 views::Label* label, | 44 views::Label* label, |
45 const std::wstring& text, | 45 const std::wstring& text, |
46 views::Link* link, | 46 views::Link* link, |
47 gfx::Rect* rect, | 47 gfx::Rect* rect, |
48 gfx::Size* position, | 48 gfx::Size* position, |
49 bool text_direction_is_rtl, | 49 bool text_direction_is_rtl, |
50 const gfx::Rect& bounds, | 50 const gfx::Rect& bounds, |
51 const gfx::Font& font); | 51 const gfx::Font& font); |
52 | 52 |
53 // A helper function for DrawTextAndPositionUrl, which simply draws the text | 53 // A helper function for DrawTextAndPositionUrl, which simply draws the text |
54 // from a certain starting point |position| and wraps within bounds. | 54 // from a certain starting point |position| and wraps within bounds. |
55 // |word_for_word| specifies whether to draw the text word for word or whether | 55 // |word_for_word| specifies whether to draw the text word for word or whether |
56 // to treat the text as one blurb (similar to the way URL's are treated inside | 56 // to treat the text as one blurb (similar to the way URL's are treated inside |
57 // RTL text. For details on the other parameters, see DrawTextAndPositionUrl. | 57 // RTL text. For details on the other parameters, see DrawTextAndPositionUrl. |
58 void DrawTextStartingFrom(gfx::Canvas* canvas, | 58 void DrawTextStartingFrom(gfx::Canvas* canvas, |
59 views::Label* label, | 59 views::Label* label, |
60 const std::wstring& text, | 60 const std::wstring& text, |
61 gfx::Size* position, | 61 gfx::Size* position, |
62 const gfx::Rect& bounds, | 62 const gfx::Rect& bounds, |
63 const gfx::Font& font, | 63 const gfx::Font& font, |
64 bool text_direction_is_rtl, | 64 bool text_direction_is_rtl, |
65 bool word_for_word); | 65 bool word_for_word); |
66 | 66 |
67 // A simply utility function that calculates whether a word of width | 67 // A simply utility function that calculates whether a word of width |
68 // |word_width| fits at position |position| within the |bounds| rectangle. If | 68 // |word_width| fits at position |position| within the |bounds| rectangle. If |
69 // not, |position| is updated to wrap to the beginning of the next line. | 69 // not, |position| is updated to wrap to the beginning of the next line. |
70 void WrapIfWordDoesntFit(int word_width, | 70 void WrapIfWordDoesntFit(int word_width, |
71 int font_height, | 71 int font_height, |
72 gfx::Size* position, | 72 gfx::Size* position, |
73 const gfx::Rect& bounds); | 73 const gfx::Rect& bounds); |
74 | 74 |
75 } // namespace view_text_utils | 75 } // namespace view_text_utils |
76 | 76 |
77 #endif // CHROME_BROWSER_VIEWS_VIEW_TEXT_UTILS_H_ | 77 #endif // CHROME_BROWSER_VIEWS_VIEW_TEXT_UTILS_H_ |
OLD | NEW |