# 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

import io
from pullenti.unisharp.Utils import Utils
from pullenti.unisharp.Streams import Stream

from pullenti.util.MiscHelper import MiscHelper
from pullenti.unitext.internal.pdf.PdfObject import PdfObject

class PdfStringValue(PdfObject):
    
    def __init__(self) -> None:
        super().__init__()
        self.is_hex = False
        self.val = None;
    
    def to_string_ex(self, lev : int) -> str:
        str0_ = PdfStringValue.get_string_by_bytes(self.val)
        if (len(str0_) > 100): 
            str0_ = (str0_[0:0+100] + "...")
        return "\"{0}\"".format(str0_)
    
    def is_simple(self, lev : int) -> bool:
        return True
    
    @staticmethod
    def get_string_by_bytes(buf : bytearray) -> str:
        if (buf is None): 
            return None
        if (len(buf) > 2 and buf[0] == (0xFF) and buf[1] == (0xFE)): 
            return MiscHelper.decode_string_unicode(buf, 2, len(buf) - 2)
        if (len(buf) > 2 and buf[0] == (0xFE) and buf[1] == (0xFF)): 
            return MiscHelper.decode_string_unicodebe(buf, 2, len(buf) - 2)
        return MiscHelper.decode_string_ascii(buf, 0, -1)
    
    @staticmethod
    def convert_gcoding(gstr : str) -> str:
        res = io.StringIO()
        i = 0
        first_pass719 = True
        while True:
            if first_pass719: first_pass719 = False
            else: i += 1
            if (not (i < len(gstr))): break
            if (((i + 3) < len(gstr)) and gstr[i] == '/' and gstr[i + 1] == 'G'): 
                pass
            else: 
                print(gstr[i], end="", file=res)
                continue
            ch0 = gstr[i + 2]
            ch1 = gstr[i + 3]
            uu = 0
            if (ch0 >= '0' and ch0 <= '9'): 
                uu += (((ord(ch0)) - (ord('0'))))
            elif (ch0 >= 'A' and ch0 <= 'Z'): 
                uu += (10 + (((ord(ch0)) - (ord('A')))))
            if (uu > 0): 
                uu *= 16
                if (ch1 >= '0' and ch1 <= '9'): 
                    uu += (((ord(ch1)) - (ord('0'))))
                elif (ch1 >= 'A' and ch1 <= 'Z'): 
                    uu += (10 + (((ord(ch1)) - (ord('A')))))
                else: 
                    uu = 0
            if (uu > 0 and (uu < len(MiscHelper._m_1251_utf))): 
                print(chr(MiscHelper._m_1251_utf[uu]), end="", file=res)
            i += 3
        return Utils.toStringStringIO(res)
    
    def write0_(self, s : Stream) -> None:
        if (self.is_hex): 
            s.writebyte(ord('<'))
            i = 0
            while i < len(self.val): 
                v = (((self.val[i]) >> 4)) & 0xF
                b = (((ord('0')) + v) if v < 10 else (((ord('A')) + v) - 10))
                s.writebyte(b)
                v = ((self.val[i]) & 0xF)
                b = (((ord('0')) + v) if v < 10 else (((ord('A')) + v) - 10))
                s.writebyte(b)
                i += 1
            s.writebyte(ord('>'))
        else: 
            s.writebyte(ord('('))
            s.write(self.val, 0, len(self.val))
            s.writebyte(ord(')'))
    
    @staticmethod
    def _new245(_arg1 : bool) -> 'PdfStringValue':
        res = PdfStringValue()
        res.is_hex = _arg1
        return res