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

    public PdfTextState() {
        ctmStack = new java.util.ArrayList<Matrix>();
        ctmStack.add(new Matrix());
    }

    public java.util.ArrayList<Matrix> ctmStack;

    public double boxHeight;

    public double fontSize;

    public PdfFont font;

    private java.util.ArrayList<PdfFont> m_FontStack = new java.util.ArrayList<PdfFont>();

    private java.util.ArrayList<Double> m_FontSizeStack = new java.util.ArrayList<Double>();

    public double charSpace = 0.0;

    public double wordSpace = 0.0;

    public double wScale = 100.0;

    public double leading = 0.0;

    public double rise = 0.0;

    public int render = 0;

    public RgbColor fillColor = null;

    private java.util.ArrayList<RgbColor> m_FillColorStack = new java.util.ArrayList<RgbColor>();

    public Matrix tlm = new Matrix();

    public Matrix tm = new Matrix();

    public boolean parseOne(java.util.ArrayList<PdfObject> lex, int i) throws java.io.IOException {
        String nam = ((PdfName)com.pullenti.unisharp.Utils.cast(lex.get(i), PdfName.class)).name;
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tc")) {
            charSpace = lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tw")) {
            wordSpace = lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tz")) {
            wScale = lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Ts")) {
            rise = lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tr")) {
            render = (int)lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "TL")) {
            leading = lex.get(i - 1).getDouble();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Td") || com.pullenti.unisharp.Utils.stringsEq(nam, "TD")) {
            double x = lex.get(i - 2).getDouble();
            double y = lex.get(i - 1).getDouble();
            if (com.pullenti.unisharp.Utils.stringsEq(nam, "TD")) 
                leading = -y;
            tlm.translate(x, y);
            tm.copyFrom(tlm);
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "T*")) {
            this.newline();
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Tm")) {
            tlm.set(lex.get(i - 6).getDouble(), lex.get(i - 5).getDouble(), lex.get(i - 4).getDouble(), lex.get(i - 3).getDouble(), lex.get(i - 2).getDouble(), lex.get(i - 1).getDouble());
            tm.copyFrom(tlm);
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "cm")) {
            Matrix mmm = new Matrix();
            mmm.set(lex.get(i - 6).getDouble(), lex.get(i - 5).getDouble(), lex.get(i - 4).getDouble(), lex.get(i - 3).getDouble(), lex.get(i - 2).getDouble(), lex.get(i - 1).getDouble());
            if (ctmStack.size() > 0) {
                mmm.multiply(ctmStack.get(0));
                com.pullenti.unisharp.Utils.putArrayValue(ctmStack, 0, mmm);
            }
            else 
                ctmStack.add(0, mmm);
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "q")) {
            Matrix mmm = new Matrix();
            if (ctmStack.size() > 0) 
                mmm.copyFrom(ctmStack.get(0));
            ctmStack.add(0, mmm);
            if (font != null) 
                m_FontStack.add(0, font);
            m_FontSizeStack.add(0, fontSize);
            m_FillColorStack.add(0, fillColor);
            return true;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "Q")) {
            if (ctmStack.size() > 0) 
                ctmStack.remove(0);
            if (m_FontStack.size() > 0) {
                font = m_FontStack.get(0);
                m_FontStack.remove(0);
            }
            if (m_FontSizeStack.size() > 0) {
                fontSize = m_FontSizeStack.get(0);
                m_FontSizeStack.remove(0);
            }
            if (m_FillColorStack.size() > 0) {
                fillColor = m_FillColorStack.get(0);
                m_FillColorStack.remove(0);
            }
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "rg") && (i - 3) >= 0) {
            RgbColor col = new RgbColor();
            col.R = (byte)((int)((lex.get(i - 3).getDouble() * (256.0))));
            col.G = (byte)((int)((lex.get(i - 2).getDouble() * (256.0))));
            col.B = (byte)((int)((lex.get(i - 1).getDouble() * (256.0))));
            fillColor = col;
        }
        if (com.pullenti.unisharp.Utils.stringsEq(nam, "g") && (i - 1) >= 0) {
            RgbColor col = new RgbColor();
            col.R = (col.G = (col.B = (byte)((int)((lex.get(i - 1).getDouble() * (256.0))))));
            fillColor = col;
        }
        return false;
    }

    public void newline() {
        tlm.translate(0.0, -leading);
        tm.copyFrom(tlm);
    }
}
