Skip to content

Commit

Permalink
Merge pull request #2467 from quran/inline_arabic_font
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre authored Nov 17, 2023
2 parents fa41932 + 79fd87a commit 3de4b72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.quran.labs.androidquran.ui.helpers

import android.graphics.Typeface
import android.text.TextPaint
import android.text.style.MetricAffectingSpan

class TypefaceWrappingSpan(private val typeface: Typeface) : MetricAffectingSpan() {

override fun updateDrawState(ds: TextPaint) {
ds.typeface = typeface
}

override fun updateMeasureState(paint: TextPaint) {
paint.typeface = typeface
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.TypefaceSpan
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.annotation.StyleRes
import androidx.core.content.ContextCompat
import com.quran.common.search.SearchTextUtil
import com.quran.labs.androidquran.R
import com.quran.labs.androidquran.common.LocalTranslation
import com.quran.labs.androidquran.common.QuranAyahInfo
import com.quran.labs.androidquran.common.TranslationMetadata
import com.quran.labs.androidquran.ui.helpers.TypefaceWrappingSpan
import com.quran.labs.androidquran.ui.translation.TranslationAdapter
import com.quran.labs.androidquran.ui.util.TypefaceManager
import com.quran.labs.androidquran.util.QuranSettings

class InlineTranslationView @JvmOverloads constructor(
Expand Down Expand Up @@ -135,7 +141,7 @@ class InlineTranslationView @JvmOverloads constructor(
}

// irrespective of whether it's a link or not, show the text
builder.append(stylize(ayah.texts[i], translationText))
builder.append(stylize(ayah.texts[i], translations[i].languageCode, translationText))
}
}
ayahView.append(builder)
Expand All @@ -159,10 +165,27 @@ class InlineTranslationView @JvmOverloads constructor(

private fun stylize(
metadata: TranslationMetadata,
languageCode: String? = null,
translationText: String
): CharSequence {
val spannableStringBuilder = SpannableStringBuilder(translationText)

if (languageCode == "ar") {
val spans = listOf(
TypefaceWrappingSpan(TypefaceManager.getTafseerTypeface(context)),
RelativeSizeSpan(1.1f)
)

spans.forEach { span ->
spannableStringBuilder.setSpan(
span,
0,
spannableStringBuilder.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}

metadata.ayat.forEach { range ->
val span = ForegroundColorSpan(inlineAyahColor)
spannableStringBuilder.setSpan(
Expand Down

0 comments on commit 3de4b72

Please sign in to comment.