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

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

Issue 119813002: Implement eliding/truncating at end in RenderText (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed merge issue Created 6 years, 11 months 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 | Annotate | Revision Log
« no previous file with comments | « ui/gfx/render_text_unittest.cc ('k') | ui/gfx/text_elider.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 10 matching lines...) Expand all
21 namespace base { 21 namespace base {
22 class FilePath; 22 class FilePath;
23 } 23 }
24 24
25 namespace gfx { 25 namespace gfx {
26 class FontList; 26 class FontList;
27 27
28 GFX_EXPORT extern const char kEllipsis[]; 28 GFX_EXPORT extern const char kEllipsis[];
29 GFX_EXPORT extern const base::char16 kEllipsisUTF16[]; 29 GFX_EXPORT extern const base::char16 kEllipsisUTF16[];
30 30
31
32 // Helper class to split + elide text, while respecting UTF16 surrogate pairs.
33 class StringSlicer {
34 public:
35 StringSlicer(const base::string16& text,
36 const base::string16& ellipsis,
37 bool elide_in_middle);
38
39 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true,
40 // the middle of the string is removed to leave equal-length pieces from the
41 // beginning and end of the string; otherwise, the end of the string is
42 // removed and only the beginning remains. If |insert_ellipsis| is true,
43 // then an ellipsis character will be inserted at the cut point.
44 base::string16 CutString(size_t length, bool insert_ellipsis);
45
46 private:
47 // Returns a valid cut boundary at or before/after |index|.
48 size_t FindValidBoundaryBefore(size_t index) const;
49 size_t FindValidBoundaryAfter(size_t index) const;
50
51 // The text to be sliced.
52 const base::string16& text_;
53
54 // Ellipsis string to use.
55 const base::string16& ellipsis_;
56
57 // If true, the middle of the string will be elided.
58 bool elide_in_middle_;
59
60 DISALLOW_COPY_AND_ASSIGN(StringSlicer);
61 };
62
31 // Elides a well-formed email address (e.g. username@domain.com) to fit into 63 // Elides a well-formed email address (e.g. username@domain.com) to fit into
32 // |available_pixel_width| using the specified |font_list|. 64 // |available_pixel_width| using the specified |font_list|.
33 // This function guarantees that the string returned will contain at least one 65 // This function guarantees that the string returned will contain at least one
34 // character, other than the ellipses, on either side of the '@'. If it is 66 // character, other than the ellipses, on either side of the '@'. If it is
35 // impossible to achieve these requirements: only an ellipsis will be returned. 67 // impossible to achieve these requirements: only an ellipsis will be returned.
36 // If possible: this elides only the username portion of the |email|. Otherwise, 68 // If possible: this elides only the username portion of the |email|. Otherwise,
37 // the domain is elided in the middle so that it splits the available width 69 // the domain is elided in the middle so that it splits the available width
38 // equally with the elided username (should the username be short enough that it 70 // equally with the elided username (should the username be short enough that it
39 // doesn't need half the available width: the elided domain will occupy that 71 // doesn't need half the available width: the elided domain will occupy that
40 // extra width). 72 // extra width).
(...skipping 19 matching lines...) Expand all
60 const gfx::FontList& font_list, 92 const gfx::FontList& font_list,
61 float available_pixel_width, 93 float available_pixel_width,
62 const std::string& languages); 94 const std::string& languages);
63 95
64 enum ElideBehavior { 96 enum ElideBehavior {
65 // Add ellipsis at the end of the string. 97 // Add ellipsis at the end of the string.
66 ELIDE_AT_END, 98 ELIDE_AT_END,
67 // Add ellipsis in the middle of the string. 99 // Add ellipsis in the middle of the string.
68 ELIDE_IN_MIDDLE, 100 ELIDE_IN_MIDDLE,
69 // Truncate the end of the string. 101 // Truncate the end of the string.
70 TRUNCATE_AT_END 102 TRUNCATE_AT_END,
103 // No eliding of the string.
104 NO_ELIDE
71 }; 105 };
72 106
73 // Elides |text| to fit in |available_pixel_width| according to the specified 107 // Elides |text| to fit in |available_pixel_width| according to the specified
74 // |elide_behavior|. 108 // |elide_behavior|.
75 GFX_EXPORT base::string16 ElideText(const base::string16& text, 109 GFX_EXPORT base::string16 ElideText(const base::string16& text,
76 const gfx::FontList& font_list, 110 const gfx::FontList& font_list,
77 float available_pixel_width, 111 float available_pixel_width,
78 ElideBehavior elide_behavior); 112 ElideBehavior elide_behavior);
79 113
80 // Elide a filename to fit a given pixel width, with an emphasis on not hiding 114 // Elide a filename to fit a given pixel width, with an emphasis on not hiding
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 // the first word break before length, adding the horizontal ellipsis 234 // the first word break before length, adding the horizontal ellipsis
201 // character (unicode character 0x2026) to render ... 235 // character (unicode character 0x2026) to render ...
202 // The supplied string is returned if the string has length characters or 236 // The supplied string is returned if the string has length characters or
203 // less. 237 // less.
204 GFX_EXPORT base::string16 TruncateString(const base::string16& string, 238 GFX_EXPORT base::string16 TruncateString(const base::string16& string,
205 size_t length); 239 size_t length);
206 240
207 } // namespace gfx 241 } // namespace gfx
208 242
209 #endif // UI_GFX_TEXT_ELIDER_H_ 243 #endif // UI_GFX_TEXT_ELIDER_H_
OLDNEW
« no previous file with comments | « ui/gfx/render_text_unittest.cc ('k') | ui/gfx/text_elider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698