Skip to content

Commit 90787bf

Browse files
veganafrodsn5ft
authored andcommitted
[TextAppearance] fix drawables not applying system level bold
PiperOrigin-RevId: 421066050
1 parent a746751 commit 90787bf

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

lib/java/com/google/android/material/chip/Chip.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818

1919
import com.google.android.material.R;
2020

21-
import static android.graphics.fonts.FontStyle.FONT_WEIGHT_MIN;
2221
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
2322

2423
import android.annotation.SuppressLint;
2524
import android.annotation.TargetApi;
2625
import android.content.Context;
2726
import android.content.res.ColorStateList;
28-
import android.content.res.Configuration;
2927
import android.content.res.TypedArray;
3028
import android.graphics.Outline;
3129
import android.graphics.PorterDuff.Mode;
@@ -178,9 +176,6 @@ public class Chip extends AppCompatCheckBox implements Delegate, Shapeable {
178176
public void onFontRetrieved(@NonNull Typeface typeface, boolean fontResolvedSynchronously) {
179177
// Set text to re-trigger internal ellipsize width calculation.
180178
setText(chipDrawable.shouldDrawText() ? chipDrawable.getText() : getText());
181-
if (!fontResolvedSynchronously) {
182-
maybeUpdateFontWeightAdjustment();
183-
}
184179
requestLayout();
185180
invalidate();
186181
}
@@ -1374,16 +1369,6 @@ private void updateTextPaintDrawState() {
13741369
TextAppearance textAppearance = getTextAppearance();
13751370
if (textAppearance != null) {
13761371
textAppearance.updateDrawState(getContext(), textPaint, fontCallback);
1377-
maybeUpdateFontWeightAdjustment();
1378-
}
1379-
}
1380-
1381-
private void maybeUpdateFontWeightAdjustment() {
1382-
Configuration config = getResources().getConfiguration();
1383-
if (VERSION.SDK_INT >= VERSION_CODES.S && config.fontWeightAdjustment >= FONT_WEIGHT_MIN) {
1384-
// Re-apply typeface to take into account fontWeightAdjustment, see similar logic used in
1385-
// TextView#onConfigurationChanged
1386-
setTypeface(getTypeface());
13871372
}
13881373
}
13891374

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

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

2121
import android.content.Context;
2222
import android.content.res.ColorStateList;
23+
import android.content.res.Configuration;
2324
import android.content.res.Resources;
2425
import android.content.res.TypedArray;
2526
import android.graphics.Color;
2627
import android.graphics.Typeface;
28+
import android.graphics.fonts.FontStyle;
2729
import android.os.Build.VERSION;
2830
import android.os.Build.VERSION_CODES;
2931
import android.text.TextPaint;
@@ -228,19 +230,19 @@ public void onFontRetrievalFailed(int reason) {
228230
* @see #getFontAsync(Context, TextAppearanceFontCallback)
229231
*/
230232
public void getFontAsync(
231-
@NonNull Context context,
233+
@NonNull final Context context,
232234
@NonNull final TextPaint textPaint,
233235
@NonNull final TextAppearanceFontCallback callback) {
234236
// Updates text paint using fallback font while waiting for font to be requested.
235-
updateTextPaintMeasureState(textPaint, getFallbackFont());
237+
updateTextPaintMeasureState(context, textPaint, getFallbackFont());
236238

237239
getFontAsync(
238240
context,
239241
new TextAppearanceFontCallback() {
240242
@Override
241243
public void onFontRetrieved(
242244
@NonNull Typeface typeface, boolean fontResolvedSynchronously) {
243-
updateTextPaintMeasureState(textPaint, typeface);
245+
updateTextPaintMeasureState(context, textPaint, typeface);
244246
callback.onFontRetrieved(typeface, fontResolvedSynchronously);
245247
}
246248

@@ -326,7 +328,7 @@ public void updateMeasureState(
326328
@NonNull TextPaint textPaint,
327329
@NonNull TextAppearanceFontCallback callback) {
328330
if (shouldLoadFontSynchronously(context)) {
329-
updateTextPaintMeasureState(textPaint, getFont(context));
331+
updateTextPaintMeasureState(context, textPaint, getFont(context));
330332
} else {
331333
getFontAsync(context, textPaint, callback);
332334
}
@@ -338,7 +340,16 @@ public void updateMeasureState(
338340
* @see android.text.style.TextAppearanceSpan#updateMeasureState(TextPaint)
339341
*/
340342
public void updateTextPaintMeasureState(
341-
@NonNull TextPaint textPaint, @NonNull Typeface typeface) {
343+
@NonNull Context context, @NonNull TextPaint textPaint, @NonNull Typeface typeface) {
344+
Configuration configuration = context.getResources().getConfiguration();
345+
if (VERSION.SDK_INT >= VERSION_CODES.S
346+
&& configuration.fontWeightAdjustment >= FontStyle.FONT_WEIGHT_MIN) {
347+
int fontWeightAdjustment = configuration.fontWeightAdjustment;
348+
typeface = Typeface.create(
349+
typeface,
350+
typeface.getWeight() + fontWeightAdjustment,
351+
typeface.isItalic());
352+
}
342353
textPaint.setTypeface(typeface);
343354

344355
int fake = textStyle & ~typeface.getStyle();

0 commit comments

Comments
 (0)