)EjI  ddlZn#eef$r ddlmZYnwxYwejZddlZddlmZ m Z ddgZ dZ e dZejejejejejejejej d Zejejejej ejejej d ZejejejejejejejejejejejejdZejejejejejejejejejejejejdZejejejejejejejdZejejejejejejejejejejejejejejejejejejejejejdZejejejejejejejejejejdZejejejejejejejejejejejejdZejejejejejejejejejejejejejdZejejejejejejejejejejejejejejd Zejejejejejejejejej!ejejejd"Z ejejejej#ejejejejejej$d%Z!ejejejej&ejej'ejej(ejejejejej)ejejejejejej*d+Z"ejej,ejej-ejej(d2d/Z#ejejejej0ejej(d2d1Z$dS)3N)cython)ErrorApproxNotFoundErrorcurve_to_quadraticcurves_to_quadraticdNaNv1v2resultch||zj}t|dkrd}|S)zReturn the dot product of two vectors. Args: v1 (complex): First vector. v2 (complex): Second vector. Returns: double: Dot product. gV瞯>! 'F 6{{U M)zden)zrzicJ|j}|j}t||z ||z S)a`Divide complex by real using Python's method (two separate divisions). This ensures bit-exact compatibility with Python's complex division, avoiding C's multiply-by-reciprocal optimization that can cause 1 ULP differences on some platforms/compilers (e.g. clang on macOS arm64). https://github.com/fonttools/fonttools/issues/3928 )rimagcomplex)rrrrs r_complex_div_by_realr?s* B B 28R#X & &&r)abcd)_1_2_3_4cz|}t|d|z}t||zd|z}||z|z|z}||||fSN@r)rrr r!r"r#r$r%s rcalc_cubic_pointsr*PsT B a % % )B a!eS ) )B .B QQB r2r>r)p0p1p2p3cN||z dz}||z dz|z }|}||z |z |z }||||fSr')r+r,r-r.r rr!rs rcalc_cubic_parametersr1^sG bCA bC!A A Q QA aA:rc |dkrtt||||S|dkrtt||||S|dkrwt||||\}}tt|d|d|d|dt|d|d|d|dzS|dkrwt||||\}}tt|d|d|d|dt|d|d|d|dzSt|||||S)aSplit a cubic Bezier into n equal parts. Splits the curve into `n` equal parts by curve time. (t=0..1/n, t=1/n..2/n, ...) 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. Returns: An iterator yielding the control points (four complex values) of the subcurves. rr)itersplit_cubic_into_twosplit_cubic_into_three_split_cubic_into_n_gen)r+r,r-r.nrrs rsplit_cubic_into_n_iterr<ls\, Avv(RR88999Avv*2r2r::;;;Avv#BB331 1qtQqT1Q4 8 8"1Q41qtQqT:: ;    Avv#BB331 "1Q41qtQqT : :$QqT1Q41qt<< =   #2r2r1 5 55r)r+r,r-r.r;)dtdelta_2delta_3i)a1b1c1d1c#8Kt||||\}}}}d|z } | | z} | | z} t|D]a} | | z} | | z}|| z}d|z| z|z| z}d|z| z|zd|z|zz| z}|| z|z||zz|| zz|z}t||||VbdS)Nrr4r3)r1ranger*)r+r,r-r.r;rrr r!r=r>r?r@t1t1_2rArBrCrDs rr:r:s'r2r266JAq!Q QB2gG7lG 1XX00 VBw [!ebj1n '!ebj1nq1ut|+r 1 Vd]QX %B . 2BB//////00r)midderiv3c||d||zzz|zdz}||z|z |z dz}|||zdz||z |f|||z||zdz|ffS)aSplit a cubic Bezier into two equal parts. Splits the curve into two equal parts at t = 0.5 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. Returns: tuple: Two cubic Beziers (each expressed as a tuple of four complex values). r4??r0)r+r,r-r.rIrJs rr8r8sq* R"W  "e +C2glR5 (F b2g_cFlC0 cFlR"WOR0 r)mid1deriv1mid2deriv2c ,d|zd|zzd|zz|zdz}|d|zzd|zz dz}|d|zzd|zzd|zzdz}d|zd|zz |z dz}|td|z|zd||z |f|||z||z |f|||zt|d|zzd|ffS) aSplit a cubic Bezier into three equal parts. Splits the curve into three equal parts at t = 1/3 and t = 2/3 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. Returns: tuple: Three cubic Beziers (each expressed as a tuple of four complex values).  r6gh/?r4r5r3r(r))r+r,r-r.rNrOrPrQs rr9r9s: FR"W q2v % *v 6D1r6kAF"v .F RK"r' !AF *v 6D"fq2vo"v .F !!b&2+s 3 3TF]DI tf}dVmT2 tf}22B;DDbI r)tr+r,r-r.)_p1_p2cD|||z dzz}|||z dzz}|||z |zzS)axApproximate a cubic Bezier using a quadratic one. Args: t (double): Position of control point. p0 (complex): Start point of curve. p1 (complex): First handle of curve. p2 (complex): Second handle of curve. p3 (complex): End point of curve. Returns: complex: Location of candidate control point on quadratic curve. g?r0)rUr+r,r-r.rVrWs rcubic_approx_controlrYs;0 R3 C R3 C #)q  r)abcdphc||z }||z }|dz} t|||z t||z }n@#t$r3||kr||ks||kr|cYStttcYSwxYw|||zzS)ayCalculate the intersection of two lines. Args: a (complex): Start point of first line. b (complex): End point of first line. c (complex): Start point of second line. d (complex): End point of second line. Returns: complex: Location of intersection if one present, ``complex(NaN,NaN)`` if no intersection was found. y?)rZeroDivisionErrorrNAN)rrr r!rZr[r\r]s rcalc_intersectras$ QB QB RA ! 1q5MMC2JJ & !!! 66qAvvaHHHsC     ! rAv:s$6A3A32A3) tolerancer+r,r-r.c*t||krt||krdS|d||zzz|zdz}t||krdS||z|z |z dz}t|||zdz||z ||ot|||z||zdz||S)aCheck 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. Tr4rLFrM)rcubic_farthest_fit_inside)r+r,r-r.rbrIrJs rrdrd8s: 2ww)B9 4 4t R"W  "e +C 3xx)u2glR5 (F $ R"WOS6\3    W #CvR3I V VWr)rb)q1c0rCc2c3c0t|d|d|d|d}tj|jrdS|d}|d}|||z dzz}|||z dzz}t d||dz ||dz d|sdS|||fS)aApproximate a cubic Bezier with a single quadratic within a given tolerance. Args: cubic (sequence): Four complex numbers representing control points of the cubic Bezier curve. tolerance (double): Permitted deviation from the original curve. Returns: Three complex numbers representing control points of the quadratic curve if it fits within the given tolerance, or ``None`` if no suitable curve could be calculated. rrr3r4NUUUUUU?)ramathisnanrrd)cubicrbrerfrhrCrgs rcubic_approx_quadraticrnbs0 a%(E!HeAh ? ?B z"'t qB qB rBw5! !B rBw5! !B $QU1X rE!H}a S St r2:r)r;rb)r@) all_quadratic)rfrCrgrh)q0renext_q1q2rDc |dkrt||S|dkr|dkr|St|d|d|d|d|}t|}td|d|d|d|d}|d}d}|d|g} t d|dzD]} |\} } } }|}|}| |kr^t|}t| |dz z |d|d|d|d}| |||zdz}n|}|}||z }t ||ks+t||||z dzz| z |||z dzz| z ||sd S| |d| S) a'Approximate a cubic Bezier curve with a spline of n quadratics. Args: cubic (sequence): Four complex numbers representing control points of the cubic Bezier curve. n (int): Number of quadratic Bezier curves in the spline. tolerance (double): Permitted deviation from the original curve. Returns: A list of ``n+2`` complex numbers, representing control points of the quadratic spline if it fits within the given tolerance, or ``None`` if no suitable spline could be calculated. rr3Frr4yrMrjN)rnr<nextrYrFappendrrd)rmr;rbrocubics next_cubicrqrrrDspliner@rfrCrgrhrpred0s rcubic_approx_splinerzs: Avv%eY777Avv-5(( $U1XuQxq58Q O OFfJ" :a=*Q-A 1 G qB BAh F 1a!e__#BB  q55fJ*QU Z]JqM:a=*UV-G MM' " " "w,#%BBB "W r77Y  &?  "r'e$ $r ) "r'e$ $r )   ' ' 44  MM%( Mr)max_err)r;Tcd|D}tdtdzD]$}t||||}|d|DcS%t|)aUApproximate a cubic Bezier curve with a spline of n quadratics. Args: cubic (sequence): Four 2D tuples representing control points of the cubic Bezier curve. max_err (double): Permitted deviation from the original curve. all_quadratic (bool): If True (default) returned value is a quadratic spline. If False, it's either a single quadratic curve or a single cubic curve. Returns: If all_quadratic is True: A list of 2D tuples representing control points of the quadratic spline. If all_quadratic is False: Either a quadratic curve (if length of output is 3), or a cubic curve (if length of output is 4). Raises: fontTools.cu2qu.errors.ApproxNotFoundError: if no suitable approximation can be found with the given parameters. c g|] }t| Sr0r.0r\s r z&curve_to_quadratic..s ( ( (QWa[ ( ( (rrNc*g|]}|j|jfSr0rrrss rrz&curve_to_quadratic..s!555QVQV$555r)rFMAX_Nrzr)curver{ror;rxs rrrs|4 ) (% ( ( (E 1eai 66$UAw FF  55f555 5 5 5  e $ $$r)llast_ir@c~d|D}t|t|krtd|sgSt|}dg|z}dx}}d} t||||||}||tkrn(|dz }|}5|||<|dz|z}||kr d|DSTt |)aReturn quadratic Bezier splines approximating the input cubic Beziers. Args: curves: A sequence of *n* curves, each curve being a sequence of four 2D tuples. max_errors: A sequence of *n* floats representing the maximum permissible deviation from each of the cubic Bezier curves. all_quadratic (bool): If True (default) returned values are a quadratic spline. If False, they are either a single quadratic curve or a single cubic curve. Example:: >>> curves_to_quadratic( [ ... [ (50,50), (100,100), (150,100), (200,50) ], ... [ (75,50), (120,100), (150,75), (200,60) ] ... ], [1,1] ) [[(50.0, 50.0), (75.0, 75.0), (125.0, 91.66666666666666), (175.0, 75.0), (200.0, 50.0)], [(75.0, 50.0), (97.5, 75.0), (135.41666666666666, 82.08333333333333), (175.0, 67.5), (200.0, 60.0)]] The returned splines have "implied oncurve points" suitable for use in TrueType ``glif`` outlines - i.e. in the first spline returned above, the first quadratic segment runs from (50,50) to ( (75 + 125)/2 , (120 + 91.666..)/2 ) = (100, 83.333...). Returns: If all_quadratic is True, a list of splines, each spline being a list of 2D tuples. If ``curves`` is empty, returns an empty list. If all_quadratic is False, a list of curves, each curve being a quadratic (length 3), or cubic (length 4). Raises: ValueError: if ``max_errors`` does not match the number of curves. fontTools.cu2qu.errors.ApproxNotFoundError: if no suitable approximation can be found for all curves with the given parameters. c&g|]}d|DS)c g|] }t| Sr0r~rs rrz2curves_to_quadratic...!s***qw{***rr0)rrs rrz'curves_to_quadratic..!s' ? ? ?u**E*** ? ? ?rz*max_errors must match the number of curvesNrrTc&g|]}d|DS)c*g|]}|j|jfSr0rrs rrz2curves_to_quadratic...7s!666!afaf%666rr0)rrxs rrz'curves_to_quadratic..7s'MMM666v666MMMr)len ValueErrorrzrr) curves max_errorsrorsplinesrr@r;rxs rrrsP@ ? ? ? ?F :#f++%%EFFF   F AfqjGNFQ A N$VAY:a=-PP >Ezz FAF  UaK ;;MMWMMM M N f % %%r)T)%rAttributeError ImportErrorfontTools.misccompiledCOMPILEDrkerrorsr Cu2QuErrorr__all__rfloatr`cfuncinlinereturnsdoublelocalsrrrr*r1r<intr:r8r9rYrardrnrzrrr0rrrs$&MMMM $&&&%%%%%%%%& ? <<<<<<<< !6 7  eEll &.V^FMJJJKJ,V]333&-FM222 ' '3243 '6>V^v~VVV ~&.V^WV  ~&.V^6>V^v~VVVWV  ~&.V^"6"6 "6J ~ ~ ~ ~ j 6>V^v~VVV }fmV]fj ~&.V^ 0 0WV 0  ~&.V^6>&.999:9 . ~ ~ ~ ~    >  >    4 m ~ ~ ~ ~ 6>v~666!!76 !$6>V^v~VVV&.V^v~WWWXWWV  : m ~ ~ ~ ~ 6>&.999WW:9W@''' ~ ~ ~ ~ ~ ('4v}555VZ((( ~&.V^ ~ ~ N ~ ~ ==)(65=@v}%%%VZ(((%%%)(&%%DFJ&*===VZ(((>&>&>&)(>=>&>&>&s