package epson.print.pdf;

import android.content.Context;


import epson.common.ExternalFileUtils;
import epson.print.CommonDefine;
import epson.print.pdf.AdobePdfContainer;
import java.io.File;

public class AreaPdfRenderer {
    private static final String TAG = "AreaPdfRenderer";
    private AdobePdfContainer mPdfContainer;

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void setPdfContainer(AdobePdfContainer adobePdfContainer) {
        this.mPdfContainer = adobePdfContainer;
    }

    public boolean openPdfFile(String str, String str2, Context context) {
        this.mPdfContainer = new AdobePdfContainer(context);
        try {
            if (!checkFileSize(str)) {
                return false;
            }
            this.mPdfContainer.LoadDocument(str, str2);
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    private boolean checkFileSize(String str) {
        File file = new File(str);
        if (file.canRead() && file.length() > 0) {
            return true;
        }
        return false;
    }

    public int totalPages() {
        AdobePdfContainer adobePdfContainer = this.mPdfContainer;
        if (adobePdfContainer != null) {
            return adobePdfContainer.CountPages();
        }
        return 0;
    }

    public boolean isPageLandscape(int i) throws AdobePdfContainer.ParameterException {
        return this.mPdfContainer.GetPageSizeX(i) > this.mPdfContainer.GetPageSizeY(i);
    }

    public boolean convertPageForPrint(String str, int i, int[] iArr) throws AdobePdfContainer.ParameterException {
        int[] inscribeSize = getInscribeSize(iArr, (float) this.mPdfContainer.GetPageSizeX(i), (float) this.mPdfContainer.GetPageSizeY(i));
        return this.mPdfContainer.decodeToJpegFile(str, i, inscribeSize[0], inscribeSize[1]);
    }

    @NonNull
    public static int[] getPortraitOrLandscapeSize(@NonNull int[] iArr, boolean z) {
        if ((iArr[0] <= iArr[1]) == z) {
            return iArr;
        }
        return new int[]{iArr[1], iArr[0]};
    }

    @VisibleForTesting
    @NonNull
    public static int[] getCircumscribedSize(@NonNull int[] iArr, @NonNull int[] iArr2) {
        float inscribedScaleFactor = getInscribedScaleFactor((float) iArr2[0], (float) iArr2[1], (float) iArr[0], (float) iArr[1]);
        return new int[]{(int) Math.ceil((double) (((float) iArr2[0]) / inscribedScaleFactor)), (int) Math.ceil((double) (((float) iArr2[1]) / inscribedScaleFactor))};
    }

    public static int[] getInscribeSize(int[] iArr, float f, float f2) {
        float inscribedScaleFactor = getInscribedScaleFactor((float) iArr[0], (float) iArr[1], f, f2);
        return new int[]{(int) (f * inscribedScaleFactor), (int) (f2 * inscribedScaleFactor)};
    }

    public static float getInscribedScaleFactor(float f, float f2, float f3, float f4) {
        return Math.min(f / f3, f2 / f4);
    }

    public static File getPdfDecodeFilename(Context context, int i) {
        String printDir = ExternalFileUtils.getInstance(context).getPrintDir();
        return new File(printDir, CommonDefine.UNDER_BAR + i + ".jpg");
    }
}
