§
    `Ò³g 1  ã                   óÌ   — d Z ddlZddlZddlZddlmZ ddlmZ	 ddl
mZ ddlmZ ej        Z ej        e¦  «        Z G d„ de	j        ¦  «        Z G d„ de	j        ¦  «        ZdS )	)ÚPdfTextPageÚPdfTextSearcheré    N)ÚPdfiumError)ÚPDFIUM_INFOc                   óz   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zdd„Zdd	„Zdd„Z	d„ Z
dd„Zd„ Zdd„Zd„ Zdd„Zˆ xZS )r   zÂ
    Text page helper class.
    
    Attributes:
        raw (FPDF_TEXTPAGE): The underlying PDFium textpage handle.
        page (PdfPage): Reference to the page this textpage belongs to.
    c                 óz   •— || _         || _        t          ¦   «                              t          j        ¦  «         d S ©N)ÚrawÚpageÚsuperÚ__init__Úpdfium_cÚFPDFText_ClosePage)Úselfr
   r   Ú	__class__s      €úY/var/www/html/mpstechhub/venv/lib/python3.11/site-packages/pypdfium2/_helpers/textpage.pyr   zPdfTextPage.__init__   s2   ø€ ØˆŒØˆŒ	Ý‰Œ×Ò�Ô4Ñ5Ô5Ð5Ð5Ð5ó    c                 ó   — | j         S r	   )r   ©r   s    r   ÚparentzPdfTextPage.parent!   s
   € àŒyÐr   r   c                 ó  — ||k    rdS t          j        | |¦  «        }|dk    r|                      |dz   ||dz   |¦  «        S t          j        | |¦  «        }|dk    r|                      ||dz
  ||dz   ¦  «        S ||||fS )Nr   éÿÿÿÿé   )r   Ú"FPDFText_GetTextIndexFromCharIndexÚ_get_active_text_range)r   Úc_startÚc_endÚ	l_passiveÚ	r_passiveÚt_startÚt_ends          r   r   z"PdfTextPage._get_active_text_range&   sš   € à�UŠ?ˆ?Ø�1åÔ=¸dÀGÑLÔLˆØ�bŠ=ˆ=Ø×.Ò.¨w°q©y¸%ÀÈ1ÁÈiÑXÔXÐXåÔ;¸DÀ%ÑHÔHˆØ�BŠ;ˆ;Ø×.Ò.¨w¸¸a¹ÀÈIÐVWÉKÑXÔXÐXà˜˜y¨)Ð3Ð3r   r   ÚignoreFc                 óª  — ||fdk    r,|s*t          j        d¦  «         |                      |¬¦  «        S |dk    r|                      ¦   «         |z
  }|                      |||z   dz
  ¦  «        }|dk    rdS |\  }}}}	||z  }|||	z   z  }|dz   |z
  }
dt
          j        cxk     rd	k     rn n|
d
z  }
|
dz  }
t          j        |
d
z  ¦  «        }t          j	        |t          j
        t          j        ¦  «        ¦  «        }t          j        | |||¦  «        }|
|k    sJ d|
› d|› �¦   «         ‚|j        d|dz
  d
z  …                              d|¬¦  «        S )aÍ  
        Warning:
            .. versionchanged:: 4.28
               For various reasons, calling this method with default params now implicitly translates to :meth:`.get_text_bounded` (pass ``force_this=True`` to circumvent).
        
        Extract text from a given range.
        
        Parameters:
            index (int): Index of the first char to include.
            count (int): Number of chars to cover, relative to the internal char list. Defaults to -1 for all remaining chars after *index*.
            errors (str): Error handling when decoding the data (see :meth:`bytes.decode`).
        Returns:
            str: The text in the range in question, or an empty string if no text was found.
        
        Note:
            * The returned text's length does not have to match *count*, even if it will for most PDFs.
              This is because the underlying API may exclude/insert chars compared to the internal list, although rare in practice.
              This means, if the char at ``i`` is excluded, ``get_text_range(i, 2)[1]`` will raise an index error.
              Pdfium provides raw APIs ``FPDFText_GetTextIndexFromCharIndex()`` / ``FPDFText_GetCharIndexFromTextIndex()`` to translate between the two views and identify excluded/inserted chars.
            * In case of leading/trailing excluded characters, pypdfium2 modifies *index* and *count* accordingly to prevent pdfium from unexpectedly reading beyond ``range(index, index+count)``.
        ©r   r   z]get_text_range() call with default params will be implicitly redirected to get_text_bounded()©Úerrorsr   r   r   Ú i  i  é   zBuffer too small: z vs Nú	utf-16-le)ÚwarningsÚwarnÚget_text_boundedÚcount_charsr   r   ÚbuildÚctypesÚcreate_string_bufferÚcastÚPOINTERÚc_ushortr   ÚFPDFText_GetTextr
   Údecode)r   ÚindexÚcountr&   Ú
force_thisÚactive_ranger    r!   r   r   Úin_countÚbufferÚ
buffer_ptrÚ	out_counts                 r   Úget_text_rangezPdfTextPage.get_text_range6   s�  € ð2 �5ˆ>˜WÒ$Ð$¨ZÐ$ÝŒMÐyÑzÔzÐzØ×(Ò(°Ð(Ñ7Ô7Ð7à�BŠ;ˆ;Ø×$Ò$Ñ&Ô&¨Ñ.ˆEð ×2Ò2°5¸%À¹+Àa¹-ÑHÔHˆØ˜1ÒÐØ�2ð 0<Ñ,ˆ�˜	 9Ø�ÑˆØ�˜YÑ&Ñ&ˆØ˜‘7˜WÑ$ˆð
 •+Ô#Ð*Ð*Ò*Ð* dÒ*Ð*Ð*Ð*Ð*Ø˜‰MˆHØ�A‰ˆåÔ,¨X¸©\Ñ:Ô:ˆÝ”[ ­¬½¼Ñ)HÔ)HÑIÔIˆ
ÝÔ-¨d°E¸5À*ÑMÔMˆ	Ø˜9Ò$Ð$Ð$Ð&T¸8Ð&TÐ&TÈÐ&TÐ&TÑ$Ô$Ð$àŒzÐ*˜I a™K¨™?Ð*Ô+×2Ò2°;ÀvÐ2ÑNÔNÐNr   Nc                 ó¾  — | j                              ¦   «         }|€|d         }|€|d         }|€|d         }|€|d         }| ||||f}t          j        g |¢d‘d‘R Ž }|dk    rdS t	          j        |dz  ¦  «        }	t	          j        |	t	          j        t          j        ¦  «        ¦  «        }
t          j        g |¢|
‘|‘R Ž  |	j	         
                    d|¬¦  «        S )	až  
        Extract text from given boundaries in PDF coordinates.
        If a boundary value is None, it defaults to the corresponding value of :meth:`.PdfPage.get_bbox`.
        
        Parameters:
            errors (str): Error treatment when decoding the data (see :meth:`bytes.decode`).
        Returns:
            str: The text on the page area in question, or an empty string if no text was found.
        Nr   r   r(   é   r'   r)   r%   )r   Úget_bboxr   ÚFPDFText_GetBoundedTextr/   r0   r1   r2   r3   r
   r5   )r   ÚleftÚbottomÚrightÚtopr&   ÚbboxÚargsÚn_charsr;   r<   s              r   r,   zPdfTextPage.get_text_boundedq   sú   € ð Œy×!Ò!Ñ#Ô#ˆØˆ<Ø˜”7ˆDØˆ>Ø˜!”WˆFØˆ=Ø˜”GˆEØˆ;Ø�q”'ˆCà�d˜C ¨Ð/ˆÝÔ2ÐB°DÐB¸$ÐBÀÐBÐBÐBˆØ�aŠ<ˆ<Ø�2åÔ,¨W°q©[Ñ9Ô9ˆÝ”[ ­¬½¼Ñ)HÔ)HÑIÔIˆ
ÝÔ(ÐD¨$ÐD°
ÐD¸GÐDÐDÐDÐDØŒz× Ò  °VÐ Ñ<Ô<Ð<r   c                 óX   — t          j        | ¦  «        }|dk    rt          d¦  «        ‚|S )zV
        Returns:
            int: The number of characters on the text page.
        r   zFailed to get character count.)r   ÚFPDFText_CountCharsr   )r   rI   s     r   r-   zPdfTextPage.count_chars‘   s1   € õ
 Ô.¨tÑ4Ô4ˆØ�bŠ=ˆ=ÝÐ>Ñ?Ô?Ð?Øˆr   c                 ó\   — t          j        | ||¦  «        }|dk    rt          d¦  «        ‚|S )a  
        Parameters:
            index (int): Start character index.
            count (int): Character count to consider (defaults to -1 for all remaining).
        Returns:
            int: The number of text rectangles in the given character range.
        r   zFailed to count rectangles.)r   ÚFPDFText_CountRectsr   )r   r6   r7   Ún_rectss       r   Úcount_rectszPdfTextPage.count_rectsœ   s5   € õ Ô.¨t°U¸EÑBÔBˆØ�bŠ=ˆ=ÝÐ;Ñ<Ô<Ð<Øˆr   c                 óF   — t          j        | ||||¦  «        }|dk     rdS |S )aÎ  
        Get the index of a character by position.
        
        Parameters:
            x (float): Horizontal position (in PDF canvas units).
            y (float): Vertical position.
            x_tol (float): Horizontal tolerance.
            y_tol (float): Vertical tolerance.
        Returns:
            int | None: The index of the character at or nearby the point (x, y).
            May be None if there is no character or an error occurred.
        r   N)r   ÚFPDFText_GetCharIndexAtPos)r   ÚxÚyÚx_tolÚy_tolr6   s         r   Ú	get_indexzPdfTextPage.get_indexª   s/   € õ Ô3°D¸!¸QÀÀuÑMÔMˆØ�1Š9ˆ9Ø�4Øˆr   c                 ó¬  — |rIt          j        ¦   «         }t          j        | ||¦  «        }|j        |j        |j        |j        f\  }}}}nst          ¦   «         t          ¦   «         t          ¦   «         t          ¦   «         f\  }}}}t          j        | |||||¦  «        }|j	        |j	        |j	        |j	        f\  }}}}|st          d¦  «        ‚||||fS )aÑ  
        Get the bounding box of a single character.
        
        Parameters:
            index (int):
                Index of the character to work with, in the page's character array.
            loose (bool):
                Get a more comprehensive box covering the entire font bounds, as opposed to the default tight box specific to the one character.
        Returns:
            Float values for left, bottom, right and top in PDF canvas units.
        zFailed to get charbox.)r   ÚFS_RECTFÚFPDFText_GetLooseCharBoxrC   rD   rE   rF   Úc_doubleÚFPDFText_GetCharBoxÚvaluer   )	r   r6   ÚlooseÚrectÚokÚlÚbÚrÚts	            r   Úget_charboxzPdfTextPage.get_charbox½   sÑ   € ð ð 	<ÝÔ$Ñ&Ô&ˆDÝÔ2°4¸ÀÑEÔEˆBØœ D¤K°´¸T¼XÐE‰JˆAˆq�!�Q�Qå!™œ¥X¡Z¤Zµ±´½X¹Z¼ZÐG‰JˆAˆq�!�QÝÔ-¨d°E¸1¸aÀÀAÑFÔFˆBØœ !¤'¨1¬7°A´GÐ;‰JˆAˆq�!�Qàð 	8ÝÐ6Ñ7Ô7Ð7à�!�Q˜ˆzÐr   c                 ó   — t          ¦   «         t          ¦   «         t          ¦   «         t          ¦   «         f\  }}}}t          j        | |||||¦  «        }|st          d¦  «        ‚|j        |j        |j        |j        fS )al  
        Get the bounding box of a text rectangle at the given index.
        Note that :meth:`.count_rects` must be called once with default parameters
        before subsequent :meth:`.get_rect` calls for this function to work (due to PDFium's API).
        
        Returns:
            Float values for left, bottom, right and top in PDF canvas units.
        zzFailed to get rectangle. (Make sure count_rects() was called with default params once before subsequent get_rect() calls.))rZ   r   ÚFPDFText_GetRectr   r\   )r   r6   r`   ra   rb   rc   r_   s          r   Úget_rectzPdfTextPage.get_rectÙ   s   € õ ‘Z”Z¥¡¤­X©Z¬Z½¹¼ÐC‰
