/*
 * 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 PdfPathState {

    private PdfPage m_Page;

    public PdfPathState(PdfPage p) {
        m_Page = p;
        m_x = new java.util.ArrayList<Double>();
        m_y = new java.util.ArrayList<Double>();
    }

    private java.util.ArrayList<Double> m_x;

    private java.util.ArrayList<Double> m_y;

    public boolean parseOne(java.util.ArrayList<PdfObject> lex, int i) throws java.io.IOException {
        if (i >= lex.size()) 
            return false;
        String nam = ((PdfName)com.pullenti.unisharp.Utils.cast(lex.get(i), PdfName.class)).name;
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "m")) {
            m_x.clear();
            m_y.clear();
            m_x.add(lex.get(i - 2).getDouble());
            m_y.add(m_Page.getHeight() - lex.get(i - 1).getDouble());
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "l")) {
            m_x.add(lex.get(i - 2).getDouble());
            m_y.add(m_Page.getHeight() - lex.get(i - 1).getDouble());
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "h")) {
            if (m_x.size() > 0) {
                m_x.add(m_x.get(0));
                m_y.add(m_y.get(0));
            }
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "re")) {
            double x = lex.get(i - 4).getDouble();
            double y = m_Page.getHeight() - lex.get(i - 3).getDouble();
            double w = lex.get(i - 2).getDouble();
            double h = lex.get(i - 1).getDouble();
            m_x.add(x);
            m_y.add(y);
            m_x.add(x);
            m_y.add(y - h);
            m_x.add(x + w);
            m_y.add(y - h);
            m_x.add(x + w);
            m_y.add(y);
            m_x.add(x);
            m_y.add(y);
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "n")) {
            m_x.clear();
            m_y.clear();
            return true;
        }
        if (((((com.pullenti.unisharp.Utils.stringsEq(nam, "S") || com.pullenti.unisharp.Utils.stringsEq(nam, "s") || com.pullenti.unisharp.Utils.stringsEq(nam, "b")) || com.pullenti.unisharp.Utils.stringsEq(nam, "B") || com.pullenti.unisharp.Utils.stringsEq(nam, "B*")) || com.pullenti.unisharp.Utils.stringsEq(nam, "b*") || com.pullenti.unisharp.Utils.stringsEq(nam, "f")) || com.pullenti.unisharp.Utils.stringsEq(nam, "F") || com.pullenti.unisharp.Utils.stringsEq(nam, "f*")) || com.pullenti.unisharp.Utils.stringsEq(nam, "W") || com.pullenti.unisharp.Utils.stringsEq(nam, "W*")) {
            if (m_x.size() == 5 && m_x.get(0) == m_x.get(4) && m_y.get(0) == m_y.get(4)) {
                PdfFig re = null;
                if (m_x.get(1) == m_x.get(0) && m_y.get(1) == m_y.get(2) && m_x.get(2) == m_x.get(3)) {
                    re = new PdfFig();
                    re.x1 = m_x.get(0);
                    re.x2 = m_x.get(2);
                    re.y1 = m_y.get(0);
                    re.y2 = m_y.get(1);
                }
                else if (m_y.get(0) == m_y.get(1) && m_x.get(1) == m_x.get(2) && m_y.get(2) == m_y.get(3)) {
                    re = new PdfFig();
                    re.x1 = m_x.get(0);
                    re.x2 = m_x.get(1);
                    re.y1 = m_y.get(0);
                    re.y2 = m_y.get(2);
                }
                if (re != null) {
                    if (re.x1 > re.x2) {
                        double d = re.x1;
                        re.x1 = re.x2;
                        re.x2 = d;
                    }
                    if (re.y1 > re.y2) {
                        double d = re.y1;
                        re.y1 = re.y2;
                        re.y2 = d;
                    }
                    m_Page.items.add(re);
                    return true;
                }
            }
            if (m_x.size() > 1000) {
            }
            else 
                for (int ii = 0; ii < (m_x.size() - 1); ii++) {
                    if (m_x.get(ii) == m_x.get(ii + 1) || m_y.get(ii) == m_y.get(ii + 1)) {
                        PdfFig re = new PdfFig();
                        re.x1 = m_x.get(ii);
                        re.x2 = m_x.get(ii + 1);
                        re.y1 = m_y.get(ii);
                        re.y2 = m_y.get(ii + 1);
                        if (re.x1 > re.x2) {
                            double d = re.x1;
                            re.x1 = re.x2;
                            re.x2 = d;
                        }
                        if (re.y1 > re.y2) {
                            double d = re.y1;
                            re.y1 = re.y2;
                            re.y2 = d;
                        }
                        m_Page.items.add(re);
                    }
                }
            return true;
        }
        return false;
    }
    public PdfPathState() {
    }
}
