/*
 * 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 PdfStringValue extends PdfObject {

    public boolean isHex = false;

    public byte[] val;

    @Override
    public String toStringEx(int lev) {
        String str = getStringByBytes(val);
        if (str.length() > 100) 
            str = str.substring(0, 0 + 100) + "...";
        return ("\"" + str + "\"");
    }

    @Override
    public boolean isSimple(int lev) {
        return true;
    }

    public static String getStringByBytes(byte[] buf) {
        if (buf == null) 
            return null;
        if (buf.length > 2 && buf[0] == ((byte)0xFF) && buf[1] == ((byte)0xFE)) 
            return com.pullenti.util.MiscHelper.decodeStringUnicode(buf, 2, buf.length - 2);
        if (buf.length > 2 && buf[0] == ((byte)0xFE) && buf[1] == ((byte)0xFF)) 
            return com.pullenti.util.MiscHelper.decodeStringUnicodeBE(buf, 2, buf.length - 2);
        return com.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.charAt(i) == '/' && gstr.charAt(i + 1) == 'G') {
            }
            else {
                res.append(gstr.charAt(i));
                continue;
            }
            char ch0 = gstr.charAt(i + 2);
            char ch1 = gstr.charAt(i + 3);
            int uu = 0;
            if (ch0 >= '0' && ch0 <= '9') 
                uu += ((((int)ch0) - ((int)'0')));
            else if (ch0 >= 'A' && ch0 <= 'Z') 
                uu += (10 + ((((int)ch0) - ((int)'A'))));
            if (uu > 0) {
                uu *= 16;
                if (ch1 >= '0' && ch1 <= '9') 
                    uu += ((((int)ch1) - ((int)'0')));
                else if (ch1 >= 'A' && ch1 <= 'Z') 
                    uu += (10 + ((((int)ch1) - ((int)'A'))));
                else 
                    uu = 0;
            }
            if (uu > 0 && (uu < com.pullenti.util.MiscHelper.m_1251_utf.length)) 
                res.append((char)com.pullenti.util.MiscHelper.m_1251_utf[uu]);
            i += 3;
        }
        return res.toString();
    }

    @Override
    public void write(com.pullenti.unisharp.Stream s) {
        try {
            if (isHex) {
                s.write((byte)'<');
                for (int i = 0; i < val.length; i++) {
                    int v = (((Byte.toUnsignedInt(val[i])) >>> 4)) & 0xF;
                    int b = (v < 10 ? (((int)'0') + v) : ((((int)'A') + v) - 10));
                    s.write((byte)b);
                    v = (Byte.toUnsignedInt(val[i])) & 0xF;
                    b = (v < 10 ? (((int)'0') + v) : ((((int)'A') + v) - 10));
                    s.write((byte)b);
                }
                s.write((byte)'>');
            }
            else {
                s.write((byte)'(');
                s.write(val, 0, val.length);
                s.write((byte)')');
            }
        } catch (Exception ex123) {
        }
    }

    public static PdfStringValue _new246(boolean _arg1) {
        PdfStringValue res = new PdfStringValue();
        res.isHex = _arg1;
        return res;
    }
    public PdfStringValue() {
        super();
    }
}
