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 // This file defines utility functions for eliding and formatting UI text. | 5 // This file defines utility functions for eliding and formatting UI text. |
6 | 6 |
7 #ifndef UI_GFX_TEXT_ELIDER_H_ | 7 #ifndef UI_GFX_TEXT_ELIDER_H_ |
8 #define UI_GFX_TEXT_ELIDER_H_ | 8 #define UI_GFX_TEXT_ELIDER_H_ |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 11 matching lines...) Expand all Loading... |
22 class FilePath; | 22 class FilePath; |
23 } | 23 } |
24 | 24 |
25 namespace gfx { | 25 namespace gfx { |
26 class Font; | 26 class Font; |
27 class FontList; | 27 class FontList; |
28 | 28 |
29 GFX_EXPORT extern const char kEllipsis[]; | 29 GFX_EXPORT extern const char kEllipsis[]; |
30 GFX_EXPORT extern const base::char16 kEllipsisUTF16[]; | 30 GFX_EXPORT extern const base::char16 kEllipsisUTF16[]; |
31 | 31 |
32 | |
33 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. | |
34 class StringSlicer { | |
35 public: | |
36 StringSlicer(const base::string16& text, | |
37 const base::string16& ellipsis, | |
38 bool elide_in_middle); | |
39 | |
40 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true, | |
41 // the middle of the string is removed to leave equal-length pieces from the | |
42 // beginning and end of the string; otherwise, the end of the string is | |
43 // removed and only the beginning remains. If |insert_ellipsis| is true, | |
44 // then an ellipsis character will be inserted at the cut point. | |
45 base::string16 CutString(size_t length, bool insert_ellipsis); | |
46 | |
47 private: | |
48 // Returns a valid cut boundary at or before/after |index|. | |
49 size_t FindValidBoundaryBefore(size_t index) const; | |
50 size_t FindValidBoundaryAfter(size_t index) const; | |
51 | |
52 // The text to be sliced. | |
53 const base::string16& text_; | |
54 | |
55 // Ellipsis string to use. | |
56 const base::string16& ellipsis_; | |
57 | |
58 // If true, the middle of the string will be elided. | |
59 bool elide_in_middle_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(StringSlicer); | |
62 }; | |
63 | |
64 // Elides a well-formed email address (e.g. username@domain.com) to fit into | 32 // Elides a well-formed email address (e.g. username@domain.com) to fit into |
65 // |available_pixel_width| using the specified |font_list|. | 33 // |available_pixel_width| using the specified |font_list|. |
66 // This function guarantees that the string returned will contain at least one | 34 // This function guarantees that the string returned will contain at least one |
67 // character, other than the ellipses, on either side of the '@'. If it is | 35 // character, other than the ellipses, on either side of the '@'. If it is |
68 // impossible to achieve these requirements: only an ellipsis will be returned. | 36 // impossible to achieve these requirements: only an ellipsis will be returned. |
69 // If possible: this elides only the username portion of the |email|. Otherwise, | 37 // If possible: this elides only the username portion of the |email|. Otherwise, |
70 // the domain is elided in the middle so that it splits the available width | 38 // the domain is elided in the middle so that it splits the available width |
71 // equally with the elided username (should the username be short enough that it | 39 // equally with the elided username (should the username be short enough that it |
72 // doesn't need half the available width: the elided domain will occupy that | 40 // doesn't need half the available width: the elided domain will occupy that |
73 // extra width). | 41 // extra width). |
(...skipping 19 matching lines...) Expand all Loading... |
93 const gfx::FontList& font_list, | 61 const gfx::FontList& font_list, |
94 float available_pixel_width, | 62 float available_pixel_width, |
95 const std::string& languages); | 63 const std::string& languages); |
96 | 64 |
97 enum ElideBehavior { | 65 enum ElideBehavior { |
98 // Add ellipsis at the end of the string. | 66 // Add ellipsis at the end of the string. |
99 ELIDE_AT_END, | 67 ELIDE_AT_END, |
100 // Add ellipsis in the middle of the string. | 68 // Add ellipsis in the middle of the string. |
101 ELIDE_IN_MIDDLE, | 69 ELIDE_IN_MIDDLE, |
102 // Truncate the end of the string. | 70 // Truncate the end of the string. |
103 TRUNCATE_AT_END, | 71 TRUNCATE_AT_END |
104 // No eliding of the string. | |
105 NO_ELIDE | |
106 }; | 72 }; |
107 | 73 |
108 // Elides |text| to fit in |available_pixel_width| according to the specified | 74 // Elides |text| to fit in |available_pixel_width| according to the specified |
109 // |elide_behavior|. | 75 // |elide_behavior|. |
110 GFX_EXPORT base::string16 ElideText(const base::string16& text, | 76 GFX_EXPORT base::string16 ElideText(const base::string16& text, |
111 const gfx::FontList& font_list, | 77 const gfx::FontList& font_list, |
112 float available_pixel_width, | 78 float available_pixel_width, |
113 ElideBehavior elide_behavior); | 79 ElideBehavior elide_behavior); |
114 // Obsolete version. Use the above version which takes gfx::FontList. | 80 // Obsolete version. Use the above version which takes gfx::FontList. |
115 GFX_EXPORT base::string16 ElideText(const base::string16& text, | 81 GFX_EXPORT base::string16 ElideText(const base::string16& text, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // the first word break before length, adding the horizontal ellipsis | 206 // the first word break before length, adding the horizontal ellipsis |
241 // character (unicode character 0x2026) to render ... | 207 // character (unicode character 0x2026) to render ... |
242 // The supplied string is returned if the string has length characters or | 208 // The supplied string is returned if the string has length characters or |
243 // less. | 209 // less. |
244 GFX_EXPORT base::string16 TruncateString(const base::string16& string, | 210 GFX_EXPORT base::string16 TruncateString(const base::string16& string, |
245 size_t length); | 211 size_t length); |
246 | 212 |
247 } // namespace gfx | 213 } // namespace gfx |
248 | 214 |
249 #endif // UI_GFX_TEXT_ELIDER_H_ | 215 #endif // UI_GFX_TEXT_ELIDER_H_ |
OLD | NEW |