
    jkewK                         d Z ddlZddlmZmZmZmZ ddlmZm	Z	 ddl
mZ g dZ G d d      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Zy)a  
=========
PointPens
=========

Where **SegmentPens** have an intuitive approach to drawing
(if you're familiar with postscript anyway), the **PointPen**
is geared towards accessing all the data in the contours of
the glyph. A PointPen has a very simple interface, it just
steps through all the points in a call from glyph.drawPoints().
This allows the caller to provide more data for each point.
For instance, whether or not a point is smooth, and its name.
    N)AnyOptionalTupleDict)AbstractPenPenError)DecomposedTransform)AbstractPointPenBasePointToSegmentPenPointToSegmentPenSegmentToPointPenGuessSmoothPointPenReverseContourPointPenc                       e Zd ZdZddee   deddfdZddZ	 	 	 	 dde	e
e
f   d	ee   d
edee   dee   deddfdZ	 ddede	e
e
e
e
e
e
f   dee   deddf
dZ	 ddededeee
f   dee   deddfdZy)r
   zBaseclass for all PointPens.N
identifierkwargsreturnc                     t         )zStart a new sub path.NotImplementedErrorselfr   r   s      9/usr/lib/python3/dist-packages/fontTools/pens/pointPen.py	beginPathzAbstractPointPen.beginPath"       !!    c                     t         )zEnd the current sub path.r   r   s    r   endPathzAbstractPointPen.endPath&   r   r   ptsegmentTypesmoothnamec                     t         )z$Add a point to the current sub path.r   r   r    r!   r"   r#   r   r   s          r   addPointzAbstractPointPen.addPoint*   s
     "!r   baseGlyphNametransformationc                     t         )zAdd a sub glyph.r   )r   r'   r(   r   r   s        r   addComponentzAbstractPointPen.addComponent6   s
     "!r   	glyphNamelocationc                     t         )zAdd a VarComponent sub glyph. The 'transformation' argument
        must be a DecomposedTransform from the fontTools.misc.transform module,
        and the 'location' argument must be a dictionary mapping axis tags
        to their locations.
        )AttributeErrorr   r+   r(   r,   r   r   s         r   addVarComponentz AbstractPointPen.addVarComponent@   s
     r   N)r   NNFNN)__name__
__module____qualname____doc__r   strr   r   r   r   floatboolr&   r*   r	   r   r0    r   r   r
   r
      s5   &"HSM "C "D "" &*"$(
"%,
" c]
" 	
"
 sm
" SM
" 
" 

"  %)	"" eUE5%FG" SM	"
 " 
" %) , sEz"	
 SM  
r   r
   c                   4    e Zd ZdZd ZddZd Zd Z	 d	dZy)
r   a/  
    Base class for retrieving the outline in a segment-oriented
    way. The PointPen protocol is simple yet also a little tricky,
    so when you need an outline presented as segments but you have
    as points, do use this base implementation as it properly takes
    care of all the edge cases.
    c                     d | _         y r1   )currentPathr   s    r   __init__zBasePointToSegmentPen.__init__Z   s
    r   Nc                 @    | j                   t        d      g | _         y )NzPath already begun.)r=   r   r   s      r   r   zBasePointToSegmentPen.beginPath]   s"    '011r   c                     t         )a  Override this method.

        It will be called for each non-empty sub path with a list
        of segments: the 'segments' argument.

        The segments list contains tuples of length 2:
                (segmentType, points)

        segmentType is one of "move", "line", "curve" or "qcurve".
        "move" may only occur as the first segment, and it signifies
        an OPEN path. A CLOSED path does NOT start with a "move", in
        fact it will not contain a "move" at ALL.

        The 'points' field in the 2-tuple is a list of point info
        tuples. The list has 1 or more items, a point tuple has
        four items:
                (point, smooth, name, kwargs)
        'point' is an (x, y) coordinate pair.

        For a closed path, the initial moveTo point is defined as
        the last point of the last segment.

        The 'points' list of "move" and "line" segments always contains
        exactly one point tuple.
        r   )r   segmentss     r   _flushContourz#BasePointToSegmentPen._flushContourb   s
    4 "!r   c                    | j                   t        d      | j                   }d | _         |sy t        |      dk(  r'|d   \  }}}}}d||||fgfg}| j                  |       y g }|d   d   dk(  r5|d   \  }}}}}|j	                  d||||fgf       |j                  d       nNd }t        t        |            D ]  }	||	   d   }||	} n ||j	                  d       n||dz   d  |d |dz    z   }g }
