1 /*
2  * Copyright (C) 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 @file:OptIn(ExperimentalTextApi::class)
18 
19 package com.android.compose.theme.typography
20 
21 import android.content.Context
22 import androidx.compose.ui.text.ExperimentalTextApi
23 import androidx.compose.ui.text.font.DeviceFontFamilyName
24 import androidx.compose.ui.text.font.Font
25 import androidx.compose.ui.text.font.FontFamily
26 import androidx.compose.ui.text.font.FontWeight
27 
28 internal class TypefaceTokens(typefaceNames: TypefaceNames) {
29     companion object {
30         val WeightMedium = FontWeight.Medium
31         val WeightRegular = FontWeight.Normal
32     }
33 
34     private val brandFont = DeviceFontFamilyName(typefaceNames.brand)
35     private val plainFont = DeviceFontFamilyName(typefaceNames.plain)
36 
37     val brand =
38         FontFamily(
39             Font(brandFont, weight = WeightMedium),
40             Font(brandFont, weight = WeightRegular),
41         )
42     val plain =
43         FontFamily(
44             Font(plainFont, weight = WeightMedium),
45             Font(plainFont, weight = WeightRegular),
46         )
47 }
48 
49 internal data class TypefaceNames
50 private constructor(
51     val brand: String,
52     val plain: String,
53 ) {
54     private enum class Config(val configName: String, val default: String) {
55         Brand("config_headlineFontFamily", "sans-serif"),
56         Plain("config_bodyFontFamily", "sans-serif"),
57     }
58 
59     companion object {
getnull60         fun get(context: Context): TypefaceNames {
61             return TypefaceNames(
62                 brand = getTypefaceName(context, Config.Brand),
63                 plain = getTypefaceName(context, Config.Plain),
64             )
65         }
66 
getTypefaceNamenull67         private fun getTypefaceName(context: Context, config: Config): String {
68             return context
69                 .getString(context.resources.getIdentifier(config.configName, "string", "android"))
70                 .takeIf { it.isNotEmpty() }
71                 ?: config.default
72         }
73     }
74 }
75