1 /* 2 * Copyright (C) 2024 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 com.android.systemui.animation 18 19 import kotlin.text.buildString 20 21 class FontVariationUtils { 22 private var mWeight = -1 23 private var mWidth = -1 24 private var mOpticalSize = -1 25 private var mRoundness = -1 26 private var mCurrentFVar = "" 27 28 /* 29 * generate fontVariationSettings string, used for key in typefaceCache in TextAnimator 30 * the order of axes should align to the order of parameters 31 */ updateFontVariationnull32 fun updateFontVariation( 33 weight: Int = -1, 34 width: Int = -1, 35 opticalSize: Int = -1, 36 roundness: Int = -1, 37 ): String { 38 var isUpdated = false 39 if (weight >= 0 && mWeight != weight) { 40 isUpdated = true 41 mWeight = weight 42 } 43 44 if (width >= 0 && mWidth != width) { 45 isUpdated = true 46 mWidth = width 47 } 48 49 if (opticalSize >= 0 && mOpticalSize != opticalSize) { 50 isUpdated = true 51 mOpticalSize = opticalSize 52 } 53 54 if (roundness >= 0 && mRoundness != roundness) { 55 isUpdated = true 56 mRoundness = roundness 57 } 58 59 if (!isUpdated) { 60 return mCurrentFVar 61 } 62 63 return buildString { 64 if (mWeight >= 0) { 65 if (!isBlank()) append(", ") 66 append("'${GSFAxes.WEIGHT.tag}' $mWeight") 67 } 68 69 if (mWidth >= 0) { 70 if (!isBlank()) append(", ") 71 append("'${GSFAxes.WIDTH.tag}' $mWidth") 72 } 73 74 if (mOpticalSize >= 0) { 75 if (!isBlank()) append(", ") 76 append("'${GSFAxes.OPTICAL_SIZE.tag}' $mOpticalSize") 77 } 78 79 if (mRoundness >= 0) { 80 if (!isBlank()) append(", ") 81 append("'${GSFAxes.ROUND.tag}' $mRoundness") 82 } 83 } 84 .also { mCurrentFVar = it } 85 } 86 } 87