U
    c|&                     @   s   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 erTd dl
mZ d dlmZ dZG dd	 d	e	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    )absolute_import)TYPE_CHECKINGList)Image	ImageDraw)QRModuleDrawer)StyledPilImage)ActiveWithNeighbors   c                   @   s   e Zd ZU dZded< dS )StyledPilQRModuleDrawerz
    A base class for StyledPilImage module drawers.

    NOTE: the color that this draws in should be whatever is equivalent to
    black in the color space, and the specified QRColorMask will handle adding
    colors as necessary to the image
    r   imgN)__name__
__module____qualname____doc____annotations__ r   r   I/tmp/pip-unpacked-wheel-7xhfqtu7/qrcode/image/styles/moduledrawers/pil.pyr      s   
r   c                       s.   e Zd ZdZ fddZedddZ  ZS )SquareModuleDrawerz-
    Draws the modules as simple squares
    c                    s"   t  j|| t| jj| _d S N)super
initializer   Drawr   _imgimgDrawselfargskwargs	__class__r   r   r   #   s    zSquareModuleDrawer.initialize	is_activec                 C   s   |r| j j|| jjd d S )Nfill)r   	rectangler   paint_colorr   boxr"   r   r   r   drawrect'   s    zSquareModuleDrawer.drawrect)r   r   r   r   r   boolr)   __classcell__r   r   r   r   r      s   r   c                       s8   e Zd ZdZd
ddZ fddZeddd	Z  ZS )GappedSquareModuleDrawerz
    Draws the modules as simple squares that are not contiguous.

    The size_ratio determines how wide the squares are relative to the width of
    the space they are printed in
    皙?c                 C   s
   || _ d S r   )
size_ratio)r   r.   r   r   r   __init__4   s    z!GappedSquareModuleDrawer.__init__c                    s:   t  j|| t| jj| _d| j | jj d | _	d S )N      )
r   r   r   r   r   r   r   r.   box_sizedeltar   r   r   r   r   7   s    z#GappedSquareModuleDrawer.initializer!   c                 C   s`   |r\|d d | j  |d d | j  |d d | j  |d d | j  f}| jj|| jjd d S )Nr   r0   r#   )r3   r   r%   r   r&   )r   r(   r"   Zsmaller_boxr   r   r   r)   <   s    z!GappedSquareModuleDrawer.drawrect)r-   )	r   r   r   r   r/   r   r*   r)   r+   r   r   r   r   r,   ,   s   
r,   c                       s2   e Zd ZdZdZ fddZedddZ  ZS )CircleModuleDrawerz&
    Draws the modules as circles
    Nc                    sz   t  j|| | jj}|t }t| jj||f| jjj	| _
t| j
jdd||f| jjd | j
||ftjj| _
d S )Nr   r#   )r   r   r   r2   ANTIALIASING_FACTORr   newmode
color_mask
back_colorcircler   r   ellipser&   resize
ResamplingLANCZOS)r   r   r   r2   Z	fake_sizer   r   r   r   N   s    
 zCircleModuleDrawer.initializer!   c                 C   s.   |r*| j j| j|d d |d d f d S Nr   r0   )r   r   paster:   r'   r   r   r   r)   \   s    zCircleModuleDrawer.drawrect)	r   r   r   r   r:   r   r*   r)   r+   r   r   r   r   r4   G   s   r4   c                       sN   e Zd ZdZdZdddZ fddZdd	 Zeee	  d
dddZ
  ZS )RoundedModuleDraweraP  
    Draws the modules with all 90 degree corners replaced with rounded edges.

    radius_ratio determines the radius of the rounded edges - a value of 1
    means that an isolated module will be drawn as a circle, while a value of 0
    means that the radius of the rounded edge will be 0 (and thus back to 90
    degrees again).
    Tr0   c                 C   s
   || _ d S r   )radius_ratio)r   rB   r   r   r   r/   m   s    zRoundedModuleDrawer.__init__c                    s,   t  j|| t| jjd | _|   d S )Nr1   )r   r   intr   r2   corner_widthsetup_cornersr   r   r   r   r   p   s    zRoundedModuleDrawer.initializec           	      C   s   | j j}| j jj}| j j}t|| j| jf|| _| jt	 }| j
