a
    ˜ëjM+  ã                   @   s€  U d Z ddlZddlmZ ddlmZmZmZmZm	Z	 ddl
mZmZ ddlmZmZmZ ddlmZ d	aeed
< d	aeed< d	aeed< dZeed< dZdZG dd„ deƒZd&e	eedf e	eedf e	eee df eeeef dœdd„Z ee! e	ee! ee	eed f e	e!e"f f f ee! dœdd„Z#ee! edœdd„Z$eeee! ee! f eee! ee! f eee! ee! f ee eedf ee!eeeeeeegdf  e!e!e!eeee!e!f  eeeee! ee! eeee!e!f  f dœdd„Z%ee	eef  ee! ee! eeedf eee"e!f d œd!d"„Z&eee! ee! ee eee!e"eeeeeeegdf  eee"ef d#œ
d$d%„Z'dS )'ze
Code related to text extraction.

Some parts are still in _page.py. In doubt, they will stay there.
é    N)ÚMapping)ÚAnyÚCallableÚLiteralÚOptionalÚUnioné   )Úis_char_neutralÚis_char_rtl)ÚDictionaryObjectÚTextStringObjectÚencode_pdfdocencoding)ÚFontÚ ÚCUSTOM_RTL_MINÚCUSTOM_RTL_MAXÚCUSTOM_RTL_SPECIAL_CHARSé   Ú LAYOUT_NEW_BT_GROUP_SPACE_WIDTHSiÿÿ c                   @   s   e Zd ZdS )ÚOrientationNotFoundErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú^/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/_text_extraction/__init__.pyr      s   r   )Ú_minÚ_maxÚspecialsÚreturnc                 C   s´   t | tƒr0t|   krtkr*n nt| ƒndant | tƒr>| at |tƒrnt|  kr\tkrhn nt|ƒndant |tƒr||at |tƒrŒ|ant |t	ƒrªd 
dd„ |D ƒ¡atttfS )aê  
    Change the Right-To-Left and special characters custom parameters.

    Args:
        _min: The new minimum value for the range of custom characters that
            will be written right to left.
            If set to ``None``, the value will not be changed.
            If set to a valid integer, it will be converted to its corresponding character.
            The default value is "", which sets no additional range to be converted.
        _max: The new maximum value for the range of custom characters that will
            be written right to left.
            If set to ``None``, the value will not be changed.
            If set to a valid integer, it will be converted to its corresponding character.
            The default value is "", which sets no additional range to be converted.
        specials: The new list of special characters to be inserted in the
            current insertion order.
            If set to ``None``, the current value will not be changed.
            If set to a string, it will be converted to a list of ASCII codes.
            The default value is an empty list.

    Returns:
        A tuple containing the new values for ``CUSTOM_RTL_MIN``,
        ``CUSTOM_RTL_MAX``, and ``CUSTOM_RTL_SPECIAL_CHARS``.

    r   c                 s   s.   | ]&}t |  krtkrn qt|ƒV  qd S ©N)ÚUNICODE_LOWER_LIMITÚUNICODE_UPPER_LIMITÚchr)Ú.0Úcharr   r   r   Ú	<genexpr>E   s   z!set_custom_rtl.<locals>.<genexpr>)Ú
isinstanceÚintr    r!   r"   r   Ústrr   r   ÚlistÚjoin)r   r   r   r   r   r   Úset_custom_rtl   s    
&

&



