File: text_utils.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (147 lines) | stat: -rw-r--r-- 5,931 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_TEXT_UTILS_H_
#define UI_GFX_TEXT_UTILS_H_

#include <stddef.h>

#include <string>
#include <string_view>

#include "base/component_export.h"
#include "ui/gfx/text_constants.h"

namespace gfx {

class FontList;
class Insets;
class Size;

// Strips the accelerator char ('&') from a menu string. Useful for platforms
// which use underlining to indicate accelerators.
//
// Single accelerator chars ('&') will be stripped from the string. Double
// accelerator chars ('&&') will be converted to a single '&'. The out params
// |accelerated_char_pos| and |accelerated_char_span| will be set to the index
// and span of the last accelerated character, respectively, or -1 and 0 if
// there was none.
COMPONENT_EXPORT(GFX)
std::u16string LocateAndRemoveAcceleratorChar(std::u16string_view s,
                                              int* accelerated_char_pos,
                                              int* accelerated_char_span);

// Strips all accelerator notation from a menu string. Useful for platforms
// which use underlining to indicate accelerators, as well as situations where
// accelerators are not indicated.
//
// Single accelerator chars ('&') will be stripped from the string. Double
// accelerator chars ('&&') will be converted to a single '&'. CJK language
// accelerators, specified as "(&x)", will be entirely removed too.
COMPONENT_EXPORT(GFX) std::u16string RemoveAccelerator(std::u16string_view s);

// Returns the number of horizontal pixels needed to display the specified
// |text| with |font_list|. |typesetter| indicates where the text will be
// displayed.
COMPONENT_EXPORT(GFX)
int GetStringWidth(std::u16string_view text, const FontList& font_list);

// Returns the size required to render |text| in |font_list|. This includes all
// leading space, descender area, etc. even if the text to render does not
// contain characters with ascenders or descenders.
COMPONENT_EXPORT(GFX)
Size GetStringSize(std::u16string_view text, const FontList& font_list);

// This is same as GetStringWidth except that fractional width is returned.
COMPONENT_EXPORT(GFX)
float GetStringWidthF(std::u16string_view text, const FontList& font_list);

// Returns a valid cut boundary at or before |index|. The surrogate pair and
// combining characters should not be separated.
COMPONENT_EXPORT(GFX)
size_t FindValidBoundaryBefore(std::u16string_view text,
                               size_t index,
                               bool trim_whitespace = false);

// Returns a valid cut boundary at or after |index|. The surrogate pair and
// combining characters should not be separated.
COMPONENT_EXPORT(GFX)
size_t FindValidBoundaryAfter(std::u16string_view text,
                              size_t index,
                              bool trim_whitespace = false);

// If the UI layout is right-to-left, flip the alignment direction.
COMPONENT_EXPORT(GFX)
HorizontalAlignment MaybeFlipForRTL(HorizontalAlignment alignment);

// Returns insets that can be used to draw a highlight or border that appears to
// be distance |desired_visual_padding| from the body of a string of text
// rendered using |font_list|. The insets are adjusted based on the box used to
// render capital letters (or the bodies of most letters in non-capital fonts
// like Hebrew and Devanagari), in order to give the best visual appearance.
//
// That is, any portion of |desired_visual_padding| overlapping the font's
// leading space or descender area are truncated, to a minimum of zero.
//
// In this example, the text is rendered in a highlight that stretches above and
// below the height of the H as well as to the left and right of the text
// (|desired_visual_padding| = {2, 2, 2, 2}). Note that the descender of the 'y'
// overlaps with the padding, as it is outside the capital letter box.
//
// The resulting padding is {1, 2, 1, 2}.
//
//  . . . . . . . . . .                               | actual top
//  .                 .  |              | leading space
//  .  |  |  _        .  | font    | capital
//  .  |--| /_\ \  /  .  | height  | height
//  .  |  | \_   \/   .  |         |
//  .            /    .  |              | descender
//  . . . . . . . . . .                               | actual bottom
//  ___             ___
//  actual        actual
//  left           right
//
COMPONENT_EXPORT(GFX)
Insets AdjustVisualBorderForFont(const FontList& font_list,
                                 const Insets& desired_visual_padding);

// Returns the y adjustment necessary to align the center of the "cap size" box
// - the space between a capital letter's top and bottom - between two fonts.
// For non-capital scripts (e.g. Hebrew, Devanagari) the box containing the body
// of most letters is used.
//
// A positive return value means the font |to_center| needs to be moved down
// relative to the font |original_font|, while a negative value means it needs
// to be moved up.
//
// Illustration:
//
//  original_font    to_center
//  ----------                    ] - return value (+1)
//  leading          ----------
//  ----------       leading
//                   ----------
//
//  cap-height       cap-height
//
//                   ----------
//  ----------       descent
//  descent          ----------
//  ----------
//
// Visual result:           Non-Latin example (Devanagari ऐ "ai"):
//                               \
//  |\   |                    ------     \
//  | \  |   |\ |              |  |    ----
//  |  \ |   | \|               \ /     \|
//  |   \|                       \       /
//                                /
//
COMPONENT_EXPORT(GFX)
int GetFontCapHeightCenterOffset(const gfx::FontList& original_font,
                                 const gfx::FontList& to_center);

}  // namespace gfx

#endif  // UI_GFX_TEXT_UTILS_H_