L i.dZddlZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZd d gZd Zd Zej&eZeZd ZGddeZdZdZddZddZ ddZ ddZdZdZ y)aConverts 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)UnequalZipLengthsErrorIncompatibleSegmentNumberErrorIncompatibleSegmentTypesErrorIncompatibleGlyphsErrorIncompatibleFontsErrorfonts_to_quadraticfont_to_quadraticgMbP?z&com.github.googlei18n.cu2qu.curve_typecpttd|Ddk7rt|tt |S)zyEnsure each argument to zip has the same length. Also make sure a list is returned for python 2/3 compatibility. c32K|]}t|ywNlen).0as Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/fontTools/cu2qu/ufo.py zzip..<s$!s1v$sr)rsetrlist_zip)argss rzipr7s6  3$t$ $%*$d++ d cFeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) GetSegmentsPenzPen 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 d|_g|_yr)_last_ptsegmentsselfs r__init__zGetSegmentsPen.__init__Hs  rcZ|dvr |d|_|jj||fy)N)movelineqcurvecurve)r!r"append)r$tagrs r _add_segmentzGetSegmentsPen._add_segmentLs, 5 5 HDM c4[)rc(|jd|y)Nr'r.r$pts rmoveTozGetSegmentsPen.moveToQ &"%rc(|jd|y)Nr(r0r1s rlineTozGetSegmentsPen.lineToTr4rc@|jd|jg|y)Nr)r.r!r$pointss rqCurveTozGetSegmentsPen.qCurveToWs(DMM;F;rc@|jd|jg|y)Nr*r8r9s rcurveTozGetSegmentsPen.curveToZs'4==:6:rc&|jdy)Ncloser0r#s r closePathzGetSegmentsPen.closePath]s '"rc&|jdy)Nendr0r#s rendPathzGetSegmentsPen.endPath`s % rcyr)r$ glyphNametransformations r addComponentzGetSegmentsPen.addComponentcs rN) __name__ __module__ __qualname____doc__r%r.r3r6r;r=r@rCrHrErrrrAs4 * &&<;#! rrcjt}t|d}|j||jS)z6Get a glyph's segments as extracted by GetSegmentsPen.T)outputImpliedClosingLine)rr drawPointsr")glyphpenpointPens r _get_segmentsrSgs2  C!tDH X <<rc|j|j}|r t|}|D]\}}|dk(r|j||dk(r|j|0|dk(r|j |ddH|dk(r|j |dd`|dk(r|jv|dk(r|jtd |zy) z=Draw segments as extracted by GetSegmentsPen back to a glyph.r'r(r*rNr)r?rBzUnhandled segment type "%s") clearContoursgetPenrr3r6r=r;r@rCAssertionError)rPr"reverse_directionrQr-rs r _set_segmentsrYzs  ,,.C$F T &= CJJ  F] CJJ  G^ CKKab " H_ CLL$qr( # G^ MMO E\ KKM !>!DE EFrctd|DsJdt|Dcgc]}|d c}||}t|dtfd|ddDsJdtdz }|j |ddz||<|sd k(r|Dcgc]}d |fc}S|Dcgc]}d |fc}Scc}wcc}wcc}w) z2Return quadratic approximations of cubic segments.c3,K|] }|ddk(yw)rr*NrE)rss rrz)_segments_to_quadratic..s11qtw1szNon-cubic given to convertrrc3:K|]}t|k(ywrr)rr\ns rrz)_segments_to_quadratic..s3qs1v{3sNzConverted incompatiblyr)r*)allrrstrget) r"max_errstats all_quadraticr\ new_points spline_lengthpr^s @r_segments_to_quadraticrjs 11 1O3OO 1$H%=qad%=w VJ JqMA 3JqrN3 3M5MM 3AJM 99]A6:E-Q'12!1 22&01! 11&>31s B6 B;( Cc t|Dcgc] }t|c}}t |sy|}g}i} t |D]j\} } | ddt fd| ddDs| D cgc]} | d c} | | <ndk(rt| |||} |s| | k7rd}| } |j| l|r)t|}t||D]\}} t|| || r t|| |Scc}w#t$r t|wxYwcc} w) 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. Frc3.K|] }|dk(yw)rNrE)rr\r-s rrz'_glyphs_to_quadratic..s511Q43;5srNr*T)r") rrSrr any enumeraterarjr,rYr )glyphsrdrXrerfgsegments_by_locationglyphs_modifiednew_segments_by_location incompatibleir"r\ new_segmentsnew_segments_by_glyphrPr-s @r_glyphs_to_quadraticrxsK5"v$F!]1%5$FG # $(O!L !56 2 8qk!n5 55-56qt6LO G^1'5-L  8"&#H ''1 2 #%= >#&v/D#E B E< %/@ A B+F\JJ ?%G !5,V4457s! C-C(C-( D(C--Dc|i}|s tdz}t|ttfr|}n|gt |z}t |t |k(sJt |||||S)aConvert 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 isinstancertuplerrx)rordrXrerf max_errorss rglyphs_to_quadraticr~sm } !D('D%=) YV, z?c&k )) )  -um rc |r|Dchc]"}|jjtd$} }t| dk(r?t t | } | dvrt jdy| dk(rn.t| t| dkDrt jdi|r |r td|s|st}t|ttfrt|t|k(sJ|} n|r|gt|z} t|ttfrOt|t|k(sJt||D cgc]\}} |jj | z } }} n(|r&|Dcgc]}|jj |z} }t#} i}t#j$d |DD]e}g}g}t| D]/\}}||vs |j'|||j'|1 t)||||r| j+|g|r t1|| rK|rIt3j5}t jd d j7fd |Dz|rF|D]A}|jjtd} |rd nd}| |k7s/||jt<C| Scc}wcc} }wcc}w#t,$r%}t j/||||<Yd}~Ed}~wwxYw)aConvert 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) quadraticmixedz%Curves already converted to quadraticFz'fonts may contain different curve typesNz4Only one of max_err and max_err_em can be specified.c3<K|]}|jywr)keys)rfs rrz%fonts_to_quadratic..-s61affh6szNew spline lengths: %sz, c32K|]}d||fzyw)z%s: %dNrE)rlres rrz%fonts_to_quadratic..DsJaQaM1Jsrr)librcCURVE_TYPE_LIB_KEYrnextiterloggerinfoNotImplementedErrorwarning TypeErrorrzr{rr|r unitsPerEmrunionr,rxaddr errorr sortedrjoin)fonts max_err_emrdrXre dump_statsremember_curve_typerfr curve_types curve_typer}emodified glyph_errorsnamerocur_max_errorsfontrexcspline_lengthsnew_curve_types ` rr r s6GLM!quuyy!3W=M M { q d;/0J33 CDw&)*55   ! NND E }gNOO '$ 'D%=)7|s5z))) YU+ *tUm,5zS_,,,8;E:8NO1aff''!+O O >CDaff''*4D DuHL 667%uj1 -KD%t| d4j)%%e, - %#(95- T"% $\22J - $yyJ>JJ L  >D&8'BJ,9[wN^+/=+,  > OIN<PD ' % LL !$L  %s)'K?#K , K> K LK==Lc t|gfi|S)zConvenience wrapper around glyphs_to_quadratic, for just one glyph. Return True if the glyph was modified, else return False. )r~)rPkwargss rglyph_to_quadraticrPs w 1& 11rc t|gfi|S)zConvenience wrapper around fonts_to_quadratic, for just one font. Return the set of modified glyph names if any, else return empty set. )r )rrs rrrXs tf / //r)T)NFNT)NNFNFTT)!rLloggingfontTools.pens.basePenrfontTools.pens.pointPenr fontTools.pens.reverseContourPenrrerrorsrr r r r __all__rzr getLoggerrIrrrrrSrYrjrxr~r rrrErrrs .5>! !4 5 =   8 $ # [# L&F02$'VNRB  `F20r