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