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
) {}
42 NS_DECL_QUERYFRAME_TARGET(nsMathMLContainerFrame
)
44 NS_DECL_ABSTRACT_FRAME(nsMathMLContainerFrame
)
46 // --------------------------------------------------------------------------
47 // Overloaded nsMathMLFrame methods -- see documentation in nsIMathMLFrame.h
50 Stretch(DrawTarget
* aDrawTarget
, nsStretchDirection aStretchDirection
,
51 nsBoundingMetrics
& aContainerSize
,
52 ReflowOutput
& aDesiredStretchSize
) override
;
55 UpdatePresentationDataFromChildAt(int32_t aFirstIndex
, int32_t aLastIndex
,
56 uint32_t aFlagsValues
,
57 uint32_t aFlagsToUpdate
) override
{
58 PropagatePresentationDataFromChildAt(this, aFirstIndex
, aLastIndex
,
59 aFlagsValues
, aFlagsToUpdate
);
63 // --------------------------------------------------------------------------
64 // Overloaded nsContainerFrame methods -- see documentation in nsIFrame.h
66 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
;
68 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
69 const nsLineList::iterator
* aPrevFrameLine
,
70 nsFrameList
&& aFrameList
) override
;
72 void RemoveFrame(DestroyContext
&, ChildListID aListID
,
73 nsIFrame
* aOldFrame
) override
;
75 nscoord
IntrinsicISize(const mozilla::IntrinsicSizeInput
& aInput
,
76 mozilla::IntrinsicISizeType aType
) override
;
79 * Return the intrinsic horizontal metrics of the frame's content area.
81 virtual void GetIntrinsicISizeMetrics(gfxContext
* aRenderingContext
,
82 ReflowOutput
& aDesiredSize
);
84 void Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aDesiredSize
,
85 const ReflowInput
& aReflowInput
,
86 nsReflowStatus
& aStatus
) override
;
88 void DidReflow(nsPresContext
* aPresContext
,
89 const ReflowInput
* aReflowInput
) override
92 mPresentationData
.flags
&= ~NS_MATHML_STRETCH_DONE
;
93 return nsContainerFrame::DidReflow(aPresContext
, aReflowInput
);
96 void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
97 const nsDisplayListSet
& aLists
) override
;
99 bool ComputeCustomOverflow(mozilla::OverflowAreas
& aOverflowAreas
) override
;
101 void MarkIntrinsicISizesDirty() override
;
103 // Notification when an attribute is changed. The MathML module uses the
104 // following paradigm:
106 // 1. If the MathML frame class doesn't have any cached automatic data that
107 // depends on the attribute:
108 // 1a. If the attribute is taken into account for the layout of the class
109 // then, we reflow (e.g., this happens with mfrac@linethickness).
110 // 2b. Otherwise, we don't force any reflow.
112 // 2. If the MathML frame class has cached automatic data that depends on
114 // 2a. If the automatic data to update resides only within the descendants,
115 // we just re-layout them using ReLayoutChildren(this);
116 // (e.g., this happens with <ms>).
117 // 2b. If the automatic data to update affects us in some way, we ask our
118 // parent to re-layout its children using ReLayoutChildren(mParent);
119 // Therefore, there is an overhead here in that our siblings are
120 // re-laid too (e.g., this happens with <munder>, <mover>,
122 // nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
123 // int32_t aModType) override;
125 // helper function to apply mirroring to a horizontal coordinate, if needed.
126 nscoord
MirrorIfRTL(nscoord aParentWidth
, nscoord aChildWidth
,
127 nscoord aChildLeading
) {
128 return StyleVisibility()->mDirection
== mozilla::StyleDirection::Rtl
129 ? aParentWidth
- aChildWidth
- aChildLeading
133 // --------------------------------------------------------------------------
134 // Additional methods
137 enum class PlaceFlag
: uint8_t {
138 // If MeasureOnly is set, compute your desired size using the information
139 // from GetReflowAndBoundingMetricsFor. However, child frames or other
140 // elements should not be repositioned.
141 // If MeasureOnly is not set, reflow is finished. You should position all
142 // your children, and return your desired size. You should now use
143 // FinishReflowChild() on your children to complete post-reflow operations.
146 // If IntrinsicSize is set, the function is actually used to determine
147 // intrinsic size (and consequently MeasureOnly is expected to be set too).
148 // - It will use nsMathMLChar::GetMaxWidth instead of nsMathMLChar::Stretch.
149 // - It will use IntrinsicISizeOffsets() for padding/border/margin instead
150 // of GetUsedBorder/Padding/Margin().
154 // If IgnoreBorderPadding is set, the function will complete without
155 // adding the border/padding around the math layout. This can be used for
156 // elements like <msqrt> that first layout their children as an <mrow>,
157 // place some radical symbol on top of them and finally add its
158 // padding/border around that radical symbol.
161 // If DoNotAdjustForWidthAndHeight is set, the function will complete
162 // without setting the computed width and height after the math layout. This
163 // can be used similarly to IgnoreBorderPadding above.
164 DoNotAdjustForWidthAndHeight
,
166 using PlaceFlags
= mozilla::EnumSet
<PlaceFlag
>;
169 * This method is used to measure or position child frames and other
170 * elements. It may be called any number of times with MeasureOnly
171 * true, and the final call of the Reflow process before
172 * returning from Reflow() or Stretch() will have MeasureOnly false
173 * to position the elements.
175 * IMPORTANT: This method uses GetReflowAndBoundingMetricsFor() which must
176 * have been set up with SaveReflowAndBoundingMetricsFor().
178 * The Place() method will use this information to compute the desired size
181 * @param aFlags [in] Flags to indicate the way the Place method should
182 * behave. See document for PlaceFlag above.
184 * @param aDesiredSize [out] parameter where you should return your desired
185 * size and your ascent/descent info. Compute your desired size using
186 * the information from GetReflowAndBoundingMetricsFor, and include
187 * any space you want for border/padding in the desired size you
190 virtual nsresult
Place(DrawTarget
* aDrawTarget
, const PlaceFlags
& aFlags
,
191 ReflowOutput
& aDesiredSize
);
193 // helper to re-sync the automatic data in our children and notify our parent
194 // to reflow us when changes (e.g., append/insert/remove) happen in our child
196 virtual nsresult
ChildListChanged(int32_t aModType
);
198 // helper to get the preferred size that a container frame should use to fire
199 // the stretch on its stretchy child frames.
200 void GetPreferredStretchSize(DrawTarget
* aDrawTarget
, uint32_t aOptions
,
201 nsStretchDirection aStretchDirection
,
202 nsBoundingMetrics
& aPreferredStretchSize
);
204 // helper used by mstyle, mphantom, mpadded and mrow in their implementation
205 // of TransmitAutomaticData() to determine whether they are space-like.
206 nsresult
TransmitAutomaticDataForMrowLikeElement();
210 * Helper to render the frame as a default mrow-like container when an error
211 * (typically invalid markup) was encountered during reflow. Parameters are
212 * the same as Place().
214 nsresult
PlaceAsMrow(DrawTarget
* aDrawTarget
, const PlaceFlags
& aFlags
,
215 ReflowOutput
& aDesiredSize
);
218 * Helper to call ReportErrorToConsole for parse errors involving
219 * attribute/value pairs.
220 * @param aAttribute The attribute for which the parse error occured.
221 * @param aValue The value for which the parse error occured.
223 nsresult
ReportParseError(const char16_t
* aAttribute
, const char16_t
* aValue
);
226 * Helper to call ReportErrorToConsole when certain tags
227 * have more than the expected amount of children.
229 nsresult
ReportChildCountError();
232 * Helper to call ReportErrorToConsole when certain tags have
234 * @param aChildTag The tag which is forbidden in this context
236 nsresult
ReportInvalidChildError(nsAtom
* aChildTag
);
239 * Helper to call ReportToConsole when an error occurs.
240 * @param aParams see nsContentUtils::ReportToConsole
242 nsresult
ReportErrorToConsole(
243 const char* aErrorMsgId
,
244 const nsTArray
<nsString
>& aParams
= nsTArray
<nsString
>());
246 // helper method to reflow a child frame. We are inline frames, and we don't
247 // know our positions until reflow is finished. That's why we ask the
248 // base method not to worry about our position.
249 void ReflowChild(nsIFrame
* aKidFrame
, nsPresContext
* aPresContext
,
250 ReflowOutput
& aDesiredSize
, const ReflowInput
& aReflowInput
,
251 nsReflowStatus
& aStatus
);
253 nsMargin
GetBorderPaddingForPlace(const PlaceFlags
& aFlags
);
255 struct WidthAndHeightForPlaceAdjustment
{
256 mozilla::Maybe
<nscoord
> width
;
257 mozilla::Maybe
<nscoord
> height
;
259 WidthAndHeightForPlaceAdjustment
GetWidthAndHeightForPlaceAdjustment(
260 const PlaceFlags
& aFlags
);
262 virtual bool IsMathContentBoxHorizontallyCentered() const { return false; }
263 nscoord
ApplyAdjustmentForWidthAndHeight(
264 const PlaceFlags
& aFlags
, const WidthAndHeightForPlaceAdjustment
& aSizes
,
265 ReflowOutput
& aReflowOutput
, nsBoundingMetrics
& aBoundingMetrics
);
268 // helper to add the inter-spacing when <math> is the immediate parent.
269 // Since we don't (yet) handle the root <math> element ourselves, we need to
270 // take special care of the inter-frame spacing on elements for which <math>
271 // is the direct xml parent. This function will be repeatedly called from
272 // left to right on the childframes of <math>, and by so doing it will
273 // emulate the spacing that would have been done by a <mrow> container.
274 // e.g., it fixes <math> <mi>f</mi> <mo>q</mo> <mi>f</mi> <mo>I</mo> </math>
275 virtual nscoord
FixInterFrameSpacing(ReflowOutput
& aDesiredSize
);
277 // helper method to complete the post-reflow hook and ensure that embellished
278 // operators don't terminate their Reflow without receiving a Stretch command.
279 virtual nsresult
FinalizeReflow(DrawTarget
* aDrawTarget
,
280 ReflowOutput
& aDesiredSize
);
282 // Record metrics of a child frame for recovery through the following method
283 static void SaveReflowAndBoundingMetricsFor(
284 nsIFrame
* aFrame
, const ReflowOutput
& aReflowOutput
,
285 const nsBoundingMetrics
& aBoundingMetrics
);
287 // helper method to facilitate getting the reflow and bounding metrics of a
288 // child frame. The argument aMathMLFrameType, when non null, will return
289 // the 'type' of the frame, which is used to determine the inter-frame
291 // IMPORTANT: This function is only meant to be called in Place() methods as
292 // the information is available only when set up with the above method
293 // during Reflow/Stretch() and GetPrefISize().
294 static void GetReflowAndBoundingMetricsFor(
295 nsIFrame
* aFrame
, ReflowOutput
& aReflowOutput
,
296 nsBoundingMetrics
& aBoundingMetrics
,
297 eMathMLFrameType
* aMathMLFrameType
= nullptr);
299 // helper method to clear metrics saved with
300 // SaveReflowAndBoundingMetricsFor() from all child frames.
301 void ClearSavedChildMetrics();
303 static nsMargin
GetMarginForPlace(const PlaceFlags
& aFlags
, nsIFrame
* aChild
);
305 static void InflateReflowAndBoundingMetrics(
306 const nsMargin
& aBorderPadding
, ReflowOutput
& aReflowOutput
,
307 nsBoundingMetrics
& aBoundingMetrics
);
309 // helper to let the update of presentation data pass through
310 // a subtree that may contain non-MathML container frames
311 static void PropagatePresentationDataFor(nsIFrame
* aFrame
,
312 uint32_t aFlagsValues
,
313 uint32_t aFlagsToUpdate
);
316 static void PropagatePresentationDataFromChildAt(nsIFrame
* aParentFrame
,
317 int32_t aFirstChildIndex
,
318 int32_t aLastChildIndex
,
319 uint32_t aFlagsValues
,
320 uint32_t aFlagsToUpdate
);
322 // Sets flags on aFrame and all descendant frames
323 static void PropagateFrameFlagFor(nsIFrame
* aFrame
, nsFrameState aFlags
);
325 // helper to let the rebuild of automatic data (presentation data
326 // and embellishement data) walk through a subtree that may contain
327 // non-MathML container frames. Note that this method re-builds the
328 // automatic data in the children -- not in aParentFrame itself (except
329 // for those particular operations that the parent frame may do in its
330 // TransmitAutomaticData()). The reason it works this way is because
331 // a container frame knows what it wants for its children, whereas children
332 // have no clue who their parent is. For example, it is <mfrac> who knows
333 // that its children have to be in scriptsizes, and has to transmit this
334 // information to them. Hence, when changes occur in a child frame, the child
335 // has to request the re-build from its parent. Unfortunately, the extra cost
336 // for this is that it will re-sync in the siblings of the child as well.
337 static void RebuildAutomaticDataForChildren(nsIFrame
* aParentFrame
);
339 // helper to blow away the automatic data cached in a frame's subtree and
340 // re-layout its subtree to reflect changes that may have happen. In the
341 // event where aParentFrame isn't a MathML frame, it will first walk up to
342 // the ancestor that is a MathML frame, and re-layout from there -- this is
343 // to guarantee that automatic data will be rebuilt properly. Note that this
344 // method re-builds the automatic data in the children -- not in the parent
345 // frame itself (except for those particular operations that the parent frame
346 // may do do its TransmitAutomaticData()). @see
347 // RebuildAutomaticDataForChildren
349 // aBits are the bits to pass to FrameNeedsReflow() when we call it.
350 static nsresult
ReLayoutChildren(nsIFrame
* aParentFrame
);
353 // Helper method which positions child frames as an <mrow> on given baseline
354 // y = aBaseline starting from x = aOffsetX, calling FinishReflowChild()
356 void PositionRowChildFrames(nscoord aOffsetX
, nscoord aBaseline
);
358 // A variant on FinishAndStoreOverflow() that uses the union of child
359 // overflows, the frame bounds, and mBoundingMetrics to set and store the
361 void GatherAndStoreOverflow(ReflowOutput
* aMetrics
);
364 * Call DidReflow() if the NS_FRAME_IN_REFLOW frame bit is set on aFirst
365 * and all its next siblings. The method does nothing if aFirst == nullptr.
367 static void DidReflowChildren(nsIFrame
* aFirst
);
370 * Recompute mIntrinsicISize if it's not already up to date.
372 void UpdateIntrinsicISize(gfxContext
* aRenderingContext
);
374 nscoord mIntrinsicISize
= NS_INTRINSIC_ISIZE_UNKNOWN
;
376 nscoord mBlockStartAscent
= 0;
379 class RowChildFrameIterator
;
380 friend class RowChildFrameIterator
;
383 // --------------------------------------------------------------------------
384 // Currently, to benefit from line-breaking inside the <math> element, <math> is
385 // simply mapping to nsBlockFrame or nsInlineFrame.
386 // A separate implemention needs to provide:
388 // 2) proper inter-frame spacing
389 // 3) firing of Stretch() (in which case FinalizeReflow() would have to be
391 // Issues: If/when mathml becomes a pluggable component, the separation will be
393 class nsMathMLmathBlockFrame final
: public nsBlockFrame
{
396 NS_DECL_FRAMEARENA_HELPERS(nsMathMLmathBlockFrame
)
398 friend nsContainerFrame
* NS_NewMathMLmathBlockFrame(
399 mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
);
401 // beware, mFrames is not set by nsBlockFrame
402 // cannot use mFrames{.FirstChild()|.etc} since the block code doesn't set
404 void SetInitialChildList(ChildListID aListID
,
405 nsFrameList
&& aChildList
) override
{
406 MOZ_ASSERT(aListID
== mozilla::FrameChildListID::Principal
||
407 aListID
== mozilla::FrameChildListID::Backdrop
,
408 "unexpected frame list");
409 nsBlockFrame::SetInitialChildList(aListID
, std::move(aChildList
));
410 if (aListID
== mozilla::FrameChildListID::Principal
) {
411 // re-resolve our subtree to set any mathml-expected data
412 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
416 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
{
417 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
418 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
419 "unexpected frame list");
420 nsBlockFrame::AppendFrames(aListID
, std::move(aFrameList
));
421 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
422 nsMathMLContainerFrame::ReLayoutChildren(this);
426 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
427 const nsLineList::iterator
* aPrevFrameLine
,
428 nsFrameList
&& aFrameList
) override
{
429 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
430 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
431 "unexpected frame list");
432 nsBlockFrame::InsertFrames(aListID
, aPrevFrame
, aPrevFrameLine
,
433 std::move(aFrameList
));
434 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
435 nsMathMLContainerFrame::ReLayoutChildren(this);
439 void RemoveFrame(DestroyContext
& aContext
, ChildListID aListID
,
440 nsIFrame
* aOldFrame
) override
{
441 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
442 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
443 "unexpected frame list");
444 nsBlockFrame::RemoveFrame(aContext
, aListID
, aOldFrame
);
445 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
446 nsMathMLContainerFrame::ReLayoutChildren(this);
450 // See nsIMathMLFrame.h
452 return mFrames
.FirstChild() != mFrames
.LastChild() || !mFrames
.FirstChild();
456 explicit nsMathMLmathBlockFrame(ComputedStyle
* aStyle
,
457 nsPresContext
* aPresContext
)
458 : nsBlockFrame(aStyle
, aPresContext
, kClassID
) {}
459 virtual ~nsMathMLmathBlockFrame() = default;
464 class nsMathMLmathInlineFrame final
: public nsInlineFrame
,
465 public nsMathMLFrame
{
468 NS_DECL_FRAMEARENA_HELPERS(nsMathMLmathInlineFrame
)
470 friend nsContainerFrame
* NS_NewMathMLmathInlineFrame(
471 mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
);
473 void SetInitialChildList(ChildListID aListID
,
474 nsFrameList
&& aChildList
) override
{
475 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
,
476 "unexpected frame list");
477 nsInlineFrame::SetInitialChildList(aListID
, std::move(aChildList
));
478 // re-resolve our subtree to set any mathml-expected data
479 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
482 void AppendFrames(ChildListID aListID
, nsFrameList
&& aFrameList
) override
{
483 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
484 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
485 "unexpected frame list");
486 nsInlineFrame::AppendFrames(aListID
, std::move(aFrameList
));
487 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
488 nsMathMLContainerFrame::ReLayoutChildren(this);
492 void InsertFrames(ChildListID aListID
, nsIFrame
* aPrevFrame
,
493 const nsLineList::iterator
* aPrevFrameLine
,
494 nsFrameList
&& aFrameList
) override
{
495 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
496 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
497 "unexpected frame list");
498 nsInlineFrame::InsertFrames(aListID
, aPrevFrame
, aPrevFrameLine
,
499 std::move(aFrameList
));
500 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
501 nsMathMLContainerFrame::ReLayoutChildren(this);
505 void RemoveFrame(DestroyContext
& aContext
, ChildListID aListID
,
506 nsIFrame
* aOldFrame
) override
{
507 NS_ASSERTION(aListID
== mozilla::FrameChildListID::Principal
||
508 aListID
== mozilla::FrameChildListID::NoReflowPrincipal
,
509 "unexpected frame list");
510 nsInlineFrame::RemoveFrame(aContext
, aListID
, aOldFrame
);
511 if (MOZ_LIKELY(aListID
== mozilla::FrameChildListID::Principal
)) {
512 nsMathMLContainerFrame::ReLayoutChildren(this);
516 bool IsMrowLike() override
{
517 return mFrames
.FirstChild() != mFrames
.LastChild() || !mFrames
.FirstChild();
521 explicit nsMathMLmathInlineFrame(ComputedStyle
* aStyle
,
522 nsPresContext
* aPresContext
)
523 : nsInlineFrame(aStyle
, aPresContext
, kClassID
) {}
525 virtual ~nsMathMLmathInlineFrame() = default;
528 #endif /* nsMathMLContainerFrame_h___ */