ˆˆ1ˆa�ÝÔ& t¨U°A°q¸!¸QÑ?Ô?ˆØð 	\Ýð  [ñ  \ô  \ð  \Ø”˜œ !¤'¨1¬7Ð3Ð3r   c                 ó¾  — t          |¦  «        dk    rt          d¦  «        ‚d}|r|t          j        z  }|r|t          j        z  }|r|t          j        z  }|dz                        d¦  «        }t          j        |t          j	        t          j
        ¦  «        ¦  «        }t          j        | |||¦  «        }	t          |	| ¦  «        }
|                      |
¦  «         |
S )au  
        Locate text on the page.
        
        Parameters:
            text (str):
                The string to search for.
            index (int):
                Character index at which to start searching.
            match_case (bool):
                If True, the search will be case-specific (upper and lower letters treated as different characters).
            match_whole_word (bool):
                If True, substring occurrences will be ignored (e. g. `cat` would not match `category`).
            consecutive (bool):
                If False (the default), :meth:`.search` will skip past the current match to look for the next match.
                If True, parts of the previous match may be caught again (e. g. searching for `aa` in `aaaa` would match 3 rather than 2 times).
        Returns:
            PdfTextSearcher: A helper object to search text.
        r   z#Text length must be greater than 0.ú r)   )ÚlenÚ
