From 011152318a2cc55f1311fa92642734e45caa375e Mon Sep 17 00:00:00 2001 From: Jakub Trzebiatowski Date: Thu, 25 Jan 2024 04:40:49 -0800 Subject: [PATCH] `CustomLineHeightSpan`: Increase the readability (#42592) Summary: Increase the readability of `CustomLineHeightSpan` by making the logic less stateful. This is a minor improvement in the context of my multi-PR work on https://github.com/react-native-community/discussions-and-proposals/issues/695. ## Changelog: [INTERNAL] [CHANGED] - Increase the readability of `CustomLineHeightSpan` Pull Request resolved: https://github.com/facebook/react-native/pull/42592 Test Plan: - Prove the equivalence of the old and the new logic - Test that the behavior of `lineHeight` doesn't change Reviewed By: NickGerleman Differential Revision: D53028467 Pulled By: mdvacca fbshipit-source-id: d533bb77c8e10c29d8f2acc8cc39565d0013b03b --- .../react/views/text/CustomLineHeightSpan.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java index 46293eb6bcff32..67ede6245a1ea2 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java @@ -54,10 +54,13 @@ public void chooseHeight( // Round up for the negative values and down for the positive values (arbitrary choice) // So that bottom - top equals additional even if it's an odd number. - fm.top -= Math.ceil(additional / 2.0f); - fm.bottom += Math.floor(additional / 2.0f); - fm.ascent = fm.top; - fm.descent = fm.bottom; + final int top = (int) (fm.top - Math.ceil(additional / 2.0f)); + final int bottom = (int) (fm.bottom + Math.floor(additional / 2.0f)); + + fm.top = top; + fm.ascent = top; + fm.descent = bottom; + fm.bottom = bottom; } } }