B
    Sc-                 @   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outZx1Zx2d1d2nZscaleZpairxd r   3lib/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startZi1Zi2Zri1Zri2endr   r   r   iup_contourQ   s2    
.
..&r,   )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$   r,   r   )r   r	   r-   r   r   r*   r+   contourr   r   r   	iup_deltay   s    	0 

r3   )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.r0   r   c             3   s4   | ],\\}}\}}t t|| ||  kV  qd S )N)abscomplex)r   r   ypq)r4   r   r   	<genexpr>   s    z%can_iup_in_between.<locals>.<genexpr>)r$   listr   r   allr   )r   r	   r    r   r4   Zinterpr   )r4   r   can_iup_in_between   s
    	0r=   )r   r	   r4   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ranger5   minmaxadd)r   r	   r4   r   forcedr    ZldZlcr   cZndZncr   ZcjZdjZlcjZldjZncjZndjZc1Zc2r   r   Zforcer   r   r   _iup_contour_bound_forced_set   sH    
"



 .

66

rE   )r   r	   r4   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   r0   )r   r@   MAX_LOOKBACKr?   rA   r=   )r   r	   rC   r4   rF   r   costschainr    	best_costr   costr   r   r   _iup_contour_optimize_dp   s(    
 
rM   )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   )rN   rO   r   r   r   r   	_rot_list  s
     rP   )srO   r   c                s$    ;   s| S  fdd| D S )Nc                s   h | ]}|   qS r   r   )r   r!   )rO   r   r   r   	<setcomp>  s    z_rot_set.<locals>.<setcomp>r   )rQ   rO   r   r   )rO   r   r   _rot_set  s     rS           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)r5   r6   )r   r8   )r4   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   r<   rE   rA   r$   rP   rS   rM   r>   rB   remover?   )r   r	   r4   r   rC   rO   rJ   rI   r    rK   r*   rL   r   )rW   rU   r   rV   r4   r   iup_contour_optimize!  sP    

	





rY   )r   r	   r-   r4   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   r.   r/   r0   )r1   r   r$   rY   r   )	r   r	   r-   r4   r   r   r*   r+   r2   r   r   r   iup_delta_optimize  s    0 
$
rZ   )r   )rT   )rT   )"typingr   r   r   Znumbersr   r   r   ImportErrorZfontTools.miscZcompiledZCOMPILEDZ_PointZ_DeltaZ_PointSegmentZ_DeltaSegmentZ_DeltaOrNoneZ_DeltaOrNoneSegmentZ
_EndpointsrH   r   r,   r3   boolr=   r>   rE   rM   r;   intrP   rS   rY   rZ   r   r   r   r   <module>   sN   )'H&d