
    jke0                        	 d dl Z e j                  Zd dlmZ d dlm	Z	 d dl
Z
d dlmZmZmZ dgZe j                    e j"                  e j$                         e j&                  e j(                  e j*                  e j*                  e j*                  e j*                         e j&                  e j*                  e j*                  	      d
                             Z e j&                  e j*                  e j*                  e j*                  e j*                        d        Ze j                    e j&                  e j$                  e j$                  e j$                  e j(                  e j(                  e j(                  e j(                  e j*                  e j*                  e j*                  e j*                        d               Z e j&                  e j$                  e j$                  e j$                  e j*                  e j*                  e j*                        d        Zeeeef   ef   Z e j&                  e j$                  e j$                        	 	 d1deee      dededeeedf      fd       Z e	dg d      Z e j&                  d2i de j$                  de j$                  de j$                  de j$                  de j$                  de j$                  d e j$                  d!e j(                  d"e j(                  d#e j(                  d$e j(                  d%e j(                  de j$                  d&e j$                  d'e j$                  d(e j*                  d)e j*                  d*e j*                  d+e j*                  d,e j*                  d-e j*                  d1d.       Zd/ Z e!d0k(  r e         yy# eef$ r d dlm Z  dZY w xY w)3    N)cythonF)splitCubicAtTC)
namedtuple)ListTupleUnionquadratic_to_curves)	tolerancep0p1p2p3)midderiv3c                     t        |      |k  rt        |      |k  ry| d||z   z  z   |z   dz  }t        |      |kD  ry||z   |z
  | z
  dz  }t        | | |z   dz  ||z
  ||      xr t        |||z   ||z   dz  ||      S )a  Check if a cubic Bezier lies within a given distance of the origin.

    "Origin" means *the* origin (0,0), not the start of the curve. Note that no
    checks are made on the start and end positions of the curve; this function
    only checks the inside of the curve.

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.
        tolerance (double): Distance from origin.

    Returns:
        bool: True if the cubic Bezier ``p`` entirely lies within a distance
        ``tolerance`` of the origin, False otherwise.
    T   g      ?F      ?)abscubic_farthest_fit_inside)r   r   r   r   r
   r   r   s          7/usr/lib/python3/dist-packages/fontTools/qu2cu/qu2cu.pyr   r   +   s    : 2w)B9 4 R"W"e
+C
3x)2glR5(F$
R"WOS6\3	 W
#CvR3I
VW    r   r   r   p1_2_3c                 0    |dz  }| | dz  |z   |dz  |z   |fS )zAGiven a quadratic bezier curve, return its degree-elevated cubic.gUUUUUU?gUUUUUU? r   s       r   elevate_quadraticr   U   s5     5\F
	u		u	
	 r   )startnk
prod_ratio	sum_ratioratiotr   r   r   r   c                    d}d}dg}t        d|      D ]f  }| ||z      }| ||z   dz
     }|d   |d   k(  sJ t        |d   |d   z
        t        |d   |d   z
        z  }	||	z  }||z  }|j                  |       h |dd D 
cg c]  }
|
|z  	 }}
| |   d   }| |   d   }| ||z   dz
     d   }| ||z   dz
     d   }|||z
  |r|d   ndz  z   }|||z
  |rd|d   z
  ndz  z   }||||f}||fS c c}
w )zGive a cubic-Bezier spline, reconstruct one cubic-Bezier
    that has the same endpoints and tangents and approxmates
    the spline.g      ?   r   r      N)ranger   append)curvesr   r   r    r!   tsr   ckc_beforer"   r#   r   r   r   r   curves                   r   merge_curvesr/   h   sq   ( JI
B1a[ 
EAI%!)a-( !u###BqEBqEM"S!x{)B%CCe
Z	
		)
 "$CR	)A!i-	)B	)	q	B	q	B		A	q	!B		A	q	!B 
