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

Commit ba9126c4 authored by Tyler Freeman's avatar Tyler Freeman
Browse files

fix(non linear font scaling): add a setLineHeight() that accepts explicit units.

This is so the TextView can properly calculate a proportional line
height when non-linear font scaling is in effect. Otherwise, a desired
2x line height at font scale 1.0 will not be 2x at font scale 2.0, due
to non-linear font scaling compressing higher SP sizes.

It also better mirrors the setTextSize() API.

The actual fix for calculating the proportional sizes will come in a
subsequent CL.

Bug: 273326061
Test: atest cts/tests/tests/widget/src/android/widget/cts/TextViewTest.java

Change-Id: I79d74ff4f5fe1886ca2ed9c4df708be484fd888f
parent 3d2226d2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -60332,6 +60332,7 @@ package android.widget {
    method public void setLineBreakStyle(int);
    method public void setLineBreakWordStyle(int);
    method public void setLineHeight(@IntRange(from=0) @Px int);
    method public void setLineHeight(int, @FloatRange(from=0) float);
    method public void setLineSpacing(float, float);
    method public void setLines(int);
    method public final void setLinkTextColor(@ColorInt int);
+33 −3
Original line number Diff line number Diff line
@@ -4604,7 +4604,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        }
    }
    private void setTextSizeInternal(int unit, float size, boolean shouldRequestLayout) {
    @NonNull
    private DisplayMetrics getDisplayMetricsOrSystem() {
        Context c = getContext();
        Resources r;
@@ -4614,8 +4615,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            r = c.getResources();
        }
        return r.getDisplayMetrics();
    }
    private void setTextSizeInternal(int unit, float size, boolean shouldRequestLayout) {
        mTextSizeUnit = unit;
        setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()),
        setRawTextSize(TypedValue.applyDimension(unit, size, getDisplayMetricsOrSystem()),
                shouldRequestLayout);
    }
@@ -6197,16 +6202,41 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     */
    @android.view.RemotableViewMethod
    public void setLineHeight(@Px @IntRange(from = 0) int lineHeight) {
        Preconditions.checkArgumentNonnegative(lineHeight);
        setLineHeightPx(lineHeight);
    }
    private void setLineHeightPx(@Px @FloatRange(from = 0) float lineHeight) {
        Preconditions.checkArgumentNonnegative((int) lineHeight);
        final int fontHeight = getPaint().getFontMetricsInt(null);
        // Make sure we don't setLineSpacing if it's not needed to avoid unnecessary redraw.
        // TODO(b/274974975): should this also check if lineSpacing needs to change?
        if (lineHeight != fontHeight) {
            // Set lineSpacingExtra by the difference of lineSpacing with lineHeight
            setLineSpacing(lineHeight - fontHeight, 1f);
        }
    }
    /**
     * Sets an explicit line height to a given unit and value for this TextView. This is equivalent
     * to the vertical distance between subsequent baselines in the TextView. See {@link
     * TypedValue} for the possible dimension units.
     *
     * @param unit The desired dimension unit. SP units are strongly recommended so that line height
     *             stays proportional to the text size when fonts are scaled up for accessibility.
     * @param lineHeight The desired line height in the given units.
     *
     * @see #setLineSpacing(float, float)
     * @see #getLineSpacingExtra()
     *
     * @attr ref android.R.styleable#TextView_lineHeight
     */
    @android.view.RemotableViewMethod
    public void setLineHeight(int unit, @FloatRange(from = 0) float lineHeight) {
        setLineHeightPx(
                TypedValue.applyDimension(unit, lineHeight, getDisplayMetricsOrSystem()));
    }
    /**
     * Set Highlights
     *