Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: ui/gfx/text_elider.h

Issue 112063003: Implement eliding/truncating at end in RenderText (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments (Patch Set 4) Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
32 // Elides a well-formed email address (e.g. username@domain.com) to fit into 64 // Elides a well-formed email address (e.g. username@domain.com) to fit into
33 // |available_pixel_width| using the specified |font_list|. 65 // |available_pixel_width| using the specified |font_list|.
34 // This function guarantees that the string returned will contain at least one 66 // 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 67 // 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. 68 // impossible to achieve these requirements: only an ellipsis will be returned.
37 // If possible: this elides only the username portion of the |email|. Otherwise, 69 // 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 70 // 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 71 // 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 72 // doesn't need half the available width: the elided domain will occupy that
41 // extra width). 73 // extra width).
(...skipping 19 matching lines...) Expand all
61 const gfx::FontList& font_list, 93 const gfx::FontList& font_list,
62 float available_pixel_width, 94 float available_pixel_width,
63 const std::string& languages); 95 const std::string& languages);
64 96
65 enum ElideBehavior { 97 enum ElideBehavior {
66 // Add ellipsis at the end of the string. 98 // Add ellipsis at the end of the string.
67 ELIDE_AT_END, 99 ELIDE_AT_END,
68 // Add ellipsis in the middle of the string. 100 // Add ellipsis in the middle of the string.
69 ELIDE_IN_MIDDLE, 101 ELIDE_IN_MIDDLE,
70 // Truncate the end of the string. 102 // Truncate the end of the string.
71 TRUNCATE_AT_END 103 TRUNCATE_AT_END,
104 // No eliding of the string.
105 NO_ELIDE
72 }; 106 };
73 107
74 // Elides |text| to fit in |available_pixel_width| according to the specified 108 // Elides |text| to fit in |available_pixel_width| according to the specified
75 // |elide_behavior|. 109 // |elide_behavior|.
76 GFX_EXPORT base::string16 ElideText(const base::string16& text, 110 GFX_EXPORT base::string16 ElideText(const base::string16& text,
77 const gfx::FontList& font_list, 111 const gfx::FontList& font_list,
78 float available_pixel_width, 112 float available_pixel_width,
79 ElideBehavior elide_behavior); 113 ElideBehavior elide_behavior);
80 // Obsolete version. Use the above version which takes gfx::FontList. 114 // Obsolete version. Use the above version which takes gfx::FontList.
81 GFX_EXPORT base::string16 ElideText(const base::string16& text, 115 GFX_EXPORT base::string16 ElideText(const base::string16& text,
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // the first word break before length, adding the horizontal ellipsis 240 // the first word break before length, adding the horizontal ellipsis
207 // character (unicode character 0x2026) to render ... 241 // character (unicode character 0x2026) to render ...
208 // The supplied string is returned if the string has length characters or 242 // The supplied string is returned if the string has length characters or
209 // less. 243 // less.
210 GFX_EXPORT base::string16 TruncateString(const base::string16& string, 244 GFX_EXPORT base::string16 TruncateString(const base::string16& string,
211 size_t length); 245 size_t length);
212 246
213 } // namespace gfx 247 } // namespace gfx
214 248
215 #endif // UI_GFX_TEXT_ELIDER_H_ 249 #endif // UI_GFX_TEXT_ELIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698