/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.pdf;

public class PdfPage extends PdfRect implements IPdfDictionaryWrapper, AutoCloseable {

    private PdfDictionary _dic;

    @Override
    public PdfDictionary getDic() {
        return _dic;
    }

    public PdfDictionary setDic(PdfDictionary value) {
        _dic = value;
        return _dic;
    }


    public PdfPage(PdfDictionary __dic, Object state0, boolean storeLexems, boolean ignoreImages) throws Exception, java.io.FileNotFoundException, java.io.IOException {
        super();
        setDic(__dic);
        __dic.obj = this;
        PdfArray crop = (PdfArray)com.pullenti.unisharp.Utils.cast(__dic.getObject("CropBox", false), PdfArray.class);
        if (crop == null) 
            crop = (PdfArray)com.pullenti.unisharp.Utils.cast(__dic.getObject("TrimBox", false), PdfArray.class);
        if (crop == null) 
            crop = (PdfArray)com.pullenti.unisharp.Utils.cast(__dic.getObject("MediaBox", false), PdfArray.class);
        if (crop == null) 
            crop = (PdfArray)com.pullenti.unisharp.Utils.cast(__dic.getObject("BBox", false), PdfArray.class);
        if (crop == null) {
            if (__dic.sourceFile.rootObject != null) {
                PdfDictionary pages = __dic.sourceFile.rootObject.getDictionary("Pages", null);
                if (pages != null) {
                    crop = (PdfArray)com.pullenti.unisharp.Utils.cast(pages.getObject("MediaBox", false), PdfArray.class);
                    if (crop == null) 
                        crop = (PdfArray)com.pullenti.unisharp.Utils.cast(pages.getObject("TrimBox", false), PdfArray.class);
                    if (crop == null) 
                        crop = (PdfArray)com.pullenti.unisharp.Utils.cast(pages.getObject("CropBox", false), PdfArray.class);
                }
            }
        }
        if (crop != null && crop.getItemsCount() == 4) {
            x2 = crop.getItem(2, false).getDouble();
            y2 = crop.getItem(3, false).getDouble();
            PdfObject rot = __dic.getObject("Rotate", false);
            if (rot != null && ((rot.getDouble() == 90 || rot.getDouble() == 270))) {
                double xx = x2;
                x2 = y2;
                y2 = xx;
            }
        }
        PdfDictionary res = getDic().getDictionary("Resources", null);
        PdfDictionary fnts = (res == null ? null : res.getDictionary("Font", null));
        PdfDictionary _imgs = (res == null ? null : res.getDictionary("XObject", null));
        byte[] val = getDic().getTotalDataStream("Contents");
        if (val == null && ((state0 != null || this.getDic().isTypeItem("XObject")))) 
            val = this.getDic().extractData();
        if (val == null || (val.length < 1)) 
            return;
        java.util.ArrayList<PdfObject> lex = null;
        try (PdfStream pstr = new PdfStream(null, val)) {
            lex = pstr.parseContent();
        }
        if (storeLexems) {
            lexems = lex;
            lexemContent = val;
        }
        java.util.HashMap<String, PdfFont> allFonts = new java.util.HashMap<String, PdfFont>();
        PdfTextState state = (PdfTextState)com.pullenti.unisharp.Utils.cast(state0, PdfTextState.class);
        if (state == null) {
            state = new PdfTextState();
            state.boxHeight = y2;
        }
        else {
            PdfArray mat = (PdfArray)com.pullenti.unisharp.Utils.cast(__dic.getObject("Matrix", false), PdfArray.class);
            if (mat != null && mat.getItemsCount() == 6) {
                Matrix m = new Matrix();
                m.set(mat.getItem(0, false).getDouble(), mat.getItem(1, false).getDouble(), mat.getItem(2, false).getDouble(), mat.getItem(3, false).getDouble(), mat.getItem(4, false).getDouble(), mat.getItem(5, false).getDouble());
                if (state.ctmStack.size() > 0) {
                    m.multiply(state.ctmStack.get(0));
                    com.pullenti.unisharp.Utils.putArrayValue(state.ctmStack, 0, m);
                }
            }
        }
        boolean textRegime = false;
        StringBuilder buf = new StringBuilder();
        PdfPathState path = new PdfPathState(this);
        for (int i = 0; i < lex.size(); i++) {
            if (!(lex.get(i) instanceof PdfName)) 
                continue;
            String nam = ((PdfName)com.pullenti.unisharp.Utils.cast(lex.get(i), PdfName.class)).name;
            if (com.pullenti.unisharp.Utils.stringsEq(nam, "ET")) {
                textRegime = false;
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(nam, "BT")) {
                textRegime = true;
                state.tm.init();
                state.tlm.init();
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(nam, "Do") && (lex.get(i - 1) instanceof PdfName)) {
                nam = ((PdfName)com.pullenti.unisharp.Utils.cast(lex.get(i - 1), PdfName.class)).name;
                PdfDictionary xobjs = (res == null ? null : res.getDictionary("XObject", null));
                PdfDictionary xobj = (xobjs == null ? null : xobjs.getDictionary(nam, null));
                if (xobj == null) {
                    for (PdfDictionary rr : __dic.sourceFile.allResources) {
                        if (com.pullenti.unisharp.Utils.stringsEq(rr.getStringItem("Name"), nam)) {
                            xobj = rr;
                            break;
                        }
                    }
                }
                if (xobj != null) {
                    PdfName sub = (PdfName)com.pullenti.unisharp.Utils.cast(xobj.getObject("Subtype", false), PdfName.class);
                    if (sub != null && com.pullenti.unisharp.Utils.stringsEq(sub.name, "Image")) {
                        lex.get(i).tag = xobj;
                        imgs.add(PdfMiscInfo._new233(xobj, (int)lex.get(i - 1).readPos, (((int)lex.get(i).readPos) + lex.get(i).readLen) - 1, nam));
                        PdfBoolValue im = (PdfBoolValue)com.pullenti.unisharp.Utils.cast(xobj.getObject("ImageMask", false), PdfBoolValue.class);
                        if (im != null && im.val) {
                        }
                        else {
                            PdfImage img = new PdfImage(xobj, false, (state == null ? null : state.fillColor), ignoreImages);
                            lex.get(i).tag = img;
                            if (state.ctmStack.size() > 0) {
                                img.x1 = (img.x2 = state.ctmStack.get(0).E);
                                img.x2 += state.ctmStack.get(0).A;
                                img.y1 = (img.y2 = state.boxHeight - state.ctmStack.get(0).F);
                                img.y1 -= state.ctmStack.get(0).D;
                                if (((img.y1 < -0.1) || img.y2 > y2 || (img.x1 < -0.1)) || img.x2 > x2) {
                                }
                            }
                            items.add(img);
                            if (storeLexems) 
                                imgs.get(imgs.size() - 1).tag = img;
                        }
                    }
                    else {
                        PdfPage ppp = new PdfPage(xobj, state, storeLexems, ignoreImages);
                        if (ppp.items.size() == 1 && (ppp.items.get(0) instanceof PdfImage)) {
                            PdfImage img = (PdfImage)com.pullenti.unisharp.Utils.cast(ppp.items.get(0), PdfImage.class);
                            lex.get(i).tag = img;
                            items.add(img);
                            if (storeLexems && ppp.imgs.size() == 1) {
                                imgs.add(ppp.imgs.get(0));
                                imgs.get(imgs.size() - 1).tag = img;
                            }
                        }
                        else {
                            com.pullenti.unisharp.Utils.addToArrayList(items, ppp.items);
                            if (subpages == null) 
                                subpages = new java.util.ArrayList<PdfPage>();
                            subpages.add(ppp);
                            lex.get(i).tag = ppp;
                        }
                    }
                }
                continue;
            }
            if (state.parseOne(lex, i)) 
                continue;
            if (path.parseOne(lex, i)) 
                continue;
            if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tf") && (lex.get(i - 2) instanceof PdfName)) {
                state.fontSize = lex.get(i - 1).getDouble();
                String fnam = ((PdfName)com.pullenti.unisharp.Utils.cast(lex.get(i - 2), PdfName.class)).name;
                if (!allFonts.containsKey(fnam)) {
                    PdfDictionary fff = null;
                    if (fnts != null) 
                        fff = fnts.getDictionary(fnam, null);
                    if (fff != null) {
                        if (fff.obj instanceof PdfFont) 
                            state.font = (PdfFont)com.pullenti.unisharp.Utils.cast(fff.obj, PdfFont.class);
                        else {
                            state.font = new PdfFont(fff);
                            state.font.alias = fnam;
                        }
                        fff.obj = state.font;
                        allFonts.put(fnam, state.font);
                    }
                }
                else 
                    state.font = allFonts.get(fnam);
                continue;
            }
            if (textRegime && (((com.pullenti.unisharp.Utils.stringsEq(nam, "Tj") || com.pullenti.unisharp.Utils.stringsEq(nam, "TJ") || com.pullenti.unisharp.Utils.stringsEq(nam, "\"")) || com.pullenti.unisharp.Utils.stringsEq(nam, "'")))) {
                if (state.font == null) 
                    continue;
                if (com.pullenti.unisharp.Utils.stringsEq(nam, "\"")) {
                    state.charSpace = lex.get(i - 2).getDouble();
                    state.wordSpace = lex.get(i - 3).getDouble();
                }
                if (com.pullenti.unisharp.Utils.stringsEq(nam, "\"") || com.pullenti.unisharp.Utils.stringsEq(nam, "'")) 
                    state.newline();
                PdfText txt = state.font.extractText(lex.get(i - 1), state);
                if (txt != null) {
                    items.add(txt);
                    lex.get(i).tag = txt;
                }
                continue;
            }
        }
        if (y2 > 0) {
            for (PdfRect it : items) {
                for (int k = 0; (k < 1000) && it.y1 > y2; k++) {
                    it.y1 -= y2;
                    it.y2 -= y2;
                }
            }
        }
        if (!storeLexems) {
            for (int i = 0; i < (items.size() - 1); i++) {
                if ((items.get(i) instanceof PdfText) && (items.get(i + 1) instanceof PdfText)) {
                    if (((PdfText)com.pullenti.unisharp.Utils.cast(items.get(i), PdfText.class)).canBeMergedWith((PdfText)com.pullenti.unisharp.Utils.cast(items.get(i + 1), PdfText.class))) {
                        ((PdfText)com.pullenti.unisharp.Utils.cast(items.get(i), PdfText.class)).mergeWith((PdfText)com.pullenti.unisharp.Utils.cast(items.get(i + 1), PdfText.class));
                        items.remove(i + 1);
                        i--;
                    }
                }
            }
        }
        double maxx = 0.0;
        for (PdfRect it : items) {
            if (it.getRight() > maxx) 
                maxx = it.getRight();
        }
        if (maxx > x2) {
            double ratio = ((x2 - (3.0))) / maxx;
            for (PdfRect it : items) {
                it.x1 *= ratio;
                it.x2 *= ratio;
            }
        }
    }

    public java.util.ArrayList<PdfObject> lexems = null;

    public java.util.ArrayList<PdfPage> subpages = null;

    public byte[] lexemContent;

    public byte[] getDataContent() throws java.io.IOException, Exception {
        byte[] val = getDic().getTotalDataStream("Contents");
        if (val == null) 
            val = this.getDic().extractData();
        if (val == null || (val.length < 1)) 
            return null;
        return val;
    }

    public void clearDataContents() throws java.io.IOException {
        getDic().clearTotalDataStream("Contents");
        getDic().setData(null);
    }

    public java.util.ArrayList<PdfRect> items = new java.util.ArrayList<PdfRect>();

    public java.util.ArrayList<PdfMiscInfo> imgs = new java.util.ArrayList<PdfMiscInfo>();

    public int beginPos;

    public int endPos;

    @Override
    public void close() {
        items.clear();
        imgs.clear();
        if (lexems != null) {
            lexems.clear();
            lexems = null;
        }
        if (subpages != null) {
            for (PdfPage sp : subpages) {
                sp.close();
            }
            subpages.clear();
            subpages = null;
        }
    }
    public PdfPage() {
        super();
    }
}
