
    h.                         S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
JrJrJrJrJr  S	S
/rSrSr\R&                  " \5      r\rS r " S S\5      rS rS rSS jrSS jr SS jr       SS jrS rS r g)a  Converts cubic bezier curves to quadratic splines.

Conversion is performed such that the quadratic splines keep the same end-curve
tangents as the original cubics. The approach is iterative, increasing the
number of segments for a spline until the error gets below a bound.

Respective curves from multiple fonts will be converted at once to ensure that
the resulting splines are interpolation-compatible.
    N)AbstractPen)PointToSegmentPen)ReverseContourPen   )curves_to_quadratic)UnequalZipLengthsErrorIncompatibleSegmentNumberErrorIncompatibleSegmentTypesErrorIncompatibleGlyphsErrorIncompatibleFontsErrorfonts_to_quadraticfont_to_quadraticgMbP?z&com.github.googlei18n.cu2qu.curve_typec                  r    [        [        S U  5       5      5      S:w  a  [        U 6 e[        [	        U 6 5      $ )zqEnsure each argument to zip has the same length. Also make sure a list is
returned for python 2/3 compatibility.
c              3   8   #    U  H  n[        U5      v   M     g 7fNlen).0as     E/var/www/html/env/lib/python3.13/site-packages/fontTools/cu2qu/ufo.py	<genexpr>zip.<locals>.<genexpr><   s     $t!s1vvts   r   )r   setr   list_zip)argss    r   zipr   7   s6    
 3$t$$%*$d++d    c                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)GetSegmentsPenA   zPen to collect segments into lists of points for conversion.

Curves always include their initial on-curve point, so some points are
duplicated between segments.
c                      S U l         / U l        g r   _last_ptsegmentsselfs    r   __init__GetSegmentsPen.__init__H   s    r   c                 \    US;   a
  US   U l         U R                  R                  X45        g )N)movelineqcurvecurve)r$   r%   append)r'   tagr   s      r   _add_segmentGetSegmentsPen._add_segmentL   s*    55 HDMc[)r   c                 (    U R                  SU5        g )Nr+   r2   r'   pts     r   moveToGetSegmentsPen.moveToQ       &"%r   c                 (    U R                  SU5        g )Nr,   r5   r6   s     r   lineToGetSegmentsPen.lineToT   r:   r   c                 @    U R                   " SU R                  /UQ76   g )Nr-   r2   r$   r'   pointss     r   qCurveToGetSegmentsPen.qCurveToW   s    (DMM;F;r   c                 @    U R                   " SU R                  /UQ76   g )Nr.   r?   r@   s     r   curveToGetSegmentsPen.curveToZ   s    '4==:6:r   c                 &    U R                  S5        g )Ncloser5   r&   s    r   	closePathGetSegmentsPen.closePath]   s    '"r   c                 &    U R                  S5        g )Nendr5   r&   s    r   endPathGetSegmentsPen.endPath`   s    % r   c                     g r    )r'   	glyphNametransformations      r   addComponentGetSegmentsPen.addComponentc   s    r   r#   N)__name__
__module____qualname____firstlineno____doc__r(   r2   r8   r<   rB   rE   rI   rM   rS   __static_attributes__rP   r   r   r    r    A   s4    *
&&<;#!r   r    c                 d    [        5       n[        USS9nU R                  U5        UR                  $ )z6Get a glyph's segments as extracted by GetSegmentsPen.T)outputImpliedClosingLine)r    r   
drawPointsr%   )glyphpenpointPens      r   _get_segmentsra   g   s2     
C !tDH	X<<r   c                    U R                  5         U R                  5       nU(       a  [        U5      nU H  u  pEUS:X  a  UR                  " U6   M  US:X  a  UR                  " U6   M3  US:X  a  UR
                  " USS 6   MM  US:X  a  UR                  " USS 6   Mg  US:X  a  UR                  5         M  US:X  a  UR                  5         M  [        S	U-  5      e   g)
z=Draw segments as extracted by GetSegmentsPen back to a glyph.r+   r,   r.   r   Nr-   rH   rL   zUnhandled segment type "%s")
clearContoursgetPenr   r8   r<   rE   rB   rI   rM   AssertionError)r^   r%   reverse_directionr_   r1   r   s         r   _set_segmentsrg   z   s     

