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/. */
7 #ifndef nsMathMLContainerFrame_h___
8 #define nsMathMLContainerFrame_h___
10 #include "mozilla/Attributes.h"
11 #include "nsContainerFrame.h"
12 #include "nsBlockFrame.h"
13 #include "nsInlineFrame.h"
14 #include "nsMathMLOperators.h"
15 #include "nsMathMLFrame.h"
16 #include "mozilla/Likely.h"
20 } // namespace mozilla
23 * Base class for MathML container frames. It acts like an inferred
24 * mrow. By default, this frame uses its Reflow() method to lay its
25 * children horizontally and ensure that their baselines are aligned.
26 * The Reflow() method relies upon Place() to position children.
27 * By overloading Place() in derived classes, it is therefore possible
28 * to position children in various customized ways.
31 // Options for the preferred size at which to stretch our stretchy children
32 #define STRETCH_CONSIDER_ACTUAL_SIZE 0x00000001 // just use our current size
33 #define STRETCH_CONSIDER_EMBELLISHMENTS \
34 0x00000002 // size calculations include embellishments
36 class nsMathMLContainerFrame
: public nsContainerFrame
, public nsMathMLFrame
{
38 nsMathMLContainerFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
,
40 : nsContainerFrame(aStyle
, aPresContext
, aID
),
41 mIntrinsicWidth(NS_INTRINSIC_ISIZE_UNKNOWN
),
42 mBlockStartAscent(0) {}
44 NS_DECL_QUERYFRAME_TARGET(nsMathMLContainerFrame
)
46 NS_DECL_ABSTRACT_FRAME(nsMathMLContainerFrame
)
48 // --------------------------------------------------------------------------
49 // Overloaded nsMathMLFrame methods -- see documentation in nsIMathMLFrame.h
52 Stretch(DrawTarget
* aDrawTarget
, nsStretchDirection aStretchDirection
,
53 nsBoundingMetrics
& aContainerSize
,
54 ReflowOutput
& aDesiredStretchSize
) override
;
57 UpdatePresentationDataFromChildAt(int32_t aFirstIndex
, int32_t aLastIndex
,
58 uint32_t aFlagsValues
,
59 uint32_t aFlagsToUpdate
) override
{
60 PropagatePresentationDataFromChildAt(this, aFirstIndex
, aLastIndex
,
61 aFlagsValues
, aFlagsToUpdate
);
65 // --------------------------------------------------------------------------
66 // Overloaded nsContainerFrame methods -- see documentation in nsIFrame.h
68 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
;
70 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
71 const nsLineList::iterator
* aPrevFrameLine
,
72 nsFrameList
&& aFrameList
) override
;
74 void RemoveFrame(DestroyContext
&, ChildListID aListID
,
75 nsIFrame
* aOldFrame
) override
;
78 * Both GetMinISize and GetPrefISize use the intrinsic width metrics
79 * returned by GetIntrinsicMetrics, including ink overflow.
81 nscoord
GetMinISize(gfxContext
* aRenderingContext
) override
;
82 nscoord
GetPrefISize(gfxContext
* aRenderingContext
) override
;
85 * Return the intrinsic horizontal metrics of the frame's content area.
87 virtual void GetIntrinsicISizeMetrics(gfxContext
* aRenderingContext
,
88 ReflowOutput
& aDesiredSize
);
90 void Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aDesiredSize
,
91 const ReflowInput
& aReflowInput
,
92 nsReflowStatus
& aStatus
) override
;
94 void DidReflow(nsPresContext
* aPresContext
,
95 const ReflowInput
* aReflowInput
) override
98 mPresentationData
.flags
&= ~NS_MATHML_STRETCH_DONE
;
99 return nsContainerFrame::DidReflow(aPresContext
, aReflowInput
);
102 void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
103 const nsDisplayListSet
& aLists
) override
;
105 bool ComputeCustomOverflow(mozilla::OverflowAreas
& aOverflowAreas
) override
;
107 void MarkIntrinsicISizesDirty() override
;
109 // Notification when an attribute is changed. The MathML module uses the
110 // following paradigm:
112 // 1. If the MathML frame class doesn't have any cached automatic data that
113 // depends on the attribute: we just reflow (e.g., this happens with
114 // <msub>, <msup>, <mmultiscripts>, etc). This is the default behavior
115 // implemented by this base class.
117 // 2. If the MathML frame class has cached automatic data that depends on
119 // 2a. If the automatic data to update resides only within the descendants,
120 // we just re-layout them using ReLayoutChildren(this);
121 // (e.g., this happens with <ms>).
122 // 2b. If the automatic data to update affects us in some way, we ask our
123 // parent to re-layout its children using ReLayoutChildren(mParent);
124 // Therefore, there is an overhead here in that our siblings are
125 // re-laid too (e.g., this happens with <munder>, <mover>,
127 nsresult
AttributeChanged(int32_t aNameSpaceID
, nsAtom
* aAttribute
,
128 int32_t aModType
) override
;
130 // helper function to apply mirroring to a horizontal coordinate, if needed.
131 nscoord
MirrorIfRTL(nscoord aParentWidth
, nscoord aChildWidth
,
132 nscoord aChildLeading
) {
133 return StyleVisibility()->mDirection
== mozilla::StyleDirection::Rtl
134 ? aParentWidth
- aChildWidth
- aChildLeading
138 // --------------------------------------------------------------------------
139 // Additional methods
143 * This method is used to measure or position child frames and other
144 * elements. It may be called any number of times with aPlaceOrigin
145 * false to measure, and the final call of the Reflow process before
146 * returning from Reflow() or Stretch() will have aPlaceOrigin true
147 * to position the elements.
149 * IMPORTANT: This method uses GetReflowAndBoundingMetricsFor() which must
150 * have been set up with SaveReflowAndBoundingMetricsFor().
152 * The Place() method will use this information to compute the desired size
155 * @param aPlaceOrigin [in]
156 * If aPlaceOrigin is false, compute your desired size using the
157 * information from GetReflowAndBoundingMetricsFor. However, child
158 * frames or other elements should not be repositioned.
160 * If aPlaceOrigin is true, reflow is finished. You should position
161 * all your children, and return your desired size. You should now
162 * use FinishReflowChild() on your children to complete post-reflow
165 * @param aDesiredSize [out] parameter where you should return your desired
166 * size and your ascent/descent info. Compute your desired size using
167 * the information from GetReflowAndBoundingMetricsFor, and include
168 * any space you want for border/padding in the desired size you
171 virtual nsresult
Place(DrawTarget
* aDrawTarget
, bool aPlaceOrigin
,
172 ReflowOutput
& aDesiredSize
);
176 // A method used by nsMathMLContainerFrame::GetIntrinsicISize to get the
177 // width that a particular Place method desires. For most frames, this will
178 // just call the object's Place method. However <msqrt> and <menclose> use
179 // nsMathMLContainerFrame::GetIntrinsicISize to measure the child frames as
180 // if in an <mrow>, and so their frames implement MeasureForWidth to use
181 // nsMathMLContainerFrame::Place.
182 virtual nsresult
MeasureForWidth(DrawTarget
* aDrawTarget
,
183 ReflowOutput
& aDesiredSize
);
185 // helper to re-sync the automatic data in our children and notify our parent
186 // to reflow us when changes (e.g., append/insert/remove) happen in our child
188 virtual nsresult
ChildListChanged(int32_t aModType
);
190 // helper to get the preferred size that a container frame should use to fire
191 // the stretch on its stretchy child frames.
192 void GetPreferredStretchSize(DrawTarget
* aDrawTarget
, uint32_t aOptions
,
193 nsStretchDirection aStretchDirection
,
194 nsBoundingMetrics
& aPreferredStretchSize
);
196 // helper used by mstyle, mphantom, mpadded and mrow in their implementation
197 // of TransmitAutomaticData() to determine whether they are space-like.
198 nsresult
TransmitAutomaticDataForMrowLikeElement();
202 * Helper to render the frame as a default mrow-like container when an error
203 * (typically invalid markup) was encountered during reflow. Parameters are
204 * the same as Place().
206 nsresult
PlaceAsMrow(DrawTarget
* aDrawTarget
, bool aPlaceOrigin
,
207 ReflowOutput
& aDesiredSize
);
210 * Helper to call ReportErrorToConsole for parse errors involving
211 * attribute/value pairs.
212 * @param aAttribute The attribute for which the parse error occured.
213 * @param aValue The value for which the parse error occured.
215 nsresult
ReportParseError(const char16_t
* aAttribute
, const char16_t
* aValue
);
218 * Helper to call ReportErrorToConsole when certain tags
219 * have more than the expected amount of children.
221 nsresult
ReportChildCountError();
224 * Helper to call ReportErrorToConsole when certain tags have
226 * @param aChildTag The tag which is forbidden in this context
228 nsresult
ReportInvalidChildError(nsAtom
* aChildTag
);
231 * Helper to call ReportToConsole when an error occurs.
232 * @param aParams see nsContentUtils::ReportToConsole
234 nsresult
ReportErrorToConsole(
235 const char* aErrorMsgId
,
236 const nsTArray
<nsString
>& aParams
= nsTArray
<nsString
>());
238 // helper method to reflow a child frame. We are inline frames, and we don't
239 // know our positions until reflow is finished. That's why we ask the
240 // base method not to worry about our position.
241 void ReflowChild(nsIFrame
* aKidFrame
, nsPresContext
* aPresContext
,
242 ReflowOutput
& aDesiredSize
, const ReflowInput
& aReflowInput
,
243 nsReflowStatus
& aStatus
);
246 // helper to add the inter-spacing when <math> is the immediate parent.
247 // Since we don't (yet) handle the root <math> element ourselves, we need to
248 // take special care of the inter-frame spacing on elements for which <math>
249 // is the direct xml parent. This function will be repeatedly called from
250 // left to right on the childframes of <math>, and by so doing it will
251 // emulate the spacing that would have been done by a <mrow> container.
252 // e.g., it fixes <math> <mi>f</mi> <mo>q</mo> <mi>f</mi> <mo>I</mo> </math>
253 virtual nscoord
FixInterFrameSpacing(ReflowOutput
& aDesiredSize
);
255 // helper method to complete the post-reflow hook and ensure that embellished
256 // operators don't terminate their Reflow without receiving a Stretch command.
257 virtual nsresult
FinalizeReflow(DrawTarget
* aDrawTarget
,
258 ReflowOutput
& aDesiredSize
);
260 // Record metrics of a child frame for recovery through the following method
261 static void SaveReflowAndBoundingMetricsFor(
262 nsIFrame
* aFrame
, const ReflowOutput
& aReflowOutput
,
263 const nsBoundingMetrics
& aBoundingMetrics
);
265 // helper method to facilitate getting the reflow and bounding metrics of a
266 // child frame. The argument aMathMLFrameType, when non null, will return
267 // the 'type' of the frame, which is used to determine the inter-frame
269 // IMPORTANT: This function is only meant to be called in Place() methods as
270 // the information is available only when set up with the above method
271 // during Reflow/Stretch() and GetPrefISize().
272 static void GetReflowAndBoundingMetricsFor(
273 nsIFrame
* aFrame
, ReflowOutput
& aReflowOutput
,
274 nsBoundingMetrics
& aBoundingMetrics
,
275 eMathMLFrameType
* aMathMLFrameType
= nullptr);
277 // helper method to clear metrics saved with
278 // SaveReflowAndBoundingMetricsFor() from all child frames.
279 void ClearSavedChildMetrics();
281 // helper to let the update of presentation data pass through
282 // a subtree that may contain non-MathML container frames
283 static void PropagatePresentationDataFor(nsIFrame
* aFrame
,
284 uint32_t aFlagsValues
,
285 uint32_t aFlagsToUpdate
);
288 static void PropagatePresentationDataFromChildAt(nsIFrame
* aParentFrame
,
289 int32_t aFirstChildIndex
,
290 int32_t aLastChildIndex
,
291 uint32_t aFlagsValues
,
292 uint32_t aFlagsToUpdate
);
294 // Sets flags on aFrame and all descendant frames
295 static void PropagateFrameFlagFor(nsIFrame
* aFrame
, nsFrameState aFlags
);
297 // helper to let the rebuild of automatic data (presentation data
298 // and embellishement data) walk through a subtree that may contain
299 // non-MathML container frames. Note that this method re-builds the
300 // automatic data in the children -- not in aParentFrame itself (except
301 // for those particular operations that the parent frame may do in its
302 // TransmitAutomaticData()). The reason it works this way is because
303 // a container frame knows what it wants for its children, whereas children
304 // have no clue who their parent is. For example, it is <mfrac> who knows
305 // that its children have to be in scriptsizes, and has to transmit this
306 // information to them. Hence, when changes occur in a child frame, the child
307 // has to request the re-build from its parent. Unfortunately, the extra cost
308 // for this is that it will re-sync in the siblings of the child as well.
309 static void RebuildAutomaticDataForChildren(nsIFrame
* aParentFrame
);
311 // helper to blow away the automatic data cached in a frame's subtree and
312 // re-layout its subtree to reflect changes that may have happen. In the
313 // event where aParentFrame isn't a MathML frame, it will first walk up to
314 // the ancestor that is a MathML frame, and re-layout from there -- this is
315 // to guarantee that automatic data will be rebuilt properly. Note that this
316 // method re-builds the automatic data in the children -- not in the parent
317 // frame itself (except for those particular operations that the parent frame
318 // may do do its TransmitAutomaticData()). @see
319 // RebuildAutomaticDataForChildren
321 // aBits are the bits to pass to FrameNeedsReflow() when we call it.
322 static nsresult
ReLayoutChildren(nsIFrame
* aParentFrame
);
325 // Helper method which positions child frames as an <mrow> on given baseline
326 // y = aBaseline starting from x = aOffsetX, calling FinishReflowChild()
328 void PositionRowChildFrames(nscoord aOffsetX
, nscoord aBaseline
);
330 // A variant on FinishAndStoreOverflow() that uses the union of child
331 // overflows, the frame bounds, and mBoundingMetrics to set and store the
333 void GatherAndStoreOverflow(ReflowOutput
* aMetrics
);
336 * Call DidReflow() if the NS_FRAME_IN_REFLOW frame bit is set on aFirst and
337 * all its next siblings up to, but not including, aStop.
338 * aStop == nullptr meaning all next siblings with the bit set.
339 * The method does nothing if aFirst == nullptr.
341 static void DidReflowChildren(nsIFrame
* aFirst
, nsIFrame
* aStop
= nullptr);
344 * Recompute mIntrinsicWidth if it's not already up to date.
346 void UpdateIntrinsicWidth(gfxContext
* aRenderingContext
);
348 nscoord mIntrinsicWidth
;
350 nscoord mBlockStartAscent
;
353 class RowChildFrameIterator
;
354 friend class RowChildFrameIterator
;
357 // --------------------------------------------------------------------------
358 // Currently, to benefit from line-breaking inside the <math> element, <math> is
359 // simply mapping to nsBlockFrame or nsInlineFrame.
360 // A separate implemention needs to provide:
362 // 2) proper inter-frame spacing
363 // 3) firing of Stretch() (in which case FinalizeReflow() would have to be
365 // Issues: If/when mathml becomes a pluggable component, the separation will be
367 class nsMathMLmathBlockFrame final
: public nsBlockFrame
{
370 NS_DECL_FRAMEARENA_HELPERS(nsMathMLmathBlockFrame
)
372 friend nsContainerFrame
* NS_NewMathMLmathBlockFrame(
373 mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
);
375 // beware, mFrames is not set by nsBlockFrame
376 // cannot use mFrames{.FirstChild()|.etc} since the block code doesn't set
378 void SetInitialChildList(ChildListID aListID
,
379 nsFrameList
&& aChildList
) override
{
380 MOZ_ASSERT(aListID
== mozilla::FrameChildListID::Principal
||
381 aListID
== mozilla::FrameChildListID::Backdrop
,
382 "unexpected frame list");
383 nsBlockFrame::SetInitialChildList(aListID
, std::move(aChildList
));
384 if (aListID
== mozilla::FrameChildListID::Principal
) {
385 // re-resolve our subtree to set any mathml-expected data
386 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
390 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
{
391 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
392 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
393 "unexpected frame list");
394 nsBlockFrame::AppendFrames(aListID
, std::move(aFrameList
));
395 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
396 nsMathMLContainerFrame::ReLayoutChildren(this);
400 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
401 const nsLineList::iterator
* aPrevFrameLine
,
402 nsFrameList
&& aFrameList
) override
{
403 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
404 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
405 "unexpected frame list");
406 nsBlockFrame::InsertFrames(aListID
, aPrevFrame
, aPrevFrameLine
,
407 std::move(aFrameList
));
408 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
409 nsMathMLContainerFrame::ReLayoutChildren(this);
413 void RemoveFrame(DestroyContext
& aContext
, ChildListID aListID
,
414 nsIFrame
* aOldFrame
) override
{
415 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
416 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
417 "unexpected frame list");
418 nsBlockFrame::RemoveFrame(aContext
, aListID
, aOldFrame
);
419 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
420 nsMathMLContainerFrame::ReLayoutChildren(this);
424 // See nsIMathMLFrame.h
426 return mFrames
.FirstChild() != mFrames
.LastChild() || !mFrames
.FirstChild();
430 explicit nsMathMLmathBlockFrame(ComputedStyle
* aStyle
,
431 nsPresContext
* aPresContext
)
432 : nsBlockFrame(aStyle
, aPresContext
, kClassID
) {
434 virtual ~nsMathMLmathBlockFrame() = default;
439 class nsMathMLmathInlineFrame final
: public nsInlineFrame
,
440 public nsMathMLFrame
{
443 NS_DECL_FRAMEARENA_HELPERS(nsMathMLmathInlineFrame
)
445 friend nsContainerFrame
* NS_NewMathMLmathInlineFrame(
446 mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
);
448 void SetInitialChildList(ChildListID aListID
,
449 nsFrameList
&& aChildList
) override
{
450 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
,
451 "unexpected frame list");
452 nsInlineFrame::SetInitialChildList(aListID
, std::move(aChildList
));
453 // re-resolve our subtree to set any mathml-expected data
454 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
457 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
{
458 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
459 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
460 "unexpected frame list");
461 nsInlineFrame::AppendFrames(aListID
, std::move(aFrameList
));
462 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
463 nsMathMLContainerFrame::ReLayoutChildren(this);
467 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
468 const nsLineList::iterator
* aPrevFrameLine
,
469 nsFrameList
&& aFrameList
) override
{
470 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
471 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
472 "unexpected frame list");
473 nsInlineFrame::InsertFrames(aListID
, aPrevFrame
, aPrevFrameLine
,
474 std::move(aFrameList
));
475 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
476 nsMathMLContainerFrame::ReLayoutChildren(this);
480 void RemoveFrame(DestroyContext
& aContext
, ChildListID aListID
,
481 nsIFrame
* aOldFrame
) override
{
482 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
483 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
484 "unexpected frame list");
485 nsInlineFrame::RemoveFrame(aContext
, aListID
, aOldFrame
);
486 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
487 nsMathMLContainerFrame::ReLayoutChildren(this);
491 bool IsMrowLike() override
{
492 return mFrames
.FirstChild() != mFrames
.LastChild() || !mFrames
.FirstChild();
496 explicit nsMathMLmathInlineFrame(ComputedStyle
* aStyle
,
497 nsPresContext
* aPresContext
)
498 : nsInlineFrame(aStyle
, aPresContext
, kClassID
) {}
500 virtual ~nsMathMLmathInlineFrame() = default;
503 #endif /* nsMathMLContainerFrame_h___ */