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


const RgbColor = require("./RgbColor");
const PdfName = require("./PdfName");
const Matrix = require("./Matrix");

class PdfTextState {
    
    constructor() {
        this.ctmStack = null;
        this.boxHeight = 0;
        this.fontSize = 0;
        this.font = null;
        this.m_FontStack = new Array();
        this.m_FontSizeStack = new Array();
        this.charSpace = 0;
        this.wordSpace = 0;
        this.wScale = 100;
        this.leading = 0;
        this.rise = 0;
        this.render = 0;
        this.fillColor = null;
        this.m_FillColorStack = new Array();
        this.tlm = new Matrix();
        this.tm = new Matrix();
        this.ctmStack = new Array();
        this.ctmStack.push(new Matrix());
    }
    
    parseOne(lex, i) {
        let nam = lex[i].name;
        if (nam === "Tc") {
            this.charSpace = lex[i - 1].getDouble();
            return true;
        }
        if (nam === "Tw") {
            this.wordSpace = lex[i - 1].getDouble();
            return true;
        }
        if (nam === "Tz") {
            this.wScale = lex[i - 1].getDouble();
            return true;
        }
        if (nam === "Ts") {
            this.rise = lex[i - 1].getDouble();
            return true;
        }
        if (nam === "Tr") {
            this.render = Math.floor(lex[i - 1].getDouble());
            return true;
        }
        if (nam === "TL") {
            this.leading = lex[i - 1].getDouble();
            return true;
        }
        if (nam === "Td" || nam === "TD") {
            let x = lex[i - 2].getDouble();
            let y = lex[i - 1].getDouble();
            if (nam === "TD") 
                this.leading = -y;
            this.tlm.translate(x, y);
            this.tm.copyFrom(this.tlm);
            return true;
        }
        if (nam === "T*") {
            this.newline();
            return true;
        }
        if (nam === "Tm") {
            this.tlm.set(lex[i - 6].getDouble(), lex[i - 5].getDouble(), lex[i - 4].getDouble(), lex[i - 3].getDouble(), lex[i - 2].getDouble(), lex[i - 1].getDouble());
            this.tm.copyFrom(this.tlm);
            return true;
        }
        if (nam === "cm") {
            let mmm = new Matrix();
            mmm.set(lex[i - 6].getDouble(), lex[i - 5].getDouble(), lex[i - 4].getDouble(), lex[i - 3].getDouble(), lex[i - 2].getDouble(), lex[i - 1].getDouble());
            if (this.ctmStack.length > 0) {
                mmm.multiply(this.ctmStack[0]);
                this.ctmStack[0] = mmm;
            }
            else 
                this.ctmStack.splice(0, 0, mmm);
            return true;
        }
        if (nam === "q") {
            let mmm = new Matrix();
            if (this.ctmStack.length > 0) 
                mmm.copyFrom(this.ctmStack[0]);
            this.ctmStack.splice(0, 0, mmm);
            if (this.font !== null) 
                this.m_FontStack.splice(0, 0, this.font);
            this.m_FontSizeStack.splice(0, 0, this.fontSize);
            this.m_FillColorStack.splice(0, 0, this.fillColor);
            return true;
        }
        if (nam === "Q") {
            if (this.ctmStack.length > 0) 
                this.ctmStack.splice(0, 1);
            if (this.m_FontStack.length > 0) {
                this.font = this.m_FontStack[0];
                this.m_FontStack.splice(0, 1);
            }
            if (this.m_FontSizeStack.length > 0) {
                this.fontSize = this.m_FontSizeStack[0];
                this.m_FontSizeStack.splice(0, 1);
            }
            if (this.m_FillColorStack.length > 0) {
                this.fillColor = this.m_FillColorStack[0];
                this.m_FillColorStack.splice(0, 1);
            }
        }
        if (nam === "rg" && (i - 3) >= 0) {
            let col = new RgbColor();
            col.R = Math.floor((lex[i - 3].getDouble() * (256)));
            col.G = Math.floor((lex[i - 2].getDouble() * (256)));
            col.B = Math.floor((lex[i - 1].getDouble() * (256)));
            this.fillColor = col;
        }
        if (nam === "g" && (i - 1) >= 0) {
            let col = new RgbColor();
            col.R = (col.G = (col.B = Math.floor((lex[i - 1].getDouble() * (256)))));
            this.fillColor = col;
        }
        return false;
    }
    
    newline() {
        this.tlm.translate(0, -this.leading);
        this.tm.copyFrom(this.tlm);
    }
}


module.exports = PdfTextState