rBwB2a5A.	.B	rBw2A2J15	5BRE"9 
*s   C/)countnum_offcurvesioff1off2onc                     t        |       }d}t        |       dz
  }t        d|      D ]7  }| |   }| |dz      }|||z
  dz  z   }|j                  |dz   |z   |       |dz  }9 |S )Nr   r&   r%   r   )listlenr(   insert)pqr0   r1   r2   r3   r4   r5   s           r   add_implicit_on_curvesr<      s     	QAEFQJM1m$ tQxTD[C''	Q#
 Hr   )cost
is_complexquadsmax_err	all_cubicreturn.c                 n   t        | d   d         t        u }|s0| D cg c]!  }|D cg c]  \  }}t        ||       c}}# } }}}| d   d   g}dg}d}	| D ]  }|d   |d   k(  sJ t        t        |      dz
        D ])  }
|	dz  }	|j	                  |	       |j	                  |	       + t        |      dd }|j                          |j                  |       |	dz  }	|j	                  |	        t        ||||      }|s|D cg c]  }t        d |D               }}|S c c}}w c c}}}w c c}w )a  Converts a connecting list of quadratic splines to a list of quadratic
    and cubic curves.

    A quadratic spline is specified as a list of points.  Either each point is
    a 2-tuple of X,Y coordinates, or each point is a complex number with
    real/imaginary components representing X,Y coordinates.

    The first and last points are on-curve points and the rest are off-curve
    points, with an implied on-curve point in the middle between every two
    consequtive off-curve points.

    Returns:
        The output is a list of tuples of points. Points are represented
        in the same format as the input, either as 2-tuples or complex numbers.

        Each tuple is either of length three, for a quadratic curve, or four,
        for a cubic curve.  Each curve's last point is the same as the next
        curve's first point.

    Args:
        quads: quadratic splines

        max_err: absolute error tolerance; defaults to 0.5

        all_cubic: if True, only cubic curves are generated; defaults to False
    r   r%   r'   r&   Nc              3   L   K   | ]  }|j                   |j                  f  y wN)realimag).0cs     r   	<genexpr>z&quadratic_to_curves.<locals>.<genexpr>   s     8Q(8s   "$)
typecomplexr(   r8   r)   r<   popextendspline_to_curvestuple)r?   r@   rA   r>   r:   xyr;   costsr=   r2   qqr*   r.   s                 r   r	   r	      sG   F eAhqk"g-J:?@@Qa0FQ'!Q-0@@	q!ACED 
u!}}s1vz" 	AAIDLLLL	 $A&qr*				T
 a;FFLMU%8%88MMM+ 1@( Ns   
D+D% D+D2%D+Solution)
num_pointserrorstart_indexis_cubicr2   jr   r   i_sol_countj_sol_countthis_sol_countr
   errrW   i_sol_errorj_sol_errorrY   r0   r   r   r   r   vuc           
      0   t        |       dk\  sJ d       t        dt        |       dz
  d      D cg c]  }t        | ||dz      }}t               }t        dt        |            D ]^  }||dz
     d   }||   d   }||   d   }	t	        ||z
        t	        |	|z
        z   |t	        |	|z
        z   kD  sN|j                  |       ` t        dddd      g}
t        t        |      dz  dz   ddd      }d}t        dt        |      dz         D ]  }|}t        ||      D ]f  }|
|   j                  |
|   j                  }}|s<|d|z  dz
     |d|z     z
  dz   }||z   }|}t        ||||z
  d      }||k  r|}|dk  r`	 t        ||||z
        \  }}t        g || }g }d}t        |      D ]E  \  }}|||z      }t	        |d   |d   z
        }t        ||      }||kD  r n|j                  |       G ||kD  rt        |      D ]D  \  }}|||z      }t        d t!        ||      D              \  }}}	}t#        |||	||      r?|dz   } n ||kD  r7|dz   }t        ||      }t        ||||z
  d      }||k  r|}|dk(  sg n |
j                  |       ||v s|} g }g } t        |
      dz
  }|rH|
