U
    c1                     @   s~   d dl mZ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 )    )ListOptionalTupleUnion   )BooleanObjectFloatObject
NameObjectNumberObjectTextStringObject)ArrayObjectDictionaryObject)DEFAULT_FITFit)RectangleObject)
hex_to_rgbc                   @   s  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$ee	e
eeeef f ee edddZeee
eef  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 )	a^  
        Add text annotation.

        Args:
            rect: array of four integers ``[xLL, yLL, xUR, yUR]``
                specifying the clickable rectangular area
            text: The text that is added to the document
            open:
            flags:

        Returns:
            A dictionary object representing the annotation.
        /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-u_n6aelj/pypdf/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 ]}|
 | d}
qT|
 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.

        Args:
            text: Text to be added
            rect: array of four integers ``[xLL, yLL, xUR, yUR]``
                specifying the clickable rectangular area
            font: Name of the Font, e.g. 'Helvetica'
            bold: Print the text in bold
            italic: Print the text in italic
            font_size: How big the text will be, e.g. '14pt'
            font_color: Hex-string for the color, e.g. cdcdcd
            border_color: Hex-string for the border color, e.g. cdcdcd
            background_color: Hex-string for the background of the annotation,
                e.g. cdcdcd

        Returns:
            A dictionary object representing 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>w   s     z/AnnotationBuilder.free_text.<locals>.<listcomp>)r   r   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.

        Args:
            p1: First point
            p2: Second point
            rect: array of four integers ``[xLL, yLL, xUR, yUR]``
                specifying the clickable rectangular area
            text: Text to be displayed as the line annotation
            title_bar: Text to be displayed in the title bar of the
                annotation; by convention this is the name of the author

        Returns:
            A dictionary object representing the annotation.
        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!   line}   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 )	a  
        Draw a rectangle on the PDF.

        This method uses the /Square annotation type of the PDF format.

        Args:
            rect: array of four integers ``[xLL, yLL, xUR, yUR]``
                specifying the clickable rectangular area
            interiour_color: None or hex-string for the color, e.g. cdcdcd
                If None is used, the interiour is transparent.

        Returns:
            A dictionary object representing the annotation.
        r   r   r   z/Squarer   c                 S   s   g | ]}t |qS r    r/   r0   r    r    r!   r3      s     z/AnnotationBuilder.rectangle.<locals>.<listcomp>r:   r   r	   r   r   r   )r   r<   Z
square_objr    r    r!   	rectangle   s       zAnnotationBuilder.rectanglec                 C   sR   t tdtdtdtdtdt| i}|rNtdd t|D |td< |S )	a  
        Draw a rectangle on the PDF.

        This method uses the /Circle annotation type of the PDF format.

        Args:
            rect: array of four integers ``[xLL, yLL, xUR, yUR]`` specifying
                the bounding box of the ellipse
            interiour_color: None or hex-string for the color, e.g. cdcdcd
                If None is used, the interiour is transparent.

        Returns:
            A dictionary object representing the annotation.
        r   r   r   z/Circler   c                 S   s   g | ]}t |qS r    r/   r0   r    r    r!   r3      s     z-AnnotationBuilder.ellipse.<locals>.<listcomp>r:   r=   )r   r<   Zellipse_objr    r    r!   ellipse   s       zAnnotationBuilder.ellipse)verticesr   c           
      C   s  t | dkrtd| d d | d d  }}| d d | d d  }}| D ]0\}}t||}t||}t||}t||}qLt||||f}g }| D ]$\}}|t| |t| qttdtdtdtdtdt|td	td
tdt|i}	|	S )Nr   z6A polygon needs at least 1 vertex with two coordinatesr   r   r   r   z/Polygonz	/Verticesz/ITZPolygonCloudr   )	len
ValueErrorminr   appendr
   r   r	   r   )
r@   Zx_minZy_minZx_maxZy_maxxyr   Z
coord_listobjr    r    r!   polygon   s8    


     	zAnnotationBuilder.polygon)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.

        Args:
            rect: array of four integers ``[xLL, yLL, xUR, yUR]``
                specifying the clickable rectangular area
            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
            url: Link to a website (if you want to make an external link)
            target_page_index: index of the page to which the link should go
                (if you want to make an internal link)
            fit: Page fit or 'zoom' option.

        Returns:
            A dictionary object representing the annotation.
        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	   r0   r    r    r!   r3   E  s     z*AnnotationBuilder.link.<locals>.<listcomp>      c                 S   s   g | ]}t |qS r    rN   r0   r    r    r!   r3   G  s     r   r   r   r   z/Linkr   z/Borderz/Sz/URIz/Actionz/A)rK   rL   fit_argsz/Dest)typesrM   rB   rA   r   rD   r
   r   r	   r   r   Zfit_typerQ   )r   rI   rJ   rK   rL   rM   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)N)__name__
__module____qualname____doc__rR   r   r   staticmethodr   r   r   floatstrboolintr   r   r6   r;   r   r>   r?   r   rH   r   r   r   rS   r    r    r    r!   r      s   
          >  

6 ! !r   N)typingr   r   r   r   _baser   r   r	   r
   r   Z_data_structuresr   r   Z_fitr   r   Z
_rectangler   _utilsr   r   r    r    r    r!   <module>   s   