o
    h:                     @   st  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mZm	Z	 d dl
mZmZ eeef Zeeef Zee Zee Ze	edf Zee Zee ZdZe je je je je je je je je je je jd	deded	ed
edef
ddZdededefddZdedededefddZe je je je je je je je je jde edededededef
ddZe je je je je je je je jed	 d/dedededefddZ e je je je je je jee jde d dfdedededefd d!Z!d"e"d#efd$d%Z#d&ed#ed'efd(d)Z$	*d0dedededefd+d,Z%	*d0dededededef
d-d.Z&dS )1    N)cython)SequenceTupleUnion)IntegralReal   )	jnx1x2d1d2scalexdcoordsrc1rd1rc2rd2c                 C   s   ddg}dD ]t}g  ||< }|| || || || f\}}	}
}||	kr>t | }|
|kr5||
g|  n|dg|  q||	krL|	|}}	||
}
}||
 |	|  }| D ]#}|| }||krc|
}n||	krj|}n
|| | }|
| }|| qVqt| S )zGiven two reference coordinates `rc1` & `rc2` and their respective
    delta vectors `rd1` & `rd2`, returns interpolated deltas for the set of
    coordinates `coords`.Nr      r   )lenextendappendzip)r   r   r   r   r   
out_arraysr	   outr   r   r   r   r
   r   pairr   r   nudge r!   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fontTools/varLib/iup.pyiup_segment   s2   $

	r#   deltasreturnc              
   C   s  t | t |ks
J d| vr| S t | }dd t| D }|s$dg| S g }t|}t|}|dkrUd|||d f\}}}	}
|t||| ||	 | |	 ||
 | |
  || |  |D ]4}|| dkr|d |||f\}}}	}
|t||| ||	 | |	 ||
 | |
  || |  |}q^||d kr|d |||d f\}}}	}
|t||| ||	 | |	 ||
 | |
  t | t |ksJ t | t |f|S )zFor the contour given in `coords`, interpolate any missing
    delta values in delta vector `deltas`.

    Returns fully filled-out delta vector.Nc                 S   s   g | ]
\}}|d ur|qS Nr!   ).0ivr!   r!   r"   
<listcomp>g   s    ziup_contour.<locals>.<listcomp>)r   r   r   r   )r   	enumerateiternextr   r#   r   )r$   r   r
   indicesr   itstarti1i2ri1ri2endr!   r!   r"   iup_contour[   sJ   
