L iBF  ddlZejZddlZddlmZ m Z ddgZ dZ e dZejej ej"ej$ej&ej(ej(ej$ d Zejej&ej(ej$ ej&ej$ej$ d Zejej ej&ej(ej(ej(ej(ej&ej(ej(ej(ej(dZejej ej&ej(ej(ej(ej(ej&ej(ej(ej(ej(dZejej ej&ej(ej(ej(ej(dZej&ej(ej(ej(ej(ej4ej&ej(ej(ej(ej(ej&ej$ej$ej$ej4ej&ej(ej(ej(ej(dZejej ej&ej(ej(ej(ej(ej&ej(ej(dZejej ej&ej(ej(ej(ej(ej&ej(ej(ej(ej(dZejej ej"ej(ej&ej$ej(ej(ej(ej(ej&ej(ej(dZejej ej"ej(ej&ej(ej(ej(ej(ej&ej(ej(ej(ej$d Zejej"ej4ej&ej$ej(ej(ej(ej(!ej&ej(ej(d"Z ejej ej&ej$#ej&ej(ej(ej(ej(ej($d%Z!ejej&ej4ej$&ej&ej4'ej&ej4(ej&ej(ej(ej(ej()ej&ej(ej(ej(ej(ej(*d+Z"ej&ej$,ej&ej4-ej&ej4(d1d.Z#ej&ej4ej4ej4/ej&ej4(d1d0Z$y#eef$r ddlmZY uwxYw)2N)cython)ErrorApproxNotFoundErrorcurve_to_quadraticcurves_to_quadraticdNaNv1v2resultc`||jzj}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)zrzicV|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_4cj|}t|d|z}t||zd|z}||z|z|z}||||fSN@)r)rrr r!r"r#r$r%s rcalc_cubic_pointsr)PsN 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_parametersr0^sG bCA bC!A A Q QA aA:rc |dk(rtt||||S|dk(rtt||||S|dk(rOt||||\}}tt|d|d|d|dt|d|d|d|dzS|dk(rOt||||\}}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), Av(RR899Av*2r2r:;;Av#BB31 1qtQqT1Q4 8"1Q41qtQqT: ;   Av#BB31 "1Q41qtQqT :$QqT1Q41qt< =  #2r2r1 55r)r*r+r,r-r:)dtdelta_2delta_3i)a1b1c1d1c#&Kt||||\}}}}d|z } | | z} | | z} t|D]]} | | z} | | z}|| z}d|z| z|z| z}d|z| z|zd|z|zz| z}|| z|z||zz|| zz|z}t||||_yw)Nrr3r2)r0ranger))r*r+r,r-r:rrr r!r<r=r>r?t1t1_2r@rArBrCs rr9r9s'r2r26JAq!Q QB2gG7lG 1X0 VBw [!ebj1n '!ebj1nq1ut|+r 1 Vd]QX %B . 2BB//0sBB)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). r3??r/)r*r+r,r-rHrIs rr7r7sq* R"W  "e +C2glR5 (F b2g_cFlC0 cFlR"WOR0 r)mid1deriv1mid2deriv2cd|zd|zzd|zz|zdz}|d|zzd|zz dz}|d|zzd|zzd|zzdz}d|zd|zz |z dz}|d|z|zdz ||z |f|||z||z |f|||z|d|zzdz |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).  r5gh/?r3r4r2r(r/)r*r+r,r-rMrNrOrPs rr8r8s: FR"W q2v % *v 6D1r6kAF"v .F RK"r' !AF *v 6D"fq2vo"v .F a"frkS $-6 tf}dVmT2 tf}rAF{c126 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?r/)rTr*r+r,r-rUrVs rcubic_approx_controlrXs;0 R3 C R3 C #)q  r)abcdphc||z }||z }|dz} t|||z t||z }|||zzS#t$r*||k(r||k(s||k(r|cYStttcYSwxYw)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!rYrZr[r\s rcalc_intersectr`s$ QB QB RA ! 1q5MC2J & rAv: ! 6qAvaHsC  !s5A(A('A() tolerancer*r+r,r-ct||krt||kry|d||zzz|zdz}t||kDry||z|z |z dz}t|||zdz||z ||xrt|||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. Tr3rKFrL)rcubic_farthest_fit_inside)r*r+r,r-rarHrIs rrcrc8s: 2w)B9 4 R"W  "e +C 3x)2glR5 (F $ R"WOS6\3   W #CvR3I VWr)ra)q1c0rBc2c3ct|d|d|d|d}tj|jry|d}|d}|||z dzz}|||z dzz}t d||dz ||dz d|sy|||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. rrr2r3NUUUUUU?)r`mathisnanrrc)cubicrardrergrBrfs rcubic_approx_quadraticrmbs0 a%(E!HeAh ?B zz"'' qB qB rBw5! !B rBw5! !B $QU1X rE!H}a S r2:r)r:ra)r?) all_quadratic)rerBrfrg)q0rdnext_q1q2rCc Z|dk(r t||S|dk(r|dk(r|St|d|d|d|d|}t|}td|d|d|d|d}|d}d}|d|g} t d|dzD]} |\} } } }|}|}| |krFt|}t| |dz z |d|d|d|d}| j |||zdz}n|}|}||z }t ||kDs(t||||z dzz| z |||z dzz| z ||ry | j |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. rr2Frr3yrLriN)rmr;nextrXrEappendrrc)rlr:rarncubics next_cubicrprqrCspliner?rerBrfrgrordd0s rcubic_approx_splinerys: Av%eY77Av-5( $U1XuQxq58Q OFfJ" :a=*Q-A 1 G qB BAh F 1a!e_#BB  q5fJ*QU Z]JqM:a=*UV-G MM' "w,#%BB "W r7Y &?  "r'e$ $r ) "r'e$ $r )   ' 9: MM%( Mr)max_err)r:c|Dcgc] }t| }}tdtdzD]:}t||||}||Dcgc]}|j|j fc}cSt |cc}wcc}w)a5Approximate 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 it fits within the given tolerance, or ``None`` if no suitable spline could be calculated. 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). r)rrEMAX_Nryrrr)curverzrnr[r:rwss rrrs0#( (QWa[ (E ( 1eai 6$UAw F  .45QVVQVV$5 5 6 e $$ ) 6s A1A6)llast_ir?c |Dcgc]}|Dcgc] }t| c}}}}t|t|k(sJt|}dg|z}dx}}d} t||| |||} | | tk(r t || dz } |}4| ||<|dz|z}||k(r6|D cgc](} | D cgc]} | j| j fc} *c} } S|cc}wcc}}wcc} wcc} } w)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 all_quadratic is False, a list of curves, each curve being a quadratic (length 3), or cubic (length 4). Raises: fontTools.cu2qu.Errors.ApproxNotFoundError: if no suitable approximation can be found for all curves with the given parameters. Nrr)rlenryr|rrr) curves max_errorsrnr}r[rsplinesrr?r:rwr~s rrrsN9? ?uE*qw{* ?F ? z?c&k )) ) F AfqjGNFQ A $VAY:a=-P >Ez f %% FAF  UaK ;ELM6v6!affaff%6M M + ?&7Ms- CC C! C#*C C#CC#)T)%rAttributeError ImportErrorfontTools.misccompiledCOMPILEDrjerrorsr Cu2QuErrorr__all__r|floatr_cfuncinlinereturnsdoublelocalsrrrr)r0r;intr9r7r8rXr`rcrmryrrr/rrrs$& ?? < !6 7  El &..V^^FMMJK,V]]3&--FMM2 '34 '6>>V^^v~~V ~~&..V^^W  ~~&..V^^6>>V^^v~~VW  ~~&..V^^"6 "6J ~~ ~~ ~~ ~~ jj 6>>V^^v~~V }}fmmV]]fjj ~~&..V^^ 0W 0  ~~&..V^^6>>&..9: . ~~ ~~ ~~ ~~    >>  >>    4 mm ~~ ~~ ~~ ~~ 6>>v~~6!7 !$6>>V^^v~~V&..V^^v~~WXW  : mm ~~ ~~ ~~ ~~ 6>>&..9W:W@' ~~ ~~ ~~ ~~ ~~ (4v}}5VZZ( ~~&..V^^ ~~ ~~ NN ~~ ~~ =)6=@v}}%VZZ(%)&%@FJJ&**=VZZ(:&)>:&K  $&%%&se++ e<;e<