Skip to content

Commit 9c073c7

Browse files
committed
Bug 1916988 - Support CSS width/height properties on MathML elements. r=emilio
This patch implements support for the width/height properties on MathML elements [1]. The general algorithm from the spec is as follows: (1) The outcome of the math layout is a "math content box". (2) The content box sets its size from computed width/height values. If auto, it's the one of the "math content box". This patch ignores percentage values for now [2] [3]. (3) math content box is shifted so that its inline-start and top edges aligns with the ones of the content box. There are exceptions elements like mfrac and munder/mover/munderover which instead horizontally center the math content box within the content box. For baseline adjustment, we follow what Chromium does, see [4]. (4) Padding+border are added around the content box. Note that we ignore the box-sizing property for now [5]. The patch essentially tweaks the various MathML layout algorithms to perform steps (3) and (4) before the calls to GetBorderPaddingForPlace and InflateReflowAndBoundingMetrics. [1] https://w3c.github.io/mathml-core/#layout-algorithms [2] w3c/mathml-core#76 [3] w3c/mathml-core#77 [4] w3c/mathml-core#259 [5] w3c/mathml-core#257 Below is more information about test coverage: - width-height-001: Verify that width, height, inline-size and block-size properties sets the size of the content box. This test used to verify they are ignored, this patch fixes the `<meta name="assert">` tag. It also adds a test for the case the specified size is smaller than the content (we force non empty descendants to make sure this content is large enough) and to verify the width is used for the preferred width. - width-height-002, width-height-003: These are reftests visually checking offsets of the math content box within a larger content box (specified by width/height) for the mtext, mrow, mpadded, mfrac, msqrt, mroot, in LTR/RTL modes. In particular they allow to verify some painted elements like fraction bar and radical symbols. - width-height-004: This test more directly checks that the math content box is horizontally centered within a larger content box for munder, mover, munderover and mfrac. This patch extends the test to cover the case when the math content box is wider (i.e. overflowing outside the content box) and removes unnecessary specified height. - width-height-005: New test for other layout algorithm that don't center the math content box, checking inline-start edges of children when a width is specified. We check both LTR/RTL modes and wider/narrower content boxes. - width-height-006: Same but checking the top edges for larger/smaller height and verifying that baseline is perserved. Differential Revision: https://phabricator.services.mozilla.com/D221436
1 parent 13e2add commit 9c073c7

24 files changed

Lines changed: 1208 additions & 176 deletions

‎layout/mathml/nsMathMLContainerFrame.cpp‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,59 @@ void nsMathMLContainerFrame::InflateReflowAndBoundingMetrics(
133133
aReflowOutput.Height() += aBorderPadding.TopBottom();
134134
}
135135

136+
nsMathMLContainerFrame::WidthAndHeightForPlaceAdjustment
137+
nsMathMLContainerFrame::GetWidthAndHeightForPlaceAdjustment(
138+
const PlaceFlags& aFlags) {
139+
WidthAndHeightForPlaceAdjustment sizes;
140+
if (aFlags.contains(PlaceFlag::DoNotAdjustForWidthAndHeight)) {
141+
return sizes;
142+
}
143+
const nsStylePosition* stylePos = StylePosition();
144+
const auto& width = stylePos->mWidth;
145+
// TODO: Resolve percentages.
146+
// https://github.com/w3c/mathml-core/issues/76
147+
if (width.ConvertsToLength()) {
148+
sizes.width = Some(width.ToLength());
149+
}
150+
if (!aFlags.contains(PlaceFlag::IntrinsicSize)) {
151+
// TODO: Resolve percentages.
152+
// https://github.com/w3c/mathml-core/issues/77
153+
const auto& height = stylePos->mHeight;
154+
if (height.ConvertsToLength()) {
155+
sizes.height = Some(height.ToLength());
156+
}
157+
}
158+
return sizes;
159+
}
160+
161+
nscoord nsMathMLContainerFrame::ApplyAdjustmentForWidthAndHeight(
162+
const PlaceFlags& aFlags, const WidthAndHeightForPlaceAdjustment& aSizes,
163+
ReflowOutput& aReflowOutput, nsBoundingMetrics& aBoundingMetrics) {
164+
nscoord shiftX = 0;
165+
if (aSizes.width) {
166+
MOZ_ASSERT(!aFlags.contains(PlaceFlag::DoNotAdjustForWidthAndHeight));
167+
auto width = *aSizes.width;
168+
auto oldWidth = aReflowOutput.Width();
169+
if (IsMathContentBoxHorizontallyCentered()) {
170+
shiftX = (width - oldWidth) / 2;
171+
} else if (StyleVisibility()->mDirection == StyleDirection::Rtl) {
172+
shiftX = width - oldWidth;
173+
}
174+
aBoundingMetrics.leftBearing = 0;
175+
aBoundingMetrics.rightBearing = width;
176+
aBoundingMetrics.width = width;
177+
aReflowOutput.mBoundingMetrics = aBoundingMetrics;
178+
aReflowOutput.Width() = width;
179+
}
180+
if (aSizes.height) {
181+
MOZ_ASSERT(!aFlags.contains(PlaceFlag::DoNotAdjustForWidthAndHeight));
182+
MOZ_ASSERT(!aFlags.contains(PlaceFlag::IntrinsicSize));
183+
auto height = *aSizes.height;
184+
aReflowOutput.Height() = height;
185+
}
186+
return shiftX;
187+
}
188+
136189
// helper to get the preferred size that a container frame should use to fire
137190
// the stretch on its stretchy child frames.
138191
void nsMathMLContainerFrame::GetPreferredStretchSize(
@@ -1169,18 +1222,24 @@ nsresult nsMathMLContainerFrame::Place(DrawTarget* aDrawTarget,
11691222
aDesiredSize.SetBlockStartAscent(ascent);
11701223
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
11711224

1225+
// Apply inline/block sizes to math content box.
1226+
auto sizes = GetWidthAndHeightForPlaceAdjustment(aFlags);
1227+
nscoord shiftX = ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
1228+
mBoundingMetrics);
1229+
11721230
// Add padding+border.
11731231
auto borderPadding = GetBorderPaddingForPlace(aFlags);
11741232
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,
11751233
mBoundingMetrics);
1234+
shiftX += borderPadding.left;
11761235

