Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / mathml / nsMathMLFrame.h
blob5de0b60ee653a682cc6d89768c4b062440ccb6c8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla MathML Project.
17 * The Initial Developer of the Original Code is
18 * The University Of Queensland.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Roger B. Sidje <rbs@maths.uq.edu.au>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsMathMLFrame_h___
40 #define nsMathMLFrame_h___
42 #include "nsCOMPtr.h"
43 #include "nsPresContext.h"
44 #include "nsIRenderingContext.h"
45 #include "nsIFontMetrics.h"
46 #include "nsStyleContext.h"
47 #include "nsMathMLAtoms.h"
48 #include "nsMathMLOperators.h"
49 #include "nsIMathMLFrame.h"
50 #include "nsFrame.h"
51 #include "nsCSSValue.h"
52 #include "nsMathMLElement.h"
54 class nsMathMLChar;
56 // Concrete base class with default methods that derived MathML frames can override
57 class nsMathMLFrame : public nsIMathMLFrame {
58 public:
60 // nsIMathMLFrame ---
62 NS_IMETHOD
63 GetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) {
64 aBoundingMetrics = mBoundingMetrics;
65 return NS_OK;
68 NS_IMETHOD
69 SetBoundingMetrics(const nsBoundingMetrics& aBoundingMetrics) {
70 mBoundingMetrics = aBoundingMetrics;
71 return NS_OK;
74 NS_IMETHOD
75 SetReference(const nsPoint& aReference) {
76 mReference = aReference;
77 return NS_OK;
80 virtual eMathMLFrameType GetMathMLFrameType();
82 NS_IMETHOD
83 Stretch(nsIRenderingContext& aRenderingContext,
84 nsStretchDirection aStretchDirection,
85 nsBoundingMetrics& aContainerSize,
86 nsHTMLReflowMetrics& aDesiredStretchSize)
88 return NS_OK;
91 NS_IMETHOD
92 GetEmbellishData(nsEmbellishData& aEmbellishData) {
93 aEmbellishData = mEmbellishData;
94 return NS_OK;
97 NS_IMETHOD
98 GetPresentationData(nsPresentationData& aPresentationData) {
99 aPresentationData = mPresentationData;
100 return NS_OK;
103 NS_IMETHOD
104 InheritAutomaticData(nsIFrame* aParent);
106 NS_IMETHOD
107 TransmitAutomaticData()
109 return NS_OK;
112 NS_IMETHOD
113 UpdatePresentationData(PRUint32 aFlagsValues,
114 PRUint32 aFlagsToUpdate);
116 NS_IMETHOD
117 UpdatePresentationDataFromChildAt(PRInt32 aFirstIndex,
118 PRInt32 aLastIndex,
119 PRUint32 aFlagsValues,
120 PRUint32 aFlagsToUpdate)
122 return NS_OK;
125 // helper to give a style context suitable for doing the stretching to the
126 // MathMLChar. Frame classes that use this should make the extra style contexts
127 // accessible to the Style System via Get/Set AdditionalStyleContext.
128 static void
129 ResolveMathMLCharStyle(nsPresContext* aPresContext,
130 nsIContent* aContent,
131 nsStyleContext* aParenStyleContext,
132 nsMathMLChar* aMathMLChar,
133 PRBool aIsMutableChar);
135 // helper to get the mEmbellishData of a frame
136 // The MathML REC precisely defines an "embellished operator" as:
137 // - an <mo> element;
138 // - or one of the elements <msub>, <msup>, <msubsup>, <munder>, <mover>,
139 // <munderover>, <mmultiscripts>, <mfrac>, or <semantics>, whose first
140 // argument exists and is an embellished operator;
141 //- or one of the elements <mstyle>, <mphantom>, or <mpadded>, such that
142 // an <mrow> containing the same arguments would be an embellished
143 // operator;
144 // - or an <maction> element whose selected subexpression exists and is an
145 // embellished operator;
146 // - or an <mrow> whose arguments consist (in any order) of one embellished
147 // operator and zero or more spacelike elements.
148 static void
149 GetEmbellishDataFrom(nsIFrame* aFrame,
150 nsEmbellishData& aEmbellishData);
152 // helper to get the presentation data of a frame. If aClimbTree is
153 // set to true and the frame happens to be surrounded by non-MathML
154 // helper frames needed for its support, we walk up the frame hierarchy
155 // until we reach a MathML ancestor or the <root> math element.
156 static void
157 GetPresentationDataFrom(nsIFrame* aFrame,
158 nsPresentationData& aPresentationData,
159 PRBool aClimbTree = PR_TRUE);
161 // helper used by <mstyle> and <mtable> to see if they have a displaystyle attribute
162 static void
163 FindAttrDisplaystyle(nsIContent* aContent,
164 nsPresentationData& aPresentationData);
166 // helper to check if a content has an attribute. If content is nsnull or if
167 // the attribute is not there, check if the attribute is on the mstyle hierarchy
168 // @return PR_TRUE --if attribute exists
169 // PR_FALSE --if attribute doesn't exist
170 static PRBool
171 GetAttribute(nsIContent* aContent,
172 nsIFrame* aMathMLmstyleFrame,
173 nsIAtom* aAttributeAtom,
174 nsString& aValue);
176 // utilities to parse and retrieve numeric values in CSS units
177 // All values are stored in twips.
178 static PRBool
179 ParseNumericValue(const nsString& aString,
180 nsCSSValue& aCSSValue) {
181 return nsMathMLElement::ParseNumericValue(aString, aCSSValue,
182 nsMathMLElement::PARSE_ALLOW_NEGATIVE |
183 nsMathMLElement::PARSE_ALLOW_UNITLESS);
186 static nscoord
187 CalcLength(nsPresContext* aPresContext,
188 nsStyleContext* aStyleContext,
189 const nsCSSValue& aCSSValue);
191 static PRBool
192 ParseNamedSpaceValue(nsIFrame* aMathMLmstyleFrame,
193 nsString& aString,
194 nsCSSValue& aCSSValue);
196 static eMathMLFrameType
197 GetMathMLFrameTypeFor(nsIFrame* aFrame)
199 if (aFrame->IsFrameOfType(nsIFrame::eMathML)) {
200 nsIMathMLFrame* mathMLFrame = do_QueryFrame(aFrame);
201 if (mathMLFrame)
202 return mathMLFrame->GetMathMLFrameType();
204 return eMathMLFrameType_UNKNOWN;
207 // estimate of the italic correction
208 static void
209 GetItalicCorrection(nsBoundingMetrics& aBoundingMetrics,
210 nscoord& aItalicCorrection)
212 aItalicCorrection = aBoundingMetrics.rightBearing - aBoundingMetrics.width;
213 if (0 > aItalicCorrection) {
214 aItalicCorrection = 0;
218 static void
219 GetItalicCorrection(nsBoundingMetrics& aBoundingMetrics,
220 nscoord& aLeftItalicCorrection,
221 nscoord& aRightItalicCorrection)
223 aRightItalicCorrection = aBoundingMetrics.rightBearing - aBoundingMetrics.width;
224 if (0 > aRightItalicCorrection) {
225 aRightItalicCorrection = 0;
227 aLeftItalicCorrection = -aBoundingMetrics.leftBearing;
228 if (0 > aLeftItalicCorrection) {
229 aLeftItalicCorrection = 0;
233 // helper methods for getting sup/subdrop's from a child
234 static void
235 GetSubDropFromChild(nsIFrame* aChild,
236 nscoord& aSubDrop)
238 const nsStyleFont* font = aChild->GetStyleFont();
239 nsCOMPtr<nsIFontMetrics> fm = aChild->PresContext()->GetMetricsFor(
240 font->mFont);
241 GetSubDrop(fm, aSubDrop);
244 static void
245 GetSupDropFromChild(nsIFrame* aChild,
246 nscoord& aSupDrop)
248 const nsStyleFont* font = aChild->GetStyleFont();
249 nsCOMPtr<nsIFontMetrics> fm = aChild->PresContext()->GetMetricsFor(
250 font->mFont);
251 GetSupDrop(fm, aSupDrop);
254 static void
255 GetSkewCorrectionFromChild(nsIFrame* aChild,
256 nscoord& aSkewCorrection)
258 // default is 0
259 // individual classes should over-ride this method if necessary
260 aSkewCorrection = 0;
263 // 2 levels of subscript shifts
264 static void
265 GetSubScriptShifts(nsIFontMetrics* fm,
266 nscoord& aSubScriptShift1,
267 nscoord& aSubScriptShift2)
269 nscoord xHeight;
270 fm->GetXHeight(xHeight);
271 aSubScriptShift1 = NSToCoordRound(150.000f/430.556f * xHeight);
272 aSubScriptShift2 = NSToCoordRound(247.217f/430.556f * xHeight);
275 // 3 levels of superscript shifts
276 static void
277 GetSupScriptShifts(nsIFontMetrics* fm,
278 nscoord& aSupScriptShift1,
279 nscoord& aSupScriptShift2,
280 nscoord& aSupScriptShift3)
282 nscoord xHeight;
283 fm->GetXHeight(xHeight);
284 aSupScriptShift1 = NSToCoordRound(412.892f/430.556f * xHeight);
285 aSupScriptShift2 = NSToCoordRound(362.892f/430.556f * xHeight);
286 aSupScriptShift3 = NSToCoordRound(288.889f/430.556f * xHeight);
289 // these are TeX specific params not found in ordinary fonts
291 static void
292 GetSubDrop(nsIFontMetrics* fm,
293 nscoord& aSubDrop)
295 nscoord xHeight;
296 fm->GetXHeight(xHeight);
297 aSubDrop = NSToCoordRound(50.000f/430.556f * xHeight);
300 static void
301 GetSupDrop(nsIFontMetrics* fm,
302 nscoord& aSupDrop)
304 nscoord xHeight;
305 fm->GetXHeight(xHeight);
306 aSupDrop = NSToCoordRound(386.108f/430.556f * xHeight);
309 static void
310 GetNumeratorShifts(nsIFontMetrics* fm,
311 nscoord& numShift1,
312 nscoord& numShift2,
313 nscoord& numShift3)
315 nscoord xHeight;
316 fm->GetXHeight(xHeight);
317 numShift1 = NSToCoordRound(676.508f/430.556f * xHeight);
318 numShift2 = NSToCoordRound(393.732f/430.556f * xHeight);
319 numShift3 = NSToCoordRound(443.731f/430.556f * xHeight);
322 static void
323 GetDenominatorShifts(nsIFontMetrics* fm,
324 nscoord& denShift1,
325 nscoord& denShift2)
327 nscoord xHeight;
328 fm->GetXHeight(xHeight);
329 denShift1 = NSToCoordRound(685.951f/430.556f * xHeight);
330 denShift2 = NSToCoordRound(344.841f/430.556f * xHeight);
333 static void
334 GetEmHeight(nsIFontMetrics* fm,
335 nscoord& emHeight)
337 #if 0
338 // should switch to this API in order to scale with changes of TextZoom
339 fm->GetEmHeight(emHeight);
340 #else
341 emHeight = NSToCoordRound(float(fm->Font().size));
342 #endif
345 static void
346 GetAxisHeight (nsIFontMetrics* fm,
347 nscoord& axisHeight)
349 fm->GetXHeight (axisHeight);
350 axisHeight = NSToCoordRound(250.000f/430.556f * axisHeight);
353 static void
354 GetBigOpSpacings(nsIFontMetrics* fm,
355 nscoord& bigOpSpacing1,
356 nscoord& bigOpSpacing2,
357 nscoord& bigOpSpacing3,
358 nscoord& bigOpSpacing4,
359 nscoord& bigOpSpacing5)
361 nscoord xHeight;
362 fm->GetXHeight(xHeight);
363 bigOpSpacing1 = NSToCoordRound(111.111f/430.556f * xHeight);
364 bigOpSpacing2 = NSToCoordRound(166.667f/430.556f * xHeight);
365 bigOpSpacing3 = NSToCoordRound(200.000f/430.556f * xHeight);
366 bigOpSpacing4 = NSToCoordRound(600.000f/430.556f * xHeight);
367 bigOpSpacing5 = NSToCoordRound(100.000f/430.556f * xHeight);
370 static void
371 GetRuleThickness(nsIFontMetrics* fm,
372 nscoord& ruleThickness)
374 nscoord xHeight;
375 fm->GetXHeight(xHeight);
376 ruleThickness = NSToCoordRound(40.000f/430.556f * xHeight);
379 // Some parameters are not accurately obtained using the x-height.
380 // Here are some slower variants to obtain the desired metrics
381 // by actually measuring some characters
382 static void
383 GetRuleThickness(nsIRenderingContext& aRenderingContext,
384 nsIFontMetrics* aFontMetrics,
385 nscoord& aRuleThickness);
387 static void
388 GetAxisHeight(nsIRenderingContext& aRenderingContext,
389 nsIFontMetrics* aFontMetrics,
390 nscoord& aAxisHeight);
392 protected:
393 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
394 nsresult DisplayBoundingMetrics(nsDisplayListBuilder* aBuilder,
395 nsIFrame* aFrame, const nsPoint& aPt,
396 const nsBoundingMetrics& aMetrics,
397 const nsDisplayListSet& aLists);
398 #endif
401 * Display a solid rectangle in the frame's text color. Used for drawing
402 * fraction separators and root/sqrt overbars.
404 nsresult DisplayBar(nsDisplayListBuilder* aBuilder,
405 nsIFrame* aFrame, const nsRect& aRect,
406 const nsDisplayListSet& aLists);
408 // information about the presentation policy of the frame
409 nsPresentationData mPresentationData;
411 // information about a container that is an embellished operator
412 nsEmbellishData mEmbellishData;
414 // Metrics that _exactly_ enclose the text of the frame
415 nsBoundingMetrics mBoundingMetrics;
417 // Reference point of the frame: mReference.y is the baseline
418 nsPoint mReference;
421 #endif /* nsMathMLFrame_h___ */