text_utils.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "ui/gfx/text_utils.h"
  5. #include <stdint.h>
  6. #include "base/i18n/char_iterator.h"
  7. #include "base/i18n/rtl.h"
  8. #include "base/numerics/safe_conversions.h"
  9. #include "third_party/icu/source/common/unicode/uchar.h"
  10. #include "third_party/icu/source/common/unicode/utf16.h"
  11. #include "ui/gfx/font_list.h"
  12. #include "ui/gfx/geometry/insets.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/geometry/size.h"
  15. namespace gfx {
  16. using base::i18n::UTF16CharIterator;
  17. namespace {
  18. constexpr char16_t kAcceleratorChar = '&';
  19. constexpr char16_t kOpenParenthesisChar = '(';
  20. constexpr char16_t kCloseParenthesisChar = ')';
  21. // Returns true if the specified character must be elided from a string.
  22. // Examples are combining marks and whitespace.
  23. bool IsCombiningMark(UChar32 c) {
  24. const int8_t char_type = u_charType(c);
  25. return char_type == U_NON_SPACING_MARK || char_type == U_ENCLOSING_MARK ||
  26. char_type == U_COMBINING_SPACING_MARK;
  27. }
  28. bool IsSpace(UChar32 c) {
  29. // Ignore NUL character.
  30. if (!c)
  31. return false;
  32. const int8_t char_type = u_charType(c);
  33. return char_type == U_SPACE_SEPARATOR || char_type == U_LINE_SEPARATOR ||
  34. char_type == U_PARAGRAPH_SEPARATOR || char_type == U_CONTROL_CHAR;
  35. }
  36. std::u16string RemoveAcceleratorChar(bool full_removal,
  37. const std::u16string& s,
  38. int* accelerated_char_pos,
  39. int* accelerated_char_span) {
  40. bool escaped = false;
  41. ptrdiff_t last_char_pos = -1;
  42. int last_char_span = 0;
  43. UTF16CharIterator chars(s);
  44. std::u16string accelerator_removed;
  45. // The states of a state machine looking for a CJK-style accelerator (i.e.
  46. // "(&x)"). |cjk_state| proceeds up from |kFoundNothing| through these states,
  47. // resetting either when it sees a complete accelerator, or gives up because
  48. // the current character doesn't match.
  49. enum {
  50. kFoundNothing,
  51. kFoundOpenParen,
  52. kFoundAcceleratorChar,
  53. kFoundAccelerator
  54. } cjk_state = kFoundNothing;
  55. size_t pre_cjk_size = 0;
  56. accelerator_removed.reserve(s.size());
  57. while (!chars.end()) {
  58. int32_t c = chars.get();
  59. int array_pos = chars.array_pos();
  60. chars.Advance();
  61. if (full_removal) {
  62. if (cjk_state == kFoundNothing && c == kOpenParenthesisChar) {
  63. pre_cjk_size = array_pos;
  64. cjk_state = kFoundOpenParen;
  65. } else if (cjk_state == kFoundOpenParen && c == kAcceleratorChar) {
  66. cjk_state = kFoundAcceleratorChar;
  67. } else if (cjk_state == kFoundAcceleratorChar) {
  68. // Accept any character as the accelerator.
  69. cjk_state = kFoundAccelerator;
  70. } else if (cjk_state == kFoundAccelerator && c == kCloseParenthesisChar) {
  71. cjk_state = kFoundNothing;
  72. accelerator_removed.resize(pre_cjk_size);
  73. pre_cjk_size = 0;
  74. escaped = false;
  75. continue;
  76. } else {
  77. cjk_state = kFoundNothing;
  78. }
  79. }
  80. if (c != kAcceleratorChar || escaped) {
  81. int span = chars.array_pos() - array_pos;
  82. if (escaped && c != kAcceleratorChar) {
  83. last_char_pos = accelerator_removed.size();
  84. last_char_span = span;
  85. }
  86. for (int i = 0; i < span; i++)
  87. accelerator_removed.push_back(s[array_pos + i]);
  88. escaped = false;
  89. } else {
  90. escaped = true;
  91. }
  92. }
  93. if (accelerated_char_pos && !full_removal)
  94. *accelerated_char_pos = last_char_pos;
  95. if (accelerated_char_span && !full_removal)
  96. *accelerated_char_span = last_char_span;
  97. return accelerator_removed;
  98. }
  99. } // namespace
  100. std::u16string LocateAndRemoveAcceleratorChar(const std::u16string& s,
  101. int* accelerated_char_pos,
  102. int* accelerated_char_span) {
  103. return RemoveAcceleratorChar(false, s, accelerated_char_pos,
  104. accelerated_char_span);
  105. }
  106. std::u16string RemoveAccelerator(const std::u16string& s) {
  107. return RemoveAcceleratorChar(true, s, nullptr, nullptr);
  108. }
  109. size_t FindValidBoundaryBefore(const std::u16string& text,
  110. size_t index,
  111. bool trim_whitespace) {
  112. UTF16CharIterator it = UTF16CharIterator::LowerBound(text, index);
  113. // First, move left until we're positioned on a code point that is not a
  114. // combining mark.
  115. while (!it.start() && IsCombiningMark(it.get()))
  116. it.Rewind();
  117. // Next, maybe trim whitespace to the left of the current position.
  118. if (trim_whitespace) {
  119. while (!it.start() && IsSpace(it.PreviousCodePoint()))
  120. it.Rewind();
  121. }
  122. return it.array_pos();
  123. }
  124. size_t FindValidBoundaryAfter(const std::u16string& text,
  125. size_t index,
  126. bool trim_whitespace) {
  127. UTF16CharIterator it = UTF16CharIterator::UpperBound(text, index);
  128. // First, move right until we're positioned on a code point that is not a
  129. // combining mark.
  130. while (!it.end() && IsCombiningMark(it.get()))
  131. it.Advance();
  132. // Next, maybe trim space at the current position.
  133. if (trim_whitespace) {
  134. // A mark combining with a space is renderable, so we'll prevent
  135. // trimming spaces with combining marks.
  136. while (!it.end() && IsSpace(it.get()) &&
  137. !IsCombiningMark(it.NextCodePoint())) {
  138. it.Advance();
  139. }
  140. }
  141. return it.array_pos();
  142. }
  143. HorizontalAlignment MaybeFlipForRTL(HorizontalAlignment alignment) {
  144. if (base::i18n::IsRTL() &&
  145. (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) {
  146. alignment =
  147. (alignment == gfx::ALIGN_LEFT) ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
  148. }
  149. return alignment;
  150. }
  151. Size GetStringSize(const std::u16string& text, const FontList& font_list) {
  152. return Size(GetStringWidth(text, font_list), font_list.GetHeight());
  153. }
  154. Insets AdjustVisualBorderForFont(const FontList& font_list,
  155. const Insets& desired_visual_padding) {
  156. Insets result = desired_visual_padding;
  157. const int baseline = font_list.GetBaseline();
  158. const int leading_space = baseline - font_list.GetCapHeight();
  159. const int descender = font_list.GetHeight() - baseline;
  160. result.set_top(std::max(0, result.top() - leading_space));
  161. result.set_bottom(std::max(0, result.bottom() - descender));
  162. return result;
  163. }
  164. int GetFontCapHeightCenterOffset(const gfx::FontList& original_font,
  165. const gfx::FontList& to_center) {
  166. const int original_cap_height = original_font.GetCapHeight();
  167. const int original_cap_leading =
  168. original_font.GetBaseline() - original_cap_height;
  169. const int to_center_cap_height = to_center.GetCapHeight();
  170. const int to_center_leading = to_center.GetBaseline() - to_center_cap_height;
  171. const int cap_height_diff = original_cap_height - to_center_cap_height;
  172. const int new_cap_top =
  173. original_cap_leading + base::ClampRound(cap_height_diff / 2.0f);
  174. const int new_top = new_cap_top - to_center_leading;
  175. // Since we assume the old font starts at zero, the new top is the adjustment.
  176. return new_top;
  177. }
  178. } // namespace gfx