11771236
mReference.x = 0;
11781237
mReference.y = aDesiredSize.BlockStartAscent();
11791238

11801239
//////////////////
11811240
// Place Children
11821241
if (!aFlags.contains(PlaceFlag::MeasureOnly)) {
1183-
PositionRowChildFrames(borderPadding.left, aDesiredSize.BlockStartAscent());
1242+
PositionRowChildFrames(shiftX, aDesiredSize.BlockStartAscent());
11841243
}
11851244

11861245
return NS_OK;

‎layout/mathml/nsMathMLContainerFrame.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ class nsMathMLContainerFrame : public nsContainerFrame, public nsMathMLFrame {
157157
// place some radical symbol on top of them and finally add its
158158
// padding/border around that radical symbol.
159159
IgnoreBorderPadding,
160+
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,
160165
};
161166
using PlaceFlags = mozilla::EnumSet<PlaceFlag>;
162167

@@ -247,6 +252,18 @@ class nsMathMLContainerFrame : public nsContainerFrame, public nsMathMLFrame {
247252

248253
nsMargin GetBorderPaddingForPlace(const PlaceFlags& aFlags);
249254

255+
struct WidthAndHeightForPlaceAdjustment {
256+
mozilla::Maybe<nscoord> width;
257+
mozilla::Maybe<nscoord> height;
258+
};
259+
WidthAndHeightForPlaceAdjustment GetWidthAndHeightForPlaceAdjustment(
260+
const PlaceFlags& aFlags);
261+
262+
virtual bool IsMathContentBoxHorizontallyCentered() const { return false; }
263+
nscoord ApplyAdjustmentForWidthAndHeight(
264+
const PlaceFlags& aFlags, const WidthAndHeightForPlaceAdjustment& aSizes,
265+
ReflowOutput& aReflowOutput, nsBoundingMetrics& aBoundingMetrics);
266+
250267
protected:
251268
// helper to add the inter-spacing when <math> is the immediate parent.
252269
// Since we don't (yet) handle the root <math> element ourselves, we need to

‎layout/mathml/nsMathMLTokenFrame.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,19 @@ nsresult nsMathMLTokenFrame::Place(DrawTarget* aDrawTarget,
178178
aDesiredSize.Height() = aDesiredSize.BlockStartAscent() +
179179
std::max(mBoundingMetrics.descent, descent);
180180

181+
// Apply width/height to math content box.
182+
auto sizes = GetWidthAndHeightForPlaceAdjustment(aFlags);
183+
auto shiftX = ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
184+
mBoundingMetrics);
185+
181186
// Add padding+border.
182187
auto borderPadding = GetBorderPaddingForPlace(aFlags);
183188
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,
184189
mBoundingMetrics);
185190

