• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Google Sans Flex emphasized styles
38     private val displayLargeEmphasizedFont =
39         DeviceFontFamilyName("variable-display-large-emphasized")
40     private val displayMediumEmphasizedFont =
41         DeviceFontFamilyName("variable-display-medium-emphasized")
42     private val displaySmallEmphasizedFont =
43         DeviceFontFamilyName("variable-display-small-emphasized")
44     private val headlineLargeEmphasizedFont =
45         DeviceFontFamilyName("variable-headline-large-emphasized")
46     private val headlineMediumEmphasizedFont =
47         DeviceFontFamilyName("variable-headline-medium-emphasized")
48     private val headlineSmallEmphasizedFont =
49         DeviceFontFamilyName("variable-headline-small-emphasized")
50     private val titleLargeEmphasizedFont = DeviceFontFamilyName("variable-title-large-emphasized")
51     private val titleMediumEmphasizedFont = DeviceFontFamilyName("variable-title-medium-emphasized")
52     private val titleSmallEmphasizedFont = DeviceFontFamilyName("variable-title-small-emphasized")
53     private val bodyLargeEmphasizedFont = DeviceFontFamilyName("variable-body-large-emphasized")
54     private val bodyMediumEmphasizedFont = DeviceFontFamilyName("variable-body-medium-emphasized")
55     private val bodySmallEmphasizedFont = DeviceFontFamilyName("variable-body-small-emphasized")
56     private val labelLargeEmphasizedFont = DeviceFontFamilyName("variable-label-large-emphasized")
57     private val labelMediumEmphasizedFont = DeviceFontFamilyName("variable-label-medium-emphasized")
58     private val labelSmallEmphasizedFont = DeviceFontFamilyName("variable-label-small-emphasized")
59 
60     val brand =
61         FontFamily(
62             Font(brandFont, weight = WeightMedium),
63             Font(brandFont, weight = WeightRegular),
64         )
65     val plain =
66         FontFamily(
67             Font(plainFont, weight = WeightMedium),
68             Font(plainFont, weight = WeightRegular),
69         )
70 
71     val displayLargeEmphasized = FontFamily(Font(displayLargeEmphasizedFont))
72     val displayMediumEmphasized = FontFamily(Font(displayMediumEmphasizedFont))
73     val displaySmallEmphasized = FontFamily(Font(displaySmallEmphasizedFont))
74     val headlineLargeEmphasized = FontFamily(Font(headlineLargeEmphasizedFont))
75     val headlineMediumEmphasized = FontFamily(Font(headlineMediumEmphasizedFont))
76     val headlineSmallEmphasized = FontFamily(Font(headlineSmallEmphasizedFont))
77     val titleLargeEmphasized = FontFamily(Font(titleLargeEmphasizedFont))
78     val titleMediumEmphasized = FontFamily(Font(titleMediumEmphasizedFont))
79     val titleSmallEmphasized = FontFamily(Font(titleSmallEmphasizedFont))
80     val bodyLargeEmphasized = FontFamily(Font(bodyLargeEmphasizedFont))
81     val bodyMediumEmphasized = FontFamily(Font(bodyMediumEmphasizedFont))
82     val bodySmallEmphasized = FontFamily(Font(bodySmallEmphasizedFont))
83     val labelLargeEmphasized = FontFamily(Font(labelLargeEmphasizedFont))
84     val labelMediumEmphasized = FontFamily(Font(labelMediumEmphasizedFont))
85     val labelSmallEmphasized = FontFamily(Font(labelSmallEmphasizedFont))
86 }
87 
88 internal data class TypefaceNames
89 private constructor(
90     val brand: String,
91     val plain: String,
92 ) {
93     private enum class Config(val configName: String, val default: String) {
94         Brand("config_headlineFontFamily", "sans-serif"),
95         Plain("config_bodyFontFamily", "sans-serif"),
96     }
97 
98     companion object {
getnull99         fun get(context: Context): TypefaceNames {
100             return TypefaceNames(
101                 brand = getTypefaceName(context, Config.Brand),
102                 plain = getTypefaceName(context, Config.Plain),
103             )
104         }
105 
getTypefaceNamenull106         private fun getTypefaceName(context: Context, config: Config): String {
107             return context
108                 .getString(context.resources.getIdentifier(config.configName, "string", "android"))
109                 .takeIf { it.isNotEmpty() }
110                 ?: config.default
111         }
112     }
113 }
114