Skip to content

Commit

Permalink
feat(android): support font assets (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Nov 2, 2023
1 parent a16e0ee commit d7dc1ba
Showing 1 changed file with 27 additions and 0 deletions.
@@ -1,6 +1,7 @@
package com.airbnb.android.react.lottie

import android.graphics.ColorFilter
import android.graphics.Typeface
import android.net.Uri
import android.util.Log
import android.widget.ImageView
Expand All @@ -10,13 +11,16 @@ import com.airbnb.lottie.LottieProperty
import com.airbnb.lottie.RenderMode
import com.airbnb.lottie.SimpleColorFilter
import com.airbnb.lottie.TextDelegate
import com.airbnb.lottie.FontAssetDelegate
import com.airbnb.lottie.model.KeyPath
import com.airbnb.lottie.value.LottieValueCallback
import com.facebook.react.BuildConfig
import com.facebook.react.bridge.ColorPropConverter
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.facebook.react.views.text.ReactFontManager
import com.facebook.react.views.text.TextAttributeProps.UNSET
import com.facebook.react.util.RNLog
import java.lang.ref.WeakReference
import java.util.regex.Pattern
Expand Down Expand Up @@ -60,6 +64,29 @@ class LottieAnimationViewPropertyManager(view: LottieAnimationView) {

init {
viewWeakReference = WeakReference(view)

view.setFontAssetDelegate(object : FontAssetDelegate() {
override fun fetchFont(fontFamily: String): Typeface {
val typeface = ReactFontManager.getInstance()
.getTypeface(fontFamily, UNSET, UNSET, view.context.assets)
return typeface?: Typeface.defaultFromStyle(400)
}

override fun fetchFont(fontFamily: String, fontStyle: String, fontName: String): Typeface {
val weight = when (fontStyle) {
"Thin" -> 100
"Light" -> 200
"Normal", "Regular" -> 400
"Medium" -> 500
"Bold" -> 700
"Black" -> 900
else -> UNSET
}
val typeface = ReactFontManager.getInstance()
.getTypeface(fontName, UNSET, weight, view.context.assets)
return typeface?: Typeface.defaultFromStyle(400)
}
})
}

/**
Expand Down

0 comments on commit d7dc1ba

Please sign in to comment.