|D ]5  \  }}}}}|
j	                  ||||f       |!|j	                  ||
f       g }
7 | j                  |       y )NzPath not begun.   r   move)NqcurveNNN)r=   r   lenrB   appendpoprange)r   pointsr    r!   r"   r#   r   rA   firstOnCurveicurrentSegments              r   r   zBasePointToSegmentPen.endPath~   s   #,--!!v;!4:1I1BVT62vtV"<!=>?Hx(!9Q<6! 5;1I1BVT6OOVr64&@%ABCJJqM
  L3v;' $Qil*#$L	
 # @Aq 0 23f=O|a?O6PP5; 	 1BVT6!!2vtV"<="OO[.9:N	  	8$r   c                 r    | j                   t        d      | j                   j                  |||||f       y NPath not begun)r=   r   rH   r%   s          r   r&   zBasePointToSegmentPen.addPoint   s;     #+,,[&$ GHr   r1   r2   )	r3   r4   r5   r6   r>   r   rB   r   r&   r:   r   r   r   r   Q   s)     
"8.%b IMIr   r   c                   &    e Zd ZdZddZd ZddZy)r   z
    Adapter class that converts the PointPen protocol to the
    (Segment)Pen protocol.

    NOTE: The segment pen does not support and will drop point names, identifiers
    and kwargs.
    c                 J    t         j                  |        || _        || _        y r1   )r   r>   penoutputImpliedClosingLine)r   
segmentPenrU   s      r   r>   zPointToSegmentPen.__init__   s    &&t,(@%r   c           	      "   |st        d      | j                  }|d   d   dk(  r=d}|d   d   }t        |      dk7  rt        dt        |             |d   \  }}}}|d= nd}|d   \  }}|d   \  }}}}|n|j                  |       | j                  }t        |      }	|}
t        |	      D ]  }||   \  }}|D cg c]	  \  }}}}| }}}|d	k(  rPt        |      dk7  rt        d
t        |             |d   }|dz   |	k7  s
|s|r||
k(  sb|j                  |       |}
v|dk(  r |j                  |  |d   }
|dk(  r |j                  |  |d   }
t        d|        |r|j                          y |j                          y c c}}w )NzMust have at least one segment.r   rE   FrD   z"Illegal move segment point count: Tlinez"Illegal line segment point count: curverF   zIllegal segmentType: )r   rT   rG   moveTorU   rJ   lineTocurveToqCurveTo	closePathr   )r   rA   rT   closedrK   movePt_r!   rU   	nSegmentslastPtrM   r    s                r   rB   zPointToSegmentPen._flushContour   s   <==hhA;q>V#Fa[^F6{a!CCK=QRR$QiOFAq! F"*2,K$RjOFAq!> JJv#'#@#@ M	y! 	FA"*1+K,23[RAqb3F3f$v;!#"%GF}#UVVAY EY&/!V|JJrNF'V$(f%!6{mDEE?	F@ MMOKKMC 4s   >FNc                 @    ~~| j                   j                  ||       y r1   )rT   r*   r   r+   	transformr   r   s        r   r*   zPointToSegmentPen.addComponent  s    i3r   )Fr1   )r3   r4   r5   r6   r>   rB   r*   r:   r   r   r   r      s    A
?B4r   r   c                   H    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)r   z]
    Adapter class that converts the (Segment)Pen protocol to the
    PointPen protocol.
    c                 T    |rt        |      | _        d | _        y || _        d | _        y r1   )r   rT   contour)r   pointPenguessSmooths      r   r>   zSegmentToPointPen.__init__  s*    *84DH   DHr   c                     | j                   }|j                          | j                  D ]  \  }}|j                  ||        |j	                          y )N)r!   )rT   r   rj   r&   r   )r   rT   r    r!   s       r   rB   zSegmentToPointPen._flushContour  sD    hh#|| 	6OBLLL5	6r   c                 L    g | _         | j                   j                  |df       y )NrE   )rj   rH   r   r    s     r   r[   zSegmentToPointPen.moveTo  s    RL)r   c                 l    | j                   t        d      | j                   j                  |df       y )N'Contour missing required initial moveTorY   )rj   r   rH   ro   s     r   r\   zSegmentToPointPen.lineTo#  s.    <<DEERL)r   c                     |st        d      | j                  t        d      |d d D ]  }| j                  j                  |d f       ! | j                  j                  |d   df       y )NMust pass in at least one pointrq   rX   rZ   	TypeErrorrj   r   rH   r   ptsr    s      r   r]   zSegmentToPointPen.curveTo(  sm    =>><<DEEcr( 	,BLLT
+	,SWg./r   c                     |st        d      |d   g | _        n| j                  t        d      |d d D ]  }| j                  j                  |d f       ! |d   !| j                  j                  |d   df       y y )Nrs   rX   rq   rF   rt   rv   s      r   r^   zSegmentToPointPen.qCurveTo1  s    =>>r7?DL||#HIIcr( 	,BLLT
+	,r7LLR( 34 r   c                 ~   | j                   t        d      t        | j                         dkD  rO| j                   d   d   | j                   d   d   k(  r*| j                   d   | j                   d<   | j                   d= n(| j                   d   \  }}|dk(  r|df| j                   d<   | j                          d | _         y )Nrq   rD   r   rX   rE   rY   )rj   r   rG   rB   )r   r    tps      r   r_   zSegmentToPointPen.closePath>  s    <<DEEt||q T\\!_Q%74<<;KA;N%N"ll2.DLLOR  \\!_FBV|"$f*Qr   c                 `    | j                   t        d      | j                          d | _         y )Nrq   )rj   r   rB   r   s    r   r   zSegmentToPointPen.endPathM  s+    <<DEEr   c                 j    | j                   t        d      | j                  j                  ||       y )N1Components must be added before or after contours)rj   r   rT   r*   )r   r+   rg   s      r   r*   zSegmentToPointPen.addComponentS  s,    <<#NOOi3r   N)T)r3   r4   r5   r6   r>   rB   r[   r\   r]   r^   r_   r   r*   r:   r   r   r   r     s4    
**
054r   r   c                   H    e Zd ZdZd
dZd ZddZd Z	 ddZddZ		 dd	Z
y)r   z
    Filtering PointPen that tries to determine whether an on-curve point
    should be "smooth", ie. that it's a "tangent" point or a "curve" point.
    c                 .    || _         || _        d | _        y r1   )_outPen_error_points)r   outPenerrors      r   r>   zGuessSmoothPointPen.__init___  s    r   c                    | j                   t        d      | j                   }t        |      }|sy |d   d   dk(  rt        d|dz
        }n|dkD  rt        d|dz
        }ng }|D ]  }||   \  }}}}}	||dz
  }
|dz   }||
   d   	||   d   ,||   d   }||
   d   }||   d   }||k7  sJ||k7  sP|d   |d   z
  |d   |d   z
  }}|d   |d   z
  |d   |d   z
  }}t	        j
                  ||      }t	        j
                  ||      }t        ||z
        | j                  k  s||d||	f||<    |D ](  \  }}}}}	 | j                  j                  ||||fi |	 * y )NrQ   r   rD   rE   rX   T)
r   r   rG   rJ   mathatan2absr   r   r&   )r   rK   nPointsindicesrM   r    r!   rb   r#   r   prevnextprevPtnextPtdx1dy1dx2dy2a1a2r"   s                        r   rB   z!GuessSmoothPointPen._flushContourd  s   <<+,,f+!9Q<6!Aw{+Gq[ B!,G G 	DA/5ay,BQf"q5Dq5Dd|A*vd|A/J1BD\!_FD\!_FV|fa56!9,befQi.?S!!9r!u,fQi"Q%.?SZZS)ZZS)rBw<$++- "KtV CF1I%	D( 6< 	K1BVT6!DLL!!"k64J6J	Kr   Nc                     | j                   t        d      g | _         |||d<    | j                  j                  di | y )NPath already begunr   r:   )r   r   r   r   r   s      r   r   zGuessSmoothPointPen.beginPath  sF    <<#/00!#-F< ((r   c                 f    | j                          | j                  j                          d | _        y r1   )rB   r   r   r   r   s    r   r   zGuessSmoothPointPen.endPath  s%    r   c                     | j                   t        d      |||d<   | j                   j                  ||d||f       y )NrQ   r   F)r   r   rH   r%   s          r   r&   zGuessSmoothPointPen.addPoint  sG     <<+,,!#-F< ReT6BCr   c                 |    | j                   t        d      |||d<    | j                  j                  ||fi | y Nr}   r   )r   r   r   r*   )r   r+   r(   r   r   s        r   r*   z GuessSmoothPointPen.addComponent  sC    <<#NOO!#-F< !!!)^FvFr   c                 ~    | j                   t        d      |||d<    | j                  j                  |||fi | y )Nz4VarComponents must be added before or after contoursr   )r   r   r   r0   r/   s         r   r0   z#GuessSmoothPointPen.addVarComponent  sG     <<#QRR!#-F< $$$YSFSr   )g?r1   r2   )r3   r4   r5   r6   r>   rB   r   r   r&   r*   r0   r:   r   r   r   r   Y  s<    

&KP) IMDG ?CTr   r   c                   <    e Zd ZdZd Zd Zd	dZd Z	 d
dZd	dZ	y)r   a  
    This is a PointPen that passes outline data to another PointPen, but
    reversing the winding direction of all contours. Components are simply
    passed through unchanged.

    Closed contours are reversed in such a way that the first point remains
    the first point.
    c                      || _         d | _        y r1   )rT   currentContour)r   outputPointPens     r   r>   zReverseContourPointPen.__init__  s    !"r   c                 t   | j                   }| j                  }|s-|j                  | j                         |j	                          y |d   d   dk7  }|sd}nT|j                  |j                  d             d }t        t        |            D ]  }||   d   |} n |d }n||   d   }|j                          |s"|d   d   |j                  d       |d   d   |j                  | j                         |D ](  \  }}}	}
}||}|}nd } |j                  |f||	|
d| * |j	                          y )N)r   r   rD   rE   )r!   r"   r#   )rT   r   r   currentContourIdentifierr   rH   rI   rJ   rG   reverser&   )r   rT   rj   r`   lastSegmentTyperL   rM   r    nextSegmentTyper"   r#   r   r!   s                r   rB   z$ReverseContourPointPen._flushContour  se   hh%%MMT%B%BMCKKMA&($O NN7;;q>*L3w<( 1:a=,#$L # #'"),"7": !*Q-'A !*Q-'!>!>?9@ 	5Bv*-"1"CLL +FIO	 	r   Nc                 \    | j                   t        d      g | _         || _        g | _        y )Nr   )r   r   r   onCurver   s      r   r   z ReverseContourPointPen.beginPath  s1    */00 (2%r   c                 `    | j                   t        d      | j                          d | _         y rP   )r   r   rB   r   s    r   r   zReverseContourPointPen.endPath  s.    &+,,"r   c                     | j                   t        d      |||d<   | j                   j                  |||||f       y )NrQ   r   )r   r   rH   r%   s          r   r&   zReverseContourPointPen.addPoint  sK     &+,,!#-F< ""BVT6#JKr   c                 r    | j                   t        d       | j                  j                  ||fd|i| y r   )r   r   rT   r*   rf   s        r   r*   z#ReverseContourPointPen.addComponent
  s:    *NOOiTzTVTr   r1   r2   )
r3   r4   r5   r6   r>   rB   r   r   r&   r*   r:   r   r   r   r     s/    #
0d# IMLUr   r   )r6   r   typingr   r   r   r   fontTools.pens.basePenr   r   fontTools.misc.transformr	   __all__r
   r   r   r   r   r   r:   r   r   <module>r      s     - - 8 8/ /dbI, bIJR4- R4jK4 K4\WT* WTtZU- ZUr   