o
    `i=3                     @   s  zd dl Z W n eefy   d dlm Z  Y nw e jZ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dd Zdd Zdd Ze je je jd		d>deee  deded eeed!f  fd"dZ e	d#g d$Z!e jd?i d%e jd&e jd'e jd(e jd)e jd*e jd+e jd,e jd-e jd.e jd/e jd0e jde jd1e jd2e jd3e jd4e jd5e jd6e jd7e jd8e jd>d9d:Z"d;d< Z#e$d=krRe#  dS dS )@    N)cython)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r"dS || | |  d }t| | | d || ||oGt||| || 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   S/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/fontTools/qu2cu/qu2cu.pyr   (   s   r   r
   r   r   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_quadraticR   s   


r   )startnk
prod_ratio	sum_ratioratiotr
   r   r   r   c                    s:  d}d dg}t d|D ];}| ||  }| || d  }|d |d ks&J 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  }
||| |rd|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)ranger   append)curvesr   r   r   tsr   ckc_beforer   r
   r   r   r   curver   r#   r   merge_curvese   s(   ( r-   )countnum_offcurvesioff1off2onc                 C   sl   t | }d}t| d }td|D ]"}| | }| |d  }||| d  }||d | | |d7 }q|S )Nr   r!   r    r   )listlenr&   insert)pqr.   r/   r0   r1   r2   r3   r   r   r   add_implicit_on_curves   s   	
r9   c                 C   s   t d|d| )Nz1Quadratic splines must connect end-to-start; got z then 
ValueError)pointprevious_pointr   r   r   _raise_incompatible_point   s   r>   c                 C   s   t | dk r
tdd S )Nr   z0Quadratic splines must contain at least 3 points)r5   r;   )spliner   r   r   _validate_spline_length   s   r@   c                 C   s   | dkrt dd S )Nr   z!max_err must be greater than zeror:   )max_errr   r   r   _validate_positive_tolerance   s   rB   )cost
is_complexr   FquadsrA   	all_cubicreturn.c                 C   s  | sg S t | | D ]}t| q
t| d d tu }|s$dd | D } | d d g}dg}d}| D ]F}|d |d krEt|d |d  tt|d D ]}	|d7 }|| || qMt|dd }
|	  |
|
 |d7 }|| q2t||||}|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.
        If ``quads`` is empty, returns an empty list.

        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

    Raises:
        ValueError: if an input spline has fewer than 3 points, or if adjacent
        splines do not connect end-to-start.
    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$          z2quadratic_to_curves.<locals>.<listcomp>.<listcomp>r   )r"   r7   r   r   r   r$      rK   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>      z1quadratic_to_curves.<locals>.<listcomp>.<genexpr>)tuple)r"   r,   r   r   r   r$     s    )rB   r@   typerH   r>   r&   r5   r'   r9   popextendspline_to_curves)rE   rA   rF   r?   rD   r8   costsrC   r7   r0   qqr(   r   r   r   r      s6   (


Solution)
num_pointserrorstart_indexis_cubicr0   jr   r   i_sol_countj_sol_countthis_sol_countr	   errr[   i_sol_errorj_sol_errorr]   r.   r
   r   r   r   vuc           $   
      s  t  dks
J d fddtdt  d dD }t }tdt |D ]/}||d  d }|| d }|| d }	t|| t|	|  |t|	|  krT|| q%tddddg}
tt |d d ddd}d}tdt |d D ]}|}t||D ]}|
| j|
| j}}|s|d| d  |d|   d }|| }|}t|||| d}||k r|}|dkrq~zt|||| \}}W n	 t	y   Y q~w t
g ||R  }g }d}t|D ]$\}}|||  }t|d |d  }t||}||kr n|| q||kr	q~t|D ]*\}}|||  }td	d
 t||D \}}}	}t|||	||s6|d } nq||kr>q~|d }t||}t|||| d}||k rW|}|dkr^ nq~|
| ||v rk|}qug }g } t |
d }|r|
| j|
| j}!}"|| | |" ||!8 }|syg }#d}ttt|| D ]0\}}"|"r|#t|||| d  nt||D ]}|# |d |d d   q|}q|#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"   r0   r8   r   r   r$   2  s    z$spline_to_curves.<locals>.<listcomp>r   r!   r    Fc                 s   s    | ]	\}}|| V  qd S rL   r   )r"   re   rf   r   r   r   rP   p  rQ   z#spline_to_curves.<locals>.<genexpr>T)r5   r&   setr   addrY   rZ   r[   r-   ZeroDivisionErrorr   	enumeratemaxr'   rR   zipr   r\   r]   reversedr4   )$r8   rW   r	   rF   elevated_quadraticsforcedr0   r
   r   r   sols
impossibler   best_solr^   r`   rd   
this_countr_   rc   i_solr,   r)   reconstructed_iterreconstructedr[   r   reconstorigrb   r   splitscubicr.   r]   r(   r   rg   r   rV     s   !
(
 

 







"rV   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):)fontTools.cu2qu.benchmarkr|   fontTools.cu2qur}   printr5   r   )r|   r}   r	   reconstruct_tolerancer,   
quadraticsr(   r   r   r   main  s   


r   __main__)r   Fr   )%r   AttributeErrorImportErrorfontTools.misccompiledCOMPILEDfontTools.misc.bezierToolsr   collectionsr   mathtypingr   r   r   __all__cfuncreturnsintlocalsdoublerH   r   r   r-   r9   floatPointr>   r@   rB   boolr   rY   rV   r   __name__r   r   r   r   <module>   s   
 
'

E
	
y

