U
    »mœd0  ã                   @   s6  zd dl Z e jZW n( eefk
r:   d dlm Z  dZY nX d dlmZ d dlm	Z	 d dl
Z
d dlmZmZmZ dgZe je  e j¡e je je je je je jd�e je je jd	�d
d„ ƒƒƒƒZe je je je je jd�dd„ ƒZe je je je je je je je je je je je je jd�dd„ ƒƒZe je je je je je je jd�dd„ ƒZeeeef ef Ze je je jd�d%eee  eeeeedf  dœdd„ƒZe	dddddgƒZe je je je je je je je je je je je je je je je je je je je je je jd�d&d d!„ƒZd"d#„ Z e!d$k�r2e ƒ  dS )'é    N)ÚcythonF)ÚsplitCubicAtTC)Ú
namedtuple)ÚListÚTupleÚUnionÚquadratic_to_curves)Ú	toleranceÚp0Úp1Úp2Úp3)ÚmidÚderiv3c                 C   s�   t |ƒ|krt |ƒ|krdS | d||   | d }t |ƒ|krDdS || | |  d }t| | | d || ||ƒoŽt||| || d ||ƒS )a�  Check if a cubic Bezier lies within a given distance of the origin.

    "Origin" means *the* origin (0,0), not the start of the curve. Note that no
    checks are made on the start and end positions of the curve; this function
    only checks the inside of the curve.

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.
        tolerance (double): Distance from origin.

    Returns:
        bool: True if the cubic Bezier ``p`` entirely lies within a distance
        ``tolerance`` of the origin, False otherwise.
    Té   g      À?Fç      à?)ÚabsÚcubic_farthest_fit_inside)r
   r   r   r   r	   r   r   © r   úN/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/fontTools/qu2cu/qu2cu.pyr   +   s     
   ÿþr   ©r
   r   r   Zp1_2_3c                 C   s$   |d }| | d | |d | |fS )zAGiven a quadratic bezier curve, return its degree-elevated cubic.gUUUUUUå?gUUUUUUÕ?r   r   r   r   r   Úelevate_quadraticU   s    


ür   )ÚstartÚnÚkÚ
prod_ratioÚ	sum_ratioÚratioÚtr
   r   r   r   c                    s<  d}d‰ dg}t d|ƒD ]v}| ||  }| || d  }|d |d ksLt‚t|d |d  ƒt|d |d  ƒ }||9 }ˆ |7 ‰ | ˆ ¡ q‡ fdd„|dd	… D ƒ}| | d }	| | d }
| || d  d }| || d  d }|	|
|	 |rþ|d nd  }
||| |�r d|d	  nd  }|	|
||f}||fS )
z…Give a cubic-Bezier spline, reconstruct one cubic-Bezier
    that has the same endpoints and tangents and approxmates
    the spline.g      ð?é   r   r   é   c                    s   g | ]}|ˆ  ‘qS r   r   )Ú.0r   ©r   r   r   Ú
<listcomp>�   s     z merge_curves.<locals>.<listcomp>Néÿÿÿÿ)ÚrangeÚAssertionErrorr   Úappend)Úcurvesr   r   r   Útsr   ZckZc_beforer   r
   r   r   r   Úcurver   r"   r   Úmerge_curvesh   s(    ("r+   )ÚcountÚnum_offcurvesÚiÚoff1Úoff2Úonc                 C   sl   t | ƒ}d}t| ƒd }td|ƒD ]D}| | }| |d  }||| d  }| |d | |¡ |d7 }q"|S )Nr   r    r   r   )ÚlistÚlenr%   Úinsert)ÚpÚqr,   r-   r.   r/   r0   r1   r   r   r   Úadd_implicit_on_curves�   s    	
r7   )ÚcostÚ
is_complexr   .)ÚquadsÚmax_errÚ	all_cubicÚreturnc                 C   sæ   t | d d ƒtk}|s&dd„ | D ƒ} | d d g}dg}d}| D ]~}|d |d ksZt‚tt|ƒd ƒD ] }|d7 }| |¡ | |¡ qjt|ƒdd… }	| ¡  | |	¡ |d7 }| |¡ qBt	||||ƒ}
|sâdd„ |
D ƒ}
|
S )	a  Converts a connecting list of quadratic splines to a list of quadratic
    and cubic curves.

    A quadratic spline is specified as a list of points.  Either each point is
    a 2-tuple of X,Y coordinates, or each point is a complex number with
    real/imaginary components representing X,Y coordinates.

    The first and last points are on-curve points and the rest are off-curve
    points, with an implied on-curve point in the middle between every two
    consequtive off-curve points.

    Returns:
        The output is a list of tuples of points. Points are represented
        in the same format as the input, either as 2-tuples or complex numbers.

        Each tuple is either of length three, for a quadratic curve, or four,
        for a cubic curve.  Each curve's last point is the same as the next
        curve's first point.

    Args:
        quads: quadratic splines

        max_err: absolute error tolerance; defaults to 0.5

        all_cubic: if True, only cubic curves are generated; defaults to False
    r   c                 S   s   g | ]}d d„ |D ƒ‘qS )c                 S   s   g | ]\}}t ||ƒ‘qS r   )Úcomplex)r!   ÚxÚyr   r   r   r#   Ú   s     z2quadratic_to_curves.<locals>.<listcomp>.<listcomp>r   )r!   r5   r   r   r   r#   Ú   s     z'quadratic_to_curves.<locals>.<listcomp>r   r$   r    Nc                 S   s   g | ]}t d d„ |D ƒƒ‘qS )c                 s   s   | ]}|j |jfV  qd S ©N)ÚrealÚimag)r!   Úcr   r   r   Ú	<genexpr>î   s     z1quadratic_to_curves.<locals>.<listcomp>.<genexpr>)Útuple)r!   r*   r   r   r   r#   î   s     )
