B
    Sc|  ã               @   s:   d Z ddlmZ ddlmZmZ dgZG dd„ deƒZdS )zUfontTools.pens.pointInsidePen -- Pen implementing "point inside" testing
for shapes.
é    )ÚBasePen)ÚsolveQuadraticÚ
solveCubicÚPointInsidePenc               @   sl   e Zd ZdZddd„Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r   a:  This pen implements "point inside" testing: to test whether
	a given point lies inside the shape (black) or outside (white).
	Instances of this class can be recycled, as long as the
	setTestPoint() method is used to set the new point to test.

	Typical usage:

		pen = PointInsidePen(glyphSet, (100, 200))
		outline.draw(pen)
		isInside = pen.getResult()

	Both the even-odd algorithm and the non-zero-winding-rule
	algorithm are implemented. The latter is the default, specify
	True for the evenOdd argument of __init__ or setTestPoint
	to use the even-odd algorithm.
	Fc             C   s   t  | |¡ |  ||¡ d S )N)r   Ú__init__ÚsetTestPoint)ÚselfZglyphSetÚ	testPointÚevenOdd© r   ú<lib/python3.7/site-packages/fontTools/pens/pointInsidePen.pyr   )   s    zPointInsidePen.__init__c             C   s   || _ || _d| _d| _dS )zASet the point to test. Call this _before_ the outline gets drawn.Nr   )r	   r
   Ú
firstPointÚintersectionCount)r   r	   r
   r   r   r   r   -   s    zPointInsidePen.setTestPointc             C   s   | j d k	r|  ¡  | jS )N)r   Ú	closePathr   )r   r   r   r   Ú
getWinding4   s    
zPointInsidePen.getWindingc             C   s*   |   ¡ }| jr|d }n
| jdk}|  S )z‡After the shape has been drawn, getResult() returns True if the test
		point lies within the (black) shape, and False if it doesn't.
		é   r   )r   r
   r   )r   ZwindingÚresultr   r   r   Ú	getResult;   s
    

zPointInsidePen.getResultc             C   s,   | j s
|r|  jd7  _n|  jd8  _d S )Né   )r
   r   )r   ZgoingUpr   r   r   Ú_addIntersectionF   s    
zPointInsidePen._addIntersectionc             C   s   | j d k	r|  ¡  || _ d S )N)r   r   )r   Úpointr   r   r   Ú_moveToL   s    
zPointInsidePen._moveToc             C   s    | j \}}|  ¡ \}}|\}}||k r2||k r2d S ||k rF||k rFd S ||krZ||krZd S || }|| }	|| |	 }
||
 | }||k rŽd S |  ||k¡ d S )N)r	   Ú_getCurrentPointr   )r   r   ÚxÚyÚx1Úy1Úx2Úy2ÚdxÚdyÚtZixr   r   r   Ú_lineToS   s     
zPointInsidePen._lineToc              C   sP  | j \}}|  ¡ \}}|\}}	|\}
}|\}}||k rR||k rR|
|k rR||k rRd S ||k rv|	|k rv||k rv||k rvd S ||krš|	|krš||krš||kršd S |}|	| d }||	 d | }|| | | }tt||||| ƒƒ}dd„ |D ƒ}|söd S |}|| d }|
| d | }|| | | }||k}d }x|D ]}||krNq:|}|| }|| }d| | d| |  | }|dk }}|dkrÐd| | d|  }|dk}| }|dkrÐ|}|dk }}|| ||  ||  | }||k rúq:|dkr|sF|  |¡ n0|d	kr2|rF|  |¡ n||kr:|  |¡ q:W d S )
Ng      @c             S   s(   g | ] }d |  krdkrn q|‘qS )g       €g      ð?r   )Ú.0r!   r   r   r   ú
<listcomp>z   s    z.PointInsidePen._curveToOne.<locals>.<listcomp>é   r   g        é   )g        g       €g      ð?)r	   r   Úsortedr   r   ) r   Zbcp1Zbcp2r   r   r   r   r   r   r   Úx3Úy3Zx4Zy4r    ZcyZbyZayÚ	solutionsr   ZcxZbxZaxZaboveZlastTr!   Zt2Zt3Ú	directionZincomingGoingUpZoutgoingGoingUpZxtr   r   r   Ú_curveToOneg   sf    
   






zPointInsidePen._curveToOnec             C   sp   | j \}}|  ¡ \}}|\}}|\}	}
|}|| d }|
| | }tt|||| ƒƒ}dd„ |D ƒ}|sld S d S )Ng       @c             S   s(   g | ] }t |  krtkrn q|‘qS r   )ZZERO_MINUS_EPSILONZONE_PLUS_EPSILON)r#   r!   r   r   r   r$   ²   s    z:PointInsidePen._qCurveToOne_unfinished.<locals>.<listcomp>)r	   r   r'   r   )r   Zbcpr   r   r   r   r   r   r   r(   r)   ÚcÚbÚar*   r   r   r   Ú_qCurveToOne_unfinished¦   s    
z&PointInsidePen._qCurveToOne_unfinishedc             C   s$   |   ¡ | jkr|  | j¡ d | _d S )N)r   r   ZlineTo)r   r   r   r   Ú
_closePath·   s    zPointInsidePen._closePathc             C   s   t ‚dS )z,Insideness is not defined for open contours.N)ÚNotImplementedError)r   r   r   r   Ú_endPath¼   s    zPointInsidePen._endPathN)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r"   r,   r0   r1   r3   r   r   r   r   r      s   

?N)r7   ZfontTools.pens.basePenr   ZfontTools.misc.bezierToolsr   r   Ú__all__r   r   r   r   r   Ú<module>   s   