package coil.decode;

import A4.d;
import C4.f;
import D.C0135f;
import I2.C0325i;
import I2.C0334s;
import J2.B0;
import J2.C0402c;
import J2.C0420l;
import J2.C0427s;
import J2.E;
import J2.L0;
import J2.U;
import J2.V;
import J2.r;
import J2.z0;
import T.p;
import V4.C0759f0;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import coil.ImageLoader;
import coil.decode.Decoder;
import coil.fetch.SourceResult;
import coil.request.Options;
import coil.request.Svgs;
import coil.size.Scale;
import coil.size.Size;
import coil.size.Sizes;
import coil.util.SvgUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import p5.InterfaceC2742l;
import w4.k;

/* JADX INFO: loaded from: classes.dex */
public final class SvgDecoder implements Decoder {
    public static final String CSS_KEY = "coil#css";
    public static final Companion Companion = new Companion(null);
    private static final float DEFAULT_SIZE = 512.0f;
    private static final String MIME_TYPE_SVG = "image/svg+xml";
    private final Options options;
    private final ImageSource source;
    private final boolean useViewBoundsAsIntrinsicSize;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private Companion() {
        }
    }

    public static final class Factory implements Decoder.Factory {
        private final boolean useViewBoundsAsIntrinsicSize;

        public Factory() {
            this(false, 1, null);
        }

        private final boolean isApplicable(SourceResult sourceResult) {
            return o.c(sourceResult.getMimeType(), SvgDecoder.MIME_TYPE_SVG) || SvgDecodeUtils.isSvg(DecodeUtils.INSTANCE, sourceResult.getSource().source());
        }

        @Override // coil.decode.Decoder.Factory
        public Decoder create(SourceResult sourceResult, Options options, ImageLoader imageLoader) {
            if (isApplicable(sourceResult)) {
                return new SvgDecoder(sourceResult.getSource(), options, this.useViewBoundsAsIntrinsicSize);
            }
            return null;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            return (obj instanceof Factory) && this.useViewBoundsAsIntrinsicSize == ((Factory) obj).useViewBoundsAsIntrinsicSize;
        }

        public final boolean getUseViewBoundsAsIntrinsicSize() {
            return this.useViewBoundsAsIntrinsicSize;
        }

        public int hashCode() {
            return Boolean.hashCode(this.useViewBoundsAsIntrinsicSize);
        }

        public Factory(boolean z6) {
            this.useViewBoundsAsIntrinsicSize = z6;
        }

        public /* synthetic */ Factory(boolean z6, int i6, h hVar) {
            this((i6 & 1) != 0 ? true : z6);
        }
    }

    public SvgDecoder(ImageSource imageSource, Options options) {
        this(imageSource, options, false, 4, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:60:0x0158  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x015b  */
    /* JADX WARN: Removed duplicated region for block: B:65:0x019f  */
    /* JADX WARN: Removed duplicated region for block: B:68:0x01b7  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x01c3  */
    /* JADX WARN: Removed duplicated region for block: B:74:0x01d7  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x01f9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final DecodeResult decode$lambda$2(SvgDecoder svgDecoder) throws IOException {
        float fWidth;
        float fHeight;
        int iD0;
        int iD02;
        p pVar;
        boolean z6;
        Boolean bool;
        E e6;
        E e7;
        C0334s c0334s;
        ArrayList arrayList;
        Iterator it;
        InterfaceC2742l interfaceC2742lSource = svgDecoder.source.source();
        try {
            f fVarY = f.y(interfaceC2742lSource.x());
            interfaceC2742lSource.close();
            V v3 = (V) fVarY.f741b;
            if (v3 == null) {
                throw new IllegalArgumentException("SVG document is empty");
            }
            C0427s c0427s = v3.f3387o;
            RectF rectF = c0427s == null ? null : new RectF(c0427s.f3444b, c0427s.f3445c, c0427s.c(), c0427s.d());
            if (svgDecoder.useViewBoundsAsIntrinsicSize && rectF != null) {
                fWidth = rectF.width();
                fHeight = rectF.height();
            } else {
                if (((V) fVarY.f741b) == null) {
                    throw new IllegalArgumentException("SVG document is empty");
                }
                fWidth = fVarY.w().f3446d;
                if (((V) fVarY.f741b) == null) {
                    throw new IllegalArgumentException("SVG document is empty");
                }
                fHeight = fVarY.w().f3447e;
            }
            k dstSize = svgDecoder.getDstSize(fWidth, fHeight, svgDecoder.options.getScale());
            float fFloatValue = ((Number) dstSize.f25469a).floatValue();
            float fFloatValue2 = ((Number) dstSize.f25470b).floatValue();
            if (fWidth <= 0.0f || fHeight <= 0.0f) {
                iD0 = L4.a.d0(fFloatValue);
                iD02 = L4.a.d0(fFloatValue2);
            } else {
                float fComputeSizeMultiplier = DecodeUtils.computeSizeMultiplier(fWidth, fHeight, fFloatValue, fFloatValue2, svgDecoder.options.getScale());
                iD0 = (int) (fComputeSizeMultiplier * fWidth);
                iD02 = (int) (fComputeSizeMultiplier * fHeight);
            }
            if (rectF == null && fWidth > 0.0f && fHeight > 0.0f) {
                V v6 = (V) fVarY.f741b;
                if (v6 == null) {
                    throw new IllegalArgumentException("SVG document is empty");
                }
                v6.f3387o = new C0427s(0.0f, 0.0f, fWidth, fHeight);
            }
            V v7 = (V) fVarY.f741b;
            if (v7 == null) {
                throw new IllegalArgumentException("SVG document is empty");
            }
            v7.f3351r = L0.s("100%");
            V v8 = (V) fVarY.f741b;
            if (v8 == null) {
                throw new IllegalArgumentException("SVG document is empty");
            }
            v8.f3352s = L0.s("100%");
            Bitmap bitmapCreateBitmap = Bitmap.createBitmap(iD0, iD02, SvgUtils.toSoftware(svgDecoder.options.getConfig()));
            String strCss = Svgs.css(svgDecoder.options.getParameters());
            if (strCss != null) {
                pVar = new p(15);
                C0325i c0325i = new C0325i(2);
                C0402c c0402c = new C0402c(strCss);
                c0402c.J();
                pVar.f6345b = c0325i.e(c0402c);
            } else {
                pVar = null;
            }
            Canvas canvas = new Canvas(bitmapCreateBitmap);
            if (pVar == null) {
                pVar = new p(15);
            }
            if (((C0427s) pVar.f6346c) == null) {
                pVar.f6346c = new C0427s(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight());
            }
            B0 b02 = new B0();
            b02.f3154a = canvas;
            b02.f3155b = fVarY;
            V v9 = (V) fVarY.f741b;
            if (v9 == null) {
                Log.w("SVGAndroidRenderer", "Nothing to render. Document is empty.");
            } else {
                C0427s c0427s2 = v9.f3387o;
                r rVar = v9.f3375n;
                C0334s c0334s2 = (C0334s) pVar.f6345b;
                boolean z7 = false;
                if (c0334s2 == null) {
                    z6 = false;
                    if (z6) {
                        ((C0334s) fVarY.f742c).c((C0334s) pVar.f6345b);
                    }
                    b02.f3156c = new z0();
                    b02.f3157d = new Stack();
                    b02.S(b02.f3156c, U.a());
                    z0 z0Var = b02.f3156c;
                    z0Var.f3493f = null;
                    z0Var.f3495h = false;
                    b02.f3157d.push(new z0(z0Var));
                    b02.f3159f = new Stack();
                    b02.f3158e = new Stack();
                    bool = v9.f3359d;
                    if (bool != null) {
                        b02.f3156c.f3495h = bool.booleanValue();
                    }
                    b02.P();
                    C0427s c0427s3 = new C0427s((C0427s) pVar.f6346c);
                    e6 = v9.f3351r;
                    if (e6 != null) {
                        c0427s3.f3446d = e6.b(b02, c0427s3.f3446d);
                    }
                    e7 = v9.f3352s;
                    if (e7 != null) {
                        c0427s3.f3447e = e7.b(b02, c0427s3.f3447e);
                    }
                    b02.G(v9, c0427s3, c0427s2, rVar);
                    b02.O();
                    c0334s = (C0334s) pVar.f6345b;
                    if (c0334s != null) {
                        ArrayList arrayList2 = c0334s.f2658b;
                        if ((arrayList2 != null ? arrayList2.size() : 0) > 0) {
                            z7 = true;
                        }
                    }
                    if (z7 && (arrayList = ((C0334s) fVarY.f742c).f2658b) != null) {
                        it = arrayList.iterator();
                        while (it.hasNext()) {
                            if (((C0420l) it.next()).f3403c == 2) {
                                it.remove();
                            }
                        }
                    }
                } else {
                    ArrayList arrayList3 = c0334s2.f2658b;
                    if ((arrayList3 != null ? arrayList3.size() : 0) > 0) {
                        z6 = true;
                    }
                    if (z6) {
                    }
                    b02.f3156c = new z0();
                    b02.f3157d = new Stack();
                    b02.S(b02.f3156c, U.a());
                    z0 z0Var2 = b02.f3156c;
                    z0Var2.f3493f = null;
                    z0Var2.f3495h = false;
                    b02.f3157d.push(new z0(z0Var2));
                    b02.f3159f = new Stack();
                    b02.f3158e = new Stack();
                    bool = v9.f3359d;
                    if (bool != null) {
                    }
                    b02.P();
                    C0427s c0427s32 = new C0427s((C0427s) pVar.f6346c);
                    e6 = v9.f3351r;
                    if (e6 != null) {
                    }
                    e7 = v9.f3352s;
                    if (e7 != null) {
                    }
                    b02.G(v9, c0427s32, c0427s2, rVar);
                    b02.O();
                    c0334s = (C0334s) pVar.f6345b;
                    if (c0334s != null) {
                    }
                    if (z7) {
                        it = arrayList.iterator();
                        while (it.hasNext()) {
                        }
                    }
                }
            }
            return new DecodeResult(new BitmapDrawable(svgDecoder.options.getContext().getResources(), bitmapCreateBitmap), true);
        } finally {
        }
    }

    private final k getDstSize(float f6, float f7, Scale scale) {
        if (!Sizes.isOriginal(this.options.getSize())) {
            Size size = this.options.getSize();
            return new k(Float.valueOf(SvgUtils.toPx(size.component1(), scale)), Float.valueOf(SvgUtils.toPx(size.component2(), scale)));
        }
        if (f6 <= 0.0f) {
            f6 = 512.0f;
        }
        if (f7 <= 0.0f) {
            f7 = 512.0f;
        }
        return new k(Float.valueOf(f6), Float.valueOf(f7));
    }

    @Override // coil.decode.Decoder
    public Object decode(d dVar) {
        return V4.E.H(A4.k.f106a, new C0759f0(new C0135f(this, 28), null), dVar);
    }

    public final boolean getUseViewBoundsAsIntrinsicSize() {
        return this.useViewBoundsAsIntrinsicSize;
    }

    public SvgDecoder(ImageSource imageSource, Options options, boolean z6) {
        this.source = imageSource;
        this.options = options;
        this.useViewBoundsAsIntrinsicSize = z6;
    }

    public /* synthetic */ SvgDecoder(ImageSource imageSource, Options options, boolean z6, int i6, h hVar) {
        this(imageSource, options, (i6 & 4) != 0 ? true : z6);
    }
}