|   j$                  |
|   j&                  }"}!|j                  |       | j                  |"       ||!z  }|rHg }#d}t)        t+        t!        ||                   D ]Z  \  }}"|"r#|#j                  t        ||||z
        d          n.t        ||      D ]  }|#j                  | |dz  |dz  dz           ! |}\ |#S c c}w # t        $ r Y tw xY w)	aF  
    q: quadratic spline with alternating on-curve / off-curve points.

    costs: cumulative list of encoding cost of q in terms of number of
      points that need to be encoded.  Implied on-curve points do not
      contribute to the cost. If all points need to be encoded, then
      costs will be range(1, len(q)+1).
    r   z+quadratic spline requires at least 3 pointsr   r&   r%   Fc              3   ,   K   | ]  \  }}||z
    y wrE   r   )rH   ra   rb   s      r   rJ   z#spline_to_curves.<locals>.<genexpr>W  s     &LAq1u&Ls   T)r8   r(   r   setr   addrU   rV   rW   r/   ZeroDivisionErrorr   	enumeratemaxr)   rP   zipr   rX   rY   reversedr7   )$r;   rS   r
   rA   r2   elevated_quadraticsforcedr   r   r   sols
impossibler   best_solrZ   r\   r`   
this_countr[   r_   i_solr.   r+   reconstructed_iterreconstructedrW   r   reconstorigr^   r   splitscubicr0   rY   r*   s$                                       r   rO   rO      s`   B q6Q;EEE; 383q6A:q2I-.1QQ<( 
 UF1c-./  Q'* #A& #A&rBw<#b2g,&Sb\)AAJJqM Q1e$%D#12Q6:Aq%HJE1c-.23 Bua <	A'+Aw'9'947==K"1q519-a!e<q@
)J6) k1q5%H8#$H?()<aQG	r
 "0!<!<!<M E'(:; .
7*1q51'!*tAw./E39$$$W-. y  (6 
7*1q51!&&LWd9K&L!LBB0RRK%ME y  &/Kk51K[+q1udCEx ay<	| 	H;EEBJ FED	AA
q'--tAw/?/?xaX	U
	 
 F	AS%7 89 8MM,':Aq1uEaHI1a[ 4aAA	234 MSN % s   NN	NNc                     ddl m}  ddlm} d}|dz  } |        } |||      }t	        d||fz         t	        dt        |      z         t        |g|      }t	        dt        |      z         t	        d	|       t	        d
|       y )Nr   )generate_curve)curve_to_quadraticg?r%   z'cu2qu tolerance %g. qu2cu tolerance %g.z+One random cubic turned into %d quadratics.z-Those quadratics turned back into %d cubics. zOriginal curve:zReconstructed curve(s):)fontTools.cu2qu.benchmarkrz   fontTools.cu2qur{   printr8   r	   )rz   r{   r
   reconstruct_tolerancer.   
quadraticsr*   s          r   mainr     s    82I%ME#E95J	1Y@U4VV 

7#j/
IJ */DEF	
9CK
GH	
U#	
#V,r   __main__)r   Fr   )"r   compiledCOMPILEDAttributeErrorImportErrorfontTools.miscfontTools.misc.bezierToolsr   collectionsr   mathtypingr   r   r   __all__cfuncreturnsintlocalsdoublerL   r   r   r/   r<   floatPointboolr	   rU   rO   r   __name__r   r   r   <module>r      s  &H 6 "   !
! 

mm~~~~~~~~ 6>>&..9W :  W@ ~~~~~~>>	

 
**jjjj}}mm
--mm~~~~~~~~$ $N 
****jj		~~

 	eE5L!7*+ 	zz 6U66 6 
%s

	6	6r j"TU  jjjj jj **	
 

 

 :: mm 	 --   jj ZZ **  ~~!" ~~#$ ~~%& ~~'( nn)* nn+.v/.vr-$ zF A 	$ %H	s   N- -O ?O 