1 // Copyright 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 #ifndef UI_GFX_TEXT_UTILS_H_
6 #define UI_GFX_TEXT_UTILS_H_
7 
8 #include <stddef.h>
9 
10 #include "base/strings/string16.h"
11 #include "ui/gfx/gfx_export.h"
12 #include "ui/gfx/text_constants.h"
13 
14 namespace gfx {
15 
16 class FontList;
17 class Insets;
18 class Size;
19 
20 // Strip the accelerator char (typically '&') from a menu string.  A double
21 // accelerator char ('&&') will be converted to a single char.  The out params
22 // |accelerated_char_pos| and |accelerated_char_span| will be set to the index
23 // and span of the last accelerated character, respectively, or -1 and 0 if
24 // there was none.
25 GFX_EXPORT base::string16 RemoveAcceleratorChar(const base::string16& s,
26                                                 base::char16 accelerator_char,
27                                                 int* accelerated_char_pos,
28                                                 int* accelerated_char_span);
29 
30 // Returns the number of horizontal pixels needed to display the specified
31 // |text| with |font_list|. |typesetter| indicates where the text will be
32 // displayed.
33 GFX_EXPORT int GetStringWidth(const base::string16& text,
34                               const FontList& font_list);
35 
36 // Returns the size required to render |text| in |font_list|. This includes all
37 // leading space, descender area, etc. even if the text to render does not
38 // contain characters with ascenders or descenders.
39 GFX_EXPORT Size GetStringSize(const base::string16& text,
40                               const FontList& font_list);
41 
42 // This is same as GetStringWidth except that fractional width is returned.
43 GFX_EXPORT float GetStringWidthF(const base::string16& text,
44                                  const FontList& font_list);
45 
46 // Returns a valid cut boundary at or before |index|. The surrogate pair and
47 // combining characters should not be separated.
48 GFX_EXPORT size_t FindValidBoundaryBefore(const base::string16& text,
49                                           size_t index,
50                                           bool trim_whitespace = false);
51 
52 // Returns a valid cut boundary at or after |index|. The surrogate pair and
53 // combining characters should not be separated.
54 GFX_EXPORT size_t FindValidBoundaryAfter(const base::string16& text,
55                                          size_t index,
56                                          bool trim_whitespace = false);
57 
58 // If the UI layout is right-to-left, flip the alignment direction.
59 GFX_EXPORT HorizontalAlignment MaybeFlipForRTL(HorizontalAlignment alignment);
60 
61 // Returns insets that can be used to draw a highlight or border that appears to
62 // be distance |desired_visual_padding| from the body of a string of text
63 // rendered using |font_list|. The insets are adjusted based on the box used to
64 // render capital letters (or the bodies of most letters in non-capital fonts
65 // like Hebrew and Devanagari), in order to give the best visual appearance.
66 //
67 // That is, any portion of |desired_visual_padding| overlapping the font's
68 // leading space or descender area are truncated, to a minimum of zero.
69 //
70 // In this example, the text is rendered in a highlight that stretches above and
71 // below the height of the H as well as to the left and right of the text
72 // (|desired_visual_padding| = {2, 2, 2, 2}). Note that the descender of the 'y'
73 // overlaps with the padding, as it is outside the capital letter box.
74 //
75 // The resulting padding is {1, 2, 1, 2}.
76 //
77 //  . . . . . . . . . .                               | actual top
78 //  .                 .  |              | leading space
79 //  .  |  |  _        .  | font    | capital
80 //  .  |--| /_\ \  /  .  | height  | height
81 //  .  |  | \_   \/   .  |         |
82 //  .            /    .  |              | descender
83 //  . . . . . . . . . .                               | actual bottom
84 //  ___             ___
85 //  actual        actual
86 //  left           right
87 //
88 GFX_EXPORT Insets
89 AdjustVisualBorderForFont(const FontList& font_list,
90                           const Insets& desired_visual_padding);
91 
92 // Returns the y adjustment necessary to align the center of the "cap size" box
93 // - the space between a capital letter's top and bottom - between two fonts.
94 // For non-capital scripts (e.g. Hebrew, Devanagari) the box containing the body
95 // of most letters is used.
96 //
97 // A positive return value means the font |to_center| needs to be moved down
98 // relative to the font |original_font|, while a negative value means it needs
99 // to be moved up.
100 //
101 // Illustration:
102 //
103 //  original_font    to_center
104 //  ----------                    ] - return value (+1)
105 //  leading          ----------
106 //  ----------       leading
107 //                   ----------
108 //
109 //  cap-height       cap-height
110 //
111 //                   ----------
112 //  ----------       descent
113 //  descent          ----------
114 //  ----------
115 //
116 // Visual result:           Non-Latin example (Devanagari ऐ "ai"):
117 //                               \
118 //  |\   |                    ------     \
119 //  | \  |   |\ |              |  |    ----
120 //  |  \ |   | \|               \ /     \|
121 //  |   \|                       \       /
122 //                                /
123 //
124 GFX_EXPORT int GetFontCapHeightCenterOffset(const gfx::FontList& original_font,
125                                             const gfx::FontList& to_center);
126 
127 }  // namespace gfx
128 
129 #endif  // UI_GFX_TEXT_UTILS_H_
130