/src/mozilla-central/layout/mathml/nsMathMLChar.h
Line | Count | Source (jump to first uncovered line) |
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 nsDisplayListBuilder; |
22 | | class nsDisplayListSet; |
23 | | class nsPresContext; |
24 | | struct nsBoundingMetrics; |
25 | | struct nsFont; |
26 | | |
27 | | namespace mozilla { |
28 | | class ComputedStyle; |
29 | | } |
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 | | |
64 | 0 | bool IsGlyphID() const { return font == -1; } |
65 | | |
66 | 0 | int32_t Length() const { |
67 | 0 | return (IsGlyphID() || code[1] == char16_t('\0') ? 1 : 2); |
68 | 0 | } |
69 | | bool Exists() const |
70 | 0 | { |
71 | 0 | return IsGlyphID() ? glyphID != 0 : code[0] != 0; |
72 | 0 | } |
73 | | bool operator==(const nsGlyphCode& other) const |
74 | 0 | { |
75 | 0 | return (other.font == font && |
76 | 0 | ((IsGlyphID() && other.glyphID == glyphID) || |
77 | 0 | (!IsGlyphID() && other.code[0] == code[0] && |
78 | 0 | other.code[1] == code[1]))); |
79 | 0 | } |
80 | | bool operator!=(const nsGlyphCode& other) const |
81 | 0 | { |
82 | 0 | return ! operator==(other); |
83 | 0 | } |
84 | | }; |
85 | | |
86 | | // Class used to handle stretchy symbols (accent, delimiter and boundary |
87 | | // symbols). |
88 | | class nsMathMLChar |
89 | | { |
90 | | public: |
91 | | typedef gfxTextRun::Range Range; |
92 | | typedef mozilla::gfx::DrawTarget DrawTarget; |
93 | | |
94 | | // constructor and destructor |
95 | | nsMathMLChar() |
96 | | : mDirection(NS_STRETCH_DIRECTION_DEFAULT) |
97 | 0 | { |
98 | 0 | MOZ_COUNT_CTOR(nsMathMLChar); |
99 | 0 | mComputedStyle = nullptr; |
100 | 0 | mUnscaledAscent = 0; |
101 | 0 | mScaleX = mScaleY = 1.0; |
102 | 0 | mDraw = DRAW_NORMAL; |
103 | 0 | mMirrored = false; |
104 | 0 | } |
105 | | |
106 | | // not a virtual destructor: this class is not intended to be subclassed |
107 | | ~nsMathMLChar(); |
108 | | |
109 | | void Display(nsDisplayListBuilder* aBuilder, |
110 | | nsIFrame* aForFrame, |
111 | | const nsDisplayListSet& aLists, |
112 | | uint32_t aIndex, |
113 | | const nsRect* aSelectedRect = nullptr); |
114 | | |
115 | | void PaintForeground(nsIFrame* aForFrame, |
116 | | gfxContext& aRenderingContext, |
117 | | nsPoint aPt, |
118 | | bool aIsSelected); |
119 | | |
120 | | // This is the method called to ask the char to stretch itself. |
121 | | // @param aContainerSize - IN - suggested size for the stretched char |
122 | | // @param aDesiredStretchSize - OUT - the size that the char wants |
123 | | nsresult |
124 | | Stretch(nsIFrame* aForFrame, |
125 | | DrawTarget* aDrawTarget, |
126 | | float aFontSizeInflation, |
127 | | nsStretchDirection aStretchDirection, |
128 | | const nsBoundingMetrics& aContainerSize, |
129 | | nsBoundingMetrics& aDesiredStretchSize, |
130 | | uint32_t aStretchHint, |
131 | | bool aRTL); |
132 | | |
133 | | void |
134 | | SetData(nsString& aData); |
135 | | |
136 | | void |
137 | 0 | GetData(nsString& aData) { |
138 | 0 | aData = mData; |
139 | 0 | } |
140 | | |
141 | | int32_t |
142 | 0 | Length() { |
143 | 0 | return mData.Length(); |
144 | 0 | } |
145 | | |
146 | | nsStretchDirection |
147 | 0 | GetStretchDirection() { |
148 | 0 | return mDirection; |
149 | 0 | } |
150 | | |
151 | | // Sometimes we only want to pass the data to another routine, |
152 | | // this function helps to avoid copying |
153 | | const char16_t* |
154 | 0 | get() { |
155 | 0 | return mData.get(); |
156 | 0 | } |
157 | | |
158 | | void |
159 | 0 | GetRect(nsRect& aRect) { |
160 | 0 | aRect = mRect; |
161 | 0 | } |
162 | | |
163 | | void |
164 | 0 | SetRect(const nsRect& aRect) { |
165 | 0 | mRect = aRect; |
166 | 0 | } |
167 | | |
168 | | // Get the maximum width that the character might have after a vertical |
169 | | // Stretch(). |
170 | | // |
171 | | // @param aStretchHint can be the value that will be passed to Stretch(). |
172 | | // It is used to determine whether the operator is stretchy or a largeop. |
173 | | nscoord |
174 | | GetMaxWidth(nsIFrame* aForFrame, |
175 | | DrawTarget* aDrawTarget, |
176 | | float aFontSizeInflation, |
177 | | uint32_t aStretchHint = NS_STRETCH_NORMAL); |
178 | | |
179 | | // Metrics that _exactly_ enclose the char. The char *must* have *already* |
180 | | // being stretched before you can call the GetBoundingMetrics() method. |
181 | | // IMPORTANT: since chars have their own ComputedStyles, and may be rendered |
182 | | // with glyphs that are not in the parent font, just calling the default |
183 | | // aRenderingContext.GetBoundingMetrics(aChar) can give incorrect results. |
184 | | void |
185 | 0 | GetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) { |
186 | 0 | aBoundingMetrics = mBoundingMetrics; |
187 | 0 | } |
188 | | |
189 | | void |
190 | 0 | SetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) { |
191 | 0 | mBoundingMetrics = aBoundingMetrics; |
192 | 0 | } |
193 | | |
194 | | // Hooks to access the extra leaf ComputedStyles given to the MathMLChars. |
195 | | // They provide an interface to make them accessible to the Style System via |
196 | | // the Get/Set AdditionalComputedStyle() APIs. Owners of MathMLChars |
197 | | // should honor these APIs. |
198 | | mozilla::ComputedStyle* GetComputedStyle() const; |
199 | | |
200 | | void SetComputedStyle(mozilla::ComputedStyle* aComputedStyle); |
201 | | |
202 | | protected: |
203 | | friend class nsGlyphTable; |
204 | | friend class nsPropertiesTable; |
205 | | friend class nsOpenTypeTable; |
206 | | nsString mData; |
207 | | |
208 | | private: |
209 | | nsRect mRect; |
210 | | nsStretchDirection mDirection; |
211 | | nsBoundingMetrics mBoundingMetrics; |
212 | | RefPtr<mozilla::ComputedStyle> mComputedStyle; |
213 | | // mGlyphs/mBmData are arrays describing the glyphs used to draw the operator. |
214 | | // See the drawing methods below. |
215 | | RefPtr<gfxTextRun> mGlyphs[4]; |
216 | | nsBoundingMetrics mBmData[4]; |
217 | | // mUnscaledAscent is the actual ascent of the char. |
218 | | nscoord mUnscaledAscent; |
219 | | // mScaleX, mScaleY are the factors by which we scale the char. |
220 | | float mScaleX, mScaleY; |
221 | | |
222 | | // mDraw indicates how we draw the stretchy operator: |
223 | | // - DRAW_NORMAL: we render the mData string normally. |
224 | | // - DRAW_VARIANT: we draw a larger size variant given by mGlyphs[0]. |
225 | | // - DRAW_PARTS: we assemble several parts given by mGlyphs[0], ... mGlyphs[4] |
226 | | // XXXfredw: the MATH table can have any numbers of parts and extenders. |
227 | | enum DrawingMethod { |
228 | | DRAW_NORMAL, DRAW_VARIANT, DRAW_PARTS |
229 | | }; |
230 | | DrawingMethod mDraw; |
231 | | |
232 | | // mMirrored indicates whether the character is mirrored. |
233 | | bool mMirrored; |
234 | | |
235 | | class StretchEnumContext; |
236 | | friend class StretchEnumContext; |
237 | | |
238 | | // helper methods |
239 | | bool |
240 | | SetFontFamily(nsPresContext* aPresContext, |
241 | | const nsGlyphTable* aGlyphTable, |
242 | | const nsGlyphCode& aGlyphCode, |
243 | | const mozilla::FontFamilyList& aDefaultFamily, |
244 | | nsFont& aFont, |
245 | | RefPtr<gfxFontGroup>* aFontGroup); |
246 | | |
247 | | nsresult |
248 | | StretchInternal(nsIFrame* aForFrame, |
249 | | DrawTarget* aDrawTarget, |
250 | | float aFontSizeInflation, |
251 | | nsStretchDirection& aStretchDirection, |
252 | | const nsBoundingMetrics& aContainerSize, |
253 | | nsBoundingMetrics& aDesiredStretchSize, |
254 | | uint32_t aStretchHint, |
255 | | float aMaxSize = NS_MATHML_OPERATOR_SIZE_INFINITY, |
256 | | bool aMaxSizeIsAbsolute = false); |
257 | | |
258 | | nsresult |
259 | | PaintVertically(nsPresContext* aPresContext, |
260 | | gfxContext* aThebesContext, |
261 | | nsRect& aRect, |
262 | | nscolor aColor); |
263 | | |
264 | | nsresult |
265 | | PaintHorizontally(nsPresContext* aPresContext, |
266 | | gfxContext* aThebesContext, |
267 | | nsRect& aRect, |
268 | | nscolor aColor); |
269 | | |
270 | | void |
271 | | ApplyTransforms(gfxContext* aThebesContext, int32_t aAppUnitsPerGfxUnit, |
272 | | nsRect &r); |
273 | | }; |
274 | | |
275 | | #endif /* nsMathMLChar_h___ */ |