ÿr+   )Zis_textZ	is_render)ÚmÚnr   c              	   C   sÈ   | d |d  | d |d   | d |d  | d |d   | d |d  | d |d   | d |d  | d |d   | d |d  | d |d   |d  | d |d  | d |d   |d  gS )Nr   é   r   é   é   r   r   )r,   r-   r   r   r   ÚmultK   s    &&úr1   )r,   r   c                 C   s4   | d dkrdS | d dk r dS | d dkr0dS dS )	Nr/   g�íµ ÷Æ°>r   g�íµ ÷Æ°¾é´   r.   éZ   é  r   )r,   r   r   r   Úorient\   s    r5   .)ÚtextÚ	cmtm_prevÚcmtm_matrixÚ	memo_cmtmÚfont_resourceÚorientationsÚoutputÚ	font_sizeÚvisitor_textÚ
str_widthsÚ
spacewidthÚ
str_heightÚ	line_spanr   c           #      C   s~  |d }|d }|d }|d }|d }|d }t ||ƒ}t ||ƒ}t|ƒ}|d |d  }|d |d  }t |d d |d d  ¡}t |d d |d d  ¡}t |d d |d d  ¡}|}||vrÖt‚|dv rè|}|}n|dv rø|}|}|dv �rdnd}|| }|d u �s*|d |k�r4t|ƒ}nL|d |d  } }!| |  k�r^|!k�rfn nd	ntt||  ƒt||! ƒƒ}||  d
d … }"|dt|| || ƒ k�rð|"dv�rä|| d 7 }|d u�rà|| d ||||ƒ d} |||f}np||
|	 | k�r|"dv�r| d7 } |"dv �s6|d u �s6|d |k�rB|||f}n|t|d |ƒt|d |ƒf}| ¡ }| ¡ }| ||||fS )Nr   r.   r0   r   r   r/   )r   r2   )r3   r4   ç        éÿÿÿÿgš™™™™™é?)r   Ú
rE   r   )r   ú rF   )	r1   r5   ÚmathÚsqrtr   ÚabsÚminÚmaxÚcopy)#r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   Zcm_prevZtm_prevÚ	cm_matrixÚ	tm_matrixZmemo_cmZmemo_tmZm_prevr,   ÚorientationZdelta_xZdelta_yZscale_prev_xZscale_prev_yZscale_yZmoved_heightZmoved_widthZ
axis_indexZaxisZdistanceÚlowerÚupperÚlastr   r   r   Úcrlf_space_checkf   sl    


:

ûÿþ"rS   )ÚoperandsrM   rN   Úfontr;   r   c                    sJ  d}d}d}i }t ||ƒ}	t|	ƒ}
d}|
|v �r8t| ƒdk�r8t| d tƒrZ| d }d}n”d}t| d tƒrxt| d ƒn| d }tˆ jtƒrÂz| ˆ jd¡}W qî ty¾   | ˆ jd¡}Y qî0 n,d 	‡ fdd	„|D ƒ¡}d 	d
d	„ |D ƒ¡}|pô|D ]@}|ˆ j
k�r|ˆ j7 }qö||v�r*ˆ  |¡||< ||| 7 }qö| ¡  |||fS )Nr   FrC   r   TÚsurrogatepassÚreplacec                 3   s    | ]}ˆ j  |t|ƒ¡V  qd S r   )ÚencodingÚgetr"   )r#   Úx©rU   r   r   r%   Û   ó    z$get_text_operands.<locals>.<genexpr>c                 s   s   | ]}t |ƒV  qd S r   )r"   )r#   Úbyter   r   r   r%   Ü   r\   )r1   r5   Úlenr&   r(   r   rX   ÚdecodeÚUnicodeDecodeErrorr*   Z
space_charZspace_widthZget_text_widthÚclear)rT   rM   rN   rU   r;   r6   Zis_str_operandsZwidthsZwidth_cacher,   rO   Zraw_charactersÚttr$   r   r[   r   Úget_text_operands»   s>    
ÿý
rc   )
r6   rM   rN   r:   rU   Útext_operandsr=   Úrtl_dirr>   r   c	                    sB  g }	i ‰ i ‰ddœ‡ ‡fdd„}
|D �]
}|j  ||¡}t|ƒdk�r|ˆ vr\t|tƒˆ |< ˆ | rz|rp||  n| | } nž|ˆvr’t|ttƒˆ|< ˆ| rØ|sÎd}|durÀ|| ||||ƒ |
ƒ  |	 | ¡ d} ||  } n@|�rd}|du�r|| ||||ƒ |
ƒ  |	 | ¡ d} | | } q$|�r(||  n| | } q$| |d 	|	¡fS )	a  
    Add the characters of ``text_operands`` to ``text`` in display order.

    Returns:
        A tuple containing the current text run, its direction, and any text runs completed
        because of a direction change. The completed runs precede the current run in display order.

    N)r   c                      s   ˆ   ¡  ˆ  ¡  d S r   )ra   r   ©Zneutral_cacheZ	rtl_cacher   r   Úclear_character_caches  s    z/get_display_str.<locals>.clear_character_cachesr.   Tr   F)
Zcharacter_maprY   r^   r	   r   r
   r   r   Úappendr*   )r6   rM   rN   r:   rU   rd   r=   re   r>   Zcompleted_textrg   Zraw_characterrZ   r   rf   r   Úget_display_stré   s@    





ri   )r   r   N)(Ú__doc__rG   Úcollections.abcr   Útypingr   r   r   r   r   Ú_utilsr	   r
   Zgenericr   r   r   Zgeneric._fontr   r   r(   Ú__annotations__r   r   r   r'   r    r!   Ú	Exceptionr   r)   Útupler+   ÚfloatÚboolr1   r5   rS   rc   ri   r   r   r   r   Ú<module>   s€      ýü1ÿÿú
&òV
ú/ö