U
    
3gQ'                     @   sz   d dl mZmZ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 ddlmZ ddlmZ G dd	 d	Zd
S )    )OptionalTupleUnion   )BooleanObjectFloatObject
NameObjectNumberObjectTextStringObject)ArrayObjectDictionaryObject)DEFAULT_FITFit)RectangleObject)
hex_to_rgbc                   @   sB  e Zd ZdZddlmZmZ edee	e
eeeef f eeeedddZedeee	e
eeeef f eeeeeeeed
ddZede
eef e
eef ee	e
eeeef f eeedddZedee	e
eeeef f ee edddZedddefee	e
eeeef f ee ee ee eedddZdS )AnnotationBuildera>  
    The AnnotationBuilder creates dictionaries representing PDF annotations.

    Those dictionaries can be modified before they are added to a PdfWriter
    instance via `writer.add_annotation`.

    See `adding PDF annotations <../user/adding-pdf-annotations.html>`_ for
    it's usage combined with PdfWriter.
       )FitTypeZoomArgTypeFr   )recttextopenflagsreturnc                 C   sT   t tdtdtdtdtdt| tdt|tdt|tdt|i}|S )	z
        Add text annotation.

        :param Tuple[int, int, int, int] rect:
            or array of four integers specifying the clickable rectangular area
            ``[xLL, yLL, xUR, yUR]``
        :param bool open:
        :param int flags:
        /Type/Annot/Subtypez/Text/Rect	/Contentsz/Openz/Flags)r   r   r   r
   r   r	   )r   r   r   r   Ztext_obj r   ?/tmp/pip-unpacked-wheel-ifm06wwb/PyPDF2/generic/_annotations.pyr      s           
zAnnotationBuilder.text	Helvetica14pt000000ffffff)
r   r   fontbolditalic	font_size
font_colorborder_colorbackground_colorr   c	                 C   s   d}	|dkr|	d }	|dkr$|	d }	|	| d | }	|	d | }	d}
t |D ]}|
t| d }
qL|
d }
t }|td	td
tdtdtdt|tdt| tdt|	tdt|
tdtdd t |D i |S )a  
        Add text in a rectangle to a page.

        :param str text: Text to be added
        :param RectangleObject rect: or array of four integers
            specifying the clickable rectangular area ``[xLL, yLL, xUR, yUR]``
        :param str font: Name of the Font, e.g. 'Helvetica'
        :param bool bold: Print the text in bold
        :param bool italic: Print the text in italic
        :param str font_size: How big the text will be, e.g. '14pt'
        :param str font_color: Hex-string for the color
        :param str border_color: Hex-string for the border color
        :param str background_color: Hex-string for the background of the annotation
        zfont: Tzbold zitalic  z;text-align:left;color:# Zrgr   r   r   z	/FreeTextr   r   z/DSz/DAz/Cc                 S   s   g | ]}t |qS r   r   .0nr   r   r    
<listcomp>n   s     z/AnnotationBuilder.free_text.<locals>.<listcomp>)r   strr   updater   r   r
   r   )r   r   r%   r&   r'   r(   r)   r*   r+   Zfont_strZbg_color_strst	free_textr   r   r    r6   :   s@           zAnnotationBuilder.free_textr-   )p1p2r   r   	title_barr   c                 C   s   t tdtdtdtdtdt|tdt|tdtt| d t| d	 t|d t|d	 gtd
ttdtdgtdttdtdtdgtdt|i}|S )a  
        Draw a line on the PDF.

        :param Tuple[float, float] p1: First point
        :param Tuple[float, float] p2: Second point
        :param RectangleObject rect: or array of four
                integers specifying the clickable rectangular area
                ``[xLL, yLL, xUR, yUR]``
        :param str text: Text to be displayed as the line annotation
        :param str title_bar: Text to be displayed in the title bar of the
            annotation; by convention this is the name of the author
        r   r   r   z/Liner   z/Tz/Lr   r   z/LEN/ICg      ?r   )r   r   r   r
   r   r   )r7   r8   r   r   r9   Zline_objr   r   r    linet   sF         



   zAnnotationBuilder.lineN)r   interiour_colorr   c                 C   sR   t tdtdtdtdtdt| i}|rNtdd t|D |td< |S )	z
        Draw a rectangle on the PDF.

        :param RectangleObject rect: or array of four
                integers specifying the clickable rectangular area
                ``[xLL, yLL, xUR, yUR]``
        r   r   r   z/Squarer   c                 S   s   g | ]}t |qS r   r.   r/   r   r   r    r2      s     z/AnnotationBuilder.rectangle.<locals>.<listcomp>r:   )r   r   r   r   r   )r   r<   Z
square_objr   r   r    	rectangle   s       zAnnotationBuilder.rectangle)r   borderurltarget_page_indexfitr   c              
   C   sF  ddl m} |dk	}|dk	}|s,|s,td|rH|rHtd| d| |dk	rdd |dd	 D }t|d
krtdd |d	 D }	||	 ntdgd	 }ttdtdtdtdtdt	| tdt|i}
|rttdtdtdtdtdt
|i|
td< |rBtt|t|j|jd}||
td< |
S )a  
        Add a link to the document.

        The link can either be an external link or an internal link.

        An external link requires the URL parameter.
        An internal link requires the target_page_index, fit, and fit args.


        :param RectangleObject rect: or array of four
            integers specifying the clickable rectangular area
            ``[xLL, yLL, xUR, yUR]``
        :param border: if provided, an array describing border-drawing
            properties. See the PDF spec for details. No border will be
            drawn if this argument is omitted.
            - horizontal corner radius,
            - vertical corner radius, and
            - border width
            - Optionally: Dash
        :param str url: Link to a website (if you want to make an external link)
        :param int target_page_index: index of the page to which the link should go
                                (if you want to make an internal link)
        :param Fit fit: Page fit or 'zoom' option.
        r   )BorderArrayTypeNzHEither 'url' or 'target_page_index' have to be provided. Both were None.z=Either 'url' or 'target_page_index' have to be provided. url=z, target_page_index=c                 S   s   g | ]}t |qS r   r   r/   r   r   r    r2      s     z*AnnotationBuilder.link.<locals>.<listcomp>      c                 S   s   g | ]}t |qS r   rC   r/   r   r   r    r2      s     r   r   r   r   z/Linkr   z/Borderz/Sz/URIz/Actionz/A)r@   rA   fit_argsz/Dest)typesrB   
ValueErrorlenr   appendr	   r   r   r   r
   Zfit_typerF   )r   r>   r?   r@   rA   rB   Zis_externalZis_internalZ
border_arrZdash_patternZlink_objZdest_deferredr   r   r    link   s^            zAnnotationBuilder.link)Fr   )r!   FFr"   r#   r#   r$   )r-   r-   )N)__name__
__module____qualname____doc__rG   r   r   staticmethodr   r   r   floatr3   boolintr   r   r6   r;   r   r=   r   r   r   rK   r   r   r   r    r      s~   
         9  

3 r   N)typingr   r   r   _baser   r   r   r	   r
   Z_data_structuresr   r   Z_fitr   r   Z
_rectangler   _utilsr   r   r   r   r   r    <module>   s   