186191
if (!aFlags.contains(PlaceFlag::MeasureOnly)) {
187192
nscoord dx = borderPadding.left;
193+
dx += shiftX;
188194
for (nsIFrame* childFrame : PrincipalChildList()) {
189195
ReflowOutput childSize(aDesiredSize.GetWritingMode());
190196
GetReflowAndBoundingMetricsFor(childFrame, childSize,

‎layout/mathml/nsMathMLmencloseFrame.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ nsresult nsMathMLmencloseFrame::Place(DrawTarget* aDrawTarget,
270270
// Measure the size of our content using the base class to format like an
271271
// inferred mrow, without border/padding.
272272
ReflowOutput baseSize(aDesiredSize.GetWritingMode());
273-
PlaceFlags flags =
274-
aFlags + PlaceFlag::MeasureOnly + PlaceFlag::IgnoreBorderPadding;
273+
PlaceFlags flags = aFlags + PlaceFlag::MeasureOnly +
274+
PlaceFlag::IgnoreBorderPadding +
275+
PlaceFlag::DoNotAdjustForWidthAndHeight;
275276
nsresult rv = nsMathMLContainerFrame::Place(aDrawTarget, flags, baseSize);
276277

277278
if (NS_FAILED(rv)) {
@@ -527,6 +528,11 @@ nsresult nsMathMLmencloseFrame::Place(DrawTarget* aDrawTarget,
527528

528529
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
529530

531+
// Apply width/height to math content box.
532+
auto sizes = GetWidthAndHeightForPlaceAdjustment(aFlags);
533+
dx_left += ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
534+
mBoundingMetrics);
535+
530536
// Add padding+border.
531537
auto borderPadding = GetBorderPaddingForPlace(aFlags);
532538
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,

‎layout/mathml/nsMathMLmfracFrame.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ nsresult nsMathMLmfracFrame::Place(DrawTarget* aDrawTarget,
355355
aDesiredSize.Width() = mBoundingMetrics.width;
356356
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
357357

358+
// Apply width/height to math content box.
359+
auto sizes = GetWidthAndHeightForPlaceAdjustment(aFlags);
360+
auto shiftX = ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
361+
mBoundingMetrics);
362+
if (sizes.width) {
363+
// MathML Core says the math content box is horizontally centered
364+
// but the fraction bar still takes the full width of the content box.
365+
dxNum += shiftX;
366+
dxDen += shiftX;
367+
width = *sizes.width;
368+
}
369+
358370
// Add padding+border.
359371
auto borderPadding = GetBorderPaddingForPlace(aFlags);
360372
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,

‎layout/mathml/nsMathMLmfracFrame.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class nsMathMLmfracFrame final : public nsMathMLContainerFrame {
9595
mLineThickness(0) {}
9696
virtual ~nsMathMLmfracFrame();
9797

98+
bool IsMathContentBoxHorizontallyCentered() const final { return true; }
99+
98100
// Display a slash
99101
void DisplaySlash(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
100102
nscoord aThickness, const nsDisplayListSet& aLists);

‎layout/mathml/nsMathMLmmultiscriptsFrame.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,11 @@ nsresult nsMathMLmmultiscriptsFrame::PlaceMultiScript(
559559
aDesiredSize.Width() = boundingMetrics.width;
560560
aDesiredSize.mBoundingMetrics = boundingMetrics;
561561

562+
// Apply width/height to math content box.
563+
auto sizes = aFrame->GetWidthAndHeightForPlaceAdjustment(aFlags);
564+
aFrame->ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
565+
boundingMetrics);
566+
562567
// Add padding+border.
563568
auto borderPadding = aFrame->GetBorderPaddingForPlace(aFlags);
564569
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize, boundingMetrics);

‎layout/mathml/nsMathMLmoFrame.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ nsMathMLmoFrame::Stretch(DrawTarget* aDrawTarget,
741741

742742
// Place our children using the default method and no border/padding.
743743
// This will allow our child text frame to get its DidReflow()
744-
PlaceFlags flags = PlaceFlag::IgnoreBorderPadding;
744+
PlaceFlags flags(PlaceFlag::IgnoreBorderPadding,
745+
PlaceFlag::DoNotAdjustForWidthAndHeight);
745746
nsresult rv = Place(aDrawTarget, flags, aDesiredStretchSize);
746747
if (NS_FAILED(rv)) {
747748
// Make sure the child frames get their DidReflow() calls.
@@ -852,8 +853,10 @@ nsMathMLmoFrame::Stretch(DrawTarget* aDrawTarget,
852853
}
853854

854855
flags = PlaceFlags();
856+
auto sizes = GetWidthAndHeightForPlaceAdjustment(flags);
855857
auto borderPadding = GetBorderPaddingForPlace(flags);
856-
if (leadingSpace || trailingSpace || !borderPadding.IsAllZero()) {
858+
if (leadingSpace || trailingSpace || !borderPadding.IsAllZero() ||
859+
sizes.width || sizes.height) {
857860
mBoundingMetrics.width += leadingSpace + trailingSpace;
858861
aDesiredStretchSize.Width() = mBoundingMetrics.width;
859862
aDesiredStretchSize.mBoundingMetrics.width = mBoundingMetrics.width;
@@ -866,6 +869,10 @@ nsMathMLmoFrame::Stretch(DrawTarget* aDrawTarget,
866869
aDesiredStretchSize.mBoundingMetrics.leftBearing += dx;
867870
aDesiredStretchSize.mBoundingMetrics.rightBearing += dx;
868871

872+
// Apply inline/block sizes to math content box.
873+
dx += ApplyAdjustmentForWidthAndHeight(flags, sizes, aDesiredStretchSize,
874+
mBoundingMetrics);
875+
869876
// Add border/padding.
870877
InflateReflowAndBoundingMetrics(borderPadding, aDesiredStretchSize,
871878
mBoundingMetrics);

‎layout/mathml/nsMathMLmpaddedFrame.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ nsresult nsMathMLmpaddedFrame::Place(DrawTarget* aDrawTarget,
286286
const PlaceFlags& aFlags,
287287
ReflowOutput& aDesiredSize) {
288288
// First perform normal row layout without border/padding.
289-
PlaceFlags flags =
290-
aFlags + PlaceFlag::MeasureOnly + PlaceFlag::IgnoreBorderPadding;
289+
PlaceFlags flags = aFlags + PlaceFlag::MeasureOnly +
290+
PlaceFlag::IgnoreBorderPadding +
291+
PlaceFlag::DoNotAdjustForWidthAndHeight;
291292
nsresult rv = nsMathMLContainerFrame::Place(aDrawTarget, flags, aDesiredSize);
292293
if (NS_FAILED(rv)) {
293294
DidReflowChildren(PrincipalChildList().FirstChild());
@@ -388,6 +389,11 @@ nsresult nsMathMLmpaddedFrame::Place(DrawTarget* aDrawTarget,
388389
mBoundingMetrics.descent = depth;
389390
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
390391

392+
// Apply width/height to math content box.
393+
auto sizes = GetWidthAndHeightForPlaceAdjustment(aFlags);
394+
dx += ApplyAdjustmentForWidthAndHeight(aFlags, sizes, aDesiredSize,
395+
mBoundingMetrics);
396+
391397
// Add padding+border.
392398
auto borderPadding = GetBorderPaddingForPlace(aFlags);
393399
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,

‎layout/mathml/nsMathMLmrootFrame.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ nsresult nsMathMLmrootFrame::Place(DrawTarget* aDrawTarget,
180180
} else {
181181
// Format our content as an mrow without border/padding to obtain the
182182
// square root base. The metrics/frame for the index are ignored.
183-
PlaceFlags flags =
184-
aFlags + PlaceFlag::MeasureOnly + PlaceFlag::IgnoreBorderPadding;
183+
PlaceFlags flags = aFlags + PlaceFlag::MeasureOnly +
184+
PlaceFlag::IgnoreBorderPadding +
185+
PlaceFlag::DoNotAdjustForWidthAndHeight;
185186
nsresult rv = nsMathMLContainerFrame::Place(aDrawTarget, flags, baseSize);
186187
if (NS_FAILED(rv)) {
187188
DidReflowChildren(PrincipalChildList().FirstChild());
@@ -317,6 +318,12 @@ nsresult nsMathMLmrootFrame::Place(DrawTarget* aDrawTarget,
317318

318319
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
319320

321+
// Apply width/height to math content box.
322+
const PlaceFlags flags;
323+
auto sizes = GetWidthAndHeightForPlaceAdjustment(flags);
324+
nscoord shiftX = ApplyAdjustmentForWidthAndHeight(flags, sizes, aDesiredSize,
325+
mBoundingMetrics);
326+
320327
// Add padding+border around the final layout.
321328
auto borderPadding = GetBorderPaddingForPlace(aFlags);
322329
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,
@@ -360,7 +367,7 @@ nsresult nsMathMLmrootFrame::Place(DrawTarget* aDrawTarget,
360367
MirrorIfRTL(aDesiredSize.Width(), baseSize.Width(), dx),
361368
dy, ReflowChildFlags::Default);
362369
} else {
363-
nscoord dx_left = borderPadding.left;
370+
nscoord dx_left = borderPadding.left + shiftX;
364371
if (!isRTL) {
365372
dx_left += bmSqr.width;
366373
}

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo