mirror of
https://github.com/androidx/media.git
synced 2025-05-16 12:09:50 +08:00
Add RelativeSizeSpan support to SpannedHtmlConverter
PiperOrigin-RevId: 309391401
This commit is contained in:
parent
192b6832b6
commit
e11ae734ed
@ -22,6 +22,7 @@ import android.text.Spanned;
|
|||||||
import android.text.style.AbsoluteSizeSpan;
|
import android.text.style.AbsoluteSizeSpan;
|
||||||
import android.text.style.BackgroundColorSpan;
|
import android.text.style.BackgroundColorSpan;
|
||||||
import android.text.style.ForegroundColorSpan;
|
import android.text.style.ForegroundColorSpan;
|
||||||
|
import android.text.style.RelativeSizeSpan;
|
||||||
import android.text.style.StyleSpan;
|
import android.text.style.StyleSpan;
|
||||||
import android.text.style.TypefaceSpan;
|
import android.text.style.TypefaceSpan;
|
||||||
import android.text.style.UnderlineSpan;
|
import android.text.style.UnderlineSpan;
|
||||||
@ -145,6 +146,9 @@ import java.util.regex.Pattern;
|
|||||||
? absoluteSizeSpan.getSize()
|
? absoluteSizeSpan.getSize()
|
||||||
: absoluteSizeSpan.getSize() / displayDensity;
|
: absoluteSizeSpan.getSize() / displayDensity;
|
||||||
return Util.formatInvariant("<span style='font-size:%.2fpx;'>", sizeCssPx);
|
return Util.formatInvariant("<span style='font-size:%.2fpx;'>", sizeCssPx);
|
||||||
|
} else if (span instanceof RelativeSizeSpan) {
|
||||||
|
return Util.formatInvariant(
|
||||||
|
"<span style='font-size:%.2f%%;'>", ((RelativeSizeSpan) span).getSizeChange());
|
||||||
} else if (span instanceof TypefaceSpan) {
|
} else if (span instanceof TypefaceSpan) {
|
||||||
@Nullable String fontFamily = ((TypefaceSpan) span).getFamily();
|
@Nullable String fontFamily = ((TypefaceSpan) span).getFamily();
|
||||||
return fontFamily != null
|
return fontFamily != null
|
||||||
@ -185,7 +189,8 @@ import java.util.regex.Pattern;
|
|||||||
if (span instanceof ForegroundColorSpan
|
if (span instanceof ForegroundColorSpan
|
||||||
|| span instanceof BackgroundColorSpan
|
|| span instanceof BackgroundColorSpan
|
||||||
|| span instanceof HorizontalTextInVerticalContextSpan
|
|| span instanceof HorizontalTextInVerticalContextSpan
|
||||||
|| span instanceof AbsoluteSizeSpan) {
|
|| span instanceof AbsoluteSizeSpan
|
||||||
|
|| span instanceof RelativeSizeSpan) {
|
||||||
return "</span>";
|
return "</span>";
|
||||||
} else if (span instanceof TypefaceSpan) {
|
} else if (span instanceof TypefaceSpan) {
|
||||||
@Nullable String fontFamily = ((TypefaceSpan) span).getFamily();
|
@Nullable String fontFamily = ((TypefaceSpan) span).getFamily();
|
||||||
|
@ -25,6 +25,7 @@ import android.text.Spanned;
|
|||||||
import android.text.style.AbsoluteSizeSpan;
|
import android.text.style.AbsoluteSizeSpan;
|
||||||
import android.text.style.BackgroundColorSpan;
|
import android.text.style.BackgroundColorSpan;
|
||||||
import android.text.style.ForegroundColorSpan;
|
import android.text.style.ForegroundColorSpan;
|
||||||
|
import android.text.style.RelativeSizeSpan;
|
||||||
import android.text.style.StyleSpan;
|
import android.text.style.StyleSpan;
|
||||||
import android.text.style.TypefaceSpan;
|
import android.text.style.TypefaceSpan;
|
||||||
import android.text.style.UnderlineSpan;
|
import android.text.style.UnderlineSpan;
|
||||||
@ -130,6 +131,20 @@ public class SpannedToHtmlConverterTest {
|
|||||||
assertThat(html).isEqualTo("String with <span style='font-size:10.00px;'>10dp</span> section");
|
assertThat(html).isEqualTo("String with <span style='font-size:10.00px;'>10dp</span> section");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convert_supportsRelativeSizeSpan() {
|
||||||
|
SpannableString spanned = new SpannableString("String with 10% section");
|
||||||
|
spanned.setSpan(
|
||||||
|
new RelativeSizeSpan(/* proportion= */ 10),
|
||||||
|
"String with ".length(),
|
||||||
|
"String with 10%".length(),
|
||||||
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
|
||||||
|
String html = SpannedToHtmlConverter.convert(spanned, displayDensity);
|
||||||
|
|
||||||
|
assertThat(html).isEqualTo("String with <span style='font-size:10.00%;'>10%</span> section");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convert_supportsTypefaceSpan() {
|
public void convert_supportsTypefaceSpan() {
|
||||||
SpannableString spanned = new SpannableString("String with Times New Roman section");
|
SpannableString spanned = new SpannableString("String with Times New Roman section");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user