Útyper>   r&   r%   r3   r'   r7   ÚpopÚextendÚspline_to_curves)r:   r;   r<   r9   r6   Úcostsr8   r5   r.   Zqqr(   r   r   r   r   µ   s*    #

ÚSolutionÚ
num_pointsÚerrorÚstart_indexÚis_cubic)r.   Újr   r   Úi_sol_countÚj_sol_countZthis_sol_countr	   ÚerrrN   Úi_sol_errorÚj_sol_errorr<   rP   r,   r
   r   r   r   ÚvÚuc           $   
      sº  t ˆ ƒdkstdƒ‚‡ fdd„tdt ˆ ƒd dƒD ƒ}tƒ }tdt |ƒƒD ]^}||d  d }|| d }|| d }	t|| ƒt|	| ƒ |t|	| ƒ krJ| |¡ qJtddddƒg}
tt |ƒd d dddƒ}d}tdt |ƒd ƒD �]ü}|}t||ƒD �]Ð}|
| j|
| j }}|�sx|d| d  |d|   d }|| }|}t|||| dƒ}||k �rl|}|dk�rxqþzt	|||| ƒ\}}W n t
k
�rª   Y qþY nX t||žŽ }g }d}t|ƒD ]N\}}|||  }t|d |d  ƒ}t||ƒ}||k�r
 �q| |¡ �qÈ||k�r$qþt|ƒD ]V\}}|||  }td	d
„ t||ƒD ƒƒ\}}}	}t|||	||ƒ�s,|d } �q„�q,||k�r�qþ|d }t||ƒ}t|||| dƒ}||k �rÂ|}|dkrþ �qÒqþ|
 |¡ ||krê|}qêg }g } t |
ƒd }|�r:|
| j|
| j }!}"| |¡ |  |"¡ ||!8 }�qþg }#d}ttt|| ƒƒƒD ]`\}}"|"�r~|# t	|||| ƒd ¡ n0t||ƒD ]$}|# ˆ |d |d d … ¡ �qˆ|}�qT|#S )aF  
    q: quadratic spline with alternating on-curve / off-curve points.

    costs: cumulative list of encoding cost of q in terms of number of
      points that need to be encoded.  Implied on-curve points do not
      contribute to the cost. If all points need to be encoded, then
      costs will be range(1, len(q)+1).
    r   z+quadratic spline requires at least 3 pointsc                    s    g | ]}t ˆ ||d  … Ž ‘qS )r   )r   )r!   r.   ©r6   r   r   r#     s    z$spline_to_curves.<locals>.<listcomp>r   r    r   Fc                 s   s   | ]\}}|| V  qd S rA   r   )r!   rW   rX   r   r   r   rE   W  s     z#spline_to_curves.<locals>.<genexpr>T)r3   r&   r%   Úsetr   ÚaddrL   rM   rN   r+   ÚZeroDivisionErrorr   Ú	enumerateÚmaxr'   rF   Úzipr   rO   rP   Úreversedr2   )$r6   rK   r	   r<   Zelevated_quadraticsZforcedr.   r
   r   r   ZsolsZ
impossibler   Zbest_solrQ   rS   rV   Z
this_countrR   rU   Zi_solr*   r)   Zreconstructed_iterZreconstructedrN   r   ZreconstÚorigrT   r   ZsplitsZcubicr,   rP   r(   r   rY   r   rJ   õ   sœ    !
ÿ( 





 






"rJ   c                  C   sˆ   ddl m}  ddlm} d}|d }| ƒ }|||ƒ}td||f ƒ tdt|ƒ ƒ t|g|ƒ}tdt|ƒ ƒ td	|ƒ td
|ƒ d S )Nr   )Úgenerate_curve)Úcurve_to_quadraticgš™™™™™©?r   z'cu2qu tolerance %g. qu2cu tolerance %g.z+One random cubic turned into %d quadratics.z-Those quadratics turned back into %d cubics. zOriginal curve:zReconstructed curve(s):)ZfontTools.cu2qu.benchmarkrb   ZfontTools.cu2qurc   Úprintr3   r   )rb   rc   r	   Zreconstruct_tolerancer*   Z
quadraticsr(   r   r   r   Úmain…  s    

ÿ
re   Ú__main__)r   F)r   F)"r   ÚcompiledZCOMPILEDÚAttributeErrorÚImportErrorZfontTools.miscZfontTools.misc.bezierToolsr   Úcollectionsr   ÚmathÚtypingr   r   r   Ú__all__ZcfuncZreturnsÚintÚlocalsÚdoubler>   r   r   r+   r7   ÚfloatZPointÚboolr   rL   rJ   re   Ú__name__r   r   r   r   Ú<module>   sº   


û ü
õ'ú
þ  ý
ü9ëy
