
    OAi                        S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  \" \5      r\" S5      r\" S5      r\" S	5      r\" S
5      r " S S5      r\R&                  " 5       4     SS jjrg)z#Parsing the matrixes in a PDF file.    )annotations)	getLogger)MatrixPage)parse_content_stream)OperatorcmDoqQc                  v    \ rS rSrSr\R                  " 5       4SS jjrS rS r	SS jr
S r\SS j5       rS	rg
)MatrixStack   a  Tracks the CTM (current transformation matrix) in a PDF content stream.

The CTM starts as the initial matrix and can be changed via the 'cm'
(concatenate matrix) operator --> CTM = CTM x CM (with CTM and CM
being 3x3 matrixes). Initial matrix is the identity matrix unless overridden.

Furthermore can the CTM be stored to the stack via the 'q' operator.
This save the CTM and subsequent 'cm' operators change a copy of that CTM
--> 'q 1 0 0 1 0 0 cm'
--> Copy CTM onto the stack and change the copy via 'cm'

With the 'Q' operator the current CTM is replaced with the previous one from the
stack.

Error handling:
1. Popping from an empty stack results in CTM being set to the initial matrix
2. Multiplying with invalid operands sets the CTM to invalid
3. Multiplying an invalid CTM with a valid CM results in an invalid CTM
4. Stacking an invalid CTM results in a copy of that invalid CTM onto the stack
--> All operations with an invalid CTM result in an invalid CTM
--> The CTM is valid again when all invalid CTMs are popped off the stack
c                4    Xl         U R                   /U l        g)z/Initializing the stack with the initial matrix.N_initial_matrix_stack)selfinitial_matrixs     N/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pikepdf/models/ctm.py__init__MatrixStack.__init__0   s    -,0,@,@+A    c                T    U R                   R                  U R                   S   5        g)z'Copying the current CTM onto the stack.N)r   appendr   s    r   stackMatrixStack.stack5   s    4;;r?+r   c                    [        U R                  5      S:  d   S5       e[        U R                  5      S:X  a  U R                  /U l        gU R                  R                  5         g)a  Removing the current CTM from the stack.

The stack is not permitted to underflow. If popped too many times, the CTM
is set to the initial matrix. Some PDFs contain invalid content streams
that would result in an underflow, therefore the initial matrix is used
as a safe fallback.
   zcan't be emptyN)lenr   r   popr   s    r   r#   MatrixStack.pop9   sP     4;;1$6&66$t{{q //0DKKKOOr   c                d    U R                   S   c  gU R                   S   U-  U R                   S'   g)z=Multiplies the CTM with `matrix`. The result is not returned.r   Nr   )r   matrixs     r   multiplyMatrixStack.multiplyG   s-    ;;r?""kk"o6DKKOr   c                "    SU R                   S'   g)z\Registers the occurence of an invalid CM.

See `# Error handling` for further informations.
Nr   r&   r   s    r   (invalidate_current_transformation_matrix4MatrixStack.invalidate_current_transformation_matrixN   s    
 Br   c                     U R                   S   $ )zDReturns the current transformation matrix or `None` if it's invalid.r   r&   r   s    r   ctmMatrixStack.ctmU   s     {{2r   r   N)r   r   returnNone)r'   r   )r0   zMatrix | None)__name__
__module____qualname____firstlineno____doc__r   identityr   r   r#   r(   r+   propertyr.   __static_attributes__ r   r   r   r      sA    . 170A B
,7  r   r   c                X   / n[        U5      n[        U 5       H  nUR                  UR                  peU[        :X  a  UR                  5         M6  U[        :X  a  UR                  5         MR  U[        :X  a   UR                  [        U6 5        Mv  U[        :X  d  M  [!        US   5      nUR"                  b  UR%                  XsR"                  45        M  [        R                  SU 35        M     U$ ! [         a-    [        R                  SU 35        UR                  5          GM  f = f)zpDetermines the current transformation matrix (CTM) for each drawn object.

Filters objects with an invalid CTM.
z&malformed operands for `cm` operator: r   z6skipping `Do` operator due to invalid CTM for object: )r   r   operatoroperandsOPERATOR_STACKr   OPERATOR_POPr#   OPERATOR_CMr(   r   	TypeErrorloggerdebugr+   OPERATOR_DOstrr.   r   )pager   objects_with_ctmmatrix_stackinstr<   r=   names           r   get_objects_with_ctmrK   [   s    	  ~.L$T*!]]DMM(~% %$H%%fh&78
 $x{#D+ ''++, LTFS- +4   HEhZPQEEGGHs   6C222D)(D)N)rF   r   r   r   r0   zlist[tuple[str, Matrix]])r6   
__future__r   loggingr   pikepdf._corer   r   pikepdf.models._content_streamr   pikepdf.objectsr   __file__rB   r@   rD   r>   r?   r   r7   rK   r:   r   r   <module>rR      s   
 * "  & ? $	8	 tn#}@ @H *0):%
% &%%r   