Skip to content

Commit 74e54cb

Browse files
strindhaugfacebook-github-bot
authored andcommitted
Fix: incorrect line-height calculation
Summary: There seems to be a rounding error in the android code for line height, so that for some fonts and at some combinations of line height and font size the actual height of the elements seems to be slightly too short. I've identified one issue that I mentioned here #10712 (comment) that could at least explain some of the problem. That when the line-height minus the original sum of the absolute value of top and bottom from the metrics, happens to be an odd number, the division by two causes a rounding error of 1, so that the actual line height is 1pt less than it should. The fix uses floating point division instead of integer division, and rounds (arbitrarily) the negative values up and the positive values down so that the total is still the correct for odd numbers. It turns out that only ascent and descent is used to give the actual line-height between lines in the same text-element. The top and bottom values are only used for padding the top and bottom of the text. So when the line-height is greater than the font size and the extra padding this PR sets the ascent and descent to the same value as the top and bottom respectively. I've renamed the shouldIncreaseAllMetricsProportionally test to evenLineHeightShouldIncreaseAllMetricsProportionally and added an extra assertion to check that bottom-top still equals the line height. Added another test oddLineHeightShouldAlsoWork that is similar but uses an odd number for the line height to test that it still works with odd numbers. This test only uses the sum of the values so that it's indifferent to what value the implementation chooses to round up or down. Improvement on #16448 Fix line-height calculation on Android. | Before | After | | ------------- |-------------| | ![without fix](https://user-images.githubusercontent.com/2144849/36150230-4404a0cc-10c3-11e8-8880-4ab84339c741.png) | ![actual fix](https://user-images.githubusercontent.com/2144849/36156620-eb496d0e-10d7-11e8-8bd1-1cb536a38fbf.png) | (All three columns have font size 16 and lineHeight: 32. The first one is has fixed height 9*32, the second is 9 Text elements, the last is one text element with lots of text limited to 9 lines, so they should be the same height. ) Closes #17952 Differential Revision: D6980333 Pulled By: hramos fbshipit-source-id: 0a501358cfbf7f139fca46056d0d972b1daf6ae3
1 parent 331cc79 commit 74e54cb

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ public void chooseHeight(
5555
// Show proportionally additional ascent / top & descent / bottom
5656
final int additional = mHeight - (-fm.top + fm.bottom);
5757

58-
fm.top -= additional / 2;
59-
fm.ascent -= additional / 2;
60-
fm.descent += additional / 2;
61-
fm.bottom += additional / 2;
58+
// Round up for the negative values and down for the positive values (arbritary choice)
59+
// So that bottom - top equals additional even if it's an odd number.
60+
fm.top -= Math.ceil(additional / 2.0f);
61+
fm.bottom += Math.floor(additional / 2.0f);
62+
fm.ascent = fm.top;
63+
fm.descent = fm.bottom;
6264
}
6365
}
6466
}

ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,35 @@
1313
public class CustomLineHeightSpanTest {
1414

1515
@Test
16-
public void shouldIncreaseAllMetricsProportionally() {
16+
public void evenLineHeightShouldIncreaseAllMetricsProportionally() {
1717
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(22);
1818
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
1919
fm.top = -10;
2020
fm.ascent = -5;
2121
fm.descent = 5;
2222
fm.bottom = 10;
2323
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
24+
// Since line height is even it should be equally added to top and bottom.
2425
assertThat(fm.top).isEqualTo(-11);
25-
assertThat(fm.ascent).isEqualTo(-6);
26-
assertThat(fm.descent).isEqualTo(6);
26+
assertThat(fm.ascent).isEqualTo(-11);
27+
assertThat(fm.descent).isEqualTo(11);
2728
assertThat(fm.bottom).isEqualTo(11);
29+
assertThat(fm.bottom - fm.top).isEqualTo(22);
30+
}
31+
32+
@Test
33+
public void oddLineHeightShouldAlsoWork() {
34+
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(23);
35+
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
36+
fm.top = -10;
37+
fm.ascent = -5;
38+
fm.descent = 5;
39+
fm.bottom = 10;
40+
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
41+
// Only test that the sum is correct so the implementation
42+
// is free to add the odd value either on top or bottom.
43+
assertThat(fm.descent - fm.ascent).isEqualTo(23);
44+
assertThat(fm.bottom - fm.top).isEqualTo(23);
2845
}
2946

3047
@Test

0 commit comments

Comments
 (0)