,,.C$	&=JJF]JJG^KKab"H_LL$qr(#G^MMOE\KKM !>!DEE r   c                   ^ [        S U  5       5      (       d   S5       e[        U  Vs/ s H  oDS   PM	     snX5      n[        US   5      m[        U4S jUSS  5       5      (       d   S5       e[        TS-
  5      nUR	                  US5      S-   X&'   U(       d  TS	:X  a  U Vs/ s H  nS
U4PM	     sn$ U Vs/ s H  nSU4PM	     sn$ s  snf s  snf s  snf )z2Return quadratic approximations of cubic segments.c              3   0   #    U  H  oS    S:H  v   M     g7f)r   r.   NrP   )r   ss     r   r   )_segments_to_quadratic.<locals>.<genexpr>   s     11tws   zNon-cubic given to convertr   r   c              3   @   >#    U  H  n[        U5      T:H  v   M     g 7fr   r   )r   rj   ns     r   r   rk      s     3Nqs1v{Ns   NzConverted incompatibly      r-   r.   )allr   r   strget)	r%   max_errstatsall_quadraticrj   
new_pointsspline_lengthprm   s	           @r   _segments_to_quadraticry      s     1111O3OO1$H%=HqdH%=wVJJqMA3JqrN333M5MM3AJM 99]A6:EQ'12z!1z22&01j!j11 &> 31s   C'C<Cc                 H  ^  [        U  Vs/ s H  n[        U5      PM     sn6 n[	        U5      (       d  gUn/ n0 n	[        U5       Hu  u  pUS   S   m[        U4S jUSS  5       5      (       d  U Vs/ s H  oS   PM	     snX'   O"TS:X  a  [        XX45      nU(       d  X:w  a  SnUnUR                  U5        Mw     U(       a(  [        U6 n[        X5       H  u  p[        XU5        M     U	(       a	  [        X	S9eU$ s  snf ! [         a    [        U 5      ef = fs  snf )	zDo the actual conversion of a set of compatible glyphs, after arguments
have been set up.

Return True if the glyphs were modified, else return False.
Fr   c              3   2   >#    U  H  oS    T:H  v   M     g7f)r   NrP   )r   rj   r1   s     r   r   '_glyphs_to_quadratic.<locals>.<genexpr>   s     51Q43;s   r   Nr.   T)r%   )r   ra   r   r	   any	enumeraterp   ry   r0   rg   r
   )glyphsrs   rf   rt   ru   gsegments_by_locationglyphs_modifiednew_segments_by_locationincompatibleir%   rj   new_segmentsnew_segments_by_glyphr^   r1   s                   @r   _glyphs_to_quadraticr      s9   5"v$Fv!]1%5v$FG #$$ (O!L !56qk!n5555-56XtX6LOG^15L  8"&#H ''1 7  #%= >#&v#EE%/@A $F +FJJ? %G! 5,V445 7s!   	D DD 5DD Dc                     Uc  0 nU(       d	  [         S-  n[        U[        [        45      (       a  UnOU/[	        U 5      -  n[	        U5      [	        U 5      :X  d   e[        XX#U5      $ )a  Convert the curves of a set of compatible of glyphs to quadratic.

All curves will be converted to quadratic at once, ensuring interpolation
compatibility. If this is not required, calling glyphs_to_quadratic with one
glyph at a time may yield slightly more optimized results.

Return True if glyphs were modified, else return False.

Raises IncompatibleGlyphsError if glyphs have non-interpolatable outlines.
i  )DEFAULT_MAX_ERR
isinstancer   tupler   r   )r   rs   rf   rt   ru   
max_errorss         r   glyphs_to_quadraticr      sl     }!D('D%=))
YV,
z?c&k)))-m r   c                   ^ U(       a  U  Vs1 s H"  oR                   R                  [        S5      iM$     n	n[        U	5      S:X  aB  [	        [        U	5      5      n
U
S;   a  [        R                  S5        gU
S:X  a  O/[        U
5      e[        U	5      S:  a  [        R                  S5        Tc  0 mU(       a  U(       a  [        S5      eU(       d  U(       d  [        n[        U[        [        45      (       a  [        U5      [        U 5      :X  d   eUnOU(       a  U/[        U 5      -  n[        U[        [        45      (       aQ  [        U 5      [        U5      :X  d   e[        X5       VVs/ s H  u  pUR                  R                   U-  PM      nnnO/U(       a(  U  Vs/ s H  oR                  R                   U-  PM     nn[#        5       n0 n[#        5       R$                  " S	 U  5       6  Hr  n/ n/ n[        U W5       H3  u  nnUU;   d  M  UR'                  UU   5        UR'                  U5        M5      [)        UUUTU5      (       a  UR+                  U5        Mr  Mt     U(       a  [1        U5      eU(       aQ  U(       aJ  [3        TR5                  5       5      n[        R                  S
SR7                  U4S jU 5       5      -  5        U(       aO  U  HI  nUR                   R                  [        S5      n
U(       a  SOSnU
U:w  d  M6  UUR                   [        '   MK     U$ s  snf s  snnf s  snf ! [,         a%  n[        R/                  U5        UX'    SnAGMr  SnAff = f)a  Convert the curves of a collection of fonts to quadratic.

All curves will be converted to quadratic at once, ensuring interpolation
compatibility. If this is not required, calling fonts_to_quadratic with one
font at a time may yield slightly more optimized results.

Return the set of modified glyph names if any, else return an empty set.

By default, cu2qu stores the curve type in the fonts' lib, under a private
key "com.github.googlei18n.cu2qu.curve_type", and will not try to convert
them again if the curve type is already set to "quadratic".
Setting 'remember_curve_type' to False disables this optimization.

Raises IncompatibleFontsError if same-named glyphs from different fonts
have non-interpolatable outlines.
cubicr   )	quadraticmixedz%Curves already converted to quadraticFz'fonts may contain different curve typesNz4Only one of max_err and max_err_em can be specified.c              3   @   #    U  H  oR                  5       v   M     g 7fr   )keys)r   fs     r   r   %fonts_to_quadratic.<locals>.<genexpr>-  s     61ffhhs   zNew spline lengths: %sz, c              3   8   >#    U  H  nS UTU   4-  v   M     g7f)z%s: %dNrP   )r   lrt   s     r   r   r   D  s     J>aQaM1>s   r   r   )librr   CURVE_TYPE_LIB_KEYr   nextiterloggerinfoNotImplementedErrorwarning	TypeErrorr   r   r   r   r   
unitsPerEmr   unionr0   r   addr   errorr   sortedr   join)fonts
max_err_emrs   rf   rt   
dump_statsremember_curve_typeru   r   curve_types
curve_typer   emodifiedglyph_errorsnamer   cur_max_errorsfontr   excspline_lengthsnew_curve_types       `                  r   r   r      s   6 GLMu!uuyy!3W=uM{q d;/0J33CDw&)*55!NNDE}gNOO'$
'D%=))7|s5z)))
	YU+
*tUm,,5zS_,,,8;E8NO8Naff''!+8N
O
	>CDeff''*4e
DuHL667uj1KD%t|d4j)%%e, 2	%#(95-  T" 8  $\22J

-$yyJ>JJL	

 D&8'BJ,9[wN^+/=+,	 
 OI N< PD  ' 	%LL!$L	%s)   )L*0%L/$"L5=%L::
M)M$$M)c                     [        U /40 UD6$ )z~Convenience wrapper around glyphs_to_quadratic, for just one glyph.
Return True if the glyph was modified, else return False.
)r   )r^   kwargss     r   glyph_to_quadraticr   P  s    
 w1&11r   c                     [        U /40 UD6$ )zConvenience wrapper around fonts_to_quadratic, for just one font.
Return the set of modified glyph names if any, else return empty set.
)r   )r   r   s     r   r   r   X  s    
 tf///r   )T)NFNT)NNFNFTT)!rY   loggingfontTools.pens.basePenr   fontTools.pens.pointPenr    fontTools.pens.reverseContourPenr    r   errorsr   r	   r
   r   r   __all__r   r   	getLoggerrU   r   r   r   r    ra   rg   ry   r   r   r   r   r   rP   r   r   <module>r      s      . 5 > !   !4
5
 = 			8	$ #[ #L&F02$'V NRB 
`F20r   