2020
2121import android .content .Context ;
2222import android .content .res .ColorStateList ;
23+ import android .content .res .Configuration ;
2324import android .content .res .Resources ;
2425import android .content .res .TypedArray ;
2526import android .graphics .Color ;
2627import android .graphics .Typeface ;
28+ import android .graphics .fonts .FontStyle ;
2729import android .os .Build .VERSION ;
2830import android .os .Build .VERSION_CODES ;
2931import 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