diff --git a/ReactCommon/fabric/graphics/Transform.cpp b/ReactCommon/fabric/graphics/Transform.cpp index a9b51ea5ca5..2ae68a7e40c 100644 --- a/ReactCommon/fabric/graphics/Transform.cpp +++ b/ReactCommon/fabric/graphics/Transform.cpp @@ -180,5 +180,25 @@ Point operator*(Point const &point, Transform const &transform) { return result; } +Rect operator*(Rect const &rect, Transform const &transform) { + auto transformedSize = rect.size * transform; + auto pointAdjustment = Point{(rect.size.width - transformedSize.width) / 2, + (rect.size.height - transformedSize.height) / 2}; + + return {rect.origin + pointAdjustment, transformedSize}; +} + +Size operator*(Size const &size, Transform const &transform) { + if (transform == Transform::Identity()) { + return size; + } + + auto result = Size{}; + result.width = transform.at(0, 0) * size.width; + result.height = transform.at(1, 1) * size.height; + + return result; +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/graphics/Transform.h b/ReactCommon/fabric/graphics/Transform.h index 1f03e8f3140..85ffc338475 100644 --- a/ReactCommon/fabric/graphics/Transform.h +++ b/ReactCommon/fabric/graphics/Transform.h @@ -79,6 +79,17 @@ struct Transform { */ Point operator*(Point const &point, Transform const &transform); +/* + * Applies tranformation to the given size. + */ +Size operator*(Size const &size, Transform const &transform); + +/* + * Applies tranformation to the given rect. + * ONLY SUPPORTS scale transformation. + */ +Rect operator*(Rect const &rect, Transform const &transform); + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/graphics/tests/TransformTest.cpp b/ReactCommon/fabric/graphics/tests/TransformTest.cpp new file mode 100644 index 00000000000..0fe7a782cef --- /dev/null +++ b/ReactCommon/fabric/graphics/tests/TransformTest.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +using namespace facebook::react; + +TEST(TransformTest, transformingSize) { + auto size = facebook::react::Size{100, 200}; + auto scaledSize = size * Transform::Scale(0.5, 0.5, 1); + + EXPECT_EQ(scaledSize.width, 50); + EXPECT_EQ(scaledSize.height, 100); +} + +TEST(TransformTest, transformingPoint) { + auto point = facebook::react::Point{100, 200}; + auto translatedPoint = point * Transform::Translate(-50, -100, 0); + + EXPECT_EQ(translatedPoint.x, 50); + EXPECT_EQ(translatedPoint.y, 100); +} + +TEST(TransformTest, transformingRect) { + auto point = facebook::react::Point{100, 200}; + auto size = facebook::react::Size{300, 400}; + auto rect = facebook::react::Rect{point, size}; + + auto transformedRect = rect * Transform::Scale(0.5, 0.5, 1); + + EXPECT_EQ(transformedRect.origin.x, 175); + EXPECT_EQ(transformedRect.origin.y, 300); + EXPECT_EQ(transformedRect.size.width, 150); + EXPECT_EQ(transformedRect.size.height, 200); +}