Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / text_elider.h
blob4e58a9f918161415b0c883388037a6f3406f5a2d
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
3 // found in the LICENSE file.
4 //
5 // This file defines utility functions for eliding and formatting UI text.
7 #ifndef UI_GFX_TEXT_ELIDER_H_
8 #define UI_GFX_TEXT_ELIDER_H_
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/strings/string16.h"
15 #include "ui/gfx/gfx_export.h"
16 #include "ui/gfx/text_constants.h"
18 class GURL;
20 namespace base {
21 class FilePath;
24 namespace gfx {
25 class FontList;
27 GFX_EXPORT extern const char kEllipsis[];
28 GFX_EXPORT extern const base::char16 kEllipsisUTF16[];
29 GFX_EXPORT extern const base::char16 kForwardSlash;
31 // Helper class to split + elide text, while respecting UTF-16 surrogate pairs
32 // and combining character sequences.
33 class GFX_EXPORT StringSlicer {
34 public:
35 // Warning: Retains a reference to |text| and |ellipsis|. They must have a
36 // longer lifetime than the StringSlicer.
37 StringSlicer(const base::string16& text,
38 const base::string16& ellipsis,
39 bool elide_in_middle,
40 bool elide_at_beginning);
42 // Cuts |text_| to be at most |length| UTF-16 code units long. If
43 // |elide_in_middle_| is true, the middle of the string is removed to leave
44 // equal-length pieces from the beginning and end of the string; otherwise,
45 // the end of the string is removed and only the beginning remains. If
46 // |insert_ellipsis| is true, then an ellipsis character will be inserted at
47 // the cut point (note that the ellipsis will does not count towards the
48 // |length| limit).
49 // Note: Characters may still be omitted even if |length| is the full string
50 // length, if surrogate pairs fall on the split boundary.
51 base::string16 CutString(size_t length, bool insert_ellipsis) const;
53 private:
54 // The text to be sliced.
55 const base::string16& text_;
57 // Ellipsis string to use.
58 const base::string16& ellipsis_;
60 // If true, the middle of the string will be elided.
61 bool elide_in_middle_;
63 // If true, the beginning of the string will be elided.
64 bool elide_at_beginning_;
66 DISALLOW_COPY_AND_ASSIGN(StringSlicer);
69 // Elides |text| to fit the |available_pixel_width| with the specified behavior.
70 GFX_EXPORT base::string16 ElideText(const base::string16& text,
71 const gfx::FontList& font_list,
72 float available_pixel_width,
73 ElideBehavior elide_behavior);
75 // Elide a filename to fit a given pixel width, with an emphasis on not hiding
76 // the extension unless we have to. If filename contains a path, the path will
77 // be removed if filename doesn't fit into available_pixel_width. The elided
78 // filename is forced to have LTR directionality, which means that in RTL UI
79 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and
80 // PDF (Pop Directional Formatting) mark.
81 GFX_EXPORT base::string16 ElideFilename(const base::FilePath& filename,
82 const gfx::FontList& font_list,
83 float available_pixel_width);
85 // Functions to elide strings when the font information is unknown. As opposed
86 // to the above functions, ElideString() and ElideRectangleString() operate in
87 // terms of character units, not pixels.
89 // If the size of |input| is more than |max_len|, this function returns
90 // true and |input| is shortened into |output| by removing chars in the
91 // middle (they are replaced with up to 3 dots, as size permits).
92 // Ex: ElideString(ASCIIToUTF16("Hello"), 10, &str) puts Hello in str and
93 // returns false. ElideString(ASCIIToUTF16("Hello my name is Tom"), 10, &str)
94 // puts "Hell...Tom" in str and returns true.
95 // TODO(tsepez): Doesn't handle UTF-16 surrogate pairs properly.
96 // TODO(tsepez): Doesn't handle bidi properly.
97 GFX_EXPORT bool ElideString(const base::string16& input, int max_len,
98 base::string16* output);
100 // Reformat |input| into |output| so that it fits into a |max_rows| by
101 // |max_cols| rectangle of characters. Input newlines are respected, but
102 // lines that are too long are broken into pieces. If |strict| is true,
103 // we break first at naturally occuring whitespace boundaries, otherwise
104 // we assume some other mechanism will do this in approximately the same
105 // spot after the fact. If the word itself is too long, we always break
106 // intra-word (respecting UTF-16 surrogate pairs) as necssary. Truncation
107 // (indicated by an added 3 dots) occurs if the result is still too long.
108 // Returns true if the input had to be truncated (and not just reformatted).
109 GFX_EXPORT bool ElideRectangleString(const base::string16& input,
110 size_t max_rows,
111 size_t max_cols,
112 bool strict,
113 base::string16* output);
115 // Indicates whether the |available_pixel_width| by |available_pixel_height|
116 // rectangle passed to |ElideRectangleText()| had insufficient space to
117 // accommodate the given |text|, leading to elision or truncation.
118 enum ReformattingResultFlags {
119 INSUFFICIENT_SPACE_HORIZONTAL = 1 << 0,
120 INSUFFICIENT_SPACE_VERTICAL = 1 << 1,
123 // Reformats |text| into output vector |lines| so that the resulting text fits
124 // into an |available_pixel_width| by |available_pixel_height| rectangle with
125 // the specified |font_list|. Input newlines are respected, but lines that are
126 // too long are broken into pieces. For words that are too wide to fit on a
127 // single line, the wrapping behavior can be specified with the |wrap_behavior|
128 // param. Returns a combination of |ReformattingResultFlags| that indicate
129 // whether the given rectangle had insufficient space to accommodate |text|,
130 // leading to elision or truncation (and not just reformatting).
131 GFX_EXPORT int ElideRectangleText(const base::string16& text,
132 const gfx::FontList& font_list,
133 float available_pixel_width,
134 int available_pixel_height,
135 WordWrapBehavior wrap_behavior,
136 std::vector<base::string16>* lines);
138 // Truncates |string| to |length| characters. This breaks the string according
139 // to the specified |break_type|, which must be either WORD_BREAK or
140 // CHARACTER_BREAK, and adds the horizontal ellipsis character (unicode
141 // character 0x2026) to render "...". The supplied string is returned if the
142 // string has |length| characters or less.
143 GFX_EXPORT base::string16 TruncateString(const base::string16& string,
144 size_t length,
145 BreakType break_type);
147 } // namespace gfx
149 #endif // UI_GFX_TEXT_ELIDER_H_