1 package com.android.systemui.animation
2 
3 private const val TAG_WGHT = "wght"
4 private const val TAG_WDTH = "wdth"
5 private const val TAG_OPSZ = "opsz"
6 private const val TAG_ROND = "ROND"
7 
8 class FontVariationUtils {
9     private var mWeight = -1
10     private var mWidth = -1
11     private var mOpticalSize = -1
12     private var mRoundness = -1
13     private var isUpdated = false
14 
15     /*
16      * generate fontVariationSettings string, used for key in typefaceCache in TextAnimator
17      * the order of axes should align to the order of parameters
18      * if every axis remains unchanged, return ""
19      */
updateFontVariationnull20     fun updateFontVariation(
21         weight: Int = -1,
22         width: Int = -1,
23         opticalSize: Int = -1,
24         roundness: Int = -1
25     ): String {
26         isUpdated = false
27         if (weight >= 0 && mWeight != weight) {
28             isUpdated = true
29             mWeight = weight
30         }
31         if (width >= 0 && mWidth != width) {
32             isUpdated = true
33             mWidth = width
34         }
35         if (opticalSize >= 0 && mOpticalSize != opticalSize) {
36             isUpdated = true
37             mOpticalSize = opticalSize
38         }
39 
40         if (roundness >= 0 && mRoundness != roundness) {
41             isUpdated = true
42             mRoundness = roundness
43         }
44         var resultString = ""
45         if (mWeight >= 0) {
46             resultString += "'$TAG_WGHT' $mWeight"
47         }
48         if (mWidth >= 0) {
49             resultString += (if (resultString.isBlank()) "" else ", ") + "'$TAG_WDTH' $mWidth"
50         }
51         if (mOpticalSize >= 0) {
52             resultString += (if (resultString.isBlank()) "" else ", ") + "'$TAG_OPSZ' $mOpticalSize"
53         }
54         if (mRoundness >= 0) {
55             resultString += (if (resultString.isBlank()) "" else ", ") + "'$TAG_ROND' $mRoundness"
56         }
57         return if (isUpdated) resultString else ""
58     }
59 }
60