mac: Add the flag "-gline-tables-only" to reduce dSYM size. (attempt #2)
[chromium-blink-merge.git] / ui / gfx / text_elider.h
blob1269caf070ed109f5336ea3d515be453452a6c60
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 // Returns a valid cut boundary at or before/after |index|.
55 size_t FindValidBoundaryBefore(size_t index) const;
56 size_t FindValidBoundaryAfter(size_t index) const;
58 // The text to be sliced.
59 const base::string16& text_;
61 // Ellipsis string to use.
62 const base::string16& ellipsis_;
64 // If true, the middle of the string will be elided.
65 bool elide_in_middle_;
67 // If true, the beginning of the string will be elided.
68 bool elide_at_beginning_;
70 DISALLOW_COPY_AND_ASSIGN(StringSlicer);
73 // Elides |text| to fit the |available_pixel_width| with the specified behavior.
74 GFX_EXPORT base::string16 ElideText(const base::string16& text,
75 const gfx::FontList& font_list,
76 float available_pixel_width,
77 ElideBehavior elide_behavior);
79 // Elide a filename to fit a given pixel width, with an emphasis on not hiding
80 // the extension unless we have to. If filename contains a path, the path will
81 // be removed if filename doesn't fit into available_pixel_width. The elided
82 // filename is forced to have LTR directionality, which means that in RTL UI
83 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and
84 // PDF (Pop Directional Formatting) mark.
85 GFX_EXPORT base::string16 ElideFilename(const base::FilePath& filename,
86 const gfx::FontList& font_list,
87 float available_pixel_width);
89 // Functions to elide strings when the font information is unknown. As opposed
90 // to the above functions, ElideString() and ElideRectangleString() operate in
91 // terms of character units, not pixels.
93 // If the size of |input| is more than |max_len|, this function returns
94 // true and |input| is shortened into |output| by removing chars in the
95 // middle (they are replaced with up to 3 dots, as size permits).
96 // Ex: ElideString(ASCIIToUTF16("Hello"), 10, &str) puts Hello in str and
97 // returns false. ElideString(ASCIIToUTF16("Hello my name is Tom"), 10, &str)
98 // puts "Hell...Tom" in str and returns true.
99 // TODO(tsepez): Doesn't handle UTF-16 surrogate pairs properly.
100 // TODO(tsepez): Doesn't handle bidi properly.
101 GFX_EXPORT bool ElideString(const base::string16& input, int max_len,
102 base::string16* output);
104 // Reformat |input| into |output| so that it fits into a |max_rows| by
105 // |max_cols| rectangle of characters. Input newlines are respected, but
106 // lines that are too long are broken into pieces. If |strict| is true,
107 // we break first at naturally occuring whitespace boundaries, otherwise
108 // we assume some other mechanism will do this in approximately the same
109 // spot after the fact. If the word itself is too long, we always break
110 // intra-word (respecting UTF-16 surrogate pairs) as necssary. Truncation
111 // (indicated by an added 3 dots) occurs if the result is still too long.
112 // Returns true if the input had to be truncated (and not just reformatted).
113 GFX_EXPORT bool ElideRectangleString(const base::string16& input,
114 size_t max_rows,
115 size_t max_cols,
116 bool strict,
117 base::string16* output);
119 // Indicates whether the |available_pixel_width| by |available_pixel_height|
120 // rectangle passed to |ElideRectangleText()| had insufficient space to
121 // accommodate the given |text|, leading to elision or truncation.
122 enum ReformattingResultFlags {
123 INSUFFICIENT_SPACE_HORIZONTAL = 1 << 0,
124 INSUFFICIENT_SPACE_VERTICAL = 1 << 1,
127 // Reformats |text| into output vector |lines| so that the resulting text fits
128 // into an |available_pixel_width| by |available_pixel_height| rectangle with
129 // the specified |font_list|. Input newlines are respected, but lines that are
130 // too long are broken into pieces. For words that are too wide to fit on a
131 // single line, the wrapping behavior can be specified with the |wrap_behavior|
132 // param. Returns a combination of |ReformattingResultFlags| that indicate
133 // whether the given rectangle had insufficient space to accommodate |text|,
134 // leading to elision or truncation (and not just reformatting).
135 GFX_EXPORT int ElideRectangleText(const base::string16& text,
136 const gfx::FontList& font_list,
137 float available_pixel_width,
138 int available_pixel_height,
139 WordWrapBehavior wrap_behavior,
140 std::vector<base::string16>* lines);
142 // Truncates |string| to |length| characters. This breaks the string according
143 // to the specified |break_type|, which must be either WORD_BREAK or
144 // CHARACTER_BREAK, and adds the horizontal ellipsis character (unicode
145 // character 0x2026) to render "...". The supplied string is returned if the
146 // string has |length| characters or less.
147 GFX_EXPORT base::string16 TruncateString(const base::string16& string,
148 size_t length,
149 BreakType break_type);
151 } // namespace gfx
153 #endif // UI_GFX_TEXT_ELIDER_H_