1 /*
2  * Copyright 2021 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.tooling
18 
19 import android.content.Context
20 import android.graphics.Typeface
21 import android.os.Build
22 import androidx.annotation.RequiresApi
23 import androidx.compose.ui.text.font.Font
24 import androidx.compose.ui.text.font.ResourceFont
25 
26 /** Layoutlib implementation for [Font.ResourceLoader] */
27 @Suppress("DEPRECATION")
28 internal class LayoutlibFontResourceLoader(private val context: Context) : Font.ResourceLoader {
29     @Deprecated(
30         "Replaced by FontFamily.Resolver, this method should not be called",
31         replaceWith = ReplaceWith("FontFamily.Resolver.resolve(font, )")
32     )
loadnull33     override fun load(font: Font): Typeface {
34         return if (font is ResourceFont && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
35             ResourceFontHelper.load(context, font)
36         } else {
37             throw IllegalArgumentException("Unknown font type: ${font.javaClass.name}")
38         }
39     }
40 }
41 
42 @RequiresApi(Build.VERSION_CODES.O)
43 private object ResourceFontHelper {
loadnull44     fun load(context: Context, font: ResourceFont): Typeface {
45         return context.resources.getFont(font.resId)
46     }
47 }
48