# 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
import math
import typing
from pullenti.unisharp.Utils import Utils
from pullenti.unisharp.Misc import RefOutArgWrapper
from pullenti.unisharp.Streams import MemoryStream
from pullenti.unisharp.Streams import Stream

from pullenti.unitext.internal.pdf.PdfName import PdfName
from pullenti.unitext.internal.pdf.IPdfDictionaryWrapper import IPdfDictionaryWrapper
from pullenti.unitext.internal.pdf.PdfRect import PdfRect
from pullenti.util.MiscHelper import MiscHelper
from pullenti.unitext.internal.pdf.PdfImage import PdfImage
from pullenti.unitext.internal.pdf.PdfText import PdfText
from pullenti.unitext.internal.pdf.PdfPage import PdfPage

class PdfPageOper:
    
    def __init__(self) -> None:
        self.args = list()
        self.name = None;
        self.text = None;
        self.begin_pos = 0
        self.end_pos = 0
        self.deleted = False
        self.replace_by = None;
        self.insert_after = None;
        self.x1 = 0
        self.x2 = 0
        self.y1 = 0
        self.y2 = 0
        self.dic = None;
        self.is_image = False
        self.is_form = False
        self.tag = None;
    
    def __str__(self) -> str:
        tmp = io.StringIO()
        i = 0
        while i < len(self.args): 
            print(self.args[i], end="", file=tmp)
            print(' ', end="", file=tmp)
            i += 1
        print(self.name, end="", file=tmp)
        return Utils.toStringStringIO(tmp)
    
    def out_html(self, res : io.StringIO) -> None:
        for a in self.args: 
            MiscHelper.correct_html_value(res, a, False, False, False)
            print(' ', end="", file=res)
        print("<b>{0}</b>".format(self.name), end="", file=res, flush=True)
        if (self.text is not None): 
            print(" text: <span style=\"color:green\">", end="", file=res)
            MiscHelper.correct_html_value(res, self.text, False, False, False)
            print("</span>", end="", file=res)
        if (self.is_image and self.dic is not None): 
            print(" <span style=\"color:green\">image: size={0}</span>".format(self.dic.get_int_item("Length")), end="", file=res, flush=True)
        if (self.is_form and self.dic is not None): 
            print(" <span style=\"color:green\">form (image): size={0}</span>".format(self.dic.get_int_item("Length")), end="", file=res, flush=True)
        if (self.x2 > 0 or self.y2 > 0): 
            print(" <span style=\"color:blue\">x=[{0} .. {1}] y=[{2} .. {3}]</span>".format(round(self.x1, 2), round(self.x2, 2), round(self.y1, 2), round(self.y2, 2)), end="", file=res, flush=True)
    
    @staticmethod
    def parse(li : typing.List['PdfObject']) -> typing.List['PdfPageOper']:
        res = list()
        if (li is None): 
            return res
        args_ = list()
        pos = 0
        i = 0
        first_pass718 = True
        while True:
            if first_pass718: first_pass718 = False
            else: i += 1
            if (not (i < len(li))): break
            lex = li[i]
            nam = PdfPageOper.__check_end_of_oper(li, i)
            if (nam is None): 
                if (len(args_) == 0): 
                    pos = (lex.read_pos)
                args_.append(str(lex))
                continue
            op = PdfPageOper()
            op.name = nam
            op.begin_pos = (lex.read_pos)
            op.end_pos = (((lex.read_pos + (lex.read_len)) - (1)))
            if (len(args_) > 0): 
                op.begin_pos = pos
                op.args.extend(args_)
                args_.clear()
            r = None
            if (isinstance(lex.tag, PdfText)): 
                r = (Utils.asObjectOrNull(lex.tag, PdfRect))
                op.text = lex.tag.text
            elif (isinstance(lex.tag, PdfRect)): 
                r = (Utils.asObjectOrNull(lex.tag, PdfRect))
                if (isinstance(lex.tag, PdfImage)): 
                    op.is_image = True
                elif (isinstance(lex.tag, PdfPage)): 
                    op.is_form = True
                if (isinstance(lex.tag, IPdfDictionaryWrapper)): 
                    op.dic = lex.tag.dic
            res.append(op)
            if (len(res) == 2686): 
                pass
        return res
    
    @staticmethod
    def __check_end_of_oper(li : typing.List['PdfObject'], i : int) -> str:
        if (i >= len(li)): 
            return None
        pname = Utils.asObjectOrNull(li[i], PdfName)
        if (pname is None or len(pname.name) > 3 or ((((i + 1) < len(li)) and (isinstance(li[i + 1], PdfName)) and li[i + 1].name == "Do"))): 
            return None
        if (((i + 2) < len(li)) and (isinstance(li[i + 2], PdfName)) and li[i + 2].name == "Tf"): 
            return None
        if (((i + 1) < len(li)) and (isinstance(li[i + 1], PdfName))): 
            if (Utils.compareStrings(li[i + 1].name, "SCN", True) == 0): 
                return None
        return pname.name
    
    def get_arg_double(self, i : int) -> float:
        if (i >= 0 and (i < len(self.args))): 
            d = 0
            wrapd233 = RefOutArgWrapper(0)
            inoutres234 = MiscHelper.try_parse_double(self.args[i], wrapd233)
            d = wrapd233.value
            if (inoutres234): 
                return d
        return 0
    
    def get_bytes_to_stream(self, s : Stream) -> None:
        for a in self.args: 
            if (str.isalpha(a[0])): 
                s.writebyte(ord('/'))
            for ch in a: 
                s.writebyte(ord(ch))
            s.writebyte(0x20)
        i = 0
        while i < len(self.name): 
            s.writebyte(ord(self.name[i]))
            i += 1
        s.writebyte(0x20)
    
    def get_bytes(self) -> bytearray:
        with MemoryStream() as mem: 
            mem.writebyte(0x20)
            self.get_bytes_to_stream(mem)
            return mem.toarray()