o
    gi                     @   s   d 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	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ G dd dZdS )z
This class implements a (single word) find/replace functionality for PDF documents.
This class uses RegularExpressionTextExtraction to find the text,
RedactAnnotation to remove the text, and Paragraph to re-insert replacement text.
    N)Decimal)Color)Font)StandardType1Font)	Rectangle)RedactAnnotation)	Alignment)	Paragraph)Document)Pagec                   @   sl   e Zd ZdZededdejdfdedede	de
je
je  ded	e
je d
ede
je de	fddZdS )SimpleFindReplacez
    This class implements a (single word) find/replace functionality for PDF documents.
    This class uses RegularExpressionTextExtraction to find the text,
    RedactAnnotation to remove the text, and Paragraph to re-insert replacement text.
    N	Helveticapatternrepldoc
page_range	repl_fontrepl_font_colorrepl_font_horizontal_alignmentrepl_font_sizereturnc                    s  ddl m}m}	 t|  ptd|| |}
|du r)dd tdD }n	fdd|D }|D ]}|	|}|
| D ]}|
 D ]}|t|td qEq?|  |
| D ]f}|
 d  }|
 d   t fdd|
 D }t fd	d|
 D td
 }|du r| }|du r| }|dkrt|||||d|t|td  td |td |td  q]q4|S )a3  
        This function finds and replaces a regular expression in a PDF by a given piece of text
        :param pattern:                         the regular expression to be replaced
        :param repl:                            the replacement string
        :param doc:                             the PDF Document
        :param page_range:                      the pages on which to apply find/replace (if empty, this will default to all pages)
        :param repl_font:                       the Font to be used to insert the replacement text
        :param repl_font_color:                 the font_color to be used to isnert the replacement text (if empty, this will default to the original font_color)
        :param repl_font_size:                  the font_size to be used to insert the replacement text (if empty, this will default to the original font_size)
        :param repl_font_horizontal_alignment   the horizontal_alignment of the replacement text (defaults to Alignment.CENTERED)
        :return:                                the PDF Document
        r   )RegularExpressionTextExtractionPDFMatchNc                 S   s   g | ]}|qS  r   .0xr   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/borb/toolkit/text/simple_find_replace.py
<listcomp>J   s    z)SimpleFindReplace.sub.<locals>.<listcomp>c                    s    g | ]}|d kr| k r|qS )r   r   r   )number_of_pagesr   r   r   L           c                        g | ]}|   kr| qS r   )get_y
get_heightr   bb_yr   r   r   c   r    c                    r"   r   )r#   	get_widthr   r%   r   r   r   d   r        )font	font_size
font_colorhorizontal_alignmentg      ?)borb.toolkitr   r   intget_document_infoget_number_of_pagesr   get_matches_for_pdfrangeget_pageget_bounding_boxesadd_annotationr   growapply_redact_annotationsget_xr#   maxsumget_font_sizeget_font_colorr	   paintr   )r   r   r   r   r   r   r   r   r   r   matches_per_pagepage_nrpage	pdf_matchbounding_boxbb_xbb_hbb_wr   )r&   r   r   sub*   sZ   

"



$zSimpleFindReplace.sub)__name__
__module____qualname____doc__staticmethodr   r   CENTEREDstrr
   typingOptionalListr/   r   r   r   rG   r   r   r   r   r      s6    	r   )rK   rO   decimalr   borb.pdf.canvas.color.colorr   borb.pdf.canvas.font.fontr   ,borb.pdf.canvas.font.simple_font.font_type_1r   "borb.pdf.canvas.geometry.rectangler   3borb.pdf.canvas.layout.annotation.redact_annotationr   %borb.pdf.canvas.layout.layout_elementr   %borb.pdf.canvas.layout.text.paragraphr	   borb.pdf.document.documentr
   borb.pdf.page.pager   r   r   r   r   r   <module>   s   