o
    @@im,                     @   s   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	 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eZdS )z;
    This class represents a single page in a PDF document
    N)Decimal)
Dictionary)List)Name)Stream)String)Canvas	Rectangle)
AnnotationPageInfoc                       s  e Zd ZdZededfdedef fddZd(d	d
Zdedd fddZde	dd fddZ
d(ddZdefddZd)ddZde	dejeje	ef  fddZdefddZdefddZde	defdd Zd(d!d"Zd(d#d$Zde	d%e	dd fd&d'Z  ZS )*Pagez?
    This class represents a single page in a PDF document
    iS  iJ  widthheightc                    s~   t t|   td| td< t d| td< | d td | d td | d t| | d t| d S )Nr   TypeTMediaBoxr   )superr   __init__r   r   set_is_inlineappendbDecimal)selfr   r   	__class__ P/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/pdf/page/page.pyr   !   s   zPage.__init__returnc                 C   s   d| vr3t  }d|td< t|d d|td< td|td< tt|d |td< || td< d	| vr>t | td	< | S )
NContents    DecodedBytes	   BytesFlateDecodeFilterLength	Resources)r   r   zlibcompressr   lenr   )r   content_streamr   r   r   _initialize_page_content_stream2   s   z$Page._initialize_page_content_stream
annotationc                 C   s   d| v r
t | d nd}td||td< | |td< d| vr/t | td< | d |  t| d ts8J | d | d|v rN|d dkrN||  | S )z
        This function appends an Annotation to this Page, returning self.
        :param annotation:  the Annotation object to append to this Page
        :return:            self
        Annotsr   zannotation-{0:03d}NMPSubtypeFreeText)	r)   r   formatr   r   
set_parent
isinstancer   _embed_font_in_page)r   r,   
len_annotsr   r   r   add_annotationK   s   	
zPage.add_annotationsc                 C   s   |    | d }t|td dkr5t|d dd dd}|dvr5|d dvr5|td  d	d7  < |td  |d7  < t|d d
|td< tt|d |td< | S )z
        This function appends a string of postfix operators to the content stream of this Page
        :param s:   the str of postfix operators to be added
        :return:    self
        r   r    r   Nlatin1)encoding) 	
r<   r!   r"   r%   )r+   r)   r   strencoder'   r(   r   )r   r8   r*   decoded_bytes_last_charr   r   r   append_to_content_streamn   s   zPage.append_to_content_streamc                 C   s   ddl m} dd | dg D }|| t |t| d d g  }|| d td< t	
| d d d| d td	< tt| d d	 | d td
< | S )zh
        This function applies the redaction annotations on this Page.
        :return:    self
        r   )RedactedCanvasStreamProcessorc              	   S   sn   g | ]3}d |v r|d  dkrd|v rt |d d |d d |d d |d d  |d d |d d  qS )r0   RedactRectr            r	   .0xr   r   r   
<listcomp>   s    

z1Page.apply_redact_annotations.<locals>.<listcomp>r-   r   r    r!   r"   r%   )0borb.pdf.canvas.redacted_canvas_stream_processorrC   getr   readioBytesIOget_redacted_contentr   r'   r(   r   r)   )r   rC   rectangles_to_redactredacted_canvas_contentr   r   r   apply_redact_annotations   s   
 zPage.apply_redact_annotationsc                 C   s   d| vrt  | td< | d S )z
        This function returns the annotation(s) on this Page
        :return:    the annotation(s) (as typing.List) on this Page
        r-   )r   r   r   r   r   r   get_annotations   s   zPage.get_annotationsDocumentc                 C   s   |   }|jjdkr|S dS )zo
        This function returns the Document from which this Page came
        :return:    the Document
        rX   N)get_rootr   __name__)r   dr   r   r   get_document   s   zPage.get_document
field_namec                    sF    fdd|  dg D }t|dksJ d|d v sJ |d d S )z
        This function returns the value of the form field with the given field_name
        :param field_name:  the field_name of the field to be queried
        :return:            the value of the form field being queried
        c                    h   g | ]0}|d ur2d|v r2|d dkr2d|v r2|d dkr2d|v r2|d dv r2d|v r2|d  kr|qS Nr   Annotr0   WidgetFTBtnChTxTr   rI   r]   r   r   rL          
z-Page.get_form_field_value.<locals>.<listcomp>r-   rF   Vr   )rN   r)   )r   r]   field_dictionariesr   rh   r   get_form_field_value   s   

zPage.get_form_field_valuec                 C   s   t | S )zq
        This function returns the PageInfo object for this Page
        :return:    the PageInfo Object
        r   rV   r   r   r   get_page_info   s   zPage.get_page_infoc                 C   s   t dd | dg D dkS )z
        This function returns True if this Page contains fields from an AcroForm
        :return:    True if this Page contains fields from an AcroForm, False otherwise
        c                 S   sT   g | ]&}|d ur(d|v r(|d dkr(d|v r(|d dkr(d|v r(|d dv r|qS )Nr   r`   r0   ra   rb   rc   r   rI   r   r   r   rL      s    z&Page.has_acroforms.<locals>.<listcomp>r-   r   r)   rN   rV   r   r   r   has_acroforms   s   
zPage.has_acroformsc                    s2   t  dksJ t  fdd| dg D dkS )z
        This function returns True if this Page contains a form field with the given name
        :param field_name:  the field_name to be queried
        :return:            True if this Page contains a form field with the given field_name
        r   c                    r^   r_   r   rI   rh   r   r   rL      ri   z'Page.has_form_field.<locals>.<listcomp>r-   rn   )r   r]   r   rh   r   has_form_field   s   

zPage.has_form_fieldc                 C   V   d}d| v rt | d }|d d }|dkr!d| v r!| d | S t|| td< | S )zt
        This function rotates the Page counterclockwise by 90 degrees.
        This function returns self.
        r   Rotatei  h  intpopr   r   r   angler   r   r   rotate_left  s   
zPage.rotate_leftc                 C   rq   )zm
        This function rotates the Page clockwise by 90 degrees.
        This function returns self.
        r   rr   Z   rs   rt   rw   r   r   r   rotate_right$  s   
zPage.rotate_rightvaluec                    sR    fdd|  dg D }t|dksJ d|d v sJ t||d td< | S )a  
        This function sets the value of the form field with the given field_name
        This function returns self
        :param field_name:  the field_name of the field being queried
        :param value:       the new value of the field
        :return:            self
        c                    r^   r_   r   rI   rh   r   r   rL   B  ri   z-Page.set_form_field_value.<locals>.<listcomp>r-   rF   rj   r   )rN   r)   r   r   )r   r]   r|   rk   r   rh   r   set_form_field_value:  s   

zPage.set_form_field_value)r   r   )r   rX   )rZ   
__module____qualname____doc__r   r   r+   r   r7   r?   rB   rU   r   rW   r\   typingOptionalUnionboolrl   r   rm   ro   rp   ry   r{   r}   __classcell__r   r   r   r   r      s(    $
#
&
	


r   )r   rP   r   r'   decimalr   borb.io.read.typesr   r   r   r   r   r   borb.pdf.canvas.canvasr   "borb.pdf.canvas.geometry.rectangler
   ,borb.pdf.canvas.layout.annotation.annotationr   borb.pdf.page.page_infor   r   r   r   r   r   <module>   s    