|
| 1 | +package org.schabi.newpipe.ui.components.common |
| 2 | + |
| 3 | +import android.graphics.Typeface |
| 4 | +import android.text.Layout |
| 5 | +import android.text.Spanned |
| 6 | +import android.text.style.AbsoluteSizeSpan |
| 7 | +import android.text.style.AlignmentSpan |
| 8 | +import android.text.style.BackgroundColorSpan |
| 9 | +import android.text.style.ForegroundColorSpan |
| 10 | +import android.text.style.RelativeSizeSpan |
| 11 | +import android.text.style.StrikethroughSpan |
| 12 | +import android.text.style.StyleSpan |
| 13 | +import android.text.style.SubscriptSpan |
| 14 | +import android.text.style.SuperscriptSpan |
| 15 | +import android.text.style.TypefaceSpan |
| 16 | +import android.text.style.URLSpan |
| 17 | +import android.text.style.UnderlineSpan |
| 18 | +import androidx.compose.ui.graphics.Color |
| 19 | +import androidx.compose.ui.text.AnnotatedString |
| 20 | +import androidx.compose.ui.text.LinkAnnotation |
| 21 | +import androidx.compose.ui.text.LinkInteractionListener |
| 22 | +import androidx.compose.ui.text.ParagraphStyle |
| 23 | +import androidx.compose.ui.text.SpanStyle |
| 24 | +import androidx.compose.ui.text.TextLinkStyles |
| 25 | +import androidx.compose.ui.text.font.FontFamily |
| 26 | +import androidx.compose.ui.text.font.FontStyle |
| 27 | +import androidx.compose.ui.text.font.FontWeight |
| 28 | +import androidx.compose.ui.text.style.BaselineShift |
| 29 | +import androidx.compose.ui.text.style.TextAlign |
| 30 | +import androidx.compose.ui.text.style.TextDecoration |
| 31 | +import androidx.compose.ui.unit.em |
| 32 | +import androidx.core.text.getSpans |
| 33 | + |
| 34 | +// The code below is copied from Html.android.kt in the Compose Text library, with some minor |
| 35 | +// changes. |
| 36 | + |
| 37 | +internal fun Spanned.toAnnotatedString( |
| 38 | + linkStyles: TextLinkStyles? = null, |
| 39 | + linkInteractionListener: LinkInteractionListener? = null |
| 40 | +): AnnotatedString { |
| 41 | + return AnnotatedString.Builder(capacity = length) |
| 42 | + .append(this) |
| 43 | + .also { |
| 44 | + it.addSpans(this, linkStyles, linkInteractionListener) |
| 45 | + } |
| 46 | + .toAnnotatedString() |
| 47 | +} |
| 48 | + |
| 49 | +private fun AnnotatedString.Builder.addSpans( |
| 50 | + spanned: Spanned, |
| 51 | + linkStyles: TextLinkStyles?, |
| 52 | + linkInteractionListener: LinkInteractionListener? |
| 53 | +) { |
| 54 | + spanned.getSpans<Any>().forEach { span -> |
| 55 | + addSpan( |
| 56 | + span, |
| 57 | + spanned.getSpanStart(span), |
| 58 | + spanned.getSpanEnd(span), |
| 59 | + linkStyles, |
| 60 | + linkInteractionListener |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +private fun AnnotatedString.Builder.addSpan( |
| 66 | + span: Any, |
| 67 | + start: Int, |
| 68 | + end: Int, |
| 69 | + linkStyles: TextLinkStyles?, |
| 70 | + linkInteractionListener: LinkInteractionListener? |
| 71 | +) { |
| 72 | + when (span) { |
| 73 | + is AbsoluteSizeSpan -> { |
| 74 | + // TODO: Add Compose's implementation when it is available. |
| 75 | + } |
| 76 | + |
| 77 | + is AlignmentSpan -> { |
| 78 | + addStyle(span.toParagraphStyle(), start, end) |
| 79 | + } |
| 80 | + |
| 81 | + is BackgroundColorSpan -> { |
| 82 | + addStyle(SpanStyle(background = Color(span.backgroundColor)), start, end) |
| 83 | + } |
| 84 | + |
| 85 | + is ForegroundColorSpan -> { |
| 86 | + addStyle(SpanStyle(color = Color(span.foregroundColor)), start, end) |
| 87 | + } |
| 88 | + |
| 89 | + is RelativeSizeSpan -> { |
| 90 | + addStyle(SpanStyle(fontSize = span.sizeChange.em), start, end) |
| 91 | + } |
| 92 | + |
| 93 | + is StrikethroughSpan -> { |
| 94 | + addStyle(SpanStyle(textDecoration = TextDecoration.LineThrough), start, end) |
| 95 | + } |
| 96 | + |
| 97 | + is StyleSpan -> { |
| 98 | + span.toSpanStyle()?.let { addStyle(it, start, end) } |
| 99 | + } |
| 100 | + |
| 101 | + is SubscriptSpan -> { |
| 102 | + addStyle(SpanStyle(baselineShift = BaselineShift.Subscript), start, end) |
| 103 | + } |
| 104 | + |
| 105 | + is SuperscriptSpan -> { |
| 106 | + addStyle(SpanStyle(baselineShift = BaselineShift.Superscript), start, end) |
| 107 | + } |
| 108 | + |
| 109 | + is TypefaceSpan -> { |
| 110 | + addStyle(span.toSpanStyle(), start, end) |
| 111 | + } |
| 112 | + |
| 113 | + is UnderlineSpan -> { |
| 114 | + addStyle(SpanStyle(textDecoration = TextDecoration.Underline), start, end) |
| 115 | + } |
| 116 | + |
| 117 | + is URLSpan -> { |
| 118 | + span.url?.let { url -> |
| 119 | + val link = LinkAnnotation.Url(url, linkStyles, linkInteractionListener) |
| 120 | + addLink(link, start, end) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +private fun AlignmentSpan.toParagraphStyle(): ParagraphStyle { |
| 127 | + val alignment = when (this.alignment) { |
| 128 | + Layout.Alignment.ALIGN_NORMAL -> TextAlign.Start |
| 129 | + Layout.Alignment.ALIGN_CENTER -> TextAlign.Center |
| 130 | + Layout.Alignment.ALIGN_OPPOSITE -> TextAlign.End |
| 131 | + else -> TextAlign.Unspecified |
| 132 | + } |
| 133 | + return ParagraphStyle(textAlign = alignment) |
| 134 | +} |
| 135 | + |
| 136 | +private fun StyleSpan.toSpanStyle(): SpanStyle? { |
| 137 | + return when (style) { |
| 138 | + Typeface.BOLD -> SpanStyle(fontWeight = FontWeight.Bold) |
| 139 | + Typeface.ITALIC -> SpanStyle(fontStyle = FontStyle.Italic) |
| 140 | + Typeface.BOLD_ITALIC -> SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic) |
| 141 | + else -> null |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +private fun TypefaceSpan.toSpanStyle(): SpanStyle { |
| 146 | + val fontFamily = when (family) { |
| 147 | + FontFamily.Cursive.name -> FontFamily.Cursive |
| 148 | + FontFamily.Monospace.name -> FontFamily.Monospace |
| 149 | + FontFamily.SansSerif.name -> FontFamily.SansSerif |
| 150 | + FontFamily.Serif.name -> FontFamily.Serif |
| 151 | + else -> { |
| 152 | + optionalFontFamilyFromName(family) |
| 153 | + } |
| 154 | + } |
| 155 | + return SpanStyle(fontFamily = fontFamily) |
| 156 | +} |
| 157 | + |
| 158 | +private fun optionalFontFamilyFromName(familyName: String?): FontFamily? { |
| 159 | + if (familyName.isNullOrEmpty()) return null |
| 160 | + val typeface = Typeface.create(familyName, Typeface.NORMAL) |
| 161 | + return typeface.takeIf { |
| 162 | + typeface != Typeface.DEFAULT && |
| 163 | + typeface != Typeface.create(Typeface.DEFAULT, Typeface.NORMAL) |
| 164 | + }?.let { FontFamily(it) } |
| 165 | +} |
0 commit comments