
    Ng&                       d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
mZmZmZmZmZmZ ddlmZ erddlmZ eeedf                  Zeeef         Zeee
f         Zeeef         Z G d	 d
e          Z	 dddZddZdS )zContent stream parsing.    )annotations)
Collection)TYPE_CHECKINGUnioncast)ContentStreamInlineImageContentStreamInstructionObject
ObjectTypePagePdfError_unparse_content_stream)Operator)PdfInlineImager   c                  $     e Zd ZdZd fd	Z xZS )PdfParsingErrorz(Error when parsing a PDF content stream.Nc                f    |sd| }t                                          |           || _        d S )Nz&Error encoding content stream at line )super__init__line)selfmessager   	__class__s      Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/pikepdf/models/_content_stream.pyr   zPdfParsingError.__init__'   s<     	FEtEEG!!!			    )NN)__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r   $   sC        22         r   r    page_or_streamObject | Page	operatorsstrreturnlist[ContentStreamInstructions]c                   t          | t          t          f          st          d          t          | t                    r=| j        t
          j        k    r(|                     d          dk    rt          d          t          | t                    r| j        } 	 |                     d          dk    r6| }t          t          t                   |                    |                    }n5| }t          t          t                   t          j        ||                    }n6# t          $ r)}dt          |          v rt          d          |||d}~ww xY w|S )aZ  Parse a PDF content stream into a sequence of instructions.

    A PDF content stream is list of instructions that describe where to render
    the text and graphics in a PDF. This is the starting point for analyzing
    PDFs.

    If the input is a page and page.Contents is an array, then the content
    stream is automatically treated as one coalesced stream.

    Each instruction contains at least one operator and zero or more operands.

    This function does not have anything to do with opening a PDF file itself or
    processing data from a whole PDF. It is for processing a specific object inside
    a PDF that is already opened.

    Args:
        page_or_stream: A page object, or the content
            stream attached to another object such as a Form XObject.
        operators: A space-separated string of operators to whitelist.
            For example 'q Q cm Do' will return only operators
            that pertain to drawing images. Use 'BI ID EI' for inline images.
            All other operators and associated tokens are ignored. If blank,
            all tokens are accepted.

    Example:
        >>> with pikepdf.Pdf.open("../tests/resources/pal-1bit-trivial.pdf") as pdf:
        ...     page = pdf.pages[0]
        ...     for operands, command in pikepdf.parse_content_stream(page):
        ...         print(command)
        q
        cm
        Do
        Q

    .. versionchanged:: 3.0
        Returns a list of ``ContentStreamInstructions`` instead of a list
        of (operand, operator) tuples. The returned items are duck-type compatible
        with the previous returned items.
    z/stream must be a pikepdf.Object or pikepdf.Pagez/Typez/Pagez4parse_content_stream called on page or stream objectz#supposed to be a stream or an arrayz0parse_content_stream called on non-stream ObjectN)
isinstancer
   r   	TypeError
_type_coder   streamgetobjr   listContentStreamInstructions_parse_page_contents_grouped_parse_stream_groupedr   r%   )r"   r$   pageinstructionsr,   es         r   parse_content_streamr6   .   sd   T nvtn55 KIJJJ 	>6**P%):::w''722NOOO.$'' ,'+g&&'11!D./11)<< LL
 $F./,VY?? L    0CFF::NOOUVVQ
 s   BD   
E*$EEr4   0Collection[UnparseableContentStreamInstructions]bytesc                    	 t          |           S # t          t          t          f$ r}t	          d          |d}~ww xY w)a  Convert collection of instructions to bytes suitable for storing in PDF.

    Given a parsed list of instructions/operand-operators, convert to bytes suitable
    for embedding in a PDF. In PDF the operator always follows the operands.

    Args:
        instructions: collection of instructions such as is returned
            by :func:`parse_content_stream()`

    Returns:
        A binary content stream, suitable for attaching to a Pdf.
        To attach to a Pdf, use :meth:`Pdf.make_stream()``.

    .. versionchanged:: 3.0
        Now accept collections that contain any mixture of
        ``ContentStreamInstruction``, ``ContentStreamInlineImage``, and the older
        operand-operator tuples from pikepdf 2.x.
    z3While unparsing a content stream, an error occurredN)r   
ValueErrorr*   RuntimeErrorr   )r4   r5   s     r   unparse_content_streamr<   z   sS    *&|444	<0   A
 
	s    =8=N)r!   )r"   r#   r$   r%   r&   r'   )r4   r7   r&   r8   )r   
__future__r   collections.abcr   typingr   r   r   pikepdf._corer   r	   r
   r   r   r   r   pikepdf.objectsr   pikepdf.models.imager   _OldContentStreamOperandstuple_OldContentStreamInstructionsr0   $UnparseableContentStreamInstructions	Exceptionr   r6   r<    r   r   <module>rI      s     " " " " " " & & & & & & - - - - - - - - - -                  % $ $ $ $ $ 4333333 'uV5E-E'FG  %&?&I J !":<T"TU ',<<( $
    i    57I I I I IX     r   