Merge "Add new class LineHeightSpan.Standard"
diff --git a/api/current.txt b/api/current.txt
index 568f4c4..d88cf0d 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -44486,6 +44486,11 @@
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);
}
diff --git a/core/java/android/text/style/LineHeightSpan.java b/core/java/android/text/style/LineHeightSpan.java
index 50ee5f3..2742ae0 100644
--- a/core/java/android/text/style/LineHeightSpan.java
+++ b/core/java/android/text/style/LineHeightSpan.java
@@ -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 @@
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 @@
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;
+ }
+ }
}