1 /*
<lambda>null2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.ui.text.font
18
19 import android.content.Context
20 import android.graphics.Typeface
21 import androidx.compose.ui.text.ExperimentalTextApi
22 import androidx.compose.ui.text.font.FontLoadingStrategy.Companion.Async
23 import androidx.compose.ui.text.font.FontLoadingStrategy.Companion.Blocking
24 import androidx.compose.ui.text.font.FontLoadingStrategy.Companion.OptionalLocal
25 import androidx.core.content.res.ResourcesCompat
26 import kotlin.coroutines.resume
27 import kotlin.runCatching
28 import kotlinx.coroutines.suspendCancellableCoroutine
29
30 /** Android implementation for [Font.ResourceLoader]. It is designed to load only [ResourceFont]. */
31 internal class AndroidFontLoader(context: Context) : PlatformFontLoader {
32 private val context = context.applicationContext
33
34 @OptIn(ExperimentalTextApi::class)
35 override fun loadBlocking(font: Font): Typeface? {
36 return when (font) {
37 is AndroidFont -> font.typefaceLoader.loadBlocking(context, font)
38 is ResourceFont ->
39 when (font.loadingStrategy) {
40 Blocking -> font.load(context)
41 OptionalLocal -> runCatching { font.load(context) }.getOrNull()
42 Async -> throw UnsupportedOperationException("Unsupported Async font load path")
43 else ->
44 throw IllegalArgumentException(
45 "Unknown loading type ${font.loadingStrategy}"
46 )
47 }.setFontVariationSettings(font.variationSettings, context)
48 else -> null
49 }
50 }
51
52 @OptIn(ExperimentalTextApi::class)
53 override suspend fun awaitLoad(font: Font): Typeface? {
54 return when (font) {
55 is AndroidFont -> font.typefaceLoader.awaitLoad(context, font)
56 is ResourceFont ->
57 font.loadAsync(context).setFontVariationSettings(font.variationSettings, context)
58 else -> throw IllegalArgumentException("Unknown font type: $font")
59 }
60 }
61
62 override val cacheKey: Any? = null
63 }
64
loadnull65 private fun ResourceFont.load(context: Context): Typeface =
66 ResourcesCompat.getFont(context, resId)!!
67
68 // TODO(seanmcq): Move to core-ktx to dedup
69 private suspend fun ResourceFont.loadAsync(context: Context): Typeface {
70 return suspendCancellableCoroutine { continuation ->
71 ResourcesCompat.getFont(
72 context,
73 resId,
74 object : ResourcesCompat.FontCallback() {
75 override fun onFontRetrieved(typeface: Typeface) {
76 continuation.resume(typeface)
77 }
78
79 override fun onFontRetrievalFailed(reason: Int) {
80 continuation.cancel(
81 IllegalStateException(
82 "Unable to load font ${this@loadAsync} (reason=$reason)"
83 )
84 )
85 }
86 },
87 null
88 )
89 }
90 }
91