• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.content.res;
18 
19 
20 import android.annotation.AnyThread;
21 import android.annotation.FlaggedApi;
22 import android.annotation.Nullable;
23 import android.ravenwood.annotation.RavenwoodKeepWholeClass;
24 
25 /**
26  * A converter for non-linear font scaling. Converts font sizes given in "sp" dimensions to a
27  * "dp" dimension according to a non-linear curve.
28  *
29  * <p>This is meant to improve readability at larger font scales: larger fonts will scale up more
30  * slowly than smaller fonts, so we don't get ridiculously huge fonts that don't fit on the screen.
31  *
32  * <p>The thinking here is that large fonts are already big enough to read, but we still want to
33  * scale them slightly to preserve the visual hierarchy when compared to smaller fonts.
34  */
35 @FlaggedApi(Flags.FLAG_FONT_SCALE_CONVERTER_PUBLIC)
36 @RavenwoodKeepWholeClass
37 public interface FontScaleConverter {
38     /**
39      * Converts a dimension in "sp" to "dp".
40      */
convertSpToDp(float sp)41     float convertSpToDp(float sp);
42 
43     /**
44      * Converts a dimension in "dp" back to "sp".
45      */
convertDpToSp(float dp)46     float convertDpToSp(float dp);
47 
48     /**
49      * Returns true if non-linear font scaling curves would be in effect for the given scale, false
50      * if the scaling would follow a linear curve or for no scaling.
51      *
52      * <p>Example usage: {@code
53      * isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)}
54      */
55     @AnyThread
isNonLinearFontScalingActive(float fontScale)56     static boolean isNonLinearFontScalingActive(float fontScale) {
57         return FontScaleConverterFactory.isNonLinearFontScalingActive(fontScale);
58     }
59 
60     /**
61      * Finds a matching FontScaleConverter for the given fontScale factor.
62      *
63      * Generally you shouldn't need this; you can use {@link
64      * android.util.TypedValue#applyDimension(int, float, DisplayMetrics)} directly and it will do
65      * the scaling conversion for you. Dimens and resources loaded from XML will also be
66      * automatically converted. But for UI frameworks or other situations where you need to do the
67      * conversion without an Android Context, you can use this method.
68      *
69      * @param fontScale the scale factor, usually from {@link Configuration#fontScale}.
70      *
71      * @return a converter for the given scale, or null if non-linear scaling should not be used.
72      */
73     @Nullable
74     @AnyThread
forScale(float fontScale)75     static FontScaleConverter forScale(float fontScale) {
76         return FontScaleConverterFactory.forScale(fontScale);
77     }
78 }
79