U
    Dvf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 || ||ot||| || 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   9/tmp/pip-unpacked-wheel-qlge9rch/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 )
zGive 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	||||}
|sd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rxqzt	|||| \}}W n t
k
r   Y qY nX t|| }g }d}t|D ]N\}}|||  }t|d |d  }t||}||kr
 q|| q||kr$qt|D ]V\}}|||  }td	d
 t||D \}}}	}t|||	||s,|d } qq,||krq|d }t||}t|||| d}||k r|}|dkr qq|
| ||kr|}qg }g } t |
d }|r:|
| j|
| j }!}"|| | |" ||!8 }qg }#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   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.benchmarkrc   ZfontTools.cu2qurd   printr3   r   )rc   rd   r	   Zreconstruct_tolerancer*   Z
quadraticsr(   r   r   r   main  s    


rf   __main__)r   F)r   F)"r   Z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   rf   __name__r   r   r   r   <module>   s   


 
'
  
9y