"""$r7   endsc                 C   s   t ||krt||r|d d ndd ksJ t|}||d |d |d |d g }g }d}|D ]}|d7 }t| || ||| }|| |}q2|S )zFor the outline given in `coords`, with contour endpoints given
    in sorted increasing order in `ends`, interpolate any missing
    delta values in delta vector `deltas`.

    Returns fully filled-out delta vector.r+   r   r            )sortedr   r7   r   )r$   r   r8   r
   r   r1   r6   contourr!   r!   r"   	iup_delta   s   0	 
r>   )r(   r	   r   ypqr(   r	   	tolerancec                    sh   || dksJ t ||d | || | | || | | }| |d | } t fddt| |D S )zReturn true if the deltas for points at `i` and `j` (`i < j`) can be
    successfully used to interpolate deltas for points in between them within
    provided error tolerance.r;   r   c                 3   s6    | ]\\}}\}}t t|| ||  kV  qd S r&   abscomplex)r'   r   r?   r@   rA   rB   r!   r"   	<genexpr>   s
    
z%can_iup_in_between.<locals>.<genexpr>)r#   allr   )r$   r   r(   r	   rB   interpr!   rF   r"   can_iup_in_between   s   ,rJ   )cjdjlcjldjncjndjforceforcedc                 C   s  t | t |ks
J t | }t }tt | d ddD ]}| |d  ||d  }}| | || }}	| || d  ||| d  }
}dD ]}|	| }|| }|| }|| }|| }|
| }||kro||}}||}}n
||}}||}}d}||krt|| |krt||krd}na||  kr|krn nt||| |  krt||| ksn d}n=||kr||k rt||krt|| |kr|| |k ||k krd}nt||krt|| |kr||| k ||k krd}|r||  nqFq|S )a  The forced set is a conservative set of points on the contour that must be encoded
    explicitly (ie. cannot be interpolated).  Calculating this set allows for significantly
    speeding up the dynamic-programming, as well as resolve circularity in DP.

    The set is precise; that is, if an index is in the returned set, then there is no way
    that IUP can generate delta for that point, given `coords` and `deltas`.
    r   r+   r   FT)r   setrangerD   minmaxadd)r$   r   rB   r
   rR   r(   ldlcr   cndncr	   rK   rL   rM   rN   rO   rP   c1c2r   r   rQ   r!   r!   r"   _iup_contour_bound_forced_set   sZ   "


*
r_   )r(   r	   	best_costbest_jcostrR   rB   lookbackc                 C   s   t | }|du r
|}t|t}ddi}ddi}td|D ]M}||d  d }	|	||< |d ||< |d |v r7qt|d t|| ddD ]$}
||
 d }||	k rbt| ||
||rb| ||< }	|
||< |
|v rh nqDq||fS )a  Straightforward Dynamic-Programming.  For each index i, find least-costly encoding of
    points 0 to i where i is explicitly encoded.  We find this by considering all previous
    explicit points j and check whether interpolation can fill points between j and i.

    Note that solution always encodes last point explicitly.  Higher-level is responsible
    for removing that restriction.

    As major speedup, we stop looking further whenever we see a "forced" point.Nr+   r   r   r;   )r   rU   MAX_LOOKBACKrT   rV   rJ   )r$   r   rR   rB   rc   r
   costschainr(   r`   r	   rb   r!   r!   r"   _iup_contour_optimize_dp%  s,   
rh   lkc                 C   s8   t | }||; }|s| S | || d | d||   S )z{Rotate list by k items forward.  Ie. item at position 0 will be
    at position k in returned list.  Negative k is allowed.N)r   )ri   rj   r
   r!   r!   r"   	_rot_listZ  s
    rk   sr
   c                    s$    ;   s| S  fdd| D S )Nc                    s   h | ]}|   qS r!   r!   )r'   r)   rj   r
   r!   r"   	<setcomp>h  s    z_rot_set.<locals>.<setcomp>r!   )rl   rj   r
   r!   rm   r"   _rot_setd  s   ro           c                    s  t }tfddD rdg| S |dkrS d tfddD r3gdg|d   S t|}|r|d t| }|dksIJ t|t||}t|||}t||\}}t |d }|durz| || }|dusm	d |ksJ |ffdd	t
|D t| S t || ||\}}d|d  }	t
|d t |d D ]7}
t |
}||
| krۈ||  || }||
| ks||
| kr||
 ||
|   }||	kr| }	q| ksJ | f fd
d	t
|D S )zFor contour with coordinates `coords`, optimize a set of delta
    values `deltas` within error `tolerance`.

    Returns delta vector that has most number of None items instead of
    the input delta.
    c                 3   s     | ]}t t|  kV  qd S r&   rC   )r'   r@   rF   r!   r"   rG   z  s    z'iup_contour_optimize.<locals>.<genexpr>Nr   r   c                 3   s    | ]} |kV  qd S r&   r!   )r'   r   )d0r!   r"   rG     s    r+   c                    s    g | ]}|v r | nd qS r&   r!   r'   r(   )r$   solutionr!   r"   r*          z(iup_contour_optimize.<locals>.<listcomp>c                    s    g | ]}| v r| nd qS r&   r!   rr   )best_solr$   r!   r"   r*     rt   )r   rH   r_   rV   rk   ro   rh   rS   rW   removerT   )r$   r   rB   r
   rR   rj   rg   rf   r(   r`   r1   rb   r!   )ru   rq   r$   rs   rB   r"   iup_contour_optimizek  s\   

	




rw   c           	      C   s   t ||krt||r|d d ndd ksJ t|}||d |d |d |d g }g }d}|D ])}t| ||d  |||d  |}t||| d ksRJ || |d }q2|S )a  For the outline given in `coords`, with contour endpoints given
    in sorted increasing order in `ends`, optimize a set of delta
    values `deltas` within error `tolerance`.

    Returns delta vector that has most number of None items instead of
    the input delta.
    r+   r   r   r9   r:   r;   )r<   r   rw   r   )	r$   r   r8   rB   r
   r   r1   r6   r=   r!   r!   r"   iup_delta_optimize  s   0 

rx   )r   )rp   )'r   AttributeErrorImportErrorfontTools.misccompiledCOMPILEDtypingr   r   r   numbersr   r   _Point_Delta_PointSegment_DeltaSegment_DeltaOrNone_DeltaOrNoneSegment
_Endpointsre   cfunclocalsintdoubler#   r7   r>   inlinereturnsrJ   rS   r_   rh   listrk   ro   rw   rx   r!   r!   r!   r"   <module>   s   34
	Q,

i