Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inverted descent/ascent Android prioritisation to match iOS lineHeight behaviour #16448

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// Copyright 2004-present Facebook. All Rights Reserved.
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

package com.facebook.react.views.text;

Expand Down Expand Up @@ -27,16 +34,16 @@ public void chooseHeight(
// This is more complicated that I wanted it to be. You can find a good explanation of what the
// FontMetrics mean here: http://stackoverflow.com/questions/27631736.
// The general solution is that if there's not enough height to show the full line height, we
// will prioritize in this order: ascent, descent, bottom, top
// will prioritize in this order: descent, ascent, bottom, top

if (-fm.ascent > mHeight) {
// Show as much ascent as possible
fm.top = fm.ascent = -mHeight;
fm.bottom = fm.descent = 0;
if (fm.descent > mHeight) {
// Show as much descent as possible
fm.bottom = fm.descent = Math.min(mHeight, fm.descent);
fm.top = fm.ascent = 0;
} else if (-fm.ascent + fm.descent > mHeight) {
// Show all ascent, and as much descent as possible
fm.top = fm.ascent;
fm.bottom = fm.descent = mHeight + fm.ascent;
// Show all descent, and as much ascent as possible
fm.bottom = fm.descent;
fm.top = fm.ascent = -mHeight + fm.descent;
} else if (-fm.ascent + fm.bottom > mHeight) {
// Show all ascent, descent, as much bottom as possible
fm.top = fm.ascent;
Expand All @@ -45,10 +52,13 @@ public void chooseHeight(
// Show all ascent, descent, bottom, as much top as possible
fm.top = fm.bottom - mHeight;
} else {
// Show proportionally additional ascent and top
// Show proportionally additional ascent / top & descent / bottom
final int additional = mHeight - (-fm.top + fm.bottom);
fm.top -= additional;
fm.ascent -= additional;

fm.top -= additional / 2;
fm.ascent -= additional / 2;
fm.descent += additional / 2;
fm.bottom += additional / 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using integers? This will cause rounding errors since aditional is an int and 2 is an int so anything that is not divisible by two will be rounded down. (See: #10712 (comment) )

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, for us it has solved the problems in the design completely, but I see that there is still something wrong for your design case. Maybe you can open up a new issue for this and see if you can get the attention of other developers, linking to these issues, to get your specific case fixed. Even better to open a new issue, with a PR that fixes it.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.facebook.react.views.text;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the license header, mind adding this so we can land this change?


import android.graphics.Paint;

import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public class CustomLineHeightSpanTest {

@Test
public void shouldIncreaseAllMetricsProportionally() {
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(22);
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
fm.top = -10;
fm.ascent = -5;
fm.descent = 5;
fm.bottom = 10;
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
assertThat(fm.top).isEqualTo(-11);
assertThat(fm.ascent).isEqualTo(-6);
assertThat(fm.descent).isEqualTo(6);
assertThat(fm.bottom).isEqualTo(11);
}

@Test
public void shouldReduceTopFirst() {
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(19);
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
fm.top = -10;
fm.ascent = -5;
fm.descent = 5;
fm.bottom = 10;
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
assertThat(fm.top).isEqualTo(-9);
assertThat(fm.ascent).isEqualTo(-5);
assertThat(fm.descent).isEqualTo(5);
assertThat(fm.bottom).isEqualTo(10);
}

@Test
public void shouldReduceBottomSecond() {
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(14);
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
fm.top = -10;
fm.ascent = -5;
fm.descent = 5;
fm.bottom = 10;
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
assertThat(fm.top).isEqualTo(-5);
assertThat(fm.ascent).isEqualTo(-5);
assertThat(fm.descent).isEqualTo(5);
assertThat(fm.bottom).isEqualTo(9);
}

@Test
public void shouldReduceAscentThird() {
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(9);
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
fm.top = -10;
fm.ascent = -5;
fm.descent = 5;
fm.bottom = 10;
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
assertThat(fm.top).isEqualTo(-4);
assertThat(fm.ascent).isEqualTo(-4);
assertThat(fm.descent).isEqualTo(5);
assertThat(fm.bottom).isEqualTo(5);
}

@Test
public void shouldReduceDescentLast() {
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(4);
Paint.FontMetricsInt fm = new Paint.FontMetricsInt();
fm.top = -10;
fm.ascent = -5;
fm.descent = 5;
fm.bottom = 10;
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm);
assertThat(fm.top).isEqualTo(0);
assertThat(fm.ascent).isEqualTo(0);
assertThat(fm.descent).isEqualTo(4);
assertThat(fm.bottom).isEqualTo(4);
}
}