ValueErrorr   ÚFPDF_MATCHCASEÚFPDF_MATCHWHOLEWORDÚFPDF_CONSECUTIVEÚencoder/   r1   r2   r3   ÚFPDFText_FindStartr   Ú_add_kid)r   Útextr6   Ú
match_caseÚmatch_whole_wordÚconsecutiveÚflagsÚenc_textÚenc_text_ptrÚraw_searcherÚsearchers              r   ÚsearchzPdfTextPage.searché   sÖ   € õ( ˆt‰9Œ9˜Š>ˆ>ÝÐBÑCÔCÐCàˆØð 	-Ø•XÔ,Ñ,ˆEØð 	2Ø•XÔ1Ñ1ˆEØð 	/Ø•XÔ.Ñ.ˆEà˜6‘M×)Ò)¨+Ñ6Ô6ˆÝ”{ 8­V¬^½F¼OÑ-LÔ-LÑMÔMˆÝÔ2°4¸ÀuÈeÑTÔTˆÝ" <°Ñ6Ô6ˆØ�Š�hÑÔÐØˆr   )r   r   )r   r   r"   F)NNNNr"   r$   )F)r   FFF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r>   r,   r-   rO   rV   rd   rg   r{   Ú__classcell__©r   s   @r   r   r      s  ø€ € € € € ðð ð6ð 6ð 6ð 6ð 6ð
 ðð ñ „Xðð4ð 4ð 4ð 4ð 8Oð 8Oð 8Oð 8Oðv=ð =ð =ð =ð@ð ð ðð ð ð ðð ð ð&ð ð ð ð84ð 4ð 4ð $ð $ð $ð $ð $ð $ð $ð $r   r   c                   óJ   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zd„ Zd„ Zd„ Z	ˆ xZ