| }|d }t|||f|}t|}|jdd||f|d |j|d||f|d |jd|||f|d || j| jftjj| _| jtjj| _| jtjj| _| jtjj| _d S Nr1   r   r#   )r   r7   r8   r9   r&   r   r6   rD   SQUAREr5   rB   r   r   r;   r%   r<   r=   r>   NW_ROUND	transpose	TransposeFLIP_TOP_BOTTOMSW_ROUNDZ
ROTATE_180SE_ROUNDFLIP_LEFT_RIGHTNE_ROUND)	r   r7   r9   front_color
fake_widthZradiusZdiameterbase	base_drawr   r   r   rE   u   s6    
 
 

  

 z!RoundedModuleDrawer.setup_cornersr	   )r(   r"   c                 C   s4  |sd S |j  o|j }|j o&|j }|j o6|j }|j oF|j  }|rR| jn| j}|rb| jn| j}|rr| jn| j}	|r| jn| j}
| j	j
||d d |d d f | j	j
||d d | j |d d f | j	j
|	|d d | j |d d | j f | j	j
|
|d d |d d | j f d S r?   )WNESrH   rG   rO   rM   rL   r   r   r@   rD   )r   r(   r"   Z
nw_roundedZ
ne_roundedZ
se_roundedZ
sw_roundednwneseswr   r   r   r)      s"    $* "zRoundedModuleDrawer.drawrect)r0   )r   r   r   r   needs_neighborsr/   r   rE   r   rC   r)   r+   r   r   r   r   rA   a   s   	
rA   c                       sD   e Zd ZdZdZdddZ fddZdd	 Zd
dddZ  Z	S )VerticalBarsDrawerz
    Draws vertically contiguous groups of modules as long rounded rectangles,
    with gaps between neighboring bands (the size of these gaps is inversely
    proportional to the horizontal_shrink).
    Tr-   c                 C   s
   || _ d S r   )horizontal_shrink)r   r^   r   r   r   r/      s    zVerticalBarsDrawer.__init__c                    sB   t  j|| t| jjd | _td| j | j | _|   d S Nr1   r0   )	r   r   rC   r   r2   half_heightr^   r3   setup_edgesr   r   r   r   r      s    zVerticalBarsDrawer.initializec                 C   s   | j j}| j jj}| j j}| j}|d }t|| j }t	|||f|| _
|t }|t }t	|||f|}	t|	}
|
jdd||d f|d |	||ftjj| _| jtjj| _d S rF   )r   r7   r8   r9   r&   r`   rC   r^   r   r6   rG   r5   r   r   r;   r<   r=   r>   	ROUND_TOPrI   rJ   rK   ROUND_BOTTOM)r   r7   r9   rP   heightwidthZshrunken_widthrQ   fake_heightrR   rS   r   r   r   ra      s$    
  
zVerticalBarsDrawer.setup_edgesr	   r!   c                 C   s   |r|j  }|j }|r| jn| j}|r.| jn| j}| jj||d d | j |d d f | jj||d d | j |d d | j	 f d S r?   )
rU   rW   rb   rG   rc   r   r   r@   r3   r`   )r   r(   r"   Ztop_roundedZbottom_roundedtopZbottomr   r   r   r)      s    * "zVerticalBarsDrawer.drawrect)r-   
r   r   r   r   r\   r/   r   ra   r)   r+   r   r   r   r   r]      s   
r]   c                       sD   e Zd ZdZdZdddZ fddZdd	 Zd
dddZ  Z	S )HorizontalBarsDrawerz
    Draws horizontally contiguous groups of modules as long rounded rectangles,
    with gaps between neighboring bands (the size of these gaps is inversely
    proportional to the vertical_shrink).
    Tr-   c                 C   s
   || _ d S r   )vertical_shrink)r   rj   r   r   r   r/      s    zHorizontalBarsDrawer.__init__c                    sB   t  j|| t| jjd | _td| j | j | _|   d S r_   )	r   r   rC   r   r2   
half_widthrj   r3   ra   r   r   r   r   r      s    zHorizontalBarsDrawer.initializec                 C   s   | j j}| j jj}| j j}| j}|d }t|| j }t	|||f|| _
|t }|t }t	|||f|}	t|	}
|
jdd|d |f|d |	||ftjj| _| jtjj| _d S rF   )r   r7   r8   r9   r&   rk   rC   rj   r   r6   rG   r5   r   r   r;   r<   r=   r>   
ROUND_LEFTrI   rJ   rN   ROUND_RIGHT)r   r7   r9   rP   re   rd   Zshrunken_heightrQ   rf   rR   rS   r   r   r   ra      s$    
  
z HorizontalBarsDrawer.setup_edgesr	   r!   c                 C   s   |r|j  }|j }|r| jn| j}|r.| jn| j}| jj||d d |d d | j f | jj||d d | j	 |d d | j f d S r?   )
rT   rV   rl   rG   rm   r   r   r@   r3   rk   )r   r(   r"   Zleft_roundedZright_roundedleftrightr   r   r   r)      s    * "zHorizontalBarsDrawer.drawrect)r-   rh   r   r   r   r   ri      s   
ri   N)
__future__r   typingr   r   Zqrcode.compat.pilr   r   Z&qrcode.image.styles.moduledrawers.baser   Zqrcode.image.styledpilr   Zqrcode.mainr	   r5   r   r   r,   r4   rA   r]   ri   r   r   r   r   <module>   s   B5