Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(860)

Unified Diff: ui/gfx/text_utils.cc

Issue 1070223004: Stop combining text runs which are connected by 'COMMON' blocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address msw@'s comment. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/text_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/text_utils.cc
diff --git a/ui/gfx/text_utils.cc b/ui/gfx/text_utils.cc
index a31ef3d3cc999cd844fb4042b17628b9db976bcf..c62b88b122ccefc2a3f6ee73cd822a408652650c 100644
--- a/ui/gfx/text_utils.cc
+++ b/ui/gfx/text_utils.cc
@@ -5,9 +5,33 @@
#include "ui/gfx/text_utils.h"
#include "base/i18n/char_iterator.h"
+#include "base/logging.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"
namespace gfx {
+namespace {
+
+// Returns true if the code point |c| is a combining mark character in Unicode.
+bool CharIsMark(UChar32 c) {
+ 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;
+}
+
+// Gets the code point of |str| at the given code unit position |index|. If
+// |index| is a surrogate code unit, returns the whole code point (unless the
+// code unit is unpaired, in which case it just returns the surrogate value).
+UChar32 GetCodePointAt(const base::string16& str, size_t index) {
+ UChar32 c;
+ U16_GET(str.data(), 0, index, str.size(), c);
+ return c;
+}
+
+} // namespace
+
base::string16 RemoveAcceleratorChar(const base::string16& s,
base::char16 accelerator_char,
int* accelerated_char_pos,
@@ -46,4 +70,40 @@ base::string16 RemoveAcceleratorChar(const base::string16& s,
return accelerator_removed;
}
+size_t FindValidBoundaryBefore(const base::string16& text, size_t index) {
+ size_t length = text.length();
+ DCHECK_LE(index, length);
+ if (index == length)
+ return index;
+
+ // If |index| straddles a combining character sequence, go back until we find
+ // a base character.
+ while (index > 0 && CharIsMark(GetCodePointAt(text, index)))
+ --index;
+
+ // If |index| straddles a UTF-16 surrogate pair, go back.
+ U16_SET_CP_START(text.data(), 0, index);
+ return index;
+}
+
+size_t FindValidBoundaryAfter(const base::string16& text, size_t index) {
+ DCHECK_LE(index, text.length());
+ if (index == text.length())
+ return index;
+
+ int32_t text_index = base::checked_cast<int32_t>(index);
+ int32_t text_length = base::checked_cast<int32_t>(text.length());
+
+ // If |index| straddles a combining character sequence, go forward until we
+ // find a base character.
+ while (text_index < text_length &&
+ CharIsMark(GetCodePointAt(text, text_index))) {
+ ++text_index;
+ }
+
+ // If |index| straddles a UTF-16 surrogate pair, go forward.
+ U16_SET_CP_LIMIT(text.data(), 0, text_index, text_length);
+ return static_cast<size_t>(text_index);
+}
+
} // namespace gfx
« no previous file with comments | « ui/gfx/text_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698