Skip to content

Commit 2d90a7a

Browse files
veganafropekingme
authored andcommitted
[CollapsingTextHelper][TextAppearance] refactor how bold typefaces are created
PiperOrigin-RevId: 425988666
1 parent 0f2b537 commit 2d90a7a

File tree

3 files changed

+80
-41
lines changed

3 files changed

+80
-41
lines changed

lib/java/com/google/android/material/internal/CollapsingTextHelper.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import android.graphics.Rect;
3535
import android.graphics.RectF;
3636
import android.graphics.Typeface;
37-
import android.graphics.fonts.FontStyle;
3837
import android.os.Build;
3938
import android.os.Build.VERSION;
4039
import android.os.Build.VERSION_CODES;
@@ -61,6 +60,7 @@
6160
import com.google.android.material.resources.CancelableFontCallback;
6261
import com.google.android.material.resources.CancelableFontCallback.ApplyFont;
6362
import com.google.android.material.resources.TextAppearance;
63+
import com.google.android.material.resources.TypefaceUtils;
6464

6565
/**
6666
* Helper class for rendering and animating collapsed text.
@@ -122,7 +122,6 @@ public final class CollapsingTextHelper {
122122
private Typeface expandedTypefaceBold;
123123
private Typeface expandedTypefaceDefault;
124124
private Typeface currentTypeface;
125-
private int fontWeightAdjustment;
126125
private CancelableFontCallback expandedFontCallback;
127126
private CancelableFontCallback collapsedFontCallback;
128127

@@ -472,7 +471,9 @@ private boolean setCollapsedTypefaceInternal(Typeface typeface) {
472471
}
473472
if (collapsedTypefaceDefault != typeface) {
474473
collapsedTypefaceDefault = typeface;
475-
collapsedTypefaceBold = maybeCloneWithAdjustment(typeface);
474+
collapsedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
475+
view.getContext().getResources().getConfiguration(),
476+
typeface);
476477
collapsedTypeface = collapsedTypefaceBold == null
477478
? collapsedTypefaceDefault : collapsedTypefaceBold;
478479
return true;
@@ -489,7 +490,9 @@ private boolean setExpandedTypefaceInternal(Typeface typeface) {
489490
}
490491
if (expandedTypefaceDefault != typeface) {
491492
expandedTypefaceDefault = typeface;
492-
expandedTypefaceBold = maybeCloneWithAdjustment(typeface);
493+
expandedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
494+
view.getContext().getResources().getConfiguration(),
495+
typeface);
493496
expandedTypeface = expandedTypefaceBold == null
494497
? expandedTypefaceDefault : expandedTypefaceBold;
495498
return true;
@@ -507,12 +510,15 @@ public Typeface getExpandedTypeface() {
507510

508511
public void maybeUpdateFontWeightAdjustment(@NonNull Configuration configuration) {
509512
if (VERSION.SDK_INT >= VERSION_CODES.S) {
510-
fontWeightAdjustment = configuration.fontWeightAdjustment;
511513
if (collapsedTypefaceDefault != null) {
512-
collapsedTypefaceBold = maybeCloneWithAdjustment(collapsedTypefaceDefault);
514+
collapsedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
515+
configuration,
516+
collapsedTypefaceDefault);
513517
}
514518
if (expandedTypefaceDefault != null) {
515-
expandedTypefaceBold = maybeCloneWithAdjustment(expandedTypefaceDefault);
519+
expandedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
520+
configuration,
521+
expandedTypefaceDefault);
516522
}
517523
collapsedTypeface = collapsedTypefaceBold != null
518524
? collapsedTypefaceBold : collapsedTypefaceDefault;
@@ -522,25 +528,6 @@ public void maybeUpdateFontWeightAdjustment(@NonNull Configuration configuration
522528
}
523529
}
524530

525-
private boolean shouldUseBoldTypefaces() {
526-
return VERSION.SDK_INT >= VERSION_CODES.S
527-
&& fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
528-
&& fontWeightAdjustment != 0;
529-
}
530-
531-
@Nullable
532-
private Typeface maybeCloneWithAdjustment(@NonNull Typeface typeface) {
533-
if (shouldUseBoldTypefaces()) {
534-
int adjustedWeight =
535-
MathUtils.clamp(
536-
typeface.getWeight() + fontWeightAdjustment,
537-
FontStyle.FONT_WEIGHT_MIN,
538-
FontStyle.FONT_WEIGHT_MAX);
539-
return Typeface.create(typeface, adjustedWeight, typeface.isItalic());
540-
}
541-
return null;
542-
}
543-
544531
/**
545532
* Set the value indicating the current scroll value. This decides how much of the background will
546533
* be displayed, as well as the title metrics/positioning.

lib/java/com/google/android/material/resources/TextAppearance.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020

2121
import android.content.Context;
2222
import android.content.res.ColorStateList;
23-
import android.content.res.Configuration;
2423
import android.content.res.Resources;
2524
import android.content.res.TypedArray;
2625
import android.graphics.Color;
2726
import android.graphics.Typeface;
28-
import android.graphics.fonts.FontStyle;
2927
import android.os.Build.VERSION;
3028
import android.os.Build.VERSION_CODES;
3129
import android.text.TextPaint;
@@ -39,7 +37,6 @@
3937
import androidx.annotation.VisibleForTesting;
4038
import androidx.core.content.res.ResourcesCompat;
4139
import androidx.core.content.res.ResourcesCompat.FontCallback;
42-
import androidx.core.math.MathUtils;
4340
import androidx.core.provider.FontsContractCompat.FontRequestCallback;
4441

4542
/**
@@ -342,18 +339,9 @@ public void updateMeasureState(
342339
*/
343340
public void updateTextPaintMeasureState(
344341
@NonNull Context context, @NonNull TextPaint textPaint, @NonNull Typeface typeface) {
345-
Configuration configuration = context.getResources().getConfiguration();
346-
if (VERSION.SDK_INT >= VERSION_CODES.S) {
347-
int fontWeightAdjustment = configuration.fontWeightAdjustment;
348-
if (fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
349-
&& fontWeightAdjustment != 0) {
350-
int adjustedWeight =
351-
MathUtils.clamp(
352-
typeface.getWeight() + fontWeightAdjustment,
353-
FontStyle.FONT_WEIGHT_MIN,
354-
FontStyle.FONT_WEIGHT_MAX);
355-
typeface = Typeface.create(typeface, adjustedWeight, typeface.isItalic());
356-
}
342+
Typeface boldTypeface = TypefaceUtils.maybeCopyWithFontWeightAdjustment(context, typeface);
343+
if (boldTypeface != null) {
344+
typeface = boldTypeface;
357345
}
358346
textPaint.setTypeface(typeface);
359347

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 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+
* https://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.google.android.material.resources;
18+
19+
import android.content.Context;
20+
import android.content.res.Configuration;
21+
import android.graphics.Typeface;
22+
import android.graphics.fonts.FontStyle;
23+
import android.os.Build.VERSION;
24+
import android.os.Build.VERSION_CODES;
25+
import androidx.annotation.NonNull;
26+
import androidx.annotation.Nullable;
27+
import androidx.annotation.RestrictTo;
28+
import androidx.annotation.RestrictTo.Scope;
29+
import androidx.core.math.MathUtils;
30+
31+
/**
32+
* Utility class that helps interact with Typeface objects.
33+
*
34+
* @hide
35+
*/
36+
@RestrictTo(Scope.LIBRARY_GROUP)
37+
public class TypefaceUtils {
38+
39+
private TypefaceUtils() {}
40+
41+
// Clones a typeface with additional boldness (weight).
42+
@Nullable
43+
public static Typeface maybeCopyWithFontWeightAdjustment(
44+
@NonNull Configuration configuration, @NonNull Typeface typeface) {
45+
if (VERSION.SDK_INT >= VERSION_CODES.S
46+
&& configuration.fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
47+
&& configuration.fontWeightAdjustment != 0) {
48+
int adjustedWeight =
49+
MathUtils.clamp(
50+
typeface.getWeight() + configuration.fontWeightAdjustment,
51+
FontStyle.FONT_WEIGHT_MIN,
52+
FontStyle.FONT_WEIGHT_MAX);
53+
return Typeface.create(typeface, adjustedWeight, typeface.isItalic());
54+
}
55+
return null;
56+
}
57+
58+
// Clones a typeface with additional boldness (weight).
59+
@Nullable
60+
public static Typeface maybeCopyWithFontWeightAdjustment(
61+
@NonNull Context context, @NonNull Typeface typeface) {
62+
return maybeCopyWithFontWeightAdjustment(context.getResources().getConfiguration(), typeface);
63+
}
64+
}

0 commit comments

Comments
 (0)