| 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_BASE_TEXT_TEXT_ELIDER_H_ | 7 #ifndef UI_BASE_TEXT_TEXT_ELIDER_H_ |
| 8 #define UI_BASE_TEXT_TEXT_ELIDER_H_ | 8 #define UI_BASE_TEXT_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/base/ui_export.h" | 17 #include "ui/base/ui_export.h" |
| 18 #include "ui/gfx/font.h" | |
| 19 | 18 |
| 20 class GURL; | 19 class GURL; |
| 21 | 20 |
| 22 namespace base { | 21 namespace base { |
| 23 class FilePath; | 22 class FilePath; |
| 24 } | 23 } |
| 25 | 24 |
| 25 namespace gfx { |
| 26 class Font; |
| 27 class FontList; |
| 28 } // namespace gfx |
| 29 |
| 26 namespace ui { | 30 namespace ui { |
| 27 | 31 |
| 28 UI_EXPORT extern const char kEllipsis[]; | 32 UI_EXPORT extern const char kEllipsis[]; |
| 29 UI_EXPORT extern const char16 kEllipsisUTF16[]; | 33 UI_EXPORT extern const char16 kEllipsisUTF16[]; |
| 30 | 34 |
| 31 // Elides a well-formed email address (e.g. username@domain.com) to fit into | 35 // Elides a well-formed email address (e.g. username@domain.com) to fit into |
| 32 // |available_pixel_width| using the specified |font|. | 36 // |available_pixel_width| using the specified |font_list|. |
| 33 // This function guarantees that the string returned will contain at least one | 37 // 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 | 38 // 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. | 39 // impossible to achieve these requirements: only an ellipsis will be returned. |
| 36 // If possible: this elides only the username portion of the |email|. Otherwise, | 40 // 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 | 41 // 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 | 42 // 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 | 43 // doesn't need half the available width: the elided domain will occupy that |
| 40 // extra width). | 44 // extra width). |
| 41 UI_EXPORT string16 ElideEmail(const string16& email, | 45 UI_EXPORT string16 ElideEmail(const string16& email, |
| 46 const gfx::FontList& font_list, |
| 47 int available_pixel_width); |
| 48 // Obsolete version. Use the above version which takes gfx::FontList. |
| 49 UI_EXPORT string16 ElideEmail(const string16& email, |
| 42 const gfx::Font& font, | 50 const gfx::Font& font, |
| 43 int available_pixel_width); | 51 int available_pixel_width); |
| 44 | 52 |
| 45 // This function takes a GURL object and elides it. It returns a string | 53 // This function takes a GURL object and elides it. It returns a string |
| 46 // which composed of parts from subdomain, domain, path, filename and query. | 54 // which composed of parts from subdomain, domain, path, filename and query. |
| 47 // A "..." is added automatically at the end if the elided string is bigger | 55 // A "..." is added automatically at the end if the elided string is bigger |
| 48 // than the |available_pixel_width|. For |available_pixel_width| == 0, a | 56 // than the |available_pixel_width|. For |available_pixel_width| == 0, a |
| 49 // formatted, but un-elided, string is returned. |languages| is a comma | 57 // formatted, but un-elided, string is returned. |languages| is a comma |
| 50 // separated list of ISO 639 language codes and is used to determine what | 58 // separated list of ISO 639 language codes and is used to determine what |
| 51 // characters are understood by a user. It should come from | 59 // characters are understood by a user. It should come from |
| 52 // |prefs::kAcceptLanguages|. | 60 // |prefs::kAcceptLanguages|. |
| 53 // | 61 // |
| 54 // Note: in RTL locales, if the URL returned by this function is going to be | 62 // Note: in RTL locales, if the URL returned by this function is going to be |
| 55 // displayed in the UI, then it is likely that the string needs to be marked | 63 // displayed in the UI, then it is likely that the string needs to be marked |
| 56 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it | 64 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it |
| 57 // is displayed properly in an RTL context. Please refer to | 65 // is displayed properly in an RTL context. Please refer to |
| 58 // http://crbug.com/6487 for more information. | 66 // http://crbug.com/6487 for more information. |
| 59 UI_EXPORT string16 ElideUrl(const GURL& url, | 67 UI_EXPORT string16 ElideUrl(const GURL& url, |
| 68 const gfx::FontList& font_list, |
| 69 int available_pixel_width, |
| 70 const std::string& languages); |
| 71 // Obsolete version. Use the above version which takes gfx::FontList. |
| 72 UI_EXPORT string16 ElideUrl(const GURL& url, |
| 60 const gfx::Font& font, | 73 const gfx::Font& font, |
| 61 int available_pixel_width, | 74 int available_pixel_width, |
| 62 const std::string& languages); | 75 const std::string& languages); |
| 63 | 76 |
| 64 enum ElideBehavior { | 77 enum ElideBehavior { |
| 65 // Add ellipsis at the end of the string. | 78 // Add ellipsis at the end of the string. |
| 66 ELIDE_AT_END, | 79 ELIDE_AT_END, |
| 67 // Add ellipsis in the middle of the string. | 80 // Add ellipsis in the middle of the string. |
| 68 ELIDE_IN_MIDDLE, | 81 ELIDE_IN_MIDDLE, |
| 69 // Truncate the end of the string. | 82 // Truncate the end of the string. |
| 70 TRUNCATE_AT_END | 83 TRUNCATE_AT_END |
| 71 }; | 84 }; |
| 72 | 85 |
| 73 // Elides |text| to fit in |available_pixel_width| according to the specified | 86 // Elides |text| to fit in |available_pixel_width| according to the specified |
| 74 // |elide_behavior|. | 87 // |elide_behavior|. |
| 75 UI_EXPORT string16 ElideText(const string16& text, | 88 UI_EXPORT string16 ElideText(const string16& text, |
| 89 const gfx::FontList& font_list, |
| 90 int available_pixel_width, |
| 91 ElideBehavior elide_behavior); |
| 92 // Obsolete version. Use the above version which takes gfx::FontList. |
| 93 UI_EXPORT string16 ElideText(const string16& text, |
| 76 const gfx::Font& font, | 94 const gfx::Font& font, |
| 77 int available_pixel_width, | 95 int available_pixel_width, |
| 78 ElideBehavior elide_behavior); | 96 ElideBehavior elide_behavior); |
| 79 | 97 |
| 80 // Elide a filename to fit a given pixel width, with an emphasis on not hiding | 98 // Elide a filename to fit a given pixel width, with an emphasis on not hiding |
| 81 // the extension unless we have to. If filename contains a path, the path will | 99 // the extension unless we have to. If filename contains a path, the path will |
| 82 // be removed if filename doesn't fit into available_pixel_width. The elided | 100 // be removed if filename doesn't fit into available_pixel_width. The elided |
| 83 // filename is forced to have LTR directionality, which means that in RTL UI | 101 // filename is forced to have LTR directionality, which means that in RTL UI |
| 84 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and | 102 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and |
| 85 // PDF (Pop Directional Formatting) mark. | 103 // PDF (Pop Directional Formatting) mark. |
| 86 UI_EXPORT string16 ElideFilename(const base::FilePath& filename, | 104 UI_EXPORT string16 ElideFilename(const base::FilePath& filename, |
| 105 const gfx::FontList& font_list, |
| 106 int available_pixel_width); |
| 107 // Obsolete version. Use the above version which takes gfx::FontList. |
| 108 UI_EXPORT string16 ElideFilename(const base::FilePath& filename, |
| 87 const gfx::Font& font, | 109 const gfx::Font& font, |
| 88 int available_pixel_width); | 110 int available_pixel_width); |
| 89 | 111 |
| 90 // SortedDisplayURL maintains a string from a URL suitable for display to the | 112 // SortedDisplayURL maintains a string from a URL suitable for display to the |
| 91 // use. SortedDisplayURL also provides a function used for comparing two | 113 // use. SortedDisplayURL also provides a function used for comparing two |
| 92 // SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. | 114 // SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. |
| 93 // | 115 // |
| 94 // SortedDisplayURL is relatively cheap and supports value semantics. | 116 // SortedDisplayURL is relatively cheap and supports value semantics. |
| 95 class UI_EXPORT SortedDisplayURL { | 117 class UI_EXPORT SortedDisplayURL { |
| 96 public: | 118 public: |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // Indicates whether the |available_pixel_width| by |available_pixel_height| | 196 // Indicates whether the |available_pixel_width| by |available_pixel_height| |
| 175 // rectangle passed to |ElideRectangleText()| had insufficient space to | 197 // rectangle passed to |ElideRectangleText()| had insufficient space to |
| 176 // accommodate the given |text|, leading to elision or truncation. | 198 // accommodate the given |text|, leading to elision or truncation. |
| 177 enum ReformattingResultFlags { | 199 enum ReformattingResultFlags { |
| 178 INSUFFICIENT_SPACE_HORIZONTAL = 1 << 0, | 200 INSUFFICIENT_SPACE_HORIZONTAL = 1 << 0, |
| 179 INSUFFICIENT_SPACE_VERTICAL = 1 << 1, | 201 INSUFFICIENT_SPACE_VERTICAL = 1 << 1, |
| 180 }; | 202 }; |
| 181 | 203 |
| 182 // Reformats |text| into output vector |lines| so that the resulting text fits | 204 // Reformats |text| into output vector |lines| so that the resulting text fits |
| 183 // into an |available_pixel_width| by |available_pixel_height| rectangle with | 205 // into an |available_pixel_width| by |available_pixel_height| rectangle with |
| 184 // the specified |font|. Input newlines are respected, but lines that are too | 206 // the specified |font_list|. Input newlines are respected, but lines that are |
| 185 // long are broken into pieces. For words that are too wide to fit on a single | 207 // too long are broken into pieces. For words that are too wide to fit on a |
| 186 // line, the wrapping behavior can be specified with the |wrap_behavior| param. | 208 // single line, the wrapping behavior can be specified with the |wrap_behavior| |
| 187 // Returns a combination of |ReformattingResultFlags| that indicate whether the | 209 // param. Returns a combination of |ReformattingResultFlags| that indicate |
| 188 // given rectangle had insufficient space to accommodate |texŧ|, leading to | 210 // whether the given rectangle had insufficient space to accommodate |texŧ|, |
| 189 // elision or truncation (and not just reformatting). | 211 // leading to elision or truncation (and not just reformatting). |
| 212 UI_EXPORT int ElideRectangleText(const string16& text, |
| 213 const gfx::FontList& font_list, |
| 214 int available_pixel_width, |
| 215 int available_pixel_height, |
| 216 WordWrapBehavior wrap_behavior, |
| 217 std::vector<string16>* lines); |
| 218 // Obsolete version. Use the above version which takes gfx::FontList. |
| 190 UI_EXPORT int ElideRectangleText(const string16& text, | 219 UI_EXPORT int ElideRectangleText(const string16& text, |
| 191 const gfx::Font& font, | 220 const gfx::Font& font, |
| 192 int available_pixel_width, | 221 int available_pixel_width, |
| 193 int available_pixel_height, | 222 int available_pixel_height, |
| 194 WordWrapBehavior wrap_behavior, | 223 WordWrapBehavior wrap_behavior, |
| 195 std::vector<string16>* lines); | 224 std::vector<string16>* lines); |
| 196 | 225 |
| 197 // Truncates the string to length characters. This breaks the string at | 226 // Truncates the string to length characters. This breaks the string at |
| 198 // the first word break before length, adding the horizontal ellipsis | 227 // the first word break before length, adding the horizontal ellipsis |
| 199 // character (unicode character 0x2026) to render ... | 228 // character (unicode character 0x2026) to render ... |
| 200 // The supplied string is returned if the string has length characters or | 229 // The supplied string is returned if the string has length characters or |
| 201 // less. | 230 // less. |
| 202 UI_EXPORT string16 TruncateString(const string16& string, size_t length); | 231 UI_EXPORT string16 TruncateString(const string16& string, size_t length); |
| 203 | 232 |
| 204 } // namespace ui | 233 } // namespace ui |
| 205 | 234 |
| 206 #endif // UI_BASE_TEXT_TEXT_ELIDER_H_ | 235 #endif // UI_BASE_TEXT_TEXT_ELIDER_H_ |
| OLD | NEW |