bug 523370 - fixing bogus OOM with empty double free lists. r=dmandelin
[mozilla-central.git] / layout / mathml / nsMathMLmunderoverFrame.cpp
blob6f448cdf857fffd2cb5043ada75d8fbf0ced5499
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>
24 * David J. Fiddes <D.J.Fiddes@hw.ac.uk>
25 * Shyjan Mahamud <mahamud@cs.cmu.edu>
26 * Pierre Phaneuf <pp@ludusdesign.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
43 #include "nsCOMPtr.h"
44 #include "nsFrame.h"
45 #include "nsPresContext.h"
46 #include "nsStyleContext.h"
47 #include "nsStyleConsts.h"
48 #include "nsINameSpaceManager.h"
49 #include "nsIRenderingContext.h"
50 #include "nsIFontMetrics.h"
52 #include "nsMathMLmunderoverFrame.h"
53 #include "nsMathMLmsubsupFrame.h"
56 // <munderover> -- attach an underscript-overscript pair to a base - implementation
59 nsIFrame*
60 NS_NewMathMLmunderoverFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
62 return new (aPresShell) nsMathMLmunderoverFrame(aContext);
65 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmunderoverFrame)
67 nsMathMLmunderoverFrame::~nsMathMLmunderoverFrame()
71 NS_IMETHODIMP
72 nsMathMLmunderoverFrame::AttributeChanged(PRInt32 aNameSpaceID,
73 nsIAtom* aAttribute,
74 PRInt32 aModType)
76 if (nsGkAtoms::accent_ == aAttribute ||
77 nsGkAtoms::accentunder_ == aAttribute) {
78 // When we have automatic data to update within ourselves, we ask our
79 // parent to re-layout its children
80 return ReLayoutChildren(mParent);
83 return nsMathMLContainerFrame::
84 AttributeChanged(aNameSpaceID, aAttribute, aModType);
87 NS_IMETHODIMP
88 nsMathMLmunderoverFrame::UpdatePresentationData(PRUint32 aFlagsValues,
89 PRUint32 aFlagsToUpdate)
91 nsMathMLContainerFrame::UpdatePresentationData(aFlagsValues, aFlagsToUpdate);
92 // disable the stretch-all flag if we are going to act like a subscript-superscript pair
93 if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
94 !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
95 mPresentationData.flags &= ~NS_MATHML_STRETCH_ALL_CHILDREN_HORIZONTALLY;
97 else {
98 mPresentationData.flags |= NS_MATHML_STRETCH_ALL_CHILDREN_HORIZONTALLY;
100 return NS_OK;
103 NS_IMETHODIMP
104 nsMathMLmunderoverFrame::UpdatePresentationDataFromChildAt(PRInt32 aFirstIndex,
105 PRInt32 aLastIndex,
106 PRUint32 aFlagsValues,
107 PRUint32 aFlagsToUpdate)
109 // munderover is special... The REC says:
110 // Within underscript, <munder> always sets displaystyle to "false",
111 // but increments scriptlevel by 1 only when accentunder is "false".
112 // Within underscript, <munderover> always sets displaystyle to "false",
113 // but increments scriptlevel by 1 only when accentunder is "false".
114 // This means that
115 // 1. don't allow displaystyle to change in the underscript & overscript
116 // 2a if the value of the accent is changed, we need to recompute the
117 // scriptlevel of the underscript. The problem is that the accent
118 // can change in the <mo> deep down the embellished hierarchy
119 // 2b if the value of the accent is changed, we need to recompute the
120 // scriptlevel of the overscript. The problem is that the accent
121 // can change in the <mo> deep down the embellished hierarchy
123 // Do #1 here, prevent displaystyle to be changed in the underscript & overscript
124 PRInt32 index = 0;
125 nsIFrame* childFrame = mFrames.FirstChild();
126 while (childFrame) {
127 if ((index >= aFirstIndex) &&
128 ((aLastIndex <= 0) || ((aLastIndex > 0) && (index <= aLastIndex)))) {
129 if (index > 0) {
130 // disable the flag
131 aFlagsToUpdate &= ~NS_MATHML_DISPLAYSTYLE;
132 aFlagsValues &= ~NS_MATHML_DISPLAYSTYLE;
134 PropagatePresentationDataFor(childFrame, aFlagsValues, aFlagsToUpdate);
136 index++;
137 childFrame = childFrame->GetNextSibling();
139 return NS_OK;
141 // For #2, changing the accent attribute will trigger a re-build of
142 // all automatic data in the embellished hierarchy
145 NS_IMETHODIMP
146 nsMathMLmunderoverFrame::InheritAutomaticData(nsIFrame* aParent)
148 // let the base class get the default from our parent
149 nsMathMLContainerFrame::InheritAutomaticData(aParent);
151 mPresentationData.flags |= NS_MATHML_STRETCH_ALL_CHILDREN_HORIZONTALLY;
153 return NS_OK;
156 NS_IMETHODIMP
157 nsMathMLmunderoverFrame::TransmitAutomaticData()
159 // At this stage, all our children are in sync and we can fully
160 // resolve our own mEmbellishData struct
161 //---------------------------------------------------------------------
164 The REC says:
166 The accent and accentunder attributes have the same effect as
167 the attributes with the same names on <mover> and <munder>,
168 respectively. Their default values are also computed in the
169 same manner as described for those elements, with the default
170 value of accent depending on overscript and the default value
171 of accentunder depending on underscript.
174 nsIFrame* overscriptFrame = nsnull;
175 nsIFrame* underscriptFrame = nsnull;
176 nsIFrame* baseFrame = mFrames.FirstChild();
177 if (baseFrame)
178 underscriptFrame = baseFrame->GetNextSibling();
179 if (underscriptFrame)
180 overscriptFrame = underscriptFrame->GetNextSibling();
182 // if our base is an embellished operator, let its state bubble to us (in particular,
183 // this is where we get the flag for NS_MATHML_EMBELLISH_MOVABLELIMITS). Our flags
184 // are reset to the default values of false if the base frame isn't embellished.
185 mPresentationData.baseFrame = baseFrame;
186 GetEmbellishDataFrom(baseFrame, mEmbellishData);
188 // The default value of accentunder is false, unless the underscript is embellished
189 // and its core <mo> is an accent
190 nsEmbellishData embellishData;
191 GetEmbellishDataFrom(underscriptFrame, embellishData);
192 if (NS_MATHML_EMBELLISH_IS_ACCENT(embellishData.flags))
193 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENTUNDER;
194 else
195 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENTUNDER;
197 static nsIContent::AttrValuesArray strings[] =
198 {&nsGkAtoms::_true, &nsGkAtoms::_false, nsnull};
200 // if we have an accentunder attribute, it overrides what the underscript said
201 switch (mContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::accentunder_,
202 strings, eCaseMatters)) {
203 case 0: mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENTUNDER; break;
204 case 1: mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENTUNDER; break;
207 // The default value of accent is false, unless the overscript is embellished
208 // and its core <mo> is an accent
209 GetEmbellishDataFrom(overscriptFrame, embellishData);
210 if (NS_MATHML_EMBELLISH_IS_ACCENT(embellishData.flags))
211 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENTOVER;
212 else
213 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENTOVER;
215 // if we have an accent attribute, it overrides what the overscript said
216 switch (mContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::accent_,
217 strings, eCaseMatters)) {
218 case 0: mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENTOVER; break;
219 case 1: mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENTOVER; break;
222 // disable the stretch-all flag if we are going to act like a superscript
223 if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
224 !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags))
225 mPresentationData.flags &= ~NS_MATHML_STRETCH_ALL_CHILDREN_HORIZONTALLY;
227 // Now transmit any change that we want to our children so that they
228 // can update their mPresentationData structs
229 //---------------------------------------------------------------------
231 /* The REC says:
232 Within underscript, <munderover> always sets displaystyle to "false",
233 but increments scriptlevel by 1 only when accentunder is "false".
235 Within overscript, <munderover> always sets displaystyle to "false",
236 but increments scriptlevel by 1 only when accent is "false".
238 The TeXBook treats 'over' like a superscript, so p.141 or Rule 13a
239 say it shouldn't be compressed. However, The TeXBook says
240 that math accents and \overline change uncramped styles to their
241 cramped counterparts.
243 PRUint32 compress = NS_MATHML_EMBELLISH_IS_ACCENTOVER(mEmbellishData.flags)
244 ? NS_MATHML_COMPRESSED : 0;
245 SetIncrementScriptLevel(2, !NS_MATHML_EMBELLISH_IS_ACCENTOVER(mEmbellishData.flags));
246 PropagatePresentationDataFor(overscriptFrame,
247 ~NS_MATHML_DISPLAYSTYLE | compress,
248 NS_MATHML_DISPLAYSTYLE | compress);
251 The TeXBook treats 'under' like a subscript, so p.141 or Rule 13a
252 say it should be compressed
254 SetIncrementScriptLevel(1, !NS_MATHML_EMBELLISH_IS_ACCENTUNDER(mEmbellishData.flags));
255 PropagatePresentationDataFor(underscriptFrame,
256 ~NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED,
257 NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED);
259 return NS_OK;
263 The REC says:
264 * If the base is an operator with movablelimits="true" (or an embellished
265 operator whose <mo> element core has movablelimits="true"), and
266 displaystyle="false", then underscript and overscript are drawn in
267 a subscript and superscript position, respectively. In this case,
268 the accent and accentunder attributes are ignored. This is often
269 used for limits on symbols such as &sum;.
271 i.e.,:
272 if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishDataflags) &&
273 !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
274 // place like subscript-superscript pair
276 else {
277 // place like underscript-overscript pair
281 /* virtual */ nsresult
282 nsMathMLmunderoverFrame::Place(nsIRenderingContext& aRenderingContext,
283 PRBool aPlaceOrigin,
284 nsHTMLReflowMetrics& aDesiredSize)
286 if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
287 !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
288 // place like sub-superscript pair
289 return nsMathMLmsubsupFrame::PlaceSubSupScript(PresContext(),
290 aRenderingContext,
291 aPlaceOrigin,
292 aDesiredSize,
293 this, 0, 0, PresContext()->PointsToAppUnits(0.5f));
296 ////////////////////////////////////
297 // Get the children's desired sizes
299 nsBoundingMetrics bmBase, bmUnder, bmOver;
300 nsHTMLReflowMetrics baseSize;
301 nsHTMLReflowMetrics underSize;
302 nsHTMLReflowMetrics overSize;
303 nsIFrame* overFrame = nsnull;
304 nsIFrame* underFrame = nsnull;
305 nsIFrame* baseFrame = mFrames.FirstChild();
306 if (baseFrame)
307 underFrame = baseFrame->GetNextSibling();
308 if (underFrame)
309 overFrame = underFrame->GetNextSibling();
310 if (!baseFrame || !underFrame || !overFrame || overFrame->GetNextSibling()) {
311 // report an error, encourage people to get their markups in order
312 return ReflowError(aRenderingContext, aDesiredSize);
314 GetReflowAndBoundingMetricsFor(baseFrame, baseSize, bmBase);
315 GetReflowAndBoundingMetricsFor(underFrame, underSize, bmUnder);
316 GetReflowAndBoundingMetricsFor(overFrame, overSize, bmOver);
318 nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
320 ////////////////////
321 // Place Children
323 aRenderingContext.SetFont(GetStyleFont()->mFont, nsnull,
324 PresContext()->GetUserFontSet());
325 nsCOMPtr<nsIFontMetrics> fm;
326 aRenderingContext.GetFontMetrics(*getter_AddRefs(fm));
328 nscoord xHeight = 0;
329 fm->GetXHeight (xHeight);
331 nscoord ruleThickness;
332 GetRuleThickness (aRenderingContext, fm, ruleThickness);
334 nscoord correction = 0;
335 GetItalicCorrection (bmBase, correction);
337 // there are 2 different types of placement depending on
338 // whether we want an accented under or not
340 nscoord underDelta1 = 0; // gap between base and underscript
341 nscoord underDelta2 = 0; // extra space beneath underscript
343 if (!NS_MATHML_EMBELLISH_IS_ACCENTUNDER(mEmbellishData.flags)) {
344 // Rule 13a, App. G, TeXbook
345 nscoord bigOpSpacing2, bigOpSpacing4, bigOpSpacing5, dummy;
346 GetBigOpSpacings (fm,
347 dummy, bigOpSpacing2,
348 dummy, bigOpSpacing4,
349 bigOpSpacing5);
350 underDelta1 = NS_MAX(bigOpSpacing2, (bigOpSpacing4 - bmUnder.ascent));
351 underDelta2 = bigOpSpacing5;
353 else {
354 // No corresponding rule in TeXbook - we are on our own here
355 // XXX tune the gap delta between base and underscript
357 // Should we use Rule 10 like \underline does?
358 underDelta1 = ruleThickness + onePixel/2;
359 underDelta2 = ruleThickness;
361 // empty under?
362 if (!(bmUnder.ascent + bmUnder.descent)) underDelta1 = 0;
364 nscoord overDelta1 = 0; // gap between base and overscript
365 nscoord overDelta2 = 0; // extra space above overscript
367 if (!NS_MATHML_EMBELLISH_IS_ACCENTOVER(mEmbellishData.flags)) {
368 // Rule 13a, App. G, TeXbook
369 nscoord bigOpSpacing1, bigOpSpacing3, bigOpSpacing5, dummy;
370 GetBigOpSpacings (fm,
371 bigOpSpacing1, dummy,
372 bigOpSpacing3, dummy,
373 bigOpSpacing5);
374 overDelta1 = NS_MAX(bigOpSpacing1, (bigOpSpacing3 - bmOver.descent));
375 overDelta2 = bigOpSpacing5;
377 // XXX This is not a TeX rule...
378 // delta1 (as computed abvove) can become really big when bmOver.descent is
379 // negative, e.g., if the content is &OverBar. In such case, we use the height
380 if (bmOver.descent < 0)
381 overDelta1 = NS_MAX(bigOpSpacing1, (bigOpSpacing3 - (bmOver.ascent + bmOver.descent)));
383 else {
384 // Rule 12, App. G, TeXbook
385 overDelta1 = ruleThickness + onePixel/2;
386 if (bmBase.ascent < xHeight) {
387 overDelta1 += xHeight - bmBase.ascent;
389 overDelta2 = ruleThickness;
391 // empty over?
392 if (!(bmOver.ascent + bmOver.descent)) overDelta1 = 0;
394 nscoord dxBase, dxOver = 0, dxUnder = 0;
396 //////////
397 // pass 1, do what <mover> does: attach the overscript on the base
399 // Ad-hoc - This is to override fonts which have ready-made _accent_
400 // glyphs with negative lbearing and rbearing. We want to position
401 // the overscript ourselves
402 nscoord overWidth = bmOver.width;
403 if (!overWidth && (bmOver.rightBearing - bmOver.leftBearing > 0)) {
404 overWidth = bmOver.rightBearing - bmOver.leftBearing;
405 dxOver = -bmOver.leftBearing;
408 if (NS_MATHML_EMBELLISH_IS_ACCENTOVER(mEmbellishData.flags)) {
409 mBoundingMetrics.width = bmBase.width;
410 dxOver += correction + (mBoundingMetrics.width - overWidth)/2;
412 else {
413 mBoundingMetrics.width = NS_MAX(bmBase.width, overWidth);
414 dxOver += correction/2 + (mBoundingMetrics.width - overWidth)/2;
416 dxBase = (mBoundingMetrics.width - bmBase.width)/2;
418 mBoundingMetrics.ascent =
419 bmBase.ascent + overDelta1 + bmOver.ascent + bmOver.descent;
420 mBoundingMetrics.descent =
421 bmBase.descent + underDelta1 + bmUnder.ascent + bmUnder.descent;
422 mBoundingMetrics.leftBearing =
423 NS_MIN(dxBase + bmBase.leftBearing, dxOver + bmOver.leftBearing);
424 mBoundingMetrics.rightBearing =
425 NS_MAX(dxBase + bmBase.rightBearing, dxOver + bmOver.rightBearing);
427 //////////
428 // pass 2, do what <munder> does: attach the underscript on the previous
429 // result. We conceptually view the previous result as an "anynomous base"
430 // from where to attach the underscript. Hence if the underscript is empty,
431 // we should end up like <mover>. If the overscript is empty, we should
432 // end up like <munder>.
434 nsBoundingMetrics bmAnonymousBase = mBoundingMetrics;
435 nscoord ascentAnonymousBase =
436 NS_MAX(mBoundingMetrics.ascent + overDelta2,
437 overSize.ascent + bmOver.descent + overDelta1 + bmBase.ascent);
439 GetItalicCorrection(bmAnonymousBase, correction);
441 // Width of non-spacing marks is zero so use left and right bearing.
442 nscoord underWidth = bmUnder.width;
443 if (!underWidth) {
444 underWidth = bmUnder.rightBearing - bmUnder.leftBearing;
445 dxUnder = -bmUnder.leftBearing;
448 nscoord maxWidth = NS_MAX(bmAnonymousBase.width, underWidth);
449 if (NS_MATHML_EMBELLISH_IS_ACCENTUNDER(mEmbellishData.flags)) {
450 dxUnder += (maxWidth - underWidth)/2;;
452 else {
453 dxUnder += -correction/2 + (maxWidth - underWidth)/2;
455 nscoord dxAnonymousBase = (maxWidth - bmAnonymousBase.width)/2;
457 // adjust the offsets of the real base and overscript since their
458 // final offsets should be relative to us...
459 dxOver += dxAnonymousBase;
460 dxBase += dxAnonymousBase;
462 mBoundingMetrics.width =
463 NS_MAX(dxAnonymousBase + bmAnonymousBase.width, dxUnder + bmUnder.width);
464 mBoundingMetrics.leftBearing =
465 NS_MIN(dxAnonymousBase + bmAnonymousBase.leftBearing, dxUnder + bmUnder.leftBearing);
466 mBoundingMetrics.rightBearing =
467 NS_MAX(dxAnonymousBase + bmAnonymousBase.rightBearing, dxUnder + bmUnder.rightBearing);
469 aDesiredSize.ascent = ascentAnonymousBase;
470 aDesiredSize.height = aDesiredSize.ascent +
471 NS_MAX(mBoundingMetrics.descent + underDelta2,
472 bmAnonymousBase.descent + underDelta1 + bmUnder.ascent +
473 underSize.height - underSize.ascent);
474 aDesiredSize.width = mBoundingMetrics.width;
475 aDesiredSize.mBoundingMetrics = mBoundingMetrics;
477 mReference.x = 0;
478 mReference.y = aDesiredSize.ascent;
480 if (aPlaceOrigin) {
481 nscoord dy;
482 // place overscript
483 dy = aDesiredSize.ascent - mBoundingMetrics.ascent + bmOver.ascent - overSize.ascent;
484 FinishReflowChild (overFrame, PresContext(), nsnull, overSize, dxOver, dy, 0);
485 // place base
486 dy = aDesiredSize.ascent - baseSize.ascent;
487 FinishReflowChild (baseFrame, PresContext(), nsnull, baseSize, dxBase, dy, 0);
488 // place underscript
489 dy = aDesiredSize.ascent + mBoundingMetrics.descent - bmUnder.descent - underSize.ascent;
490 FinishReflowChild (underFrame, PresContext(), nsnull, underSize, dxUnder, dy, 0);
492 return NS_OK;