S )r   zÓ
    Text searcher helper class.
    
    Attributes:
        raw (FPDF_SCHHANDLE): The underlying PDFium searcher handle.
        textpage (PdfTextPage): Reference to the textpage this searcher belongs to.
    c                 óz   •— || _         || _        t          ¦   «                              t          j        ¦  «         d S r	   )r
   Útextpager   r   r   ÚFPDFText_FindClose)r   r
   r…   r   s      €r   r   zPdfTextSearcher.__init__  s2   ø€ ØˆŒØ ˆŒÝ‰Œ×Ò�Ô4Ñ5Ô5Ð5Ð5Ð5r   c                 ó   — | j         S r	   )r…   r   s    r   r   zPdfTextSearcher.parent  s
   € àŒ}Ðr   c                 óx   —  || ¦  «        }|sd S t          j        | ¦  «        }t          j        | ¦  «        }||fS r	   )r   ÚFPDFText_GetSchResultIndexÚFPDFText_GetSchCount)r   Ú	find_funcr_   r6   r7   s        r   Ú_get_occurrencezPdfTextSearcher._get_occurrence#  sF   € ØˆY�t‰_Œ_ˆØð 	Ø�4ÝÔ3°DÑ9Ô9ˆÝÔ-¨dÑ3Ô3ˆØ�eˆ|Ðr   c                 ó@   — |                       t          j        ¦  «        S )z¡
        Returns:
            (int, int): Start character index and count of the next occurrence,
            or None if the last occurrence was passed.
        )rŒ   r   ÚFPDFText_FindNextr   s    r   Úget_nextzPdfTextSearcher.get_next+  ó   € ð ×#Ò#¥HÔ$>Ñ?Ô?Ð?r   c                 ó@   — |                       t          j        ¦  «        S )zÖ
        Returns:
            (int, int): Start character index and count of the previous occurrence (i. e. the one before the last valid occurrence),
            or None if the last occurrence was passed.
        )rŒ   r   ÚFPDFText_FindPrevr   s    r   Úget_prevzPdfTextSearcher.get_prev3  r�   r   )r|   r}   r~   r   r   r€   r   rŒ   r�   r“   r�   r‚   s   @r   r   r     s”   ø€ € € € € ðð ð6ð 6ð 6ð 6ð 6ð
 ðð ñ „Xððð ð ð@ð @ð @ð@ð @ð @ð @ð @ð @ð @r   r   )Ú__all__r/   Úloggingr*   Úpypdfium2.rawr
   r   Úpypdfium2.internalÚinternalÚpdfium_iÚpypdfium2._helpers.miscr   Úpypdfium2.versionr   rZ   Ú	getLoggerr|   ÚloggerÚAutoCloseabler   r   © r   r   ú<module>r       sÿ   ðð -€à €€€Ø €€€Ø €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø %Ð %Ð %Ð %Ð %Ð %Ø /Ð /Ð /Ð /Ð /Ð /Ø )Ð )Ð )Ð )Ð )Ð )àŒ?€à	ˆÔ	˜8Ñ	$Ô	$€ðzð zð zð zð z�8Ô)ñ zô zð zðz)@ð )@ð )@ð )@ð )@�xÔ-ñ )@ô )@ð )@ð )@ð )@r   