B
    Jd-                 @   s  d dl mZmZmZ d dlmZmZ yd dlZW n  ek
rP   d dl	mZ Y nX ej
r^dZnd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eeeeedd	d
ZeeedddZeeeedddZeeeeeedddZd&eeeedddZi d dfeeeedddZeedddZeeedddZd'eeeedd!d"Z d(eeeeed#d$d%Z!dS ))    )SequenceTupleUnion)IntegralRealN)cythonTF   )coordsrc1rd1rc2rd2returnc             C   s  ddg}xdD ]}g  ||< }|| || || || f\}}	}
}||	kr~t | }|
|krl||
g|  q|dg|  q||	kr|	| }}	||
 }
}||
 |	|  }xJ| D ]B}|| }||kr|
}n||	kr|}n|
|| |  }|| qW qW 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   Z
out_arraysjoutx1Zx2d1d2nscalepairxd r   F/var/www/html/venv/lib/python3.7/site-packages/fontTools/varLib/iup.pyiup_segment$   s.    
$


r    )deltasr	   r   c          
   C   s  t | t |kstd| kr | S t | }dd t| D }|sHdg| S g }t|}t|}|dkrd|||d f\}}}	}
|t||| ||	 | |	 ||
 | |
  || |  xr|D ]j}|| dkr|d |||f\}}}	}
|t||| ||	 | |	 ||
 | |
  || |  |}qW ||d kr|d |||d f\}}}	}
|t||| ||	 | |	 ||
 | |
  t | t |kst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 k	r|qS )Nr   ).0ivr   r   r   
<listcomp>^   s    ziup_contour.<locals>.<listcomp>)r   r   r   r   )r   AssertionError	enumerateiternextr   r    r   )r!   r	   r   indicesr   itstarti1i2Zri1Zri2endr   r   r   iup_contourQ   s2    
.
..&r1   )r!   r	   endsr   c             C   s   t ||kr,t||r"|d d ndd ks0tt|}||d |d |d |d g }g }d}x<|D ]4}|d7 }t| || ||| }|| |}qfW |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   r'   r1   r   )r!   r	   r2   r   r   r-   r0   contourr   r   r   	iup_deltay   s    	0 

r8   )r!   r	   r#   r   	tolerancer   c                s   || dkst tt||d | || | | || | | }| |d | } t| t|ksdt 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.r5   r   c             3   s4   | ],\\}}\}}t t|| ||  kV  qd S )N)abscomplex)r"   r   ypq)r9   r   r   	<genexpr>   s    z%can_iup_in_between.<locals>.<genexpr>)r'   listr    r   allr   )r!   r	   r#   r   r9   Zinterpr   )r9   r   can_iup_in_between   s
    	0rB   )r!   r	   r9   r   c             C   s(  t | t |kstt | }t }xtt | d ddD ]}| |d  ||d   }}| | ||  }}	| || d  ||| d   }
}xdD ]}|	| }|| }|| }|| }|| }|
| }||kr|| }}|| }}n|| }}|| }}d}||kr2t|| |krt||krd}n||  krJ|krn n4t||| |  krzt||| ksn d}n||kr||k rt||krt|| |kr|| |k ||k krd}n:t||krt|| |kr||| k ||k krd}|r|| P qW q:W |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   r   FT)r   r'   setranger:   minmaxadd)r!   r	   r9   r   forcedr#   ldlcr   cndncr   cjZdjZlcjZldjZncjZndjc1c2r   r   forcer   r   r   _iup_contour_bound_forced_set   sH    
"



 .

66

rR   )r!   r	   r9   lookbackc             C   s   t | }|dkr|}t|t}ddi}ddi}xtd|D ]}||d  d }	|	||< |d ||< |d |krpq:xdt|d t|| ddD ]F}
||
 d }||	k rt| ||
||r| ||< }	|
||< |
|krP qW q:W ||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   r5   )r   rE   MAX_LOOKBACKrD   rF   rB   )r!   r	   rH   r9   rS   r   costschainr#   	best_costr   costr   r   r   _iup_contour_optimize_dp   s(    
 
rZ   )lkc             C   s8   t | }||; }|s| S | || d | d||   S )zxRotate list by k items forward.  Ie. item at position 0 will be
	at position k in returned list.  Negative k is allowed.N)r   )r[   r\   r   r   r   r   	_rot_list  s
     r]   )sr\   r   c                s$    ;   s| S  fdd| D S )Nc                s   h | ]}|   qS r   r   )r"   r$   )r\   r   r   r   	<setcomp>  s    z_rot_set.<locals>.<setcomp>r   )r^   r\   r   r   )r\   r   r   _rot_set  s     r`           c                s$  t }tfddD r(dg| S |dkr4S d tfddD rfgdg|d   S t|}|r:|d t| }|dkstt|t||}t|||}t||\}}t |d }x|dk	r	| || }qW 
d |kst|ffdd	t|D t| nt || ||\}}d|d   }	xt|d t |d D ]r}
t |
}x*||
| kr	||  || }qW ||
| kr|||
 ||
|   }||	kr||  }	q|W | kst| 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 )N)r:   r;   )r"   r=   )r9   r   r   r?   0  s    z'iup_contour_optimize.<locals>.<genexpr>Nr   r   c             3   s   | ]} |kV  qd S )Nr   )r"   r   )d0r   r   r?   9  s    r&   c                s    g | ]}|kr | nd qS )Nr   )r"   r#   )r!   solutionr   r   r%   c  s    z(iup_contour_optimize.<locals>.<listcomp>c                s    g | ]}| kr| nd qS )Nr   )r"   r#   )best_solr!   r   r   r%     s    )r   rA   rR   rF   r'   r]   r`   rZ   rC   rG   removerD   )r!   r	   r9   r   rH   r\   rW   rV   r#   rX   r-   rY   r   )rd   rb   r!   rc   r9   r   iup_contour_optimize!  sP    

	





rf   )r!   r	   r2   r9   r   c       	      C   s   t ||kr,t||r"|d d ndd ks0tt|}||d |d |d |d g }g }d}xZ|D ]R}t| ||d  |||d  |}t||| d kst|| |d }qfW |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   r3   r4   r5   )r6   r   r'   rf   r   )	r!   r	   r2   r9   r   r   r-   r0   r7   r   r   r   iup_delta_optimize  s    0 
$
rg   )r   )ra   )ra   )"typingr   r   r   numbersr   r   r   ImportErrorZfontTools.misccompiledZCOMPILEDZ_PointZ_DeltaZ_PointSegmentZ_DeltaSegmentZ_DeltaOrNoneZ_DeltaOrNoneSegmentZ
_EndpointsrU   r    r1   r8   boolrB   rC   rR   rZ   r@   intr]   r`   rf   rg   r   r   r   r   <module>   sN   )'H&d