o
    j=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   V/var/www/html/Deteccion_Ine/venv/lib/python3.10/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_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   ZckZ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,   r-   r.   r/   r0   r   r   r   add_implicit_on_curves   s   	
r6   c                 C   s   t d|d| )Nz1Quadratic splines must connect end-to-start; got z then 
ValueError)pointZ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)r2   r8   )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 zeror7   )max_errr   r   r   _validate_positive_tolerance   s   r>   )cost
is_complexr   Fquadsr=   	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!   r4   r   r   r   r#      rG   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    )r>   r<   typerD   r:   r%   r2   r&   r6   popextendspline_to_curves)rA   r=   rB   r;   r@   r5   costsr?   r4   r-   Zqqr'   r   r   r   r      s6   (


Solution)
num_pointserrorstart_indexis_cubicr-   jr   r   i_sol_countj_sol_countZthis_sol_countr	   errrV   i_sol_errorj_sol_errorrX   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!   r-   r5   r   r   r#   2  s    z$spline_to_curves.<locals>.<listcomp>r   r    r   Fc                 s   s    | ]	\}}|| V  qd S rH   r   )r!   r_   r`   r   r   r   rL   p  rM   z#spline_to_curves.<locals>.<genexpr>T)r2   r%   setr   addrT   rU   rV   r*   ZeroDivisionErrorr   	enumeratemaxr&   rN   zipr   rW   rX   reversedr1   )$r5   rS   r	   rB   Zelevated_quadraticsZforcedr-   r
   r   r   ZsolsZ
impossibler   Zbest_solrY   r[   r^   Z
this_countrZ   r]   Zi_solr)   r(   Zreconstructed_iterZreconstructedrV   r   Zreconstorigr\   r   ZsplitsZcubicr+   rX   r'   r   ra   r   rR     s   !
(
 

 







"rR   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.benchmarkrj   ZfontTools.cu2qurk   printr2   r   )rj   rk   r	   Zreconstruct_tolerancer)   Z
quadraticsr'   r   r   r   main  s   


rm   __main__)r   Fr   )%r   AttributeErrorImportErrorZfontTools.misccompiledZCOMPILEDZfontTools.misc.bezierToolsr   collectionsr   mathtypingr   r   r   __all__ZcfuncreturnsintlocalsdoublerD   r   r   r*   r6   floatZPointr:   r<   r>   boolr   rT   rR   rm   __name__r   r   r   r   <module>   s   
 
'

E
	
y

