OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef APP_TEXT_ELIDER_H_ | |
6 #define APP_TEXT_ELIDER_H_ | |
7 #pragma once | |
8 | |
9 #include <unicode/coll.h> | |
10 #include <unicode/uchar.h> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/string16.h" | |
14 #include "gfx/font.h" | |
15 | |
16 class FilePath; | |
17 class GURL; | |
18 | |
19 // TODO(port): this file should deal in string16s rather than wstrings. | |
20 namespace gfx { | |
21 | |
22 // This function takes a GURL object and elides it. It returns a string | |
23 // which composed of parts from subdomain, domain, path, filename and query. | |
24 // A "..." is added automatically at the end if the elided string is bigger | |
25 // than the available pixel width. For available pixel width = 0, empty | |
26 // string is returned. |languages| is a comma separted list of ISO 639 | |
27 // language codes and is used to determine what characters are understood | |
28 // by a user. It should come from |prefs::kAcceptLanguages|. | |
29 // | |
30 // Note: in RTL locales, if the URL returned by this function is going to be | |
31 // displayed in the UI, then it is likely that the string needs to be marked | |
32 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it | |
33 // is displayed properly in an RTL context. Please refer to | |
34 // http://crbug.com/6487 for more information. | |
35 string16 ElideUrl(const GURL& url, | |
36 const gfx::Font& font, | |
37 int available_pixel_width, | |
38 const std::wstring& languages); | |
39 | |
40 // Elides |text| to fit in |available_pixel_width|. If |elide_in_middle| is | |
41 // set the ellipsis is placed in the middle of the string; otherwise it is | |
42 // placed at the end. | |
43 string16 ElideText(const string16& text, | |
44 const gfx::Font& font, | |
45 int available_pixel_width, | |
46 bool elide_in_middle); | |
47 | |
48 // Elide a filename to fit a given pixel width, with an emphasis on not hiding | |
49 // the extension unless we have to. If filename contains a path, the path will | |
50 // be removed if filename doesn't fit into available_pixel_width. The elided | |
51 // filename is forced to have LTR directionality, which means that in RTL UI | |
52 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and | |
53 // PDF (Pop Directional Formatting) mark. | |
54 string16 ElideFilename(const FilePath& filename, | |
55 const gfx::Font& font, | |
56 int available_pixel_width); | |
57 | |
58 // SortedDisplayURL maintains a string from a URL suitable for display to the | |
59 // use. SortedDisplayURL also provides a function used for comparing two | |
60 // SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. | |
61 // | |
62 // SortedDisplayURL is relatively cheap and supports value semantics. | |
63 class SortedDisplayURL { | |
64 public: | |
65 SortedDisplayURL(const GURL& url, const std::wstring& languages); | |
66 SortedDisplayURL(); | |
67 ~SortedDisplayURL(); | |
68 | |
69 // Compares this SortedDisplayURL to |url| using |collator|. Returns a value | |
70 // < 0, = 1 or > 0 as to whether this url is less then, equal to or greater | |
71 // than the supplied url. | |
72 int Compare(const SortedDisplayURL& other, icu::Collator* collator) const; | |
73 | |
74 // Returns the display string for the URL. | |
75 const string16& display_url() const { return display_url_; } | |
76 | |
77 private: | |
78 // Returns everything after the host. This is used by Compare if the hosts | |
79 // match. | |
80 string16 AfterHost() const; | |
81 | |
82 // Host name minus 'www.'. Used by Compare. | |
83 string16 sort_host_; | |
84 | |
85 // End of the prefix (spec and separator) in display_url_. | |
86 size_t prefix_end_; | |
87 | |
88 string16 display_url_; | |
89 }; | |
90 | |
91 // Functions to elide strings when the font information is unknown. As | |
92 // opposed to the above functions, the ElideString() and | |
93 // ElideRectangleString() functions operate in terms of character units, | |
94 // not pixels. | |
95 | |
96 // If the size of |input| is more than |max_len|, this function returns | |
97 // true and |input| is shortened into |output| by removing chars in the | |
98 // middle (they are replaced with up to 3 dots, as size permits). | |
99 // Ex: ElideString(L"Hello", 10, &str) puts Hello in str and returns false. | |
100 // ElideString(L"Hello my name is Tom", 10, &str) puts "Hell...Tom" in str | |
101 // and returns true. | |
102 // TODO(tsepez): Doesn't handle UTF-16 surrogate pairs properly. | |
103 // TODO(tsepez): Doesn't handle bidi properly | |
104 bool ElideString(const std::wstring& input, int max_len, std::wstring* output); | |
105 | |
106 // Reformat |input| into |output| so that it fits into a |max_rows| by | |
107 // |max_cols| rectangle of characters. Input newlines are respected, but | |
108 // lines that are too long are broken into pieces, first at naturally | |
109 // occuring whitespace boundaries, and then intra-word (respecting UTF-16 | |
110 // surrogate pairs) as necssary. Truncation (indicated by an added 3 dots) | |
111 // occurs if the result is still too long. Returns true if the input had | |
112 // to be truncated (and not just reformatted). | |
113 bool ElideRectangleString(const string16& input, size_t max_rows, | |
114 size_t max_cols, string16* output); | |
115 | |
116 | |
117 } // namespace gfx. | |
118 | |
119 #endif // APP_TEXT_ELIDER_H_ | |
OLD | NEW |