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 | |
msw
2013/12/12 19:28:29
nit: remove blank line
Anuj
2013/12/13 01:23:50
Done.
| |
51 size_t FindValidBoundaryAfter(size_t index) const; | |
52 | |
53 // The text to be sliced. | |
54 const base::string16& text_; | |
55 | |
56 // Ellipsis string to use. | |
57 const base::string16& ellipsis_; | |
58 | |
59 // If true, the middle of the string will be elided. | |
60 bool elide_in_middle_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(StringSlicer); | |
63 }; | |
64 | |
32 // Elides a well-formed email address (e.g. username@domain.com) to fit into | 65 // Elides a well-formed email address (e.g. username@domain.com) to fit into |
33 // |available_pixel_width| using the specified |font_list|. | 66 // |available_pixel_width| using the specified |font_list|. |
34 // This function guarantees that the string returned will contain at least one | 67 // This function guarantees that the string returned will contain at least one |
35 // character, other than the ellipses, on either side of the '@'. If it is | 68 // character, other than the ellipses, on either side of the '@'. If it is |
36 // impossible to achieve these requirements: only an ellipsis will be returned. | 69 // impossible to achieve these requirements: only an ellipsis will be returned. |
37 // If possible: this elides only the username portion of the |email|. Otherwise, | 70 // If possible: this elides only the username portion of the |email|. Otherwise, |
38 // the domain is elided in the middle so that it splits the available width | 71 // the domain is elided in the middle so that it splits the available width |
39 // equally with the elided username (should the username be short enough that it | 72 // equally with the elided username (should the username be short enough that it |
40 // doesn't need half the available width: the elided domain will occupy that | 73 // doesn't need half the available width: the elided domain will occupy that |
41 // extra width). | 74 // extra width). |
(...skipping 19 matching lines...) Expand all Loading... | |
61 const gfx::FontList& font_list, | 94 const gfx::FontList& font_list, |
62 float available_pixel_width, | 95 float available_pixel_width, |
63 const std::string& languages); | 96 const std::string& languages); |
64 | 97 |
65 enum ElideBehavior { | 98 enum ElideBehavior { |
66 // Add ellipsis at the end of the string. | 99 // Add ellipsis at the end of the string. |
67 ELIDE_AT_END, | 100 ELIDE_AT_END, |
68 // Add ellipsis in the middle of the string. | 101 // Add ellipsis in the middle of the string. |
69 ELIDE_IN_MIDDLE, | 102 ELIDE_IN_MIDDLE, |
70 // Truncate the end of the string. | 103 // Truncate the end of the string. |
71 TRUNCATE_AT_END | 104 TRUNCATE_AT_END, |
105 // No eliding of the string. | |
106 NO_ELIDE | |
72 }; | 107 }; |
73 | 108 |
74 // Elides |text| to fit in |available_pixel_width| according to the specified | 109 // Elides |text| to fit in |available_pixel_width| according to the specified |
75 // |elide_behavior|. | 110 // |elide_behavior|. |
76 GFX_EXPORT base::string16 ElideText(const base::string16& text, | 111 GFX_EXPORT base::string16 ElideText(const base::string16& text, |
77 const gfx::FontList& font_list, | 112 const gfx::FontList& font_list, |
78 float available_pixel_width, | 113 float available_pixel_width, |
79 ElideBehavior elide_behavior); | 114 ElideBehavior elide_behavior); |
80 // Obsolete version. Use the above version which takes gfx::FontList. | 115 // Obsolete version. Use the above version which takes gfx::FontList. |
81 GFX_EXPORT base::string16 ElideText(const base::string16& text, | 116 GFX_EXPORT base::string16 ElideText(const base::string16& text, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 // the first word break before length, adding the horizontal ellipsis | 241 // the first word break before length, adding the horizontal ellipsis |
207 // character (unicode character 0x2026) to render ... | 242 // character (unicode character 0x2026) to render ... |
208 // The supplied string is returned if the string has length characters or | 243 // The supplied string is returned if the string has length characters or |
209 // less. | 244 // less. |
210 GFX_EXPORT base::string16 TruncateString(const base::string16& string, | 245 GFX_EXPORT base::string16 TruncateString(const base::string16& string, |
211 size_t length); | 246 size_t length); |
212 | 247 |
213 } // namespace gfx | 248 } // namespace gfx |
214 | 249 |
215 #endif // UI_GFX_TEXT_ELIDER_H_ | 250 #endif // UI_GFX_TEXT_ELIDER_H_ |
OLD | NEW |