o
    h 0                     @   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e je je jd		d8deee  dededeeedf  fddZe	dg dZe jd9i 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d*e jde jd+e jd,e jd-e jd.e jd/e jd0e jd1e jd2e jd8d3d4Zd5d6 Z e!d7krFe   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   i/var/www/html/construction_image-detection-poc/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   )cost
is_complexr   Fquadsmax_err	all_cubicreturn.c                 C   s   t | d d tu }|sdd | D } | d d g}dg}d}| D ]?}|d |d ks-J tt|d D ]}|d7 }|| || q5t|dd }	|  ||	 |d7 }|| q!t||||}
|sqd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$          z2quadratic_to_curves.<locals>.<listcomp>.<listcomp>r   )r"   r7   r   r   r   r$      rC   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    )	typer@   r&   r5   r'   r9   popextendspline_to_curves)r<   r=   r>   r;   r8   costsr:   r7   r0   qqr(   r   r   r   r      s*   #

Solution)
num_pointserrorstart_indexis_cubicr0   jr   r   i_sol_countj_sol_countthis_sol_countr	   errrS   i_sol_errorj_sol_errorrU   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$     s    z$spline_to_curves.<locals>.<listcomp>r   r!   r    Fc                 s   s    | ]	\}}|| V  qd S rD   r   )r"   r]   r^   r   r   r   rH   T  rI   z#spline_to_curves.<locals>.<genexpr>T)r5   r&   setr   addrQ   rR   rS   r-   ZeroDivisionErrorr   	enumeratemaxr'   rJ   zipr   rT   rU   reversedr4   )$r8   rO   r	   r>   elevated_quadraticsforcedr0   r
   r   r   sols
impossibler   best_solrV   rX   r\   
this_countrW   r[   i_solr,   r)   reconstructed_iterreconstructedrS   r   reconstorigrZ   r   splitscubicr.   rU   r(   r   r_   r   rN      s   !
(
 

 







"rN   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.benchmarkrt   fontTools.cu2quru   printr5   r   )rt   ru   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doubler@   r   r   r-   r9   floatPointboolr   rQ   rN   r{   __name__r   r   r   r   <module>   s   
 
'

9
	
y

