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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// 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.
#include "ui/gfx/text_utils.h"
#include <stdint.h>
#include <string>
#include <string_view>
#include "base/i18n/char_iterator.h"
#include "base/i18n/rtl.h"
#include "base/numerics/safe_conversions.h"
#include "third_party/icu/source/common/unicode/uchar.h"
#include "third_party/icu/source/common/unicode/utf16.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace gfx {
using base::i18n::UTF16CharIterator;
namespace {
constexpr char16_t kAcceleratorChar = '&';
constexpr char16_t kOpenParenthesisChar = '(';
constexpr char16_t kCloseParenthesisChar = ')';
// Returns true if the specified character must be elided from a string.
// Examples are combining marks and whitespace.
bool IsCombiningMark(UChar32 c) {
const int8_t char_type = u_charType(c);
return char_type == U_NON_SPACING_MARK || char_type == U_ENCLOSING_MARK ||
char_type == U_COMBINING_SPACING_MARK;
}
bool IsSpace(UChar32 c) {
// Ignore NUL character.
if (!c)
return false;
const int8_t char_type = u_charType(c);
return char_type == U_SPACE_SEPARATOR || char_type == U_LINE_SEPARATOR ||
char_type == U_PARAGRAPH_SEPARATOR || char_type == U_CONTROL_CHAR;
}
std::u16string RemoveAcceleratorChar(bool full_removal,
std::u16string_view s,
int* accelerated_char_pos,
int* accelerated_char_span) {
bool escaped = false;
ptrdiff_t last_char_pos = -1;
int last_char_span = 0;
UTF16CharIterator chars(s);
std::u16string accelerator_removed;
// The states of a state machine looking for a CJK-style accelerator (i.e.
// "(&x)"). |cjk_state| proceeds up from |kFoundNothing| through these states,
// resetting either when it sees a complete accelerator, or gives up because
// the current character doesn't match.
enum {
kFoundNothing,
kFoundOpenParen,
kFoundAcceleratorChar,
kFoundAccelerator
} cjk_state = kFoundNothing;
size_t pre_cjk_size = 0;
accelerator_removed.reserve(s.size());
while (!chars.end()) {
int32_t c = chars.get();
int array_pos = chars.array_pos();
chars.Advance();
if (full_removal) {
if (cjk_state == kFoundNothing && c == kOpenParenthesisChar) {
pre_cjk_size = array_pos;
cjk_state = kFoundOpenParen;
} else if (cjk_state == kFoundOpenParen && c == kAcceleratorChar) {
cjk_state = kFoundAcceleratorChar;
} else if (cjk_state == kFoundAcceleratorChar) {
// Accept any character as the accelerator.
cjk_state = kFoundAccelerator;
} else if (cjk_state == kFoundAccelerator && c == kCloseParenthesisChar) {
cjk_state = kFoundNothing;
accelerator_removed.resize(pre_cjk_size);
pre_cjk_size = 0;
escaped = false;
continue;
} else {
cjk_state = kFoundNothing;
}
}
if (c != kAcceleratorChar || escaped) {
int span = chars.array_pos() - array_pos;
if (escaped && c != kAcceleratorChar) {
last_char_pos = accelerator_removed.size();
last_char_span = span;
}
for (int i = 0; i < span; i++)
accelerator_removed.push_back(s[array_pos + i]);
escaped = false;
} else {
escaped = true;
}
}
if (accelerated_char_pos && !full_removal)
*accelerated_char_pos = last_char_pos;
if (accelerated_char_span && !full_removal)
*accelerated_char_span = last_char_span;
return accelerator_removed;
}
} // namespace
std::u16string LocateAndRemoveAcceleratorChar(std::u16string_view s,
int* accelerated_char_pos,
int* accelerated_char_span) {
return RemoveAcceleratorChar(false, s, accelerated_char_pos,
accelerated_char_span);
}
std::u16string RemoveAccelerator(std::u16string_view s) {
return RemoveAcceleratorChar(true, s, nullptr, nullptr);
}
size_t FindValidBoundaryBefore(std::u16string_view text,
size_t index,
bool trim_whitespace) {
UTF16CharIterator it = UTF16CharIterator::LowerBound(text, index);
// First, move left until we're positioned on a code point that is not a
// combining mark.
while (!it.start() && IsCombiningMark(it.get()))
it.Rewind();
// Next, maybe trim whitespace to the left of the current position.
if (trim_whitespace) {
while (!it.start() && IsSpace(it.PreviousCodePoint()))
it.Rewind();
}
return it.array_pos();
}
size_t FindValidBoundaryAfter(std::u16string_view text,
size_t index,
bool trim_whitespace) {
UTF16CharIterator it = UTF16CharIterator::UpperBound(text, index);
// First, move right until we're positioned on a code point that is not a
// combining mark.
while (!it.end() && IsCombiningMark(it.get()))
it.Advance();
// Next, maybe trim space at the current position.
if (trim_whitespace) {
// A mark combining with a space is renderable, so we'll prevent
// trimming spaces with combining marks.
while (!it.end() && IsSpace(it.get()) &&
!IsCombiningMark(it.NextCodePoint())) {
it.Advance();
}
}
return it.array_pos();
}
HorizontalAlignment MaybeFlipForRTL(HorizontalAlignment alignment) {
if (base::i18n::IsRTL() &&
(alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) {
alignment =
(alignment == gfx::ALIGN_LEFT) ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
}
return alignment;
}
Size GetStringSize(std::u16string_view text, const FontList& font_list) {
return Size(GetStringWidth(text, font_list), font_list.GetHeight());
}
Insets AdjustVisualBorderForFont(const FontList& font_list,
const Insets& desired_visual_padding) {
Insets result = desired_visual_padding;
const int baseline = font_list.GetBaseline();
const int leading_space = baseline - font_list.GetCapHeight();
const int descender = font_list.GetHeight() - baseline;
result.set_top(std::max(0, result.top() - leading_space));
result.set_bottom(std::max(0, result.bottom() - descender));
return result;
}
int GetFontCapHeightCenterOffset(const gfx::FontList& original_font,
const gfx::FontList& to_center) {
const int original_cap_height = original_font.GetCapHeight();
const int original_cap_leading =
original_font.GetBaseline() - original_cap_height;
const int to_center_cap_height = to_center.GetCapHeight();
const int to_center_leading = to_center.GetBaseline() - to_center_cap_height;
const int cap_height_diff = original_cap_height - to_center_cap_height;
const int new_cap_top =
original_cap_leading + base::ClampRound(cap_height_diff / 2.0f);
const int new_top = new_cap_top - to_center_leading;
// Since we assume the old font starts at zero, the new top is the adjustment.
return new_top;
}
} // namespace gfx
|