Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / layout / mathml / nsMathMLChar.h
blob07d803b00c575121402ba14be12410e378705af7
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/. */
7 #ifndef nsMathMLChar_h___
8 #define nsMathMLChar_h___
10 #include "nsColor.h"
11 #include "nsMathMLOperators.h"
12 #include "nsPoint.h"
13 #include "nsRect.h"
14 #include "nsString.h"
15 #include "nsBoundingMetrics.h"
16 #include "gfxTextRun.h"
18 class gfxContext;
19 class nsGlyphTable;
20 class nsIFrame;
21 class nsPresContext;
22 struct nsBoundingMetrics;
23 struct nsFont;
25 namespace mozilla {
26 class nsDisplayListBuilder;
27 class nsDisplayListSet;
28 class ComputedStyle;
29 } // namespace mozilla
31 // Hints for Stretch() to indicate criteria for stretching
32 enum {
33 // Don't stretch
34 NS_STRETCH_NONE = 0x00,
35 // Variable size stretches
36 NS_STRETCH_VARIABLE_MASK = 0x0F,
37 NS_STRETCH_NORMAL = 0x01, // try to stretch to requested size
38 NS_STRETCH_NEARER = 0x02, // stretch very close to requested size
39 NS_STRETCH_SMALLER = 0x04, // don't stretch more than requested size
40 NS_STRETCH_LARGER = 0x08, // don't stretch less than requested size
41 // A largeop in displaystyle
42 NS_STRETCH_LARGEOP = 0x10,
44 // Intended for internal use:
45 // Find the widest metrics that might be returned from a vertical stretch
46 NS_STRETCH_MAXWIDTH = 0x20
49 // A single glyph in our internal representation is either
50 // 1) a 'code@font' pair from the mathfontFONTFAMILY.properties table. The
51 // 'code' is interpreted as a Unicode point. The 'font' is a numeric
52 // identifier given to the font to which the glyph belongs, which is 0 for the
53 // FONTFAMILY and > 0 for 'external' fonts.
54 // 2) a glyph index from the Open Type MATH table. In that case, all the glyphs
55 // come from the font containing that table and 'font' is just set to -1.
56 struct nsGlyphCode {
57 union {
58 char16_t code[2];
59 uint32_t glyphID;
61 int8_t font;
63 bool IsGlyphID() const { return font == -1; }
65 int32_t Length() const {
66 return (IsGlyphID() || code[1] == char16_t('\0') ? 1 : 2);
68 bool Exists() const { return IsGlyphID() ? glyphID != 0 : code[0] != 0; }
69 bool operator==(const nsGlyphCode& other) const {
70 return (other.font == font && ((IsGlyphID() && other.glyphID == glyphID) ||
71 (!IsGlyphID() && other.code[0] == code[0] &&
72 other.code[1] == code[1])));
74 bool operator!=(const nsGlyphCode& other) const { return !operator==(other); }
77 // Class used to handle stretchy symbols (accent, delimiter and boundary
78 // symbols).
79 class nsMathMLChar {
80 public:
81 typedef gfxTextRun::Range Range;
82 typedef mozilla::gfx::DrawTarget DrawTarget;
84 // constructor and destructor
85 nsMathMLChar() : mDirection(NS_STRETCH_DIRECTION_DEFAULT) {
86 MOZ_COUNT_CTOR(nsMathMLChar);
87 mComputedStyle = nullptr;
88 mUnscaledAscent = 0;
89 mScaleX = mScaleY = 1.0;
90 mDraw = DRAW_NORMAL;
91 mMirrored = false;
94 // not a virtual destructor: this class is not intended to be subclassed
95 ~nsMathMLChar();
97 void Display(mozilla::nsDisplayListBuilder* aBuilder, nsIFrame* aForFrame,
98 const mozilla::nsDisplayListSet& aLists, uint32_t aIndex,
99 const nsRect* aSelectedRect = nullptr);
101 void PaintForeground(nsIFrame* aForFrame, gfxContext& aRenderingContext,
102 nsPoint aPt, bool aIsSelected);
104 // This is the method called to ask the char to stretch itself.
105 // @param aContainerSize - IN - suggested size for the stretched char
106 // @param aDesiredStretchSize - OUT - the size that the char wants
107 nsresult Stretch(nsIFrame* aForFrame, DrawTarget* aDrawTarget,
108 float aFontSizeInflation,
109 nsStretchDirection aStretchDirection,
110 const nsBoundingMetrics& aContainerSize,
111 nsBoundingMetrics& aDesiredStretchSize,
112 uint32_t aStretchHint, bool aRTL);
114 void SetData(nsString& aData);
116 void GetData(nsString& aData) { aData = mData; }
118 int32_t Length() { return mData.Length(); }
120 nsStretchDirection GetStretchDirection() { return mDirection; }
122 // Sometimes we only want to pass the data to another routine,
123 // this function helps to avoid copying
124 const char16_t* get() { return mData.get(); }
126 void GetRect(nsRect& aRect) { aRect = mRect; }
128 void SetRect(const nsRect& aRect) { mRect = aRect; }
130 // Get the maximum width that the character might have after a vertical
131 // Stretch().
133 // @param aStretchHint can be the value that will be passed to Stretch().
134 // It is used to determine whether the operator is stretchy or a largeop.
135 nscoord GetMaxWidth(nsIFrame* aForFrame, DrawTarget* aDrawTarget,
136 float aFontSizeInflation,
137 uint32_t aStretchHint = NS_STRETCH_NORMAL);
139 // Metrics that _exactly_ enclose the char. The char *must* have *already*
140 // being stretched before you can call the GetBoundingMetrics() method.
141 // IMPORTANT: since chars have their own ComputedStyles, and may be rendered
142 // with glyphs that are not in the parent font, just calling the default
143 // aRenderingContext.GetBoundingMetrics(aChar) can give incorrect results.
144 void GetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) {
145 aBoundingMetrics = mBoundingMetrics;
148 void SetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) {
149 mBoundingMetrics = aBoundingMetrics;
152 // Hooks to access the extra leaf ComputedStyles given to the MathMLChars.
153 // They provide an interface to make them accessible to the Style System via
154 // the Get/Set AdditionalComputedStyle() APIs. Owners of MathMLChars
155 // should honor these APIs.
156 mozilla::ComputedStyle* GetComputedStyle() const;
158 void SetComputedStyle(mozilla::ComputedStyle* aComputedStyle);
160 protected:
161 friend class nsGlyphTable;
162 friend class nsPropertiesTable;
163 friend class nsOpenTypeTable;
164 nsString mData;
166 private:
167 nsRect mRect;
168 nsStretchDirection mDirection;
169 nsBoundingMetrics mBoundingMetrics;
170 RefPtr<mozilla::ComputedStyle> mComputedStyle;
171 // mGlyphs/mBmData are arrays describing the glyphs used to draw the operator.
172 // See the drawing methods below.
173 RefPtr<gfxTextRun> mGlyphs[4];
174 nsBoundingMetrics mBmData[4];
175 // mUnscaledAscent is the actual ascent of the char.
176 nscoord mUnscaledAscent;
177 // mScaleX, mScaleY are the factors by which we scale the char.
178 float mScaleX, mScaleY;
180 // mDraw indicates how we draw the stretchy operator:
181 // - DRAW_NORMAL: we render the mData string normally.
182 // - DRAW_VARIANT: we draw a larger size variant given by mGlyphs[0].
183 // - DRAW_PARTS: we assemble several parts given by mGlyphs[0], ... mGlyphs[4]
184 // XXXfredw: the MATH table can have any numbers of parts and extenders.
185 enum DrawingMethod { DRAW_NORMAL, DRAW_VARIANT, DRAW_PARTS };
186 DrawingMethod mDraw;
188 // mMirrored indicates whether the character is mirrored.
189 bool mMirrored;
191 class StretchEnumContext;
192 friend class StretchEnumContext;
194 // helper methods
195 bool SetFontFamily(nsPresContext* aPresContext,
196 const nsGlyphTable* aGlyphTable,
197 const nsGlyphCode& aGlyphCode,
198 const mozilla::StyleFontFamilyList& aDefaultFamily,
199 nsFont& aFont, RefPtr<gfxFontGroup>* aFontGroup);
201 nsresult StretchInternal(nsIFrame* aForFrame, DrawTarget* aDrawTarget,
202 float aFontSizeInflation,
203 nsStretchDirection& aStretchDirection,
204 const nsBoundingMetrics& aContainerSize,
205 nsBoundingMetrics& aDesiredStretchSize,
206 uint32_t aStretchHint,
207 float aMaxSize = NS_MATHML_OPERATOR_SIZE_INFINITY,
208 bool aMaxSizeIsAbsolute = false);
210 nsresult PaintVertically(nsPresContext* aPresContext,
211 gfxContext* aThebesContext, nsRect& aRect,
212 nscolor aColor);
214 nsresult PaintHorizontally(nsPresContext* aPresContext,
215 gfxContext* aThebesContext, nsRect& aRect,
216 nscolor aColor);
218 void ApplyTransforms(gfxContext* aThebesContext, int32_t aAppUnitsPerGfxUnit,
219 nsRect& r);
222 #endif /* nsMathMLChar_h___ */