Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e6256e8 authored by Tyler Freeman's avatar Tyler Freeman Committed by Automerger Merge Worker
Browse files

feat(non linear font scaling): add...

feat(non linear font scaling): add FontScaleConverterFactory.isNonLinearFontScalingActive() am: da945139 am: ae84a33a

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22537770



Change-Id: If31cbfbbfdcfcc6bd9bb8849a0d073cfc6d27a08
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 42a82a15 ae84a33a
Loading
Loading
Loading
Loading
+22 −4
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ public class FontScaleConverterFactory {
    @VisibleForTesting
    static final SparseArray<FontScaleConverter> LOOKUP_TABLES = new SparseArray<>();

    private static float sMinScaleBeforeCurvesApplied = 1.05f;

    static {
        // These were generated by frameworks/base/tools/fonts/font-scaling-array-generator.js and
        // manually tweaked for optimum readability.
@@ -82,10 +84,29 @@ public class FontScaleConverterFactory {
                        new float[] {  16f,   20f,   24f,   26f,   30f,   34f,   36f,   38f,  100})
        );

        sMinScaleBeforeCurvesApplied = getScaleFromKey(LOOKUP_TABLES.keyAt(0)) - 0.02f;
        if (sMinScaleBeforeCurvesApplied <= 1.0f) {
            throw new IllegalStateException(
                    "You should only apply non-linear scaling to font scales > 1"
            );
        }
    }

    private FontScaleConverterFactory() {}

    /**
     * Returns true if non-linear font scaling curves would be in effect for the given scale, false
     * if the scaling would follow a linear curve or for no scaling.
     *
     * <p>Example usage:
     * <code>isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)</code>
     *
     * @hide
     */
    public static boolean isNonLinearFontScalingActive(float fontScale) {
        return fontScale >= sMinScaleBeforeCurvesApplied;
    }

    /**
     * Finds a matching FontScaleConverter for the given fontScale factor.
     *
@@ -97,10 +118,7 @@ public class FontScaleConverterFactory {
     */
    @Nullable
    public static FontScaleConverter forScale(float fontScale) {
        if (fontScale <= 1) {
            // We don't need non-linear curves for shrinking text or for 100%.
            // Also, fontScale==0 should not have a curve either.
            // And ignore negative font scales; that's just silly.
        if (!isNonLinearFontScalingActive(fontScale)) {
            return null;
        }

+16 −0
Original line number Diff line number Diff line
@@ -122,6 +122,22 @@ class FontScaleConverterFactoryTest {
        }
    }

    @SmallTest
    fun testIsNonLinearFontScalingActive() {
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(0f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(-1f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(0.85f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1.02f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1.10f)).isFalse()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1.15f)).isTrue()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1.1499999f))
                .isTrue()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(1.5f)).isTrue()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(2f)).isTrue()
        assertThat(FontScaleConverterFactory.isNonLinearFontScalingActive(3f)).isTrue()
    }

    @LargeTest
    @Test
    fun allFeasibleScalesAndConversionsDoNotCrash() {