1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsFont_h___
8 #define nsFont_h___
9 
10 #include <stdint.h>     // for uint8_t, uint16_t
11 #include <sys/types.h>  // for int16_t
12 #include "gfxFontFamilyList.h"
13 #include "gfxFontConstants.h"  // for NS_FONT_KERNING_AUTO, etc
14 #include "gfxFontFeatures.h"
15 #include "gfxFontVariations.h"
16 #include "mozilla/RefPtr.h"  // for RefPtr
17 #include "nsColor.h"         // for nsColor and NS_RGBA
18 #include "nsCoord.h"         // for nscoord
19 #include "nsStringFwd.h"     // for nsAString
20 #include "nsString.h"        // for nsString
21 #include "nsTArray.h"        // for nsTArray
22 
23 struct gfxFontStyle;
24 
25 // XXX we need a method to enumerate all of the possible fonts on the
26 // system across family, weight, style, size, etc. But not here!
27 
28 // Enumerator callback function. Return false to stop
29 typedef bool (*nsFontFamilyEnumFunc)(const nsString& aFamily, bool aGeneric,
30                                      void* aData);
31 
32 // IDs for generic fonts
33 // NOTE: 0, 1 are reserved for the special IDs of the default variable
34 // and fixed fonts in the presentation context, see nsPresContext.h
35 const uint8_t kGenericFont_NONE = 0x00;
36 // Special
37 const uint8_t kGenericFont_moz_variable =
38     0x00;  // for the default variable width font
39 const uint8_t kGenericFont_moz_fixed =
40     0x01;  // our special "use the user's fixed font"
41 // CSS
42 const uint8_t kGenericFont_serif = 0x02;
43 const uint8_t kGenericFont_sans_serif = 0x04;
44 const uint8_t kGenericFont_monospace = 0x08;
45 const uint8_t kGenericFont_cursive = 0x10;
46 const uint8_t kGenericFont_fantasy = 0x20;
47 
48 // Font structure.
49 struct nsFont {
50   // list of font families, either named or generic
51   mozilla::FontFamilyList fontlist;
52 
53   // The style of font (normal, italic, oblique; see gfxFontConstants.h)
54   uint8_t style = NS_FONT_STYLE_NORMAL;
55 
56   // Force this font to not be considered a 'generic' font, even if
57   // the name is the same as a CSS generic font family.
58   bool systemFont = false;
59 
60   // Variant subproperties
61   uint8_t variantCaps = NS_FONT_VARIANT_CAPS_NORMAL;
62   uint8_t variantNumeric = NS_FONT_VARIANT_NUMERIC_NORMAL;
63   uint8_t variantPosition = NS_FONT_VARIANT_POSITION_NORMAL;
64   uint8_t variantWidth = NS_FONT_VARIANT_WIDTH_NORMAL;
65 
66   uint16_t variantLigatures = NS_FONT_VARIANT_LIGATURES_NORMAL;
67   uint16_t variantEastAsian = NS_FONT_VARIANT_EAST_ASIAN_NORMAL;
68 
69   // Some font-variant-alternates property values require
70   // font-specific settings defined via @font-feature-values rules.
71   // These are resolved *after* font matching occurs.
72 
73   // -- bitmask for both enumerated and functional propvals
74   uint16_t variantAlternates = NS_FONT_VARIANT_ALTERNATES_NORMAL;
75 
76   // Smoothing - controls subpixel-antialiasing (currently OSX only)
77   uint8_t smoothing = NS_FONT_SMOOTHING_AUTO;
78 
79   // The estimated background color behind the text. Enables a special
80   // rendering mode when NS_GET_A(.) > 0. Only used for text in the chrome.
81   nscolor fontSmoothingBackgroundColor = NS_RGBA(0, 0, 0, 0);
82 
83   // The weight of the font; see gfxFontConstants.h.
84   uint16_t weight = NS_FONT_WEIGHT_NORMAL;
85 
86   // The stretch of the font (the sum of various NS_FONT_STRETCH_*
87   // constants; see gfxFontConstants.h).
88   int16_t stretch = NS_FONT_STRETCH_NORMAL;
89 
90   // Kerning
91   uint8_t kerning = NS_FONT_KERNING_AUTO;
92 
93   // Whether automatic optical sizing should be applied to variation fonts
94   // that include an 'opsz' axis
95   uint8_t opticalSizing = NS_FONT_OPTICAL_SIZING_AUTO;
96 
97   // Synthesis setting, controls use of fake bolding/italics
98   uint8_t synthesis = NS_FONT_SYNTHESIS_WEIGHT | NS_FONT_SYNTHESIS_STYLE;
99 
100   // The logical size of the font, in nscoord units
101   nscoord size = 0;
102 
103   // The aspect-value (ie., the ratio actualsize:actualxheight) that any
104   // actual physical font created from this font structure must have when
105   // rendering or measuring a string. A value of -1.0 means no adjustment
106   // needs to be done; otherwise the value must be nonnegative.
107   float sizeAdjust = -1.0f;
108 
109   // -- list of value tags for font-specific alternate features
110   nsTArray<gfxAlternateValue> alternateValues;
111 
112   // -- object used to look these up once the font is matched
113   RefPtr<gfxFontFeatureValueSet> featureValueLookup;
114 
115   // Font features from CSS font-feature-settings
116   nsTArray<gfxFontFeature> fontFeatureSettings;
117 
118   // Font variations from CSS font-variation-settings
119   nsTArray<gfxFontVariation> fontVariationSettings;
120 
121   // Language system tag, to override document language;
122   // this is an OpenType "language system" tag represented as a 32-bit integer
123   // (see http://www.microsoft.com/typography/otspec/languagetags.htm).
124   uint32_t languageOverride = 0;
125 
126   // initialize the font with a fontlist
127   nsFont(const mozilla::FontFamilyList& aFontlist, nscoord aSize);
128 
129   // initialize the font with a single generic
130   nsFont(mozilla::FontFamilyType aGenericType, nscoord aSize);
131 
132   // Make a copy of the given font
133   nsFont(const nsFont& aFont);
134 
135   // leave members uninitialized
136   nsFont();
137 
138   ~nsFont();
139 
140   bool operator==(const nsFont& aOther) const { return Equals(aOther); }
141 
142   bool operator!=(const nsFont& aOther) const { return !Equals(aOther); }
143 
144   bool Equals(const nsFont& aOther) const;
145 
146   nsFont& operator=(const nsFont& aOther);
147 
148   enum class MaxDifference : uint8_t { eNone, eVisual, eLayoutAffecting };
149 
150   MaxDifference CalcDifference(const nsFont& aOther) const;
151 
152   void CopyAlternates(const nsFont& aOther);
153 
154   // Add featureSettings into style
155   void AddFontFeaturesToStyle(gfxFontStyle* aStyle, bool aVertical) const;
156 
157   void AddFontVariationsToStyle(gfxFontStyle* aStyle) const;
158 };
159 
160 #define NS_FONT_VARIANT_NORMAL 0
161 #define NS_FONT_VARIANT_SMALL_CAPS 1
162 
163 #endif /* nsFont_h___ */
164