Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / style / nsStyleTransformMatrix.cpp
blobc49c31e466d3c0594ad4ccf72d632936d89bc178
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 /*
8 * A class used for intermediate representations of the transform and
9 * transform-like properties.
12 #include "nsStyleTransformMatrix.h"
13 #include "nsLayoutUtils.h"
14 #include "nsPresContext.h"
15 #include "mozilla/MotionPathUtils.h"
16 #include "mozilla/ServoBindings.h"
17 #include "mozilla/StaticPrefs_layout.h"
18 #include "mozilla/StyleAnimationValue.h"
19 #include "mozilla/SVGUtils.h"
20 #include "gfxMatrix.h"
21 #include "gfxQuaternion.h"
23 using namespace mozilla;
24 using namespace mozilla::gfx;
26 namespace nsStyleTransformMatrix {
28 /* Note on floating point precision: The transform matrix is an array
29 * of single precision 'float's, and so are most of the input values
30 * we get from the style system, but intermediate calculations
31 * involving angles need to be done in 'double'.
34 // Define UNIFIED_CONTINUATIONS here and in nsDisplayList.cpp
35 // to have the transform property try
36 // to transform content with continuations as one unified block instead of
37 // several smaller ones. This is currently disabled because it doesn't work
38 // correctly, since when the frames are initially being reflowed, their
39 // continuations all compute their bounding rects independently of each other
40 // and consequently get the wrong value.
41 // #define UNIFIED_CONTINUATIONS
43 static nsRect GetSVGBox(const nsIFrame* aFrame) {
44 auto computeViewBox = [&]() {
45 // Percentages in transforms resolve against the width/height of the
46 // nearest viewport (or its viewBox if one is applied), and the
47 // transform is relative to {0,0} in current user space.
48 CSSSize size = CSSSize::FromUnknownSize(SVGUtils::GetContextSize(aFrame));
49 return nsRect(-aFrame->GetPosition(), CSSPixel::ToAppUnits(size));
52 // For SVG elements without associated CSS layout box, the used value for
53 // content-box is fill-box and for border-box is stroke-box.
54 // https://drafts.csswg.org/css-transforms-1/#transform-box
55 switch (aFrame->StyleDisplay()->mTransformBox) {
56 case StyleTransformBox::ContentBox:
57 case StyleTransformBox::FillBox: {
58 // Percentages in transforms resolve against the SVG bbox, and the
59 // transform is relative to the top-left of the SVG bbox.
60 nsRect bboxInAppUnits = nsLayoutUtils::ComputeSVGReferenceRect(
61 const_cast<nsIFrame*>(aFrame), StyleGeometryBox::FillBox);
62 // The mRect of an SVG nsIFrame is its user space bounds *including*
63 // stroke and markers, whereas bboxInAppUnits is its user space bounds
64 // including fill only. We need to note the offset of the reference box
65 // from the frame's mRect in mX/mY.
66 return {bboxInAppUnits.x - aFrame->GetPosition().x,
67 bboxInAppUnits.y - aFrame->GetPosition().y, bboxInAppUnits.width,
68 bboxInAppUnits.height};
70 case StyleTransformBox::BorderBox:
71 if (!StaticPrefs::layout_css_transform_box_content_stroke_enabled()) {
72 // If stroke-box is disabled, we shouldn't use it and fall back to
73 // view-box.
74 return computeViewBox();
76 [[fallthrough]];
77 case StyleTransformBox::StrokeBox: {
78 // We are using SVGUtils::PathExtentsToMaxStrokeExtents() to compute the
79 // bbox contribution for stroke box (if it doesn't have simple bounds),
80 // so the |strokeBox| here may be larger than the author's expectation.
81 // Using Moz2D to compute the tighter bounding box is another way but it
82 // has some potential issues (see SVGGeometryFrame::GetBBoxContribution()
83 // for more details), and its result depends on the drawing backend. So
84 // for now we still rely on our default calcuclation for SVG geometry
85 // frame reflow code. At least this works for the shape elements which
86 // have simple bounds.
87 // FIXME: Bug 1849054. We may have to update
88 // SVGGeometryFrame::GetBBoxContribution() to get tighter stroke bounds.
89 nsRect strokeBox = nsLayoutUtils::ComputeSVGReferenceRect(
90 const_cast<nsIFrame*>(aFrame), StyleGeometryBox::StrokeBox);
91 // The |nsIFrame::mRect| includes markers, so we have to compute the
92 // offsets without markers.
93 return nsRect{strokeBox.x - aFrame->GetPosition().x,
94 strokeBox.y - aFrame->GetPosition().y, strokeBox.width,
95 strokeBox.height};
97 case StyleTransformBox::ViewBox:
98 return computeViewBox();
101 MOZ_ASSERT_UNREACHABLE("All transform box should be handled.");
102 return {};
105 void TransformReferenceBox::EnsureDimensionsAreCached() {
106 if (mIsCached) {
107 return;
110 MOZ_ASSERT(mFrame);
112 mIsCached = true;
114 if (mFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
115 mBox = GetSVGBox(mFrame);
116 return;
119 // For elements with associated CSS layout box, the used value for fill-box is
120 // content-box and for stroke-box and view-box is border-box.
121 // https://drafts.csswg.org/css-transforms-1/#transform-box
122 switch (mFrame->StyleDisplay()->mTransformBox) {
123 case StyleTransformBox::FillBox:
124 case StyleTransformBox::ContentBox: {
125 mBox = mFrame->GetContentRectRelativeToSelf();
126 return;
128 case StyleTransformBox::StrokeBox:
129 // TODO: Implement this in the following patches.
130 return;
131 case StyleTransformBox::ViewBox:
132 case StyleTransformBox::BorderBox: {
133 // If UNIFIED_CONTINUATIONS is not defined, this is simply the frame's
134 // bounding rectangle, translated to the origin. Otherwise, it is the
135 // smallest rectangle containing a frame and all of its continuations. For
136 // example, if there is a <span> element with several continuations split
137 // over several lines, this function will return the rectangle containing
138 // all of those continuations.
140 nsRect rect;
142 #ifndef UNIFIED_CONTINUATIONS
143 rect = mFrame->GetRect();
144 #else
145 // Iterate the continuation list, unioning together the bounding rects:
146 for (const nsIFrame* currFrame = mFrame->FirstContinuation();
147 currFrame != nullptr; currFrame = currFrame->GetNextContinuation()) {
148 // Get the frame rect in local coordinates, then translate back to the
149 // original coordinates:
150 rect.UnionRect(result, nsRect(currFrame->GetOffsetTo(mFrame),
151 currFrame->GetSize()));
153 #endif
155 mBox = {0, 0, rect.Width(), rect.Height()};
156 return;
161 float ProcessTranslatePart(
162 const LengthPercentage& aValue, TransformReferenceBox* aRefBox,
163 TransformReferenceBox::DimensionGetter aDimensionGetter) {
164 return aValue.ResolveToCSSPixelsWith([&] {
165 return aRefBox && !aRefBox->IsEmpty()
166 ? CSSPixel::FromAppUnits((aRefBox->*aDimensionGetter)())
167 : CSSCoord(0);
172 * Helper functions to process all the transformation function types.
174 * These take a matrix parameter to accumulate the current matrix.
177 /* Helper function to process a matrix entry. */
178 static void ProcessMatrix(Matrix4x4& aMatrix,
179 const StyleTransformOperation& aOp) {
180 const auto& matrix = aOp.AsMatrix();
181 gfxMatrix result;
183 result._11 = matrix.a;
184 result._12 = matrix.b;
185 result._21 = matrix.c;
186 result._22 = matrix.d;
187 result._31 = matrix.e;
188 result._32 = matrix.f;
190 aMatrix = result * aMatrix;
193 static void ProcessMatrix3D(Matrix4x4& aMatrix,
194 const StyleTransformOperation& aOp) {
195 Matrix4x4 temp;
197 const auto& matrix = aOp.AsMatrix3D();
199 temp._11 = matrix.m11;
200 temp._12 = matrix.m12;
201 temp._13 = matrix.m13;
202 temp._14 = matrix.m14;
203 temp._21 = matrix.m21;
204 temp._22 = matrix.m22;
205 temp._23 = matrix.m23;
206 temp._24 = matrix.m24;
207 temp._31 = matrix.m31;
208 temp._32 = matrix.m32;
209 temp._33 = matrix.m33;
210 temp._34 = matrix.m34;
212 temp._41 = matrix.m41;
213 temp._42 = matrix.m42;
214 temp._43 = matrix.m43;
215 temp._44 = matrix.m44;
217 aMatrix = temp * aMatrix;
220 // For accumulation for transform functions, |aOne| corresponds to |aB| and
221 // |aTwo| corresponds to |aA| for StyleAnimationValue::Accumulate().
222 class Accumulate {
223 public:
224 template <typename T>
225 static T operate(const T& aOne, const T& aTwo, double aCoeff) {
226 return aOne + aTwo * aCoeff;
229 static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
230 double aCoeff) {
231 return (aOne - Point4D(0, 0, 0, 1)) +
232 (aTwo - Point4D(0, 0, 0, 1)) * aCoeff + Point4D(0, 0, 0, 1);
234 static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
235 double aCoeff) {
236 // For scale, the identify element is 1, see AddTransformScale in
237 // StyleAnimationValue.cpp.
238 return (aOne - Point3D(1, 1, 1)) + (aTwo - Point3D(1, 1, 1)) * aCoeff +
239 Point3D(1, 1, 1);
242 static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
243 const gfxQuaternion& aTwo, double aCoeff) {
244 if (aCoeff == 0.0) {
245 return aOne.ToMatrix();
248 double theta = acos(mozilla::clamped(aTwo.w, -1.0, 1.0));
249 double scale = (theta != 0.0) ? 1.0 / sin(theta) : 0.0;
250 theta *= aCoeff;
251 scale *= sin(theta);
253 gfxQuaternion result = gfxQuaternion(scale * aTwo.x, scale * aTwo.y,
254 scale * aTwo.z, cos(theta)) *
255 aOne;
256 return result.ToMatrix();
259 static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
260 const Matrix4x4& aMatrix2,
261 double aProgress) {
262 return aMatrix1;
265 static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
266 const Matrix4x4& aMatrix2, double aCount) {
267 Matrix4x4 result;
268 Servo_MatrixTransform_Operate(MatrixTransformOperator::Accumulate,
269 &aMatrix1.components, &aMatrix2.components,
270 aCount, &result.components);
271 return result;
275 class Interpolate {
276 public:
277 template <typename T>
278 static T operate(const T& aOne, const T& aTwo, double aCoeff) {
279 return aOne + (aTwo - aOne) * aCoeff;
282 static Point4D operateForPerspective(const Point4D& aOne, const Point4D& aTwo,
283 double aCoeff) {
284 return aOne + (aTwo - aOne) * aCoeff;
287 static Point3D operateForScale(const Point3D& aOne, const Point3D& aTwo,
288 double aCoeff) {
289 return aOne + (aTwo - aOne) * aCoeff;
292 static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
293 const gfxQuaternion& aTwo, double aCoeff) {
294 return aOne.Slerp(aTwo, aCoeff).ToMatrix();
297 static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
298 const Matrix4x4& aMatrix2,
299 double aProgress) {
300 return aProgress < 0.5 ? aMatrix1 : aMatrix2;
303 static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
304 const Matrix4x4& aMatrix2, double aProgress) {
305 Matrix4x4 result;
306 Servo_MatrixTransform_Operate(MatrixTransformOperator::Interpolate,
307 &aMatrix1.components, &aMatrix2.components,
308 aProgress, &result.components);
309 return result;
313 template <typename Operator>
314 static void ProcessMatrixOperator(Matrix4x4& aMatrix,
315 const StyleTransform& aFrom,
316 const StyleTransform& aTo, float aProgress,
317 TransformReferenceBox& aRefBox) {
318 float appUnitPerCSSPixel = AppUnitsPerCSSPixel();
319 Matrix4x4 matrix1 = ReadTransforms(aFrom, aRefBox, appUnitPerCSSPixel);
320 Matrix4x4 matrix2 = ReadTransforms(aTo, aRefBox, appUnitPerCSSPixel);
321 aMatrix = Operator::operateByServo(matrix1, matrix2, aProgress) * aMatrix;
324 /* Helper function to process two matrices that we need to interpolate between
326 void ProcessInterpolateMatrix(Matrix4x4& aMatrix,
327 const StyleTransformOperation& aOp,
328 TransformReferenceBox& aRefBox) {
329 const auto& args = aOp.AsInterpolateMatrix();
330 ProcessMatrixOperator<Interpolate>(aMatrix, args.from_list, args.to_list,
331 args.progress._0, aRefBox);
334 void ProcessAccumulateMatrix(Matrix4x4& aMatrix,
335 const StyleTransformOperation& aOp,
336 TransformReferenceBox& aRefBox) {
337 const auto& args = aOp.AsAccumulateMatrix();
338 ProcessMatrixOperator<Accumulate>(aMatrix, args.from_list, args.to_list,
339 args.count, aRefBox);
342 /* Helper function to process a translatex function. */
343 static void ProcessTranslateX(Matrix4x4& aMatrix,
344 const LengthPercentage& aLength,
345 TransformReferenceBox& aRefBox) {
346 Point3D temp;
347 temp.x =
348 ProcessTranslatePart(aLength, &aRefBox, &TransformReferenceBox::Width);
349 aMatrix.PreTranslate(temp);
352 /* Helper function to process a translatey function. */
353 static void ProcessTranslateY(Matrix4x4& aMatrix,
354 const LengthPercentage& aLength,
355 TransformReferenceBox& aRefBox) {
356 Point3D temp;
357 temp.y =
358 ProcessTranslatePart(aLength, &aRefBox, &TransformReferenceBox::Height);
359 aMatrix.PreTranslate(temp);
362 static void ProcessTranslateZ(Matrix4x4& aMatrix, const Length& aLength) {
363 Point3D temp;
364 temp.z = aLength.ToCSSPixels();
365 aMatrix.PreTranslate(temp);
368 /* Helper function to process a translate function. */
369 static void ProcessTranslate(Matrix4x4& aMatrix, const LengthPercentage& aX,
370 const LengthPercentage& aY,
371 TransformReferenceBox& aRefBox) {
372 Point3D temp;
373 temp.x = ProcessTranslatePart(aX, &aRefBox, &TransformReferenceBox::Width);
374 temp.y = ProcessTranslatePart(aY, &aRefBox, &TransformReferenceBox::Height);
375 aMatrix.PreTranslate(temp);
378 static void ProcessTranslate3D(Matrix4x4& aMatrix, const LengthPercentage& aX,
379 const LengthPercentage& aY, const Length& aZ,
380 TransformReferenceBox& aRefBox) {
381 Point3D temp;
383 temp.x = ProcessTranslatePart(aX, &aRefBox, &TransformReferenceBox::Width);
384 temp.y = ProcessTranslatePart(aY, &aRefBox, &TransformReferenceBox::Height);
385 temp.z = aZ.ToCSSPixels();
387 aMatrix.PreTranslate(temp);
390 /* Helper function to set up a scale matrix. */
391 static void ProcessScaleHelper(Matrix4x4& aMatrix, float aXScale, float aYScale,
392 float aZScale) {
393 aMatrix.PreScale(aXScale, aYScale, aZScale);
396 static void ProcessScale3D(Matrix4x4& aMatrix,
397 const StyleTransformOperation& aOp) {
398 const auto& scale = aOp.AsScale3D();
399 ProcessScaleHelper(aMatrix, scale._0, scale._1, scale._2);
402 /* Helper function that, given a set of angles, constructs the appropriate
403 * skew matrix.
405 static void ProcessSkewHelper(Matrix4x4& aMatrix, const StyleAngle& aXAngle,
406 const StyleAngle& aYAngle) {
407 aMatrix.SkewXY(aXAngle.ToRadians(), aYAngle.ToRadians());
410 static void ProcessRotate3D(Matrix4x4& aMatrix, float aX, float aY, float aZ,
411 const StyleAngle& aAngle) {
412 Matrix4x4 temp;
413 temp.SetRotateAxisAngle(aX, aY, aZ, aAngle.ToRadians());
414 aMatrix = temp * aMatrix;
417 static void ProcessPerspective(
418 Matrix4x4& aMatrix,
419 const StyleGenericPerspectiveFunction<Length>& aPerspective) {
420 if (aPerspective.IsNone()) {
421 return;
423 float p = aPerspective.AsLength().ToCSSPixels();
424 if (!std::isinf(p)) {
425 aMatrix.Perspective(std::max(p, 1.0f));
429 static void MatrixForTransformFunction(Matrix4x4& aMatrix,
430 const StyleTransformOperation& aOp,
431 TransformReferenceBox& aRefBox) {
432 /* Get the keyword for the transform. */
433 switch (aOp.tag) {
434 case StyleTransformOperation::Tag::TranslateX:
435 ProcessTranslateX(aMatrix, aOp.AsTranslateX(), aRefBox);
436 break;
437 case StyleTransformOperation::Tag::TranslateY:
438 ProcessTranslateY(aMatrix, aOp.AsTranslateY(), aRefBox);
439 break;
440 case StyleTransformOperation::Tag::TranslateZ:
441 ProcessTranslateZ(aMatrix, aOp.AsTranslateZ());
442 break;
443 case StyleTransformOperation::Tag::Translate:
444 ProcessTranslate(aMatrix, aOp.AsTranslate()._0, aOp.AsTranslate()._1,
445 aRefBox);
446 break;
447 case StyleTransformOperation::Tag::Translate3D:
448 return ProcessTranslate3D(aMatrix, aOp.AsTranslate3D()._0,
449 aOp.AsTranslate3D()._1, aOp.AsTranslate3D()._2,
450 aRefBox);
451 break;
452 case StyleTransformOperation::Tag::ScaleX:
453 ProcessScaleHelper(aMatrix, aOp.AsScaleX(), 1.0f, 1.0f);
454 break;
455 case StyleTransformOperation::Tag::ScaleY:
456 ProcessScaleHelper(aMatrix, 1.0f, aOp.AsScaleY(), 1.0f);
457 break;
458 case StyleTransformOperation::Tag::ScaleZ:
459 ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aOp.AsScaleZ());
460 break;
461 case StyleTransformOperation::Tag::Scale:
462 ProcessScaleHelper(aMatrix, aOp.AsScale()._0, aOp.AsScale()._1, 1.0f);
463 break;
464 case StyleTransformOperation::Tag::Scale3D:
465 ProcessScale3D(aMatrix, aOp);
466 break;
467 case StyleTransformOperation::Tag::SkewX:
468 ProcessSkewHelper(aMatrix, aOp.AsSkewX(), StyleAngle::Zero());
469 break;
470 case StyleTransformOperation::Tag::SkewY:
471 ProcessSkewHelper(aMatrix, StyleAngle::Zero(), aOp.AsSkewY());
472 break;
473 case StyleTransformOperation::Tag::Skew:
474 ProcessSkewHelper(aMatrix, aOp.AsSkew()._0, aOp.AsSkew()._1);
475 break;
476 case StyleTransformOperation::Tag::RotateX:
477 aMatrix.RotateX(aOp.AsRotateX().ToRadians());
478 break;
479 case StyleTransformOperation::Tag::RotateY:
480 aMatrix.RotateY(aOp.AsRotateY().ToRadians());
481 break;
482 case StyleTransformOperation::Tag::RotateZ:
483 aMatrix.RotateZ(aOp.AsRotateZ().ToRadians());
484 break;
485 case StyleTransformOperation::Tag::Rotate:
486 aMatrix.RotateZ(aOp.AsRotate().ToRadians());
487 break;
488 case StyleTransformOperation::Tag::Rotate3D:
489 ProcessRotate3D(aMatrix, aOp.AsRotate3D()._0, aOp.AsRotate3D()._1,
490 aOp.AsRotate3D()._2, aOp.AsRotate3D()._3);
491 break;
492 case StyleTransformOperation::Tag::Matrix:
493 ProcessMatrix(aMatrix, aOp);
494 break;
495 case StyleTransformOperation::Tag::Matrix3D:
496 ProcessMatrix3D(aMatrix, aOp);
497 break;
498 case StyleTransformOperation::Tag::InterpolateMatrix:
499 ProcessInterpolateMatrix(aMatrix, aOp, aRefBox);
500 break;
501 case StyleTransformOperation::Tag::AccumulateMatrix:
502 ProcessAccumulateMatrix(aMatrix, aOp, aRefBox);
503 break;
504 case StyleTransformOperation::Tag::Perspective:
505 ProcessPerspective(aMatrix, aOp.AsPerspective());
506 break;
507 default:
508 MOZ_ASSERT_UNREACHABLE("Unknown transform function!");
512 Matrix4x4 ReadTransforms(const StyleTransform& aTransform,
513 TransformReferenceBox& aRefBox,
514 float aAppUnitsPerMatrixUnit) {
515 Matrix4x4 result;
517 for (const StyleTransformOperation& op : aTransform.Operations()) {
518 MatrixForTransformFunction(result, op, aRefBox);
521 float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
522 result.PreScale(1 / scale, 1 / scale, 1 / scale);
523 result.PostScale(scale, scale, scale);
525 return result;
528 static void ProcessTranslate(Matrix4x4& aMatrix,
529 const StyleTranslate& aTranslate,
530 TransformReferenceBox& aRefBox) {
531 switch (aTranslate.tag) {
532 case StyleTranslate::Tag::None:
533 return;
534 case StyleTranslate::Tag::Translate:
535 return ProcessTranslate3D(aMatrix, aTranslate.AsTranslate()._0,
536 aTranslate.AsTranslate()._1,
537 aTranslate.AsTranslate()._2, aRefBox);
538 default:
539 MOZ_ASSERT_UNREACHABLE("Huh?");
543 static void ProcessRotate(Matrix4x4& aMatrix, const StyleRotate& aRotate) {
544 switch (aRotate.tag) {
545 case StyleRotate::Tag::None:
546 return;
547 case StyleRotate::Tag::Rotate:
548 aMatrix.RotateZ(aRotate.AsRotate().ToRadians());
549 return;
550 case StyleRotate::Tag::Rotate3D:
551 return ProcessRotate3D(aMatrix, aRotate.AsRotate3D()._0,
552 aRotate.AsRotate3D()._1, aRotate.AsRotate3D()._2,
553 aRotate.AsRotate3D()._3);
554 default:
555 MOZ_ASSERT_UNREACHABLE("Huh?");
559 static void ProcessScale(Matrix4x4& aMatrix, const StyleScale& aScale) {
560 switch (aScale.tag) {
561 case StyleScale::Tag::None:
562 return;
563 case StyleScale::Tag::Scale:
564 return ProcessScaleHelper(aMatrix, aScale.AsScale()._0,
565 aScale.AsScale()._1, aScale.AsScale()._2);
566 default:
567 MOZ_ASSERT_UNREACHABLE("Huh?");
571 Matrix4x4 ReadTransforms(const StyleTranslate& aTranslate,
572 const StyleRotate& aRotate, const StyleScale& aScale,
573 const ResolvedMotionPathData* aMotion,
574 const StyleTransform& aTransform,
575 TransformReferenceBox& aRefBox,
576 float aAppUnitsPerMatrixUnit) {
577 Matrix4x4 result;
579 ProcessTranslate(result, aTranslate, aRefBox);
580 ProcessRotate(result, aRotate);
581 ProcessScale(result, aScale);
583 if (aMotion) {
584 // Create the equivalent translate and rotate function, according to the
585 // order in spec. We combine the translate and then the rotate.
586 // https://drafts.fxtf.org/motion-1/#calculating-path-transform
588 // Besides, we have to shift the object by the delta between anchor-point
589 // and transform-origin, to make sure we rotate the object according to
590 // anchor-point.
591 result.PreTranslate(aMotion->mTranslate.x + aMotion->mShift.x,
592 aMotion->mTranslate.y + aMotion->mShift.y, 0.0);
593 if (aMotion->mRotate != 0.0) {
594 result.RotateZ(aMotion->mRotate);
596 // Shift the origin back to transform-origin.
597 result.PreTranslate(-aMotion->mShift.x, -aMotion->mShift.y, 0.0);
600 for (const StyleTransformOperation& op : aTransform.Operations()) {
601 MatrixForTransformFunction(result, op, aRefBox);
604 float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
605 result.PreScale(1 / scale, 1 / scale, 1 / scale);
606 result.PostScale(scale, scale, scale);
608 return result;
611 mozilla::CSSPoint Convert2DPosition(const mozilla::LengthPercentage& aX,
612 const mozilla::LengthPercentage& aY,
613 const CSSSize& aSize) {
614 return {
615 aX.ResolveToCSSPixels(aSize.width),
616 aY.ResolveToCSSPixels(aSize.height),
620 CSSPoint Convert2DPosition(const LengthPercentage& aX,
621 const LengthPercentage& aY,
622 TransformReferenceBox& aRefBox) {
623 return {
624 aX.ResolveToCSSPixelsWith(
625 [&] { return CSSPixel::FromAppUnits(aRefBox.Width()); }),
626 aY.ResolveToCSSPixelsWith(
627 [&] { return CSSPixel::FromAppUnits(aRefBox.Height()); }),
631 Point Convert2DPosition(const LengthPercentage& aX, const LengthPercentage& aY,
632 TransformReferenceBox& aRefBox,
633 int32_t aAppUnitsPerPixel) {
634 float scale = mozilla::AppUnitsPerCSSPixel() / float(aAppUnitsPerPixel);
635 CSSPoint p = Convert2DPosition(aX, aY, aRefBox);
636 return {p.x * scale, p.y * scale};
639 } // namespace nsStyleTransformMatrix