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