
    OAi                        S r SSKJr  SSKJr  SSKJrJrJr  SSK	J
r
JrJrJrJrJrJr  SSKJr  \(       a  SSKJr  \\\S4      r\\\4   r\\
-  r\\-  r " S	 S
\5      r S     SS jjr    SS jrg)zContent stream parsing.    )annotations)
Collection)TYPE_CHECKINGUnioncast)ContentStreamInlineImageContentStreamInstructionObject
ObjectTypePagePdfError_unparse_content_stream)Operator)PdfInlineImager   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )PdfParsingError$   z(Error when parsing a PDF content stream.c                H   > U(       d  SU 3n[         TU ]  U5        X l        g )Nz&Error encoding content stream at line )super__init__line)selfmessager   	__class__s      Z/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pikepdf/models/_content_stream.pyr   PdfParsingError.__init__'   s$    >tfEG!	    )r   )NN)__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__)r   s   @r   r   r   $   s    2 r   r   c                v   [        U [        [        -  5      (       d  [        S5      e[        U [        5      (       a>  U R                  [
        R                  :w  a   U R                  S5      S:w  a  [        S5      e[        U [        5      (       a  U R                  n  U R                  S5      S:X  a*  U n[        [        [           UR                  U5      5      nU$ U n[        [        [           [        R                  " XA5      5      n U$ ! [         a"  nS[        U5      ;   a  [        S5      UeXUeSnAff = f)a  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   str)page_or_stream	operatorspageinstructionsr)   es         r   parse_content_streamr6   .   s$   T nftm44IJJ 	>6**%%):)::w'72NOO.$'''++g&'1!D./11)<L  $F./,,V?L   0CF:NOUVVs   =D -D 
D8D33D8c                l     [        U 5      $ ! [        [        [        4 a  n[	        S5      UeSnAff = f)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   s<    *&|44	<0 A
	s   
 3.3N) )r1   zObject | Pager2   r0   returnzlist[ContentStreamInstructions])r4   z0Collection[UnparseableContentStreamInstructions]r<   bytes)r"   
__future__r   collections.abcr   typingr   r   r   pikepdf._corer   r	   r
   r   r   r   r   pikepdf.objectsr   pikepdf.models.imager   _OldContentStreamOperandstuple_OldContentStreamInstructionsr-   $UnparseableContentStreamInstructions	Exceptionr   r6   r:    r   r   <module>rJ      s     " & - -   %3 'uV5E-E'FG  %&?&I J 47OO   == %
i  57I!I.1I$IXB
r   