1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsMathMLContainerFrame_h___
7 #define nsMathMLContainerFrame_h___
9 #include "mozilla/Attributes.h"
10 #include "nsContainerFrame.h"
11 #include "nsBlockFrame.h"
12 #include "nsInlineFrame.h"
13 #include "nsMathMLOperators.h"
14 #include "nsMathMLFrame.h"
15 #include "mozilla/Likely.h"
18 * Base class for MathML container frames. It acts like an inferred
19 * mrow. By default, this frame uses its Reflow() method to lay its
20 * children horizontally and ensure that their baselines are aligned.
21 * The Reflow() method relies upon Place() to position children.
22 * By overloading Place() in derived classes, it is therefore possible
23 * to position children in various customized ways.
26 // Options for the preferred size at which to stretch our stretchy children
27 #define STRETCH_CONSIDER_ACTUAL_SIZE 0x00000001 // just use our current size
28 #define STRETCH_CONSIDER_EMBELLISHMENTS 0x00000002 // size calculations include embellishments
30 class nsMathMLContainerFrame
: public nsContainerFrame
,
31 public nsMathMLFrame
{
32 friend class nsMathMLmfencedFrame
;
34 explicit nsMathMLContainerFrame(nsStyleContext
* aContext
) : nsContainerFrame(aContext
) {}
36 NS_DECL_QUERYFRAME_TARGET(nsMathMLContainerFrame
)
38 NS_DECL_FRAMEARENA_HELPERS
40 // --------------------------------------------------------------------------
41 // Overloaded nsMathMLFrame methods -- see documentation in nsIMathMLFrame.h
44 Stretch(nsRenderingContext
& aRenderingContext
,
45 nsStretchDirection aStretchDirection
,
46 nsBoundingMetrics
& aContainerSize
,
47 nsHTMLReflowMetrics
& aDesiredStretchSize
) MOZ_OVERRIDE
;
50 UpdatePresentationDataFromChildAt(int32_t aFirstIndex
,
52 uint32_t aFlagsValues
,
53 uint32_t aFlagsToUpdate
) MOZ_OVERRIDE
55 PropagatePresentationDataFromChildAt(this, aFirstIndex
, aLastIndex
,
56 aFlagsValues
, aFlagsToUpdate
);
60 // helper to set the "increment script level" flag on the element belonging
61 // to a child frame given by aChildIndex.
62 // When this flag is set, the style system will increment the scriptlevel
63 // for the child element. This is needed for situations where the style system
64 // cannot itself determine the scriptlevel (mfrac, munder, mover, munderover).
65 // This should be called during reflow. We set the flag and if it changed,
66 // we request appropriate restyling and also queue a post-reflow callback
67 // to ensure that restyle and reflow happens immediately after the current
70 SetIncrementScriptLevel(int32_t aChildIndex
, bool aIncrement
);
72 // --------------------------------------------------------------------------
73 // Overloaded nsContainerFrame methods -- see documentation in nsIFrame.h
75 virtual bool IsFrameOfType(uint32_t aFlags
) const MOZ_OVERRIDE
77 return !(aFlags
& nsIFrame::eLineParticipant
) &&
78 nsContainerFrame::IsFrameOfType(aFlags
&
79 ~(nsIFrame::eMathML
| nsIFrame::eExcludesIgnorableWhitespace
));
83 AppendFrames(ChildListID aListID
,
84 nsFrameList
& aFrameList
) MOZ_OVERRIDE
;
87 InsertFrames(ChildListID aListID
,
89 nsFrameList
& aFrameList
) MOZ_OVERRIDE
;
92 RemoveFrame(ChildListID aListID
,
93 nsIFrame
* aOldFrame
) MOZ_OVERRIDE
;
96 * Both GetMinISize and GetPrefISize use the intrinsic width metrics
97 * returned by GetIntrinsicMetrics, including ink overflow.
99 virtual nscoord
GetMinISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
100 virtual nscoord
GetPrefISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
103 * Return the intrinsic horizontal metrics of the frame's content area.
106 GetIntrinsicISizeMetrics(nsRenderingContext
* aRenderingContext
,
107 nsHTMLReflowMetrics
& aDesiredSize
);
110 Reflow(nsPresContext
* aPresContext
,
111 nsHTMLReflowMetrics
& aDesiredSize
,
112 const nsHTMLReflowState
& aReflowState
,
113 nsReflowStatus
& aStatus
) MOZ_OVERRIDE
;
116 WillReflow(nsPresContext
* aPresContext
) MOZ_OVERRIDE
118 mPresentationData
.flags
&= ~NS_MATHML_ERROR
;
119 nsContainerFrame::WillReflow(aPresContext
);
122 virtual void DidReflow(nsPresContext
* aPresContext
,
123 const nsHTMLReflowState
* aReflowState
,
124 nsDidReflowStatus aStatus
) MOZ_OVERRIDE
127 mPresentationData
.flags
&= ~NS_MATHML_STRETCH_DONE
;
128 return nsContainerFrame::DidReflow(aPresContext
, aReflowState
, aStatus
);
131 virtual void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
132 const nsRect
& aDirtyRect
,
133 const nsDisplayListSet
& aLists
) MOZ_OVERRIDE
;
135 virtual bool UpdateOverflow() MOZ_OVERRIDE
;
137 // Notification when an attribute is changed. The MathML module uses the
138 // following paradigm:
140 // 1. If the MathML frame class doesn't have any cached automatic data that
141 // depends on the attribute: we just reflow (e.g., this happens with <msub>,
142 // <msup>, <mmultiscripts>, etc). This is the default behavior implemented
143 // by this base class.
145 // 2. If the MathML frame class has cached automatic data that depends on
147 // 2a. If the automatic data to update resides only within the descendants,
148 // we just re-layout them using ReLayoutChildren(this);
149 // (e.g., this happens with <ms>).
150 // 2b. If the automatic data to update affects us in some way, we ask our parent
151 // to re-layout its children using ReLayoutChildren(mParent);
152 // Therefore, there is an overhead here in that our siblings are re-laid
153 // too (e.g., this happens with <munder>, <mover>, <munderover>).
155 AttributeChanged(int32_t aNameSpaceID
,
157 int32_t aModType
) MOZ_OVERRIDE
;
159 // helper function to apply mirroring to a horizontal coordinate, if needed.
161 MirrorIfRTL(nscoord aParentWidth
, nscoord aChildWidth
, nscoord aChildLeading
)
163 return (StyleVisibility()->mDirection
?
164 aParentWidth
- aChildWidth
- aChildLeading
: aChildLeading
);
167 // --------------------------------------------------------------------------
168 // Additional methods
172 * This method is used to measure or position child frames and other
173 * elements. It may be called any number of times with aPlaceOrigin
174 * false to measure, and the final call of the Reflow process before
175 * returning from Reflow() or Stretch() will have aPlaceOrigin true
176 * to position the elements.
178 * IMPORTANT: This method uses GetReflowAndBoundingMetricsFor() which must
179 * have been set up with SaveReflowAndBoundingMetricsFor().
181 * The Place() method will use this information to compute the desired size
184 * @param aPlaceOrigin [in]
185 * If aPlaceOrigin is false, compute your desired size using the
186 * information from GetReflowAndBoundingMetricsFor. However, child
187 * frames or other elements should not be repositioned.
189 * If aPlaceOrigin is true, reflow is finished. You should position
190 * all your children, and return your desired size. You should now
191 * use FinishReflowChild() on your children to complete post-reflow
194 * @param aDesiredSize [out] parameter where you should return your desired
195 * size and your ascent/descent info. Compute your desired size using
196 * the information from GetReflowAndBoundingMetricsFor, and include
197 * any space you want for border/padding in the desired size you
201 Place(nsRenderingContext
& aRenderingContext
,
203 nsHTMLReflowMetrics
& aDesiredSize
);
207 // A method used by nsMathMLContainerFrame::GetIntrinsicISize to get the
208 // width that a particular Place method desires. For most frames, this will
209 // just call the object's Place method. However <msqrt> and <menclose> use
210 // nsMathMLContainerFrame::GetIntrinsicISize to measure the child frames as
211 // if in an <mrow>, and so their frames implement MeasureForWidth to use
212 // nsMathMLContainerFrame::Place.
214 MeasureForWidth(nsRenderingContext
& aRenderingContext
,
215 nsHTMLReflowMetrics
& aDesiredSize
);
218 // helper to re-sync the automatic data in our children and notify our parent to
219 // reflow us when changes (e.g., append/insert/remove) happen in our child list
221 ChildListChanged(int32_t aModType
);
223 // helper to get the preferred size that a container frame should use to fire
224 // the stretch on its stretchy child frames.
226 GetPreferredStretchSize(nsRenderingContext
& aRenderingContext
,
228 nsStretchDirection aStretchDirection
,
229 nsBoundingMetrics
& aPreferredStretchSize
);
231 // helper used by mstyle, mphantom, mpadded and mrow in their implementation
232 // of TransmitAutomaticData() to determine whether they are space-like.
234 TransmitAutomaticDataForMrowLikeElement();
237 // error handlers to provide a visual feedback to the user when an error
238 // (typically invalid markup) was encountered during reflow.
240 ReflowError(nsRenderingContext
& aRenderingContext
,
241 nsHTMLReflowMetrics
& aDesiredSize
);
243 * Helper to call ReportErrorToConsole for parse errors involving
244 * attribute/value pairs.
245 * @param aAttribute The attribute for which the parse error occured.
246 * @param aValue The value for which the parse error occured.
249 ReportParseError(const char16_t
* aAttribute
,
250 const char16_t
* aValue
);
253 * Helper to call ReportErrorToConsole when certain tags
254 * have more than the expected amount of children.
257 ReportChildCountError();
260 * Helper to call ReportErrorToConsole when certain tags have
262 * @param aChildTag The tag which is forbidden in this context
265 ReportInvalidChildError(nsIAtom
* aChildTag
);
268 * Helper to call ReportToConsole when an error occurs.
269 * @param aParams see nsContentUtils::ReportToConsole
272 ReportErrorToConsole(const char* aErrorMsgId
,
273 const char16_t
** aParams
= nullptr,
274 uint32_t aParamCount
= 0);
276 // helper method to reflow a child frame. We are inline frames, and we don't
277 // know our positions until reflow is finished. That's why we ask the
278 // base method not to worry about our position.
280 ReflowChild(nsIFrame
* aKidFrame
,
281 nsPresContext
* aPresContext
,
282 nsHTMLReflowMetrics
& aDesiredSize
,
283 const nsHTMLReflowState
& aReflowState
,
284 nsReflowStatus
& aStatus
);
287 // helper to add the inter-spacing when <math> is the immediate parent.
288 // Since we don't (yet) handle the root <math> element ourselves, we need to
289 // take special care of the inter-frame spacing on elements for which <math>
290 // is the direct xml parent. This function will be repeatedly called from
291 // left to right on the childframes of <math>, and by so doing it will
292 // emulate the spacing that would have been done by a <mrow> container.
293 // e.g., it fixes <math> <mi>f</mi> <mo>q</mo> <mi>f</mi> <mo>I</mo> </math>
295 FixInterFrameSpacing(nsHTMLReflowMetrics
& aDesiredSize
);
297 // helper method to complete the post-reflow hook and ensure that embellished
298 // operators don't terminate their Reflow without receiving a Stretch command.
300 FinalizeReflow(nsRenderingContext
& aRenderingContext
,
301 nsHTMLReflowMetrics
& aDesiredSize
);
303 // Record metrics of a child frame for recovery through the following method
305 SaveReflowAndBoundingMetricsFor(nsIFrame
* aFrame
,
306 const nsHTMLReflowMetrics
& aReflowMetrics
,
307 const nsBoundingMetrics
& aBoundingMetrics
);
309 // helper method to facilitate getting the reflow and bounding metrics of a
310 // child frame. The argument aMathMLFrameType, when non null, will return
311 // the 'type' of the frame, which is used to determine the inter-frame
313 // IMPORTANT: This function is only meant to be called in Place() methods as
314 // the information is available only when set up with the above method
315 // during Reflow/Stretch() and GetPrefISize().
317 GetReflowAndBoundingMetricsFor(nsIFrame
* aFrame
,
318 nsHTMLReflowMetrics
& aReflowMetrics
,
319 nsBoundingMetrics
& aBoundingMetrics
,
320 eMathMLFrameType
* aMathMLFrameType
= nullptr);
322 // helper method to clear metrics saved with
323 // SaveReflowAndBoundingMetricsFor() from all child frames.
324 void ClearSavedChildMetrics();
326 // helper to let the update of presentation data pass through
327 // a subtree that may contain non-MathML container frames
329 PropagatePresentationDataFor(nsIFrame
* aFrame
,
330 uint32_t aFlagsValues
,
331 uint32_t aFlagsToUpdate
);
335 PropagatePresentationDataFromChildAt(nsIFrame
* aParentFrame
,
336 int32_t aFirstChildIndex
,
337 int32_t aLastChildIndex
,
338 uint32_t aFlagsValues
,
339 uint32_t aFlagsToUpdate
);
341 // Sets flags on aFrame and all descendant frames
343 PropagateFrameFlagFor(nsIFrame
* aFrame
,
344 nsFrameState aFlags
);
346 // helper to let the rebuild of automatic data (presentation data
347 // and embellishement data) walk through a subtree that may contain
348 // non-MathML container frames. Note that this method re-builds the
349 // automatic data in the children -- not in aParentFrame itself (except
350 // for those particular operations that the parent frame may do in its
351 // TransmitAutomaticData()). The reason it works this way is because
352 // a container frame knows what it wants for its children, whereas children
353 // have no clue who their parent is. For example, it is <mfrac> who knows
354 // that its children have to be in scriptsizes, and has to transmit this
355 // information to them. Hence, when changes occur in a child frame, the child
356 // has to request the re-build from its parent. Unfortunately, the extra cost
357 // for this is that it will re-sync in the siblings of the child as well.
359 RebuildAutomaticDataForChildren(nsIFrame
* aParentFrame
);
361 // helper to blow away the automatic data cached in a frame's subtree and
362 // re-layout its subtree to reflect changes that may have happen. In the
363 // event where aParentFrame isn't a MathML frame, it will first walk up to
364 // the ancestor that is a MathML frame, and re-layout from there -- this is
365 // to guarantee that automatic data will be rebuilt properly. Note that this
366 // method re-builds the automatic data in the children -- not in the parent
367 // frame itself (except for those particular operations that the parent frame
368 // may do do its TransmitAutomaticData()). @see RebuildAutomaticDataForChildren
370 // aBits are the bits to pass to FrameNeedsReflow() when we call it.
372 ReLayoutChildren(nsIFrame
* aParentFrame
);
375 // Helper method which positions child frames as an <mrow> on given baseline
376 // y = aBaseline starting from x = aOffsetX, calling FinishReflowChild()
379 PositionRowChildFrames(nscoord aOffsetX
, nscoord aBaseline
);
381 // A variant on FinishAndStoreOverflow() that uses the union of child
382 // overflows, the frame bounds, and mBoundingMetrics to set and store the
384 void GatherAndStoreOverflow(nsHTMLReflowMetrics
* aMetrics
);
387 * Call DidReflow() if the NS_FRAME_IN_REFLOW frame bit is set on aFirst and
388 * all its next siblings up to, but not including, aStop.
389 * aStop == nullptr meaning all next siblings with the bit set.
390 * The method does nothing if aFirst == nullptr.
392 static void DidReflowChildren(nsIFrame
* aFirst
, nsIFrame
* aStop
= nullptr);
395 class RowChildFrameIterator
;
396 friend class RowChildFrameIterator
;
400 // --------------------------------------------------------------------------
401 // Currently, to benefit from line-breaking inside the <math> element, <math> is
402 // simply mapping to nsBlockFrame or nsInlineFrame.
403 // A separate implemention needs to provide:
405 // 2) proper inter-frame spacing
406 // 3) firing of Stretch() (in which case FinalizeReflow() would have to be cleaned)
407 // Issues: If/when mathml becomes a pluggable component, the separation will be needed.
408 class nsMathMLmathBlockFrame
: public nsBlockFrame
{
410 NS_DECL_QUERYFRAME_TARGET(nsMathMLmathBlockFrame
)
412 NS_DECL_FRAMEARENA_HELPERS
414 friend nsContainerFrame
* NS_NewMathMLmathBlockFrame(nsIPresShell
* aPresShell
,
415 nsStyleContext
* aContext
, nsFrameState aFlags
);
417 // beware, mFrames is not set by nsBlockFrame
418 // cannot use mFrames{.FirstChild()|.etc} since the block code doesn't set mFrames
420 SetInitialChildList(ChildListID aListID
,
421 nsFrameList
& aChildList
) MOZ_OVERRIDE
423 NS_ASSERTION(aListID
== kPrincipalList
, "unexpected frame list");
424 nsBlockFrame::SetInitialChildList(aListID
, aChildList
);
425 // re-resolve our subtree to set any mathml-expected data
426 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
430 AppendFrames(ChildListID aListID
,
431 nsFrameList
& aFrameList
) MOZ_OVERRIDE
433 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
434 "unexpected frame list");
435 nsBlockFrame::AppendFrames(aListID
, aFrameList
);
436 if (MOZ_LIKELY(aListID
== kPrincipalList
))
437 nsMathMLContainerFrame::ReLayoutChildren(this);
441 InsertFrames(ChildListID aListID
,
442 nsIFrame
* aPrevFrame
,
443 nsFrameList
& aFrameList
) MOZ_OVERRIDE
445 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
446 "unexpected frame list");
447 nsBlockFrame::InsertFrames(aListID
, aPrevFrame
, aFrameList
);
448 if (MOZ_LIKELY(aListID
== kPrincipalList
))
449 nsMathMLContainerFrame::ReLayoutChildren(this);
453 RemoveFrame(ChildListID aListID
,
454 nsIFrame
* aOldFrame
) MOZ_OVERRIDE
456 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
457 "unexpected frame list");
458 nsBlockFrame::RemoveFrame(aListID
, aOldFrame
);
459 if (MOZ_LIKELY(aListID
== kPrincipalList
))
460 nsMathMLContainerFrame::ReLayoutChildren(this);
463 virtual bool IsFrameOfType(uint32_t aFlags
) const MOZ_OVERRIDE
{
464 return nsBlockFrame::IsFrameOfType(aFlags
&
465 ~(nsIFrame::eMathML
| nsIFrame::eExcludesIgnorableWhitespace
));
468 // See nsIMathMLFrame.h
470 return mFrames
.FirstChild() != mFrames
.LastChild() ||
471 !mFrames
.FirstChild();
475 explicit nsMathMLmathBlockFrame(nsStyleContext
* aContext
) : nsBlockFrame(aContext
) {
476 // We should always have a float manager. Not that things can really try
477 // to float out of us anyway, but we need one for line layout.
478 AddStateBits(NS_BLOCK_FLOAT_MGR
);
480 virtual ~nsMathMLmathBlockFrame() {}
485 class nsMathMLmathInlineFrame
: public nsInlineFrame
,
486 public nsMathMLFrame
{
488 NS_DECL_QUERYFRAME_TARGET(nsMathMLmathInlineFrame
)
490 NS_DECL_FRAMEARENA_HELPERS
492 friend nsContainerFrame
* NS_NewMathMLmathInlineFrame(nsIPresShell
* aPresShell
,
493 nsStyleContext
* aContext
);
496 SetInitialChildList(ChildListID aListID
,
497 nsFrameList
& aChildList
) MOZ_OVERRIDE
499 NS_ASSERTION(aListID
== kPrincipalList
, "unexpected frame list");
500 nsInlineFrame::SetInitialChildList(aListID
, aChildList
);
501 // re-resolve our subtree to set any mathml-expected data
502 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
506 AppendFrames(ChildListID aListID
,
507 nsFrameList
& aFrameList
) MOZ_OVERRIDE
509 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
510 "unexpected frame list");
511 nsInlineFrame::AppendFrames(aListID
, aFrameList
);
512 if (MOZ_LIKELY(aListID
== kPrincipalList
))
513 nsMathMLContainerFrame::ReLayoutChildren(this);
517 InsertFrames(ChildListID aListID
,
518 nsIFrame
* aPrevFrame
,
519 nsFrameList
& aFrameList
) MOZ_OVERRIDE
521 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
522 "unexpected frame list");
523 nsInlineFrame::InsertFrames(aListID
, aPrevFrame
, aFrameList
);
524 if (MOZ_LIKELY(aListID
== kPrincipalList
))
525 nsMathMLContainerFrame::ReLayoutChildren(this);
529 RemoveFrame(ChildListID aListID
,
530 nsIFrame
* aOldFrame
) MOZ_OVERRIDE
532 NS_ASSERTION(aListID
== kPrincipalList
|| aListID
== kNoReflowPrincipalList
,
533 "unexpected frame list");
534 nsInlineFrame::RemoveFrame(aListID
, aOldFrame
);
535 if (MOZ_LIKELY(aListID
== kPrincipalList
))
536 nsMathMLContainerFrame::ReLayoutChildren(this);
539 virtual bool IsFrameOfType(uint32_t aFlags
) const MOZ_OVERRIDE
{
540 return nsInlineFrame::IsFrameOfType(aFlags
&
541 ~(nsIFrame::eMathML
| nsIFrame::eExcludesIgnorableWhitespace
));
545 IsMrowLike() MOZ_OVERRIDE
{
546 return mFrames
.FirstChild() != mFrames
.LastChild() ||
547 !mFrames
.FirstChild();
551 explicit nsMathMLmathInlineFrame(nsStyleContext
* aContext
) : nsInlineFrame(aContext
) {}
552 virtual ~nsMathMLmathInlineFrame() {}
555 #endif /* nsMathMLContainerFrame_h___ */