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

Commit 444be8bd authored by Haoyu Zhang's avatar Haoyu Zhang
Browse files

Add new class LineHeightSpan.Standard

Add default implementation of interface LineHeightSpan

Bug: 112621694
Test: atest StaticLayoutTest LineHeightSpan_StandardTest
Change-Id: I21ec0d8f72b7234d87bf25e5e6fe6cb7c19953a7
parent 2943823a
Loading
Loading
Loading
Loading
+5 −0
Original line number Original line Diff line number Diff line
@@ -44483,6 +44483,11 @@ package android.text.style {
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt);
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt);
  }
  }
  public static class LineHeightSpan.Standard implements android.text.style.LineHeightSpan {
    ctor public LineHeightSpan.Standard(int);
    method public void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt);
  }
  public static abstract interface LineHeightSpan.WithDensity implements android.text.style.LineHeightSpan {
  public static abstract interface LineHeightSpan.WithDensity implements android.text.style.LineHeightSpan {
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt, android.text.TextPaint);
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt, android.text.TextPaint);
  }
  }
+42 −3
Original line number Original line Diff line number Diff line
@@ -16,11 +16,16 @@


package android.text.style;
package android.text.style;


import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Px;
import android.graphics.Paint;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextPaint;


import com.android.internal.util.Preconditions;

/**
/**
 * The classes that affect the height of the line should implement this interface.
 * The classes that affect the line height of paragraph should implement this interface.
 */
 */
public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
    /**
    /**
@@ -38,8 +43,8 @@ public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
            Paint.FontMetricsInt fm);
            Paint.FontMetricsInt fm);


    /**
    /**
     * The classes that affect the height of the line with respect to density, should implement this
     * The classes that affect the line height of paragraph with respect to density,
     * interface.
     * should implement this interface.
     */
     */
    public interface WithDensity extends LineHeightSpan {
    public interface WithDensity extends LineHeightSpan {


@@ -57,4 +62,38 @@ public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
                int spanstartv, int lineHeight,
                int spanstartv, int lineHeight,
                Paint.FontMetricsInt fm, TextPaint paint);
                Paint.FontMetricsInt fm, TextPaint paint);
    }
    }

    /**
     * Default implementation of the {@link LineHeightSpan}, which changes the line height of the
     * attached paragraph.
     * <p>
     * LineHeightSpan will change the line height of the entire paragraph, even though it
     * covers only part of the paragraph.
     * </p>
     */
    class Standard implements LineHeightSpan {

        private final @Px int mHeight;
        /**
         * Set the line height of the paragraph to <code>height</code> physical pixels.
         */
        public Standard(@Px @IntRange(from = 1) int height) {
            Preconditions.checkArgument(height > 0, "Height:" + height + "must be positive");
            mHeight = height;
        }

        @Override
        public void chooseHeight(@NonNull CharSequence text, int start, int end,
                int spanstartv, int lineHeight,
                @NonNull Paint.FontMetricsInt fm) {
            final int originHeight = fm.descent - fm.ascent;
            // If original height is not positive, do nothing.
            if (originHeight <= 0) {
                return;
            }
            final float ratio = mHeight * 1.0f / originHeight;
            fm.descent = Math.round(fm.descent * ratio);
            fm.ascent = fm.descent - mHeight;
        }
    }
}
}