• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkScalar.h"
12 
13 /** \class SkFontMetrics
14     The metrics of an SkFont.
15     The metric values are consistent with the Skia y-down coordinate system.
16  */
17 struct SK_API SkFontMetrics {
18     bool operator==(const SkFontMetrics& that) {
19         return
20         this->fFlags == that.fFlags &&
21         this->fTop == that.fTop &&
22         this->fAscent == that.fAscent &&
23         this->fDescent == that.fDescent &&
24         this->fBottom == that.fBottom &&
25         this->fLeading == that.fLeading &&
26         this->fAvgCharWidth == that.fAvgCharWidth &&
27         this->fMaxCharWidth == that.fMaxCharWidth &&
28         this->fXMin == that.fXMin &&
29         this->fXMax == that.fXMax &&
30         this->fXHeight == that.fXHeight &&
31         this->fCapHeight == that.fCapHeight &&
32         this->fUnderlineThickness == that.fUnderlineThickness &&
33         this->fUnderlinePosition == that.fUnderlinePosition &&
34         this->fStrikeoutThickness == that.fStrikeoutThickness &&
35         this->fStrikeoutPosition == that.fStrikeoutPosition;
36     }
37 
38     /** \enum FontMetricsFlags
39      FontMetricsFlags indicate when certain metrics are valid;
40      the underline or strikeout metrics may be valid and zero.
41      Fonts with embedded bitmaps may not have valid underline or strikeout metrics.
42      */
43     enum FontMetricsFlags {
44         kUnderlineThicknessIsValid_Flag = 1 << 0, //!< set if fUnderlineThickness is valid
45         kUnderlinePositionIsValid_Flag  = 1 << 1, //!< set if fUnderlinePosition is valid
46         kStrikeoutThicknessIsValid_Flag = 1 << 2, //!< set if fStrikeoutThickness is valid
47         kStrikeoutPositionIsValid_Flag  = 1 << 3, //!< set if fStrikeoutPosition is valid
48         kBoundsInvalid_Flag             = 1 << 4, //!< set if fTop, fBottom, fXMin, fXMax invalid
49     };
50 
51     uint32_t fFlags;              //!< FontMetricsFlags indicating which metrics are valid
52     SkScalar fTop;                //!< greatest extent above origin of any glyph bounding box, typically negative; deprecated with variable fonts
53     SkScalar fAscent;             //!< distance to reserve above baseline, typically negative
54     SkScalar fDescent;            //!< distance to reserve below baseline, typically positive
55     SkScalar fBottom;             //!< greatest extent below origin of any glyph bounding box, typically positive; deprecated with variable fonts
56     SkScalar fLeading;            //!< distance to add between lines, typically positive or zero
57     SkScalar fAvgCharWidth;       //!< average character width, zero if unknown
58     SkScalar fMaxCharWidth;       //!< maximum character width, zero if unknown
59     SkScalar fXMin;               //!< greatest extent to left of origin of any glyph bounding box, typically negative; deprecated with variable fonts
60     SkScalar fXMax;               //!< greatest extent to right of origin of any glyph bounding box, typically positive; deprecated with variable fonts
61     SkScalar fXHeight;            //!< height of lower-case 'x', zero if unknown, typically negative
62     SkScalar fCapHeight;          //!< height of an upper-case letter, zero if unknown, typically negative
63     SkScalar fUnderlineThickness; //!< underline thickness
64     SkScalar fUnderlinePosition;  //!< distance from baseline to top of stroke, typically positive
65     SkScalar fStrikeoutThickness; //!< strikeout thickness
66     SkScalar fStrikeoutPosition;  //!< distance from baseline to bottom of stroke, typically negative
67 
68     /** Returns true if SkFontMetrics has a valid underline thickness, and sets
69      thickness to that value. If the underline thickness is not valid,
70      return false, and ignore thickness.
71 
72      @param thickness  storage for underline width
73      @return           true if font specifies underline width
74      */
hasUnderlineThicknessSkFontMetrics75     bool hasUnderlineThickness(SkScalar* thickness) const {
76         if (SkToBool(fFlags & kUnderlineThicknessIsValid_Flag)) {
77             *thickness = fUnderlineThickness;
78             return true;
79         }
80         return false;
81     }
82 
83     /** Returns true if SkFontMetrics has a valid underline position, and sets
84      position to that value. If the underline position is not valid,
85      return false, and ignore position.
86 
87      @param position  storage for underline position
88      @return          true if font specifies underline position
89      */
hasUnderlinePositionSkFontMetrics90     bool hasUnderlinePosition(SkScalar* position) const {
91         if (SkToBool(fFlags & kUnderlinePositionIsValid_Flag)) {
92             *position = fUnderlinePosition;
93             return true;
94         }
95         return false;
96     }
97 
98     /** Returns true if SkFontMetrics has a valid strikeout thickness, and sets
99      thickness to that value. If the underline thickness is not valid,
100      return false, and ignore thickness.
101 
102      @param thickness  storage for strikeout width
103      @return           true if font specifies strikeout width
104      */
hasStrikeoutThicknessSkFontMetrics105     bool hasStrikeoutThickness(SkScalar* thickness) const {
106         if (SkToBool(fFlags & kStrikeoutThicknessIsValid_Flag)) {
107             *thickness = fStrikeoutThickness;
108             return true;
109         }
110         return false;
111     }
112 
113     /** Returns true if SkFontMetrics has a valid strikeout position, and sets
114      position to that value. If the underline position is not valid,
115      return false, and ignore position.
116 
117      @param position  storage for strikeout position
118      @return          true if font specifies strikeout position
119      */
hasStrikeoutPositionSkFontMetrics120     bool hasStrikeoutPosition(SkScalar* position) const {
121         if (SkToBool(fFlags & kStrikeoutPositionIsValid_Flag)) {
122             *position = fStrikeoutPosition;
123             return true;
124         }
125         return false;
126     }
127 
128     /** Returns true if SkFontMetrics has a valid fTop, fBottom, fXMin, and fXMax.
129      If the bounds are not valid, return false.
130 
131      @return        true if font specifies maximum glyph bounds
132      */
hasBoundsSkFontMetrics133     bool hasBounds() const {
134         return !SkToBool(fFlags & kBoundsInvalid_Flag);
135     }
136 };
137 
138 #endif
139