Skip to content

Commit

Permalink
[CollapsingTextHelper][TextAppearance] refactor how bold typefaces ar…
Browse files Browse the repository at this point in the history
…e created

PiperOrigin-RevId: 425988666
  • Loading branch information
veganafro authored and pekingme committed Feb 3, 2022
1 parent 0f2b537 commit 2d90a7a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 deletions.
Expand Up @@ -34,7 +34,6 @@
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.fonts.FontStyle;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
Expand All @@ -61,6 +60,7 @@
import com.google.android.material.resources.CancelableFontCallback;
import com.google.android.material.resources.CancelableFontCallback.ApplyFont;
import com.google.android.material.resources.TextAppearance;
import com.google.android.material.resources.TypefaceUtils;

/**
* Helper class for rendering and animating collapsed text.
Expand Down Expand Up @@ -122,7 +122,6 @@ public final class CollapsingTextHelper {
private Typeface expandedTypefaceBold;
private Typeface expandedTypefaceDefault;
private Typeface currentTypeface;
private int fontWeightAdjustment;
private CancelableFontCallback expandedFontCallback;
private CancelableFontCallback collapsedFontCallback;

Expand Down Expand Up @@ -472,7 +471,9 @@ private boolean setCollapsedTypefaceInternal(Typeface typeface) {
}
if (collapsedTypefaceDefault != typeface) {
collapsedTypefaceDefault = typeface;
collapsedTypefaceBold = maybeCloneWithAdjustment(typeface);
collapsedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
view.getContext().getResources().getConfiguration(),
typeface);
collapsedTypeface = collapsedTypefaceBold == null
? collapsedTypefaceDefault : collapsedTypefaceBold;
return true;
Expand All @@ -489,7 +490,9 @@ private boolean setExpandedTypefaceInternal(Typeface typeface) {
}
if (expandedTypefaceDefault != typeface) {
expandedTypefaceDefault = typeface;
expandedTypefaceBold = maybeCloneWithAdjustment(typeface);
expandedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
view.getContext().getResources().getConfiguration(),
typeface);
expandedTypeface = expandedTypefaceBold == null
? expandedTypefaceDefault : expandedTypefaceBold;
return true;
Expand All @@ -507,12 +510,15 @@ public Typeface getExpandedTypeface() {

public void maybeUpdateFontWeightAdjustment(@NonNull Configuration configuration) {
if (VERSION.SDK_INT >= VERSION_CODES.S) {
fontWeightAdjustment = configuration.fontWeightAdjustment;
if (collapsedTypefaceDefault != null) {
collapsedTypefaceBold = maybeCloneWithAdjustment(collapsedTypefaceDefault);
collapsedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
configuration,
collapsedTypefaceDefault);
}
if (expandedTypefaceDefault != null) {
expandedTypefaceBold = maybeCloneWithAdjustment(expandedTypefaceDefault);
expandedTypefaceBold = TypefaceUtils.maybeCopyWithFontWeightAdjustment(
configuration,
expandedTypefaceDefault);
}
collapsedTypeface = collapsedTypefaceBold != null
? collapsedTypefaceBold : collapsedTypefaceDefault;
Expand All @@ -522,25 +528,6 @@ public void maybeUpdateFontWeightAdjustment(@NonNull Configuration configuration
}
}

private boolean shouldUseBoldTypefaces() {
return VERSION.SDK_INT >= VERSION_CODES.S
&& fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
&& fontWeightAdjustment != 0;
}

@Nullable
private Typeface maybeCloneWithAdjustment(@NonNull Typeface typeface) {
if (shouldUseBoldTypefaces()) {
int adjustedWeight =
MathUtils.clamp(
typeface.getWeight() + fontWeightAdjustment,
FontStyle.FONT_WEIGHT_MIN,
FontStyle.FONT_WEIGHT_MAX);
return Typeface.create(typeface, adjustedWeight, typeface.isItalic());
}
return null;
}

/**
* Set the value indicating the current scroll value. This decides how much of the background will
* be displayed, as well as the title metrics/positioning.
Expand Down
18 changes: 3 additions & 15 deletions lib/java/com/google/android/material/resources/TextAppearance.java
Expand Up @@ -20,12 +20,10 @@

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.fonts.FontStyle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.TextPaint;
Expand All @@ -39,7 +37,6 @@
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.content.res.ResourcesCompat.FontCallback;
import androidx.core.math.MathUtils;
import androidx.core.provider.FontsContractCompat.FontRequestCallback;

/**
Expand Down Expand Up @@ -342,18 +339,9 @@ public void updateMeasureState(
*/
public void updateTextPaintMeasureState(
@NonNull Context context, @NonNull TextPaint textPaint, @NonNull Typeface typeface) {
Configuration configuration = context.getResources().getConfiguration();
if (VERSION.SDK_INT >= VERSION_CODES.S) {
int fontWeightAdjustment = configuration.fontWeightAdjustment;
if (fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
&& fontWeightAdjustment != 0) {
int adjustedWeight =
MathUtils.clamp(
typeface.getWeight() + fontWeightAdjustment,
FontStyle.FONT_WEIGHT_MIN,
FontStyle.FONT_WEIGHT_MAX);
typeface = Typeface.create(typeface, adjustedWeight, typeface.isItalic());
}
Typeface boldTypeface = TypefaceUtils.maybeCopyWithFontWeightAdjustment(context, typeface);
if (boldTypeface != null) {
typeface = boldTypeface;
}
textPaint.setTypeface(typeface);

Expand Down
64 changes: 64 additions & 0 deletions lib/java/com/google/android/material/resources/TypefaceUtils.java
@@ -0,0 +1,64 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.material.resources;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.graphics.fonts.FontStyle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.math.MathUtils;

/**
* Utility class that helps interact with Typeface objects.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public class TypefaceUtils {

private TypefaceUtils() {}

// Clones a typeface with additional boldness (weight).
@Nullable
public static Typeface maybeCopyWithFontWeightAdjustment(
@NonNull Configuration configuration, @NonNull Typeface typeface) {
if (VERSION.SDK_INT >= VERSION_CODES.S
&& configuration.fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED
&& configuration.fontWeightAdjustment != 0) {
int adjustedWeight =
MathUtils.clamp(
typeface.getWeight() + configuration.fontWeightAdjustment,
FontStyle.FONT_WEIGHT_MIN,
FontStyle.FONT_WEIGHT_MAX);
return Typeface.create(typeface, adjustedWeight, typeface.isItalic());
}
return null;
}

// Clones a typeface with additional boldness (weight).
@Nullable
public static Typeface maybeCopyWithFontWeightAdjustment(
@NonNull Context context, @NonNull Typeface typeface) {
return maybeCopyWithFontWeightAdjustment(context.getResources().getConfiguration(), typeface);
}
}

0 comments on commit 2d90a7a

Please sign in to comment.