text_utils.h 5.8 KB

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