/*
 * 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.IO;
using System.Text;

namespace Pullenti.Unitext.Internal.Pdf
{
    public class PdfStringValue : PdfObject
    {
        public bool IsHex = false;
        public byte[] Val;
        public override string ToStringEx(int lev)
        {
            string str = GetStringByBytes(Val);
            if (str.Length > 100) 
                str = str.Substring(0, 100) + "...";
            return string.Format("\"{0}\"", str);
        }
        public override bool IsSimple(int lev)
        {
            return true;
        }
        public static string GetStringByBytes(byte[] buf)
        {
            if (buf == null) 
                return null;
            if (buf.Length > 2 && buf[0] == 0xFF && buf[1] == 0xFE) 
                return Pullenti.Util.MiscHelper.DecodeStringUnicode(buf, 2, buf.Length - 2);
            if (buf.Length > 2 && buf[0] == 0xFE && buf[1] == 0xFF) 
                return Pullenti.Util.MiscHelper.DecodeStringUnicodeBE(buf, 2, buf.Length - 2);
            return Pullenti.Util.MiscHelper.DecodeStringAscii(buf, 0, -1);
        }
        public static string ConvertGCoding(string gstr)
        {
            StringBuilder res = new StringBuilder();
            for (int i = 0; i < gstr.Length; i++) 
            {
                if (((i + 3) < gstr.Length) && gstr[i] == '/' && gstr[i + 1] == 'G') 
                {
                }
                else 
                {
                    res.Append(gstr[i]);
                    continue;
                }
                char ch0 = gstr[i + 2];
                char ch1 = gstr[i + 3];
                int uu = 0;
                if (ch0 >= '0' && ch0 <= '9') 
                    uu += ((int)((ch0 - '0')));
                else if (ch0 >= 'A' && ch0 <= 'Z') 
                    uu += (10 + ((int)((ch0 - 'A'))));
                if (uu > 0) 
                {
                    uu *= 16;
                    if (ch1 >= '0' && ch1 <= '9') 
                        uu += ((int)((ch1 - '0')));
                    else if (ch1 >= 'A' && ch1 <= 'Z') 
                        uu += (10 + ((int)((ch1 - 'A'))));
                    else 
                        uu = 0;
                }
                if (uu > 0 && (uu < Pullenti.Util.MiscHelper.m_1251_utf.Length)) 
                    res.Append((char)Pullenti.Util.MiscHelper.m_1251_utf[uu]);
                i += 3;
            }
            return res.ToString();
        }
        public override void Write(Stream s)
        {
            if (IsHex) 
            {
                s.WriteByte((byte)'<');
                for (int i = 0; i < Val.Length; i++) 
                {
                    int v = ((Val[i] >> 4)) & 0xF;
                    int b = (v < 10 ? (((int)'0') + v) : ((((int)'A') + v) - 10));
                    s.WriteByte((byte)b);
                    v = Val[i] & 0xF;
                    b = (v < 10 ? (((int)'0') + v) : ((((int)'A') + v) - 10));
                    s.WriteByte((byte)b);
                }
                s.WriteByte((byte)'>');
            }
            else 
            {
                s.WriteByte((byte)'(');
                s.Write(Val, 0, Val.Length);
                s.WriteByte((byte)')');
            }
        }
    }
}