o ?}h[@sddlmZddlTddlmmZddlZddlZddlmZm Z ddl m Z ddl Z ddl ZddlZddlmZddlmZmZmZmZmZmZd Zed Zed Zeed ZGd ddZGdddZ GdddZ!dS)) annotations)*N)ListUnion)minidom) union_bbox) SVGCommandSVGCommandMoveSVGCommandCloseSVGCommandBezierSVGCommandLine SVGCommandArcZMmZzLlHhVvCcSsQqTtAaz([MmZzLlHhVvCcSsQqTtAa])z([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?c@seZdZdZdZdS) OrientationrrN)__name__ __module__ __qualname__COUNTER_CLOCKWISE CLOCKWISErr5/data/cref/OmniSVG-kaiyuan/deepsvg/svglib/svg_path.pyrsrc@seZdZdZdZdZdS)FillingrrN)rrrOUTLINEFILLERASErrrrrsrc@seZdZdddejfdtddZed d Zed d Zed dZ ddZ duddZ ddZ ddZ duddZddZeddZedvd d!Zedejdfdwd$d%Zedxdyd(d)Zedejddfdzd*d+Zd,d-Zdxd.d/Zd{d1d2Zd|d3d4Zed5fd6d7Zd}d8d9Zd:d;Zdd?Zd@dAZ dBdCZ!dDdEZ"d~dGdHZ#dIdJZ$dKdLZ%dMdNZ&dudOdPZ'dQdRZ(dSdTZ)dUdVZ*dWdXZ+dYdZZ,dd\d]Z-dd_d`Z.dadbZ/dcddZ0ddgdhZ1ddidjZ2dkdlZ3ddndoZ4dpdqZ5drdsZ6dS)SVGPathNF path_commandsList[SVGCommand]originPointcCs$|ptd|_||_||_||_dS)Nr)r!r rclosedfilling)selfrr r"r#rrr__init__$s zSVGPath.__init__cCst|j|jSN)r r start_posr$rrr start_command+szSVGPath.start_commandcC |jdjSNr)rr'r(rrrr'/ zSVGPath.start_poscCr*)N)rend_posr(rrrr.3r,zSVGPath.end_poscOs$ddlm}||gg|Ri|S)Nr SVGPathGroup) svg_primitiver0)r$argskwargsr0rrrto_group7s zSVGPath.to_groupTcCs|rtj|_|Stj|_|Sr&)rrrr#)r$r#rrr set_filling;s zSVGPath.set_fillingcCsdt|jt|jS)Nr)lenrintr"r(rrr__len__?zSVGPath.__len__cCs|dkr|jS|j|dS)Nrr)r)r)r$idxrrr __getitem__BszSVGPath.__getitem__cCsD|jr|jr|rt|jdj|jgnd}|jg|j|S)Nr-r)r"rr r.copyr'r))r$ with_closeZ close_cmdrrr all_commandsGs.zSVGPath.all_commandscCs&tdd|jD|j|j|jdS)NcSg|]}|qSr)r<.0Z path_commandrrr Mz SVGPath.copy..r#)rrr r<r"r#r(rrrr<Ls&z SVGPath.copyccsNd}t|D]}|r|tvr|}q|dur$|tttt|fVqdSr&) COMMAND_REsplitCOMMANDSlistmapfloatFLOAT_REfindall)path_strcmdxrrr_tokenize_pathOs zSVGPath._tokenize_pathrOminidom.ElementcCsr|d}|d}|d}|d p|ddk }|ds%tjnt|d}|d}tj|||dS) Nstroke dasharrayz stroke-widthfillnoner#d)rTr#) getAttribute hasAttributerrr7rfrom_str)rOrRrS stroke_widthrTr#srrrfrom_xmlXs    zSVGPath.from_xmlr[strc Csdg}td}}d}t|D]\}} t|| |||\} }}| d}|| qtj||||dS)Nrr-)rTr# add_closing)r!rrPr rYextend from_commands) r[rTr#r^rpos initial_pos prev_commandrNr2 cmd_parsedrrrrYes  zSVGPath.from_strtensor torch.TensorcCstjdd|D|dS)NcSsg|]}t|qSr)r from_tensor)rArowrrrrBssz'SVGPath.from_tensor..) allow_empty)rr`)rerirrrrgqszSVGPath.from_tensorc CsBddlm}|s |gSg}d}|D]l}t|tr@|dur5|s"|jr5|r'd|_|js0|jt||tg|j |d}q|durEqt|t rx|sO|jrud|_|j |j krg|jt |j |j |jsp|jt||d}q|j|q|dur|s|jr|rd|_|js|jt|||||dS)Nrr/TrD)rT)r1r0 isinstancer rr"append empty_commandrr'r<r r.r ) rrTr#r^rir0 svg_pathssvg_pathcommandrrrr`usB            zSVGPath.from_commandscCsdddd|DS)Nz SVGPath({}) cs|]}|VqdSr&)__repr__rArorrr z#SVGPath.__repr__..)formatjoinr>r(rrrrrszSVGPath.__repr__cCsddd|DS)Nrpcsrqr&)to_strrsrrrrtruz!SVGPath.to_str..)rwr>)r$rTrrrrxr9zSVGPath.to_strrcstfdd|DS)Ncsg|]}|jdqS)PAD_VAL) to_tensorrsryrrrBsz%SVGPath.to_tensor..)torchstackr>)r$rzrryrr{szSVGPath.to_tensorcCs0|r|||nd}|r|nd}g||S)Nr)_get_points_viz_get_handles_viz)r$ with_points with_handles with_bboxescolor_firstlast with_movespointshandlesrrr_get_viz_elementss zSVGPath._get_viz_elementscOs*ddlm}||g|dj|i|S)NrSVG)viewbox)svgrr4draw)r$rr2r3rrrrrs z SVGPath.drawc Cshg}|jdd}t|}t|D]!\}}t|tr|r1|j|o"|dk|o)||dkd}||q|S)NF)r=rr)firstlast)r>r6 enumeraterjr get_points_vizr_) r$rrrcommandsniro points_vizrrrr~s " zSVGPath._get_points_vizcCs"g}|jD] }||q|Sr&)rr_get_handles_viz)r$rrorrrrs zSVGPath._get_handles_vizcCs,g}|D] }||qtt|Sr&)r>r_ get_geomsrHset)r$Zgeomsrorrr_get_unique_geomss  zSVGPath._get_unique_geomscC|D]}||q|Sr&)r translate)r$vecgeomrrrr  zSVGPath.translatecCrr&)rrotate_)r$anglerrrrrotaterzSVGPath.rotatecCrr&)rscale)r$factorrrrrrrz SVGPath.scalecCs2g}|jD]}|j|js||q||_|Sr&)rr'iscloser.rkr$rrorrrfilter_consecutivess  zSVGPath.filter_consecutives皙?cCs\g}d}|jD]!}|dur|||}|j|j|kr(|j|_|||}q||_|Sr&)rrkr.distr')r$min_distrZcurrent_commandrorrrfilter_duplicatess   zSVGPath.filter_duplicatescCs*t|j|jg|jt|j|j|_|Sr&)r r'rr.r(rrrduplicate_extremitiess  zSVGPath.duplicate_extremitiescCsXt|jdkr|jd}|j|jkSd}|jD] }|t|j|j7}q|dkS)Nrrr)r6rr'tolistr.rdet)r$rNZ det_totalrrr is_clockwises  zSVGPath.is_clockwisecCs||kr|S|S)zC orientation: 1 (clockwise), 0 (counter-clockwise) )rreverse)r$ orientationrrrset_orientations zSVGPath.set_orientationcCs ||_|Sr&)r")r$r"rrr set_closedszSVGPath.set_closedcCs,g}t|jD] }||q||_|Sr&)reversedrrkrrrrrr s zSVGPath.reversecCs|j|js |S|Sr&)r'rr.rr(rrrreverse_non_closedszSVGPath.reverse_non_closedcCsZg}|jD]"}t|tr"|jrq|j|jrq|| q| |q||_|Sr&) rrjrradiusiszeror'rr.r_ to_beziersrkrrrr simplify_arcss    zSVGPath.simplify_arcscCs>d}d}t|jD]\}}|dus||r|}|}q ||fSr+)rr is_left_to)r$topleftmost_cmdtopleftmost_idxrrNrrr_get_topleftmost_command)sz SVGPath._get_topleftmost_commandcCs2|\}}g|j|d|jd||_|Sr&)rr)r$rrrrrreorder4s   zSVGPath.reordergreycCs(ddlm}ddlm}m}|durg}|durg}gg}} |D]l} | j| j} } t| t r>|| | ddd} | | || t ddd }| |t |j |d }t t | | gj dd }|rf||gn|g}|g|| |jd d d d }| |t|d|d_| | g} q#||fS)Nrr)SVGLine SVGCircletealg?)colorrS皙?red)rr)rFT) do_display return_pngrrr-)rrr1rrr>r'r.rjr rkRadiusrr4rnparrayr)r$wrapperclips svg_commandsrrrrZsvg_dotsZ svg_movesror'r.movedotrnZ svg_new_pathrmimrrrto_video?s.         zSVGPath.to_videocCs|D]}||qdSr&)r> numericalize)r$rrorrrr`s  zSVGPath.numericalizecCst|j}|jgdd|jD}|dd|dg}dg}tdg|d}td|D]D}||dk}d}|r;dnd} |rAdnd} |rGdnd} |||d} || | || ||| ||d| ||dq-||d||d||d<t|dd d D]}||||d||||<qd||||dd||<t|D]&}||||d} }||d|||d}}t| ||||j|<q|S) Ncs|]}|jVqdSr&r.)rAZ path_commmandrrrrtgz!SVGPath.smooth..rrrrr-)r6rr'r!rangerkr )r$rknotsrfprinternalabuvmp1p2c1c2rrrsmoothds.     4 "  zSVGPath.smoothcCs&|jdddjddddjdd S) NrF)max_dist include_linesrr) toleranceepsilonangle_thresholdg@)r)r<rFsimplifyr(rrrsimplify_heuristics zSVGPath.simplify_heuristicr`f@c s jgdd jD fdd}gd fdd  fd d d d dfd d d fdd fdd fdd fdd d fdd |}|rg|dt dna|r|d} d|d|t|dd|ddD]\}}||d|dd |dd|d|q|d}||d|dd |ddt d|n dt d| _ S)Ncsrr&rr@rrrrtrz#SVGPath.simplify..csg}g}d}tjD]0\}}t|tr |r||g}d}q |dur4||kr4|r4||g}|||}q |rC|||Sr&)rrrjr rkr) segments_listZcurrent_segmentrcrro)rr$rrsubdivide_indicess&     z+SVGPath.simplify..subdivide_indicescurver csdd}||dd}td||D]}|||||d}||kr-|}||}q||fSNrrr)revalr)rrrrmaxDistindexrrrrrcomputeMaxErrors z)SVGPath.simplify..computeMaxErrorc sdg}td||dD]}|||d||||dq t|dddD]\}}|||d<q0|S)Nrrr-)rrkrr)rrrr_rrrchordLengthParametrizes 2z0SVGPath.simplify..chordLengthParametrizecSsd}|| ko ||kS)Ng6$.isMachineZerocsb|||}|j|dd|j|dd}}||}||||}|r+|S|||S)a Newton's root finding algorithm calculates f(x)=0 by reiterating x_n+1 = x_n - f(x_n)/f'(x_n) We are trying to find curve parameter u for some point p that minimizes the distance from that point to the curve. Distance point to curve is d=q(u)-p. At minimum distance the point is perpendicular to the curve. We are solving f = q(u)-p * q'(u) = 0 with f' = q'(u) * q'(u) + q(u)-p * q''(u) gives u_n+1 = u_n - |q(u_n)-p * q'(u_n)| / |q'(u_n)**2 + q(u_n)-p * q''(u_n)| rrr)r derivativer)rpointrdiffd1d2 numerator denominator)rrrfindRoots  z"SVGPath.simplify..findRootcsftd||dD]}|||||||<q tdt|D]}||||dkr0dSq!dS)NrrFT)rr6)rrrrr)rrrr reparametrizesz'SVGPath.simplify..reparametrizec!sd}||}}td}td} t||dD]w} || } d| } d| | } | d}| | }| | }| d}||}||}|| ||||||}|d||7<|d||7<|d|d<|d ||7<| d ||7<| d||7<q|d|d |d|d}t||kr|d| d|d| d }| d |d | d|d}||}||}n,|d|d}|d|d }t||kr| d |n t||kr| d|nd }}||}||}d}}||ks||kr|d}}n%||} ||}||}|| || |dkrC|d}}d}}|dusM|durU||}||}t||||||S) Ng-q=)rrrrr)rr)rr)rr)rrr)rzerosrrabsrr )!rruPrimetan1tan2rrrCXrrtrb0b1b2b3a1a2tmpZdetC0C1ZdetC0XZdetXC1alpha1alpha2c0rZ segLengthepshandle1handle2linerrrgenerateBeziersZ   $      8  z(SVGPath.simplify..generateBeziercsbd}||dd}||}}t|d|D]}|||}||kr,|}|}q||fSr)r distToLine)rrrrrrrrrrrcomputeLinearMaxErrorsz/SVGPath.simplify..computeLinearMaxErrorcsX||\}}||kr||||||dS||}}t||dSr&)rkr )rrr max_error split_indexrr)r rrramerDouglasPeuckerrrr#&s  z-SVGPath.simplify..ramerDouglasPeuckercs\|dur |dur |d|}|d|}||dkrJ||}}||d}t||||||||dS||}t||d} d} tdD]0} |||||} ||| |\} }| |kr}| r}| dS| | krn |||| } | } q\|d|d}||||||||| |dS)NrrrT) normalizerrkr maxr)errorrrr r rrrr ZmaxErrorZparametersInOrderrrr!r"Z tanCenter)rrfitCubicrrrrrrr(0s0 $    z"SVGPath.simplify..fitCubicrrr-)rr )NN)r'rr6zip) r$rrr force_smoothrrsegZseg_nextr) rrr rrr(rrrrr#rr$rrs6   9  #"zSVGPath.simplifycCsjg}|jD]*}t|tr|s||q|}|dur&tt||d}||j |dq||_|S)Nrr) rrjr rklengthr&mathceilr_rF)r$rrrrrolrrrrFis  z SVGPath.splitcCstdd|jDS)NcSr?r)bboxrArNrrrrB{rCz SVGPath.bbox..)rrr(rrrr0zsz SVGPath.bbox皙?cCsVg}|jD]}|}tt||d}||j|dddqtj|dd}|S)NrT)r return_arrayr)axis) rr,r&r-r.r_ sample_pointsr concatenate)r$rrror/rrrrr5}s zSVGPath.sample_pointscCs$tj|}|js|d}|Sr+)shapelygeometryPolygonr5is_validbuffer)r$polygonrrr to_shapelys zSVGPath.to_shapelycCs t|jjgdd|jDS)Ncss|]}|jjVqdSr&)r.rar1rrrrtruz$SVGPath.to_points..)rrr'rarr(rrr to_pointss zSVGPath.to_points)rrr r!)T)rOrQ)r[r])F)rerf)rr)r)FFFFT)TT)r)NNr)r)rrrF)NNT)r2)7rrrrrr%propertyr)r'r.r4r5r8r;r>r< staticmethodrPr\rYrgr`rrrxr{rBboxrr~rrrrrrrrrrrrrrrrrrrrrrFr0r5r=r>rrrrr#sr         +         !  a  r)" __future__rrdeepsvg.svglib.geomsvglibrer|typingrrxml.domrr-Zshapely.geometryr7numpyrr svg_commandr r r r r rrGcompilerErKr!rlrrrrrrrs&