a
    [Xh2                     @   s   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ eeddfZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zeeeeeeeeeeeeed Zd!S )"z%Calculate bounding boxes of SVG tags.    )
atanatan2cosinfisinfpiradianssinsqrttan   )PATH_LETTERS)	normalizepointc                 C   s   |j tvrtS t|j  | ||}t|s,tS |r|j dkrt| |dr| |dd|}|d |d  |d |d  |d | |d | f}|S )	zBounding box for any node.gstrokezstroke-widthZ1pxr      r      )tagBOUNDING_BOX_METHODSEMPTY_BOUNDING_BOXis_valid_bounding_boxanyZ	get_paintgetlength)svgnode	font_sizer   ZboxZstroke_width r   `/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/svg/bounding_box.pybounding_box   s    
"r    c                 C   sH   |  |d|d|\}}|  |d|d|\}}||||fS )zBounding box for rect node.xywidthheightr   r   )r   r   r   r!   r"   r#   r$   r   r   r   bounding_box_rect   s
    r&   c                 C   sL   |  |d|d|\}}| |d|}|| || d| d| fS )zBounding box for circle node.cxcyrr   )r   r   r   )r   r   r   r'   r(   r)   r   r   r   bounding_box_circle"   s    r*   c                 C   sX   |  |d|d|\}}|  |d|d|\}}|| || d| d| fS )zBounding box for ellipse node.rxryr'   r(   r   r%   )r   r   r   r+   r,   r'   r(   r   r   r   bounding_box_ellipse)   s    r-   c                 C   s|   |  |d|d|\}}|  |d|d|\}}t||t|| }}t||| t|||  }	}
|||	|
fS )zBounding box for line node.x1y1x2y2)r   r   minmax)r   r   r   r.   r/   r0   r1   r!   r"   r#   r$   r   r   r   bounding_box_line0   s
    r4   c                 C   sH   t }g }t|dd}|r>t| ||\}}}|||f qt||S )zBounding box for polyline node.points )r   r   r   r   appendextend_bounding_box)r   r   r   r    r5   Znormalized_pointsr!   r"   r   r   r   bounding_box_polyline9   s    r9   c              
   C   s  | dd}tD ]}||d| d}qt|}t}d}d}d}|r
| }|ddd tv rz| ddd\}}|dv rt| ||\}}	}|dd\}
}tt	|
}
|d |dd   }}|d	 
 s||d  |dd   }}q|d |dd   }}|d	 
 sB||d  |dd   }}qtt|tt| }}t| ||\}}}|d
kr||7 }||7 }t||||	|
||||	}|\}}}}|| }|| }||f||ff}t||}|}|}n|dv rt| ||\}}}t| ||\}}}t| ||\}}}|dkr\||7 }||7 }||7 }||7 }||7 }||7 }t|||f||f||ff}|}|}n||dv r| ddd\}}| |d|\}}|dkr||7 }t|||ff}|}n |dv r0t| ||\}}}|dv r||7 }||7 }t|||ff}|}|}n|dv rt| ||\}}}t| ||\}}}|dv r||7 }||7 }||7 }||7 }t|||f||ff}|}|}nX|dv r | ddd\}}| d||\}}|dkr||7 }t|||ff}|}| }qB|S )zBounding box for path node.dr6    r   Mr   ZaANaZcCcZhHhZlLmMtTZlmtZqQsSqsZvVv)r   r   replacer   r   stripsplitr   r   floatisdigitboolint_bounding_box_elliptical_arcr8   )r   r   r   Z	path_dataletterr    Z
previous_xZ
previous_yr+   r,   Zrotationlargesweepr!   r"   Zarc_bounding_boxr.   r/   r#   r$   r0   r1   r5   _r   r   r   bounding_box_pathD   s    
 "














rO   c                 C   s
   | dS )zBounding box for text node.Ztext_bounding_box)r   )r   r   r   r   r   r   bounding_box_text   s    rP   c                 C   sZ   t }|D ]L}| ||}t|r|\}}}}	|| ||	  }
}t|||f|
|ff}q|S )zBounding box for g node.)r   Zcalculate_bounding_boxr   r8   )r   r   r   r    childZchild_bounding_boxminxminyr#   r$   maxxmaxyr   r   r   bounding_box_g   s    rV   c                 C   s6   ddl m} || || }du r$tS t| ||dS dS )zBounding box for use node.r   )get_use_treeNT)ZdefsrW   r   r    )r   r   r   rW   treer   r   r   bounding_box_use   s    rY   c	                 C   sH  t |t | }}d||fv rFt|| t||t ||  t || fS t|| |  d t|||  d  }	t| | |  d t|||  d  }
|d |d  |d |
d   |d |	d   }||d |
d  |d |	d    }d }}|dk rb|| }|
d |	d |d   }|dk rPt|| t||t ||  t || fS t|}|| }n<||krpdndt| }|| |
 | }| | |	 | }|t| |t|  | | d  }|t| |t|  || d  }|dtfv rD|| }td| }|| }td|}|| }t| d}|| }t|d}n|td dt d fv r|| }td| }|| }td|}|| }t| d}|| }t|d}n>t|t| |  }tt|t| |  }||t| t|  |t| t|  }||t| t|  |t| t|  }||krR|| }}|| }}||t| t|  |t| t|  }t|| || }||t| t|  |t| t|  }t|| || }t|t||  }t|t||  t }||t| t|  |t| t|  }||t| t|  |t| t|  }||krp|| }}|| }}||t| t|  |t| t|  }t|| || }||t| t|  |t| t|  }t|| || }t|| | | }t|| || }|s || }}d}||kr<|| }}d}|sV||ksp||k sp|rz||ksz||k szt|| }|s||ks||k s|r||ks||k st	|| }|s||ks||k s|r||ks||k st||}|s||ks*||k s*|r4||ks4||k s4t	||}|||| || fS )z/Bounding box of an elliptical arc in path node.r   r   r=   r   r   FT)
absr2   r   r	   r
   r   r   r   r   r3   )r.   r/   r+   r,   phirL   rM   r!   r"   Zx1primeZy1primeZradicantZcxprimeZcyprimeZratioZfactorr'   r(   rR   ZtminxrT   ZtmaxxrS   ZtminyrU   ZtmaxyZtmp_yZtmp_xZangle1Zangle2Z	other_arcr   r   r   rJ      s    ((*.$

(
$$

,,


,,,,


,,






rJ   c           
      C   s   | \}}}}t |rt n|| t |r.t n||  }}t| \}}	t|g|R  t|g|	R  t|g|R  t|g|	R  f\}}}}|||| || fS )z.Extend a bounding box to include given points.)r   r   zipr2   r3   )
r    r5   rR   rS   r#   r$   rT   rU   Zx_listZy_listr   r   r   r8   O  s    r8   c                 C   s   | ot | d | d   S )u=   Check that a bounding box doesn’t have infinite boundaries.r   r   )r   )r    r   r   r   r   \  s    r   )ZrectZcircleZellipselineZpolylineZpolygonpathr   ZusemarkertextZtspantextPathN) __doc__mathr   r   r   r   r   r   r   r	   r
   r   r^   r   utilsr   r   r   r    r&   r*   r-   r4   r9   rO   rP   rV   rY   rJ   r8   r   r   r   r   r   r   <module>   s@   0	 
d