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

using System;
using System.Collections;
using System.Collections.Generic;

namespace Pullenti.Unitext.Internal.Pdf
{
    class PdfTextState
    {
        public PdfTextState()
        {
            CtmStack = new List<Matrix>();
            CtmStack.Add(new Matrix());
        }
        public List<Matrix> CtmStack;
        public double BoxHeight;
        public double FontSize;
        public PdfFont Font;
        List<PdfFont> m_FontStack = new List<PdfFont>();
        List<double> m_FontSizeStack = new List<double>();
        public double CharSpace = 0;
        public double WordSpace = 0;
        public double WScale = 100;
        public double Leading = 0;
        public double Rise = 0;
        public int Render = 0;
        public RgbColor FillColor = null;
        List<RgbColor> m_FillColorStack = new List<RgbColor>();
        public Matrix Tlm = new Matrix();
        public Matrix Tm = new Matrix();
        public bool ParseOne(List<PdfObject> lex, int i)
        {
            string nam = (lex[i] as PdfName).Name;
            if (nam == "Tc") 
            {
                CharSpace = lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "Tw") 
            {
                WordSpace = lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "Tz") 
            {
                WScale = lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "Ts") 
            {
                Rise = lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "Tr") 
            {
                Render = (int)lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "TL") 
            {
                Leading = lex[i - 1].GetDouble();
                return true;
            }
            if (nam == "Td" || nam == "TD") 
            {
                double x = lex[i - 2].GetDouble();
                double y = lex[i - 1].GetDouble();
                if (nam == "TD") 
                    Leading = -y;
                Tlm.Translate(x, y);
                Tm.CopyFrom(Tlm);
                return true;
            }
            if (nam == "T*") 
            {
                this.Newline();
                return true;
            }
            if (nam == "Tm") 
            {
                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());
                Tm.CopyFrom(Tlm);
                return true;
            }
            if (nam == "cm") 
            {
                Matrix 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 (CtmStack.Count > 0) 
                {
                    mmm.Multiply(CtmStack[0]);
                    CtmStack[0] = mmm;
                }
                else 
                    CtmStack.Insert(0, mmm);
                return true;
            }
            if (nam == "q") 
            {
                Matrix mmm = new Matrix();
                if (CtmStack.Count > 0) 
                    mmm.CopyFrom(CtmStack[0]);
                CtmStack.Insert(0, mmm);
                if (Font != null) 
                    m_FontStack.Insert(0, Font);
                m_FontSizeStack.Insert(0, FontSize);
                m_FillColorStack.Insert(0, FillColor);
                return true;
            }
            if (nam == "Q") 
            {
                if (CtmStack.Count > 0) 
                    CtmStack.RemoveAt(0);
                if (m_FontStack.Count > 0) 
                {
                    Font = m_FontStack[0];
                    m_FontStack.RemoveAt(0);
                }
                if (m_FontSizeStack.Count > 0) 
                {
                    FontSize = m_FontSizeStack[0];
                    m_FontSizeStack.RemoveAt(0);
                }
                if (m_FillColorStack.Count > 0) 
                {
                    FillColor = m_FillColorStack[0];
                    m_FillColorStack.RemoveAt(0);
                }
            }
            if (nam == "rg" && (i - 3) >= 0) 
            {
                RgbColor col = new RgbColor();
                col.R = (byte)((int)((lex[i - 3].GetDouble() * 256)));
                col.G = (byte)((int)((lex[i - 2].GetDouble() * 256)));
                col.B = (byte)((int)((lex[i - 1].GetDouble() * 256)));
                FillColor = col;
            }
            if (nam == "g" && (i - 1) >= 0) 
            {
                RgbColor col = new RgbColor();
                col.R = (col.G = (col.B = (byte)((int)((lex[i - 1].GetDouble() * 256)))));
                FillColor = col;
            }
            return false;
        }
        public void Newline()
        {
            Tlm.Translate(0, -Leading);
            Tm.CopyFrom(Tlm);
        }
    }
}