diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt index fdb0b828bec9..2acb45d374a0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt @@ -32,6 +32,56 @@ import kotlin.math.ceil import kotlin.math.floor import kotlin.math.round +private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float = + if (lengthPercentage.type == LengthPercentageType.PERCENT) { + lengthPercentage.resolve(availableSpace) + } else { + lengthPercentage.resolve(availableSpace).dpToPx() + } + +internal fun calculateBackgroundImageSize( + containerWidth: Float, + containerHeight: Float, + imageWidth: Float, + imageHeight: Float, + backgroundSize: BackgroundSize?, + repeat: BackgroundRepeat?, +): Pair { + var finalWidth = imageWidth + var finalHeight = imageHeight + + if (backgroundSize is BackgroundSize.LengthPercentageAuto) { + val w = backgroundSize.lengthPercentage.x + val h = backgroundSize.lengthPercentage.y + if (w != null) { + finalWidth = positionToPixels(w, containerWidth) + } + if (h != null) { + finalHeight = positionToPixels(h, containerHeight) + } + } + + if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) { + if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) { + val numRepeats = round(containerWidth / finalWidth) + if (numRepeats > 0) { + finalWidth = containerWidth / numRepeats + } + } + } + + if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) { + if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) { + val numRepeats = round(containerHeight / finalHeight) + if (numRepeats > 0) { + finalHeight = containerHeight / numRepeats + } + } + } + + return finalWidth to finalHeight +} + internal class BackgroundImageDrawable( private val context: Context, /* @@ -343,54 +393,6 @@ internal class BackgroundImageDrawable( } } - private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float = - if (lengthPercentage.type == LengthPercentageType.PERCENT) { - lengthPercentage.resolve(availableSpace) - } else { - lengthPercentage.resolve(availableSpace).dpToPx() - } - - private fun calculateBackgroundImageSize( - containerWidth: Float, - containerHeight: Float, - imageWidth: Float, - imageHeight: Float, - backgroundSize: BackgroundSize?, - repeat: BackgroundRepeat?, - ): Pair { - var finalWidth = imageWidth - var finalHeight = imageHeight - - if (backgroundSize is BackgroundSize.LengthPercentageAuto) { - val w = backgroundSize.lengthPercentage.x - val h = backgroundSize.lengthPercentage.y - if (w != null && h != null) { - finalWidth = positionToPixels(w, containerWidth) - finalHeight = positionToPixels(h, containerHeight) - } - } - - if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) { - if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) { - val numRepeats = round(containerWidth / finalWidth) - if (numRepeats > 0) { - finalWidth = containerWidth / numRepeats - } - } - } - - if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) { - if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) { - val numRepeats = round(containerHeight / finalHeight) - if (numRepeats > 0) { - finalHeight = containerHeight / numRepeats - } - } - } - - return finalWidth to finalHeight - } - private fun calculateBackgroundPosition( tileWidth: Float, tileHeight: Float, diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt new file mode 100644 index 000000000000..713b257c989f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager.drawable + +import com.facebook.react.uimanager.LengthPercentage +import com.facebook.react.uimanager.LengthPercentageType +import com.facebook.react.uimanager.style.BackgroundSize +import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class BackgroundImageDrawableTest { + @Test + fun autoYAxisKeepsContainerHeight() { + val size = + BackgroundSize.LengthPercentageAuto( + BackgroundSizeLengthPercentage( + LengthPercentage(10f, LengthPercentageType.PERCENT), + null, + ), + ) + + val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null) + + assertThat(result).isEqualTo(20f to 100f) + } + + @Test + fun explicitAxesAreResolvedIndependently() { + val size = + BackgroundSize.LengthPercentageAuto( + BackgroundSizeLengthPercentage( + LengthPercentage(25f, LengthPercentageType.PERCENT), + LengthPercentage(12f, LengthPercentageType.PERCENT), + ), + ) + + val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null) + + assertThat(result).isEqualTo(50f to 12f) + } +}