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

Commit 1a05d886 authored by Haoyu Zhang's avatar Haoyu Zhang Committed by Android (Google) Code Review
Browse files

Merge "Add new class LineHeightSpan.Standard"

parents b0b67fc1 444be8bd
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -44486,6 +44486,11 @@ package android.text.style {
    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 {
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt, android.text.TextPaint);
  }
+42 −3
Original line number Diff line number Diff line
@@ -16,11 +16,16 @@

package android.text.style;

import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Px;
import android.graphics.Paint;
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 {
    /**
@@ -38,8 +43,8 @@ public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
            Paint.FontMetricsInt fm);

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

@@ -57,4 +62,38 @@ public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan {
                int spanstartv, int lineHeight,
                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;
        }
    }
}