
    jke                     @    d Z ddlmZ ddlmZmZ dgZ G d de      Zy)zUfontTools.pens.pointInsidePen -- Pen implementing "point inside" testing
for shapes.
    )BasePen)solveQuadratic
solveCubicPointInsidePenc                   V    e Zd ZdZddZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)r   as  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.
    c                 T    t        j                  | |       | j                  ||       y N)r   __init__setTestPoint)selfglyphSet	testPointevenOdds       ?/usr/lib/python3/dist-packages/fontTools/pens/pointInsidePen.pyr
   zPointInsidePen.__init__)   s"    x()W-    c                 <    || _         || _        d| _        d| _        y)zASet the point to test. Call this _before_ the outline gets drawn.Nr   )r   r   
firstPointintersectionCount)r   r   r   s      r   r   zPointInsidePen.setTestPoint-   s    "!"r   c                 R    | j                   | j                          | j                  S r	   )r   	closePathr   r   s    r   
getWindingzPointInsidePen.getWinding4   s$    ??& NN%%%r   c                 r    | j                         }| j                  r	|dz  }|  S | j                  dk7  }|  S )zAfter 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   windingresults      r   	getResultzPointInsidePen.getResult;   sH     //#<<q[F :~ ++q0F:~r   c                 v    | j                   s|r| xj                  dz  c_        y | xj                  dz  c_        y )N   )r   r   )r   goingUps     r   _addIntersectionzPointInsidePen._addIntersectionF   s-    <<7""a'"""a'"r   c                 J    | j                   | j                          || _         y r	   )r   r   )r   points     r   _moveTozPointInsidePen._moveToL   s    ??& NNr   c                     | j                   \  }}| j                         \  }}|\  }}||k  r||k  ry ||k  r||k  ry ||k\  r||k\  ry ||z
  }||z
  }	||z
  |	z  }
||
z  |z   }||k  ry | j                  ||kD         y r	   )r   _getCurrentPointr!   )r   r#   xyx1y1x2y2dxdytixs               r   _lineTozPointInsidePen._lineToS   s    ~~1&&(BB6b1f6b1f7rQw"W"WVrM!Vb[6b2g&r   c           	      t   | j                   \  }}| j                         \  }}|\  }}	|\  }
}|\  }}||k  r||k  r|
|k  r||k  ry ||k  r|	|k  r||k  r||k  ry ||k\  r|	|k\  r||k\  r||k\  ry |}|	|z
  dz  }||	z
  dz  |z
  }||z
  |z
  |z
  }t        t        |||||z
              }|D cg c]  }d|cxk  rdk  sn n| }}|sy |}||z
  dz  }|
|z
  dz  |z
  }||z
  |z
  |z
  }||k\  }d }|D ]  }||k(  r	|}||z  }||z  }d|z  |z  d|z  |z  z   |z   }|dkD  x}}|dk(  r$d|z  |z  d|z  z   }|dkD  }| }|dk(  r	|}|dkD  x}}||z  ||z  z   ||z  z   |z   }||k  rs|dv r|rz| j	                  |       |dk(  r|s| j	                  |       ||k(  s| j	                  |        y c c}w )	Ng      @       g      ?   r              )r5   r3   )r   r&   sortedr   r!   ) r   bcp1bcp2r#   r'   r(   r)   r*   r+   r,   x3y3x4y4r.   cybyay	solutionsr/   r-   cxbxaxabovelastTt2t3	directionincomingGoingUpoutgoingGoingUpxts                                    r   _curveToOnezPointInsidePen._curveToOneg   se   ~~1&&(BBBB6b1faBF6b1faBF7rQw27rQw2g_2g_r!"Wr\B:b"b"q&9:	 )>1TQ-=#-=Q>	>2g_2g_r!"Wr\Ba 	;AEzEQBaBBa"fqj025I09C?OoCFQJR/	"+c/&5"5# "I8ACGOob27"R!V+b0BAvK&))/:c"))/:"o5))/:;	; ?s   /F5 F5c                    | j                   \  }}| j                         \  }}|\  }}|\  }	}
|}||z
  dz  }|
|z
  |z
  }t        t        ||||z
              }|D cg c]  }t        |cxk  r
t
        k  sn n| }}|sy y c c}w )Ng       @)r   r&   r7   r   ZERO_MINUS_EPSILONONE_PLUS_EPSILON)r   bcpr#   r'   r(   r)   r*   r+   r,   r:   r;   cbarA   r/   s                   r   _qCurveToOne_unfinishedz&PointInsidePen._qCurveToOne_unfinished   s     ~~1&&(BBB!VsNFQJ>!QA67	 
$6!$O?O$OA
	 
  
s   B5Bc                     | j                         | j                  k7  r| j                  | j                         d | _        y r	   )r&   r   lineTor   s    r   
_closePathzPointInsidePen._closePath   s.      "doo5KK(r   c                     t         )z,Insideness is not defined for open contours.)NotImplementedErrorr   s    r   _endPathzPointInsidePen._endPath   s    !!r   N)F)__name__
__module____qualname____doc__r
   r   r   r   r!   r$   r1   rM   rU   rX   r[    r   r   r   r      s?    6.#&	( '(;;~&
"r   N)r_   fontTools.pens.basePenr   fontTools.misc.bezierToolsr   r   __all__r   r`   r   r   <module>rd      s*    + A 
t"W t"r   