package com.google.android.material.internal;

import android.content.Context;
import android.text.TextPaint;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;

/* JADX INFO: loaded from: classes2.dex */
public final class n0 {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public float f22794c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public float f22795d;

    @Nullable
    private q9.g textAppearance;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final TextPaint f22792a = new TextPaint(1);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final f9.b f22793b = new f9.b(this, 1);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f22796e = true;

    @Nullable
    private WeakReference<m0> delegate = new WeakReference<>(null);

    public n0(@Nullable m0 m0Var) {
        setDelegate(m0Var);
    }

    private float calculateTextHeight(@Nullable String str) {
        if (str == null) {
            return 0.0f;
        }
        return Math.abs(this.f22792a.getFontMetrics().ascent);
    }

    private float calculateTextWidth(@Nullable CharSequence charSequence) {
        if (charSequence == null) {
            return 0.0f;
        }
        return this.f22792a.measureText(charSequence, 0, charSequence.length());
    }

    public final float b(String str) {
        if (!this.f22796e) {
            return this.f22794c;
        }
        this.f22794c = calculateTextWidth(str);
        this.f22795d = calculateTextHeight(str);
        this.f22796e = false;
        return this.f22794c;
    }

    public final void c(Context context) {
        this.textAppearance.d(context, this.f22792a, this.f22793b);
    }

    @Nullable
    public q9.g getTextAppearance() {
        return this.textAppearance;
    }

    public float getTextHeight(@Nullable String str) {
        if (!this.f22796e) {
            return this.f22795d;
        }
        this.f22794c = calculateTextWidth(str);
        float fCalculateTextHeight = calculateTextHeight(str);
        this.f22795d = fCalculateTextHeight;
        this.f22796e = false;
        return fCalculateTextHeight;
    }

    public void setDelegate(@Nullable m0 m0Var) {
        this.delegate = new WeakReference<>(m0Var);
    }

    public void setTextAppearance(@Nullable q9.g gVar, Context context) {
        if (this.textAppearance != gVar) {
            this.textAppearance = gVar;
            if (gVar != null) {
                TextPaint textPaint = this.f22792a;
                f9.b bVar = this.f22793b;
                gVar.e(context, textPaint, bVar);
                m0 m0Var = this.delegate.get();
                if (m0Var != null) {
                    textPaint.drawableState = m0Var.getState();
                }
                gVar.d(context, textPaint, bVar);
                this.f22796e = true;
            }
            m0 m0Var2 = this.delegate.get();
            if (m0Var2 != null) {
                m0Var2.a();
                m0Var2.onStateChange(m0Var2.getState());
            }
        }
    }
}
