| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "third_party/icu/source/common/unicode/uchar.h" | 15 #include "third_party/icu/source/common/unicode/uchar.h" |
| 16 #include "third_party/icu/source/i18n/unicode/coll.h" | 16 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 17 #include "ui/gfx/gfx_export.h" | 17 #include "ui/gfx/gfx_export.h" |
| 18 | 18 |
| 19 class GURL; | 19 class GURL; |
| 20 | 20 |
| 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 GFX_EXPORT extern const base::char16 kForwardSlash; |
| 30 | 31 |
| 31 | 32 |
| 32 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. | 33 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. |
| 33 class StringSlicer { | 34 class StringSlicer { |
| 34 public: | 35 public: |
| 35 StringSlicer(const base::string16& text, | 36 StringSlicer(const base::string16& text, |
| 36 const base::string16& ellipsis, | 37 const base::string16& ellipsis, |
| 37 bool elide_in_middle); | 38 bool elide_in_middle); |
| 38 | 39 |
| 39 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true, | 40 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 67 // impossible to achieve these requirements: only an ellipsis will be returned. | 68 // impossible to achieve these requirements: only an ellipsis will be returned. |
| 68 // 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, |
| 69 // 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 |
| 70 // 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 |
| 71 // 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 |
| 72 // extra width). | 73 // extra width). |
| 73 GFX_EXPORT base::string16 ElideEmail(const base::string16& email, | 74 GFX_EXPORT base::string16 ElideEmail(const base::string16& email, |
| 74 const gfx::FontList& font_list, | 75 const gfx::FontList& font_list, |
| 75 float available_pixel_width); | 76 float available_pixel_width); |
| 76 | 77 |
| 77 // This function takes a GURL object and elides it. It returns a string | |
| 78 // which composed of parts from subdomain, domain, path, filename and query. | |
| 79 // A "..." is added automatically at the end if the elided string is bigger | |
| 80 // than the |available_pixel_width|. For |available_pixel_width| == 0, a | |
| 81 // formatted, but un-elided, string is returned. |languages| is a comma | |
| 82 // separated list of ISO 639 language codes and is used to determine what | |
| 83 // characters are understood by a user. It should come from | |
| 84 // |prefs::kAcceptLanguages|. | |
| 85 // | |
| 86 // Note: in RTL locales, if the URL returned by this function is going to be | |
| 87 // displayed in the UI, then it is likely that the string needs to be marked | |
| 88 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it | |
| 89 // is displayed properly in an RTL context. Please refer to | |
| 90 // http://crbug.com/6487 for more information. | |
| 91 GFX_EXPORT base::string16 ElideUrl(const GURL& url, | |
| 92 const gfx::FontList& font_list, | |
| 93 float available_pixel_width, | |
| 94 const std::string& languages); | |
| 95 | |
| 96 enum ElideBehavior { | 78 enum ElideBehavior { |
| 97 // Add ellipsis at the end of the string. | 79 // Add ellipsis at the end of the string. |
| 98 ELIDE_AT_END, | 80 ELIDE_AT_END, |
| 99 // Add ellipsis in the middle of the string. | 81 // Add ellipsis in the middle of the string. |
| 100 ELIDE_IN_MIDDLE, | 82 ELIDE_IN_MIDDLE, |
| 101 // Truncate the end of the string. | 83 // Truncate the end of the string. |
| 102 TRUNCATE_AT_END, | 84 TRUNCATE_AT_END, |
| 103 // No eliding of the string. | 85 // No eliding of the string. |
| 104 NO_ELIDE | 86 NO_ELIDE |
| 105 }; | 87 }; |
| 106 | 88 |
| 107 // Elides |text| to fit in |available_pixel_width| according to the specified | 89 // Elides |text| to fit in |available_pixel_width| according to the specified |
| 108 // |elide_behavior|. | 90 // |elide_behavior|. |
| 109 GFX_EXPORT base::string16 ElideText(const base::string16& text, | 91 GFX_EXPORT base::string16 ElideText(const base::string16& text, |
| 110 const gfx::FontList& font_list, | 92 const gfx::FontList& font_list, |
| 111 float available_pixel_width, | 93 float available_pixel_width, |
| 112 ElideBehavior elide_behavior); | 94 ElideBehavior elide_behavior); |
| 113 | 95 |
| 114 // Elide a filename to fit a given pixel width, with an emphasis on not hiding | 96 // Elide a filename to fit a given pixel width, with an emphasis on not hiding |
| 115 // the extension unless we have to. If filename contains a path, the path will | 97 // the extension unless we have to. If filename contains a path, the path will |
| 116 // be removed if filename doesn't fit into available_pixel_width. The elided | 98 // be removed if filename doesn't fit into available_pixel_width. The elided |
| 117 // filename is forced to have LTR directionality, which means that in RTL UI | 99 // filename is forced to have LTR directionality, which means that in RTL UI |
| 118 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and | 100 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and |
| 119 // PDF (Pop Directional Formatting) mark. | 101 // PDF (Pop Directional Formatting) mark. |
| 120 GFX_EXPORT base::string16 ElideFilename(const base::FilePath& filename, | 102 GFX_EXPORT base::string16 ElideFilename(const base::FilePath& filename, |
| 121 const gfx::FontList& font_list, | 103 const gfx::FontList& font_list, |
| 122 float available_pixel_width); | 104 float available_pixel_width); |
| 123 | 105 |
| 124 // SortedDisplayURL maintains a string from a URL suitable for display to the | |
| 125 // use. SortedDisplayURL also provides a function used for comparing two | |
| 126 // SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. | |
| 127 // | |
| 128 // SortedDisplayURL is relatively cheap and supports value semantics. | |
| 129 class GFX_EXPORT SortedDisplayURL { | |
| 130 public: | |
| 131 SortedDisplayURL(const GURL& url, const std::string& languages); | |
| 132 SortedDisplayURL(); | |
| 133 ~SortedDisplayURL(); | |
| 134 | |
| 135 // Compares this SortedDisplayURL to |url| using |collator|. Returns a value | |
| 136 // < 0, = 1 or > 0 as to whether this url is less then, equal to or greater | |
| 137 // than the supplied url. | |
| 138 int Compare(const SortedDisplayURL& other, icu::Collator* collator) const; | |
| 139 | |
| 140 // Returns the display string for the URL. | |
| 141 const base::string16& display_url() const { return display_url_; } | |
| 142 | |
| 143 private: | |
| 144 // Returns everything after the host. This is used by Compare if the hosts | |
| 145 // match. | |
| 146 base::string16 AfterHost() const; | |
| 147 | |
| 148 // Host name minus 'www.'. Used by Compare. | |
| 149 base::string16 sort_host_; | |
| 150 | |
| 151 // End of the prefix (spec and separator) in display_url_. | |
| 152 size_t prefix_end_; | |
| 153 | |
| 154 base::string16 display_url_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(SortedDisplayURL); | |
| 157 }; | |
| 158 | |
| 159 // Functions to elide strings when the font information is unknown. As | 106 // Functions to elide strings when the font information is unknown. As |
| 160 // opposed to the above functions, the ElideString() and | 107 // opposed to the above functions, the ElideString() and |
| 161 // ElideRectangleString() functions operate in terms of character units, | 108 // ElideRectangleString() functions operate in terms of character units, |
| 162 // not pixels. | 109 // not pixels. |
| 163 | 110 |
| 164 // If the size of |input| is more than |max_len|, this function returns | 111 // If the size of |input| is more than |max_len|, this function returns |
| 165 // true and |input| is shortened into |output| by removing chars in the | 112 // true and |input| is shortened into |output| by removing chars in the |
| 166 // middle (they are replaced with up to 3 dots, as size permits). | 113 // middle (they are replaced with up to 3 dots, as size permits). |
| 167 // Ex: ElideString(ASCIIToUTF16("Hello"), 10, &str) puts Hello in str and | 114 // Ex: ElideString(ASCIIToUTF16("Hello"), 10, &str) puts Hello in str and |
| 168 // returns false. ElideString(ASCIIToUTF16("Hello my name is Tom"), 10, &str) | 115 // returns false. ElideString(ASCIIToUTF16("Hello my name is Tom"), 10, &str) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // the first word break before length, adding the horizontal ellipsis | 181 // the first word break before length, adding the horizontal ellipsis |
| 235 // character (unicode character 0x2026) to render ... | 182 // character (unicode character 0x2026) to render ... |
| 236 // The supplied string is returned if the string has length characters or | 183 // The supplied string is returned if the string has length characters or |
| 237 // less. | 184 // less. |
| 238 GFX_EXPORT base::string16 TruncateString(const base::string16& string, | 185 GFX_EXPORT base::string16 TruncateString(const base::string16& string, |
| 239 size_t length); | 186 size_t length); |
| 240 | 187 |
| 241 } // namespace gfx | 188 } // namespace gfx |
| 242 | 189 |
| 243 #endif // UI_GFX_TEXT_ELIDER_H_ | 190 #endif // UI_GFX_TEXT_ELIDER_H_ |
| OLD | NEW |