SkFontMetrics.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkFontMetrics_DEFINED
  8. #define SkFontMetrics_DEFINED
  9. #include "include/core/SkScalar.h"
  10. struct SK_API SkFontMetrics {
  11. /** \enum FontMetricsFlags
  12. FontMetricsFlags are set in fFlags when underline and strikeout metrics are valid;
  13. the underline or strikeout metric may be valid and zero.
  14. Fonts with embedded bitmaps may not have valid underline or strikeout metrics.
  15. */
  16. enum FontMetricsFlags {
  17. kUnderlineThicknessIsValid_Flag = 1 << 0, //!< set if fUnderlineThickness is valid
  18. kUnderlinePositionIsValid_Flag = 1 << 1, //!< set if fUnderlinePosition is valid
  19. kStrikeoutThicknessIsValid_Flag = 1 << 2, //!< set if fStrikeoutThickness is valid
  20. kStrikeoutPositionIsValid_Flag = 1 << 3, //!< set if fStrikeoutPosition is valid
  21. };
  22. uint32_t fFlags; //!< is set to FontMetricsFlags when metrics are valid
  23. SkScalar fTop; //!< extent above baseline
  24. SkScalar fAscent; //!< distance to reserve above baseline
  25. SkScalar fDescent; //!< distance to reserve below baseline
  26. SkScalar fBottom; //!< extent below baseline
  27. SkScalar fLeading; //!< distance to add between lines
  28. SkScalar fAvgCharWidth; //!< average character width
  29. SkScalar fMaxCharWidth; //!< maximum character width
  30. SkScalar fXMin; //!< minimum x
  31. SkScalar fXMax; //!< maximum x
  32. SkScalar fXHeight; //!< height of lower-case 'x'
  33. SkScalar fCapHeight; //!< height of an upper-case letter
  34. SkScalar fUnderlineThickness; //!< underline thickness
  35. SkScalar fUnderlinePosition; //!< underline position relative to baseline
  36. SkScalar fStrikeoutThickness; //!< strikeout thickness
  37. SkScalar fStrikeoutPosition; //!< strikeout position relative to baseline
  38. /** Returns true if SkFontMetrics has a valid underline thickness, and sets
  39. thickness to that value. If the underline thickness is not valid,
  40. return false, and ignore thickness.
  41. @param thickness storage for underline width
  42. @return true if font specifies underline width
  43. */
  44. bool hasUnderlineThickness(SkScalar* thickness) const {
  45. if (SkToBool(fFlags & kUnderlineThicknessIsValid_Flag)) {
  46. *thickness = fUnderlineThickness;
  47. return true;
  48. }
  49. return false;
  50. }
  51. /** Returns true if SkFontMetrics has a valid underline position, and sets
  52. position to that value. If the underline position is not valid,
  53. return false, and ignore position.
  54. @param position storage for underline position
  55. @return true if font specifies underline position
  56. */
  57. bool hasUnderlinePosition(SkScalar* position) const {
  58. if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
  59. *position = fUnderlinePosition;
  60. return true;
  61. }
  62. return false;
  63. }
  64. /** Returns true if SkFontMetrics has a valid strikeout thickness, and sets
  65. thickness to that value. If the underline thickness is not valid,
  66. return false, and ignore thickness.
  67. @param thickness storage for strikeout width
  68. @return true if font specifies strikeout width
  69. */
  70. bool hasStrikeoutThickness(SkScalar* thickness) const {
  71. if (SkToBool(fFlags & kStrikeoutThicknessIsValid_Flag)) {
  72. *thickness = fStrikeoutThickness;
  73. return true;
  74. }
  75. return false;
  76. }
  77. /** Returns true if SkFontMetrics has a valid strikeout position, and sets
  78. position to that value. If the underline position is not valid,
  79. return false, and ignore position.
  80. @param position storage for strikeout position
  81. @return true if font specifies strikeout position
  82. */
  83. bool hasStrikeoutPosition(SkScalar* position) const {
  84. if (SkToBool(fFlags & kStrikeoutPositionIsValid_Flag)) {
  85. *position = fStrikeoutPosition;
  86. return true;
  87. }
  88. return false;
  89. }
  90. };
  91. #endif