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