
    jke2                         d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dl
mZ  G d d	e      Z G d
 de      Z G d d      Zy)    N)curve_to_quadraticcurves_to_quadratic)decomposeSuperBezierSegment)	FilterPen)ReverseContourPen)BasePointToSegmentPen)ReverseContourPointPenc                   :     e Zd ZdZ	 	 	 d fd	Z fdZd Z xZS )Cu2QuPena	  A filter pen to convert cubic bezier curves to quadratic b-splines
    using the FontTools SegmentPen protocol.

    Args:

        other_pen: another SegmentPen used to draw the transformed outline.
        max_err: maximum approximation error in font units. For optimal results,
            if you know the UPEM of the font, we recommend setting this to a
            value equal, or close to UPEM / 1000.
        reverse_direction: flip the contours' direction but keep starting point.
        stats: a dictionary counting the point numbers of quadratic segments.
        all_quadratic: if True (default), only quadratic b-splines are generated.
            if False, quadratic curves or cubic curves are generated depending
            on which one is more economical.
    c                 h    |rt        |      }t        | 	  |       || _        || _        || _        y N)r   super__init__max_errstatsall_quadratic)self	other_penr   reverse_directionr   r   	__class__s         9/usr/lib/python3/dist-packages/fontTools/pens/cu2quPen.pyr   zCu2QuPen.__init__)   s5     ))4I#
*    c                    | j                   |||f}t        || j                  | j                        }| j                  Ct        t        |      dz
        }| j                  j                  |d      dz   | j                  |<   | j                  r | j                  |dd    y t        |      dk(  r | j                  |dd    y t        |      dk(  sJ t        | (  |dd    y )N   r            )
current_ptr   r   r   r   strlengetqCurveTor   curveTo)r   pt1pt2pt3curveresultnr   s          r   _convert_curvezCu2QuPen._convert_curve8   s    #sC0#E4<<9K9KL::!CK!O$A JJNN1a014DJJqMDMM6!":&6{avabz*6{a''',r   c                     t        |      }|dk(  r | j                  |  y |dkD  r t        |      D ]  } | j                  |   y  | j                  |  y )Nr   )r    r*   r   r"   )r   pointsr)   segments       r   r#   zCu2QuPen.curveToG   s]    K6D(U6v> .###W-. DMM6"r   FNT)__name__
__module____qualname____doc__r   r*   r#   __classcell__)r   s   @r   r   r      s"    (  +-	#r   r   c                       e Zd ZdZdej
                  fdej
                  fdej                  fdej
                  fdZ	 	 	 ddZd Z	d	 Z
d
 Zd Zy)Cu2QuPointPena  A filter pen to convert cubic bezier curves to quadratic b-splines
    using the FontTools PointPen protocol.

    Args:
        other_point_pen: another PointPen used to draw the transformed outline.
        max_err: maximum approximation error in font units. For optimal results,
            if you know the UPEM of the font, we recommend setting this to a
            value equal, or close to UPEM / 1000.
        reverse_direction: reverse the winding direction of all contours.
        stats: a dictionary counting the point numbers of quadratic segments.
        all_quadratic: if True (default), only quadratic b-splines are generated.
            if False, quadratic curves or cubic curves are generated depending
            on which one is more economical.
    r   r   r   )movelineqcurver'   Nc                     t        j                  |        |rt        |      | _        n|| _        || _        || _        || _        y r   )r   r   r	   penr   r   r   )r   other_point_penr   r   r   r   s         r   r   zCu2QuPointPen.__init__j   s>     	&&t,-o>DH&DH
*r   c           	      :   t        |      dk\  sJ |d   d   dk7  }g }|d   d   }|d   d   }|D ]E  \  }}|dk(  r| j                  |      D ]  }|d   \  }	}
}}|d   d   |d   d   }}||||	g}t        || j                  | j                        }| j
                  Ct        t        |      dz
        }| j
                  j                  |d      dz   | j
                  |<   |dd D cg c]  }|dd i f
 }}|j                  |	|
||f       | j                  st        |      dk(  r|j                  d|g       n|j                  d|g       |d   d   }
 +|j                  ||g       |d   d   }H |r|dd  |d d z   }| j                  |       y c c}w )	Nr   r   r6   r'   r   Fr8   )
r    _split_super_bezier_segmentsr   r   r   r   r   r!   append_drawPoints)r   segmentsclosednew_segmentsprev_pointsprev_on_curvesegment_typer,   
sub_pointson_curvesmoothnamekwargsbcp1bcp2cubicquadr)   pt
new_pointss                       r   _flushContourzCu2QuPointPen._flushContour{   s   8}!!!!Q6)rl1o#B*$, 	. L&w&"&"C"CF"K 6J5?^2HfdF!+Aq!1:a=3C$D*D$AE-eT\\4CUCUVDzz-D	A.(,

q!(<q(@

1BFq*!MB2udB"7!MJ!M%%xv&FG))S_-A$++Xz,BC$++Wj,AB$.rN1$5M6  ##\6$:; &r
1'	.(  (,|CR/@@L& "Ns   )Fc                    g }t        |      dz
  }|dk(  r|j                  |       |S |dkD  r|d   \  }}}}|dz
  }t        t        |D 	
cg c]	  \  }	}
}
}
|	 c}
}	            D ]m  \  }}g }|d d D ]  }|j                  |dd i f        ||dz
  k(  r|j                  ||||f       n|j                  |d   dd i f       |j                  |       o |S t	        d|z        c c}
}	w )Nr   r   r=   FTz$expected 2 control points, found: %d)r    r?   	enumerater   AssertionError)r   r,   sub_segmentsr)   rH   rI   rJ   rK   num_sub_segmentsrP   _irG   new_segmentpoints                  r   r>   z*Cu2QuPointPen._split_super_bezier_segments   s/   K!O6'( ' U-3BZ*HfdF 1u!*+6,JKB1aR,JK" 1: !'_ AE&&udB'?@A)A-.&&&$'GH  &&
2dB'GH##K01  !!G!!KLL -Ks   C&c           
      $   | j                   }|j                          g }| j                  }t        |      D ]  \  }\  }}||v r||   \  }}	 |	t	        |      |      sJ d|d| dt	        |              |d d }
|dk(  r|
}n"|
D ]  \  }}}} |j
                  |d ||fi |  |d   \  }}}}||dk(  sJ  |j
                  ||||fi | t        d|z         |D ]  \  }}}} |j
                  |d ||fi |  |j                          y )Nzillegal z segment point count: expected z, got r=   r   r8   zunexpected segment type: %r)r:   	beginPath_Cu2QuPointPen__points_requiredrT   r    addPointrU   endPath)r   rA   r:   last_offcurvespoints_requiredrY   rF   r,   r)   op	offcurvesrP   rI   rJ   rK   s                  r   r@   zCu2QuPointPen._drawPoints   st   hh00)28)< 	S%A%f.'52#f+q) |. /  !s&V7) #3BK	6 &/N4= G0FD&$RvtFvFG+1":(FD&:'8333  CLL\64J6J$%B\%QRR3	S4 )7 	;$BfCLLT64:6:	;r   c                 X    | j                   J | j                  j                  ||       y r   )currentPathr:   addComponent)r   baseGlyphNametransformations      r   rg   zCu2QuPointPen.addComponent   s(    '''m^<r   r.   )r/   r0   r1   r2   operatoreqger^   r   rR   r>   r@   rg    r   r   r5   r5   S   sg      HKK HKK hkk"X[[!	  +"'B8!F=r   r5   c                   Z    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d Zd Zd Zy)Cu2QuMultiPena  A filter multi-pen to convert cubic bezier curves to quadratic b-splines
    in a interpolation-compatible manner, using the FontTools SegmentPen protocol.

    Args:

        other_pens: list of SegmentPens used to draw the transformed outlines.
        max_err: maximum approximation error in font units. For optimal results,
            if you know the UPEM of the font, we recommend setting this to a
            value equal, or close to UPEM / 1000.
        reverse_direction: flip the contours' direction but keep starting point.

    This pen does not follow the normal SegmentPen protocol. Instead, its
    moveTo/lineTo/qCurveTo/curveTo methods take a list of tuples that are
    arguments that would normally be passed to a SegmentPen, one item for
    each of the pens in other_pens.
    c                 ~    |r|D cg c]  }t        |d       }}|| _        || _        d | _        d | _        y c c}w )NT)outputImpliedClosingLine)r   pensr   	start_ptscurrent_pts)r   
other_pensr   r   r:   s        r   r   zCu2QuMultiPen.__init__   sO     & "#EJ  	s   :c                 2    | j                   t        d      y )NzmoveTo is requiredrt   rU   r   s    r   _check_contour_is_openz$Cu2QuMultiPen._check_contour_is_open   s    # !566 $r   c                 2    | j                   t        d      y )Nz closePath or endPath is requiredrw   rx   s    r   _check_contour_is_closedz&Cu2QuMultiPen._check_contour_is_closed  s    ' !CDD (r   c                     | j                   ?t        | j                   | j                        D ]  \  }} |j                  |   d | _         y y r   )rs   ziprr   moveTo)r   rP   r:   s      r   _add_moveTozCu2QuMultiPen._add_moveTo  sG    >>%t~~tyy9  C

B !DN &r   c                 `    | j                          |x| _        | _        | j                          y r   )r{   rs   rt   r   )r   ptss     r   r~   zCu2QuMultiPen.moveTo  s)    %%',//)r   c                     | j                          | j                          t        || j                        D ]  \  }} |j                  |   || _        y r   )ry   r   r}   rr   lineTort   )r   r   rP   r:   s       r   r   zCu2QuMultiPen.lineTo  sM    ##%3		* 	GBCJJO	r   c                 N   | j                          t        |d         dk(  r$| j                  |D cg c]  }|d   f
 c}       y | j                          g }t	        || j
                        D ])  \  }} |j                  |  |j                  |d   f       + || _        y c c}w )Nr   r   r=   )	ry   r    r   r   r}   rr   r"   r?   rt   )r   
pointsListr,   rt   r:   s        r   r"   zCu2QuMultiPen.qCurveTo  s    ##%z!}"KKJ?&&)?@z4995 	.KFCCLL&!r
}-	. ' @s   B"c                    g }t        | j                  |      D ]  \  }}|j                  ||z           t        || j                  gt        |      z        }g }|D ]  }|j                  |dd          | j                  |       y )Nr   )r}   rt   r?   r   r   r    r"   )r   r   curvesr   r,   
quadratics	quadratics          r   _curves_to_quadraticz"Cu2QuMultiPen._curves_to_quadratic&  s    "%d&6&6
"C 	/JMM*v-.	/($,,#f+1MN

# 	-Iim,	-j!r   c                 F    | j                          | j                  |       y r   )ry   r   )r   r   s     r   r#   zCu2QuMultiPen.curveTo0  s    ##%!!*-r   c                     | j                          | j                  !| j                  D ]  }|j                           d x| _        | _        y r   )ry   rs   rr   	closePathrt   r   r:   s     r   r   zCu2QuMultiPen.closePath4  sC    ##%>>!yy   ,004>r   c                     | j                          | j                  !| j                  D ]  }|j                           d x| _        | _        y r   )ry   rs   rr   r`   rt   r   s     r   r`   zCu2QuMultiPen.endPath;  sC    ##%>>!yy ,004>r   c                     | j                          t        || j                        D ]  \  }}|j                  ||        y r   )r{   r}   rr   rg   )r   	glyphNametransformationstransr:   s        r   rg   zCu2QuMultiPen.addComponentB  s<    %%'otyy9 	/JE3Y.	/r   N)F)r/   r0   r1   r2   r   ry   r{   r   r~   r   r"   r   r#   r   r`   rg   rm   r   r   ro   ro      sD    (	 7E"

'".11/r   ro   )rj   fontTools.cu2qur   r   fontTools.pens.basePenr   fontTools.pens.filterPenr    fontTools.pens.reverseContourPenr   fontTools.pens.pointPenr   r	   r   r5   ro   rm   r   r   <module>r      sE     C > . > 9 :8#y 8#vJ=) J=Ze/ e/r   