o ?}hA@sddlmZddlTddlZddlZddlmZmZddlm Z ddl m Z ddl m Z mZmZmZddlZddlZddlZddlZed Zd d ZGd d d ZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZ dS)) annotations)*N)ListUnion)minidom)SVGPath)SVGCommandLine SVGCommandArcSVGCommandBezierSVGCommandClosez([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?cCstttt|SN)listmapfloatFLOAT_REfindallargsr:/data/cref/OmniSVG-kaiyuan/deepsvg/svglib/svg_primitive.py extract_argsrc@sleZdZdZdddZd d Zedd dZedfddZ dddZ ddZ ddZ ddZ d ddZdS)! SVGPrimitivez[ Reference: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes blackFN.3?cCs"||_||_||_||_||_dSr )color dasharray stroke_widthopacityfill)selfrr!rrr rrr__init__s  zSVGPrimitive.__init__cCs`|jrd|jd|jdnd|jd|jd|jd}|jdur.|js.|d|jd7}|S)Nzfill="z" fill-opacity=""zfill="none" stroke="z" stroke-width="z" stroke-opacity="z stroke-dasharray=")r!rr rr)r" fill_attrrrr_get_fill_attr"s:zSVGPrimitive._get_fill_attrxminidom.ElementcCtr NotImplementedError)clsr'rrrfrom_xml(szSVGPrimitive.from_xmlcOs&ddlm}||g|dj|i|S)Nr)SVG)viewbox)svgr/draw)r"r0rkwargsr/rrrr2,s zSVGPrimitive.drawTcCsgSr r)r" with_points with_handles with_bboxescolor_firstlast with_movesrrr_get_viz_elements0zSVGPrimitive._get_viz_elementscCr)r r*r"rrrto_path3r:zSVGPrimitive.to_pathcCr)r r*r;rrrcopy6r:zSVGPrimitive.copycCr)r r*r;rrrbbox9r:zSVGPrimitive.bboxcCs ||_|Sr r!)r"r!rrrfill_<szSVGPrimitive.fill_)rFNrrr'r(FFFTT)T)__name__ __module__ __qualname____doc__r#r& classmethodr-Bboxr2r9r<r=r>r@rrrrrs   rcDeZdZdfdd ZddZd d Zedd dZddZZ S) SVGEllipsecenterPointradiusRadiusc"tj|i|||_||_dSr )superr#rKrM)r"rKrMrr3 __class__rrr#B zSVGEllipse.__init__cCd|jd|jdS)Nz SVGEllipse(c= r=)rKrMr;rrr__repr__HzSVGEllipse.__repr__c O<|}d|d|jjd|jjd|jjd|jjd S)Nz r&rKr'yrMr"rr3r%rrrto_strK4zSVGEllipse.to_strr'r(cCsf|d p |ddk }tt|dt|d}tt|dt|d}t|||dS)Nr!nonecxcyrxryr?) hasAttribute getAttributerLrrNrJ_r'r!rKrMrrrr-OszSVGEllipse.from_xmlc Cs|j|j|j|j}}|j|j|j|j}}t||jtdtdtd|t||jtdtdtd|t||jtdtdtd|t||jtdtdtd|g}t|ddj|j dS)NrTclosedr?) rKrMxprojyprojr AngleFlagrto_groupr!r"p0p1p2p3commandsrrrr<Ws""zSVGEllipse.to_path)rKrLrMrNrA rCrDrEr#rXrarGr-r< __classcell__rrrQrrJA rJcs:eZdZfddZddZddZed d d ZZS) SVGCirclectj|i|dSr rPr#r"rr3rQrrr#drYzSVGCircle.__init__cCrT)Nz SVGCircle(c=rUrVrWr;rrrrXgrYzSVGCircle.__repr__c Os2|}d|d|jjd|jjd|jjd S)Nz cSg|]}|qSrra.0prrr z&SVGPolyline.to_str..r&formatjoinrr`rrrrazSVGPolyline.to_strr'r(csz|d p |ddk }t|dtddks'Jdtdfdd tdddD}|||d S) Nr!rcrrz3Expected even number of arguments for SVGPolyline: z givencs&g|]\}}t|d|dqS)rr)rL)rir'rrrr&z(SVGPolyline.from_xml..r?)rhrirlen enumerate)r,r'r!rrrrr-s $  zSVGPolyline.from_xmlcCsJddt|jdd|jddD}|jjdk}t||dj|jdS)NcSsg|] \}}t||qSr)r )rrvrwrrrrsz'SVGPolyline.to_path..r SVGPolygonrmr?)ziprrRrCrrsr!)r"ry is_closedrrrr<s( zSVGPolyline.to_path)rrrArzrrrQrrs rcs,eZdZfddZddZddZZS)rcr~r rrrQrrr#rYzSVGPolygon.__init__cCr)NzSVGPolygon(points=rVrr;rrrrXrzSVGPolygon.__repr__cOr)NzrcSrrrrrrrrrz%SVGPolygon.to_str..rr`rrrrarzSVGPolygon.to_str)rCrDrEr#rXrar{rrrQrrs rcseZdZdfdgfdd ZeddZedd Zd d Zd d ZddZ eddZ eddZ dhddZ diddZ ddZddZdjd"d#Zd$d%Zd&d'Z dkd(d)Zdld+d,Zd-d.Zd/d0Zdmd3d4Zd5d6Zdnd8d9Zd:d;Zdd?Zd@dAZdBdCZdDdEZdFdGZ dHdIZ!dodLdMZ"dNdOZ#dpdPdQZ$dRdSZ%dTdUZ&dVdWZ'dXdYZ(dZd[Z)d\d]Z*dqd_d`Z+drdbdcZ,dddeZ-Z.S)s SVGPathGroupN svg_paths List[SVGPath]cs2tj|i|||_|durtd}||_dS)Nrl)rPr#rrLorigin)r"rrrr3rQrrr#s  zSVGPathGroup.__init__cCs|jSr rr;rrrpathsszSVGPathGroup.pathscCs|jsdS|jdSNrrr;rrrpaths zSVGPathGroup.pathcCs |j|Sr r)r"idxrrr __getitem__ zSVGPathGroup.__getitem__cCs t|jSr )rrr;rrr__len__rzSVGPathGroup.__len__cCtdd|jDS)NcSsg|]}t|qSr)rrrrrrrrz*SVGPathGroup.total_len..)sumrr;rrr total_lenrzSVGPathGroup.total_lencCs |jdjSr)rrr;rrrrs zSVGPathGroup.start_poscCs|jd}|jr |jS|jSNr)rrnrr)r" last_pathrrrrs zSVGPathGroup.end_posrrLcCs$||_|jr ||jd_|dSr)rrrecompute_origins)r"rrrr set_origins  zSVGPathGroup.set_originrrcCs|j|dSr )rappend)r"rrrrr szSVGPathGroup.appendcCs0tdd|jD|j|j|j|j|j|jS)NcSrr)r=rsvg_pathrrrrrz%SVGPathGroup.copy..) rrrr=rr!rrr r;rrrr=szSVGPathGroup.copycCsdddd|jDS)NzSVGPathGroup({})z, cs|]}|VqdSr )rXrrrr z(SVGPathGroup.__repr__..)rrrr;rrrrXszSVGPathGroup.__repr__FTc Cs>g}|jD]}|||||||q|r|||Sr )rextendr9r _get_bbox_viz)r"r4r5r6r7r8 viz_elementsrrrrr9s  zSVGPathGroup._get_viz_elementscCs(|jdkrdn|j}|j|d}|S)Nrred)r)rr> to_rectangle)r"rr>rrrr szSVGPathGroup._get_bbox_vizcCs|Sr rr;rrrr<%r:zSVGPathGroup.to_pathc Os|dust|dr|jdust|dr|jdurdS|jr'tdd|jDr)dSz|}|r2dnd}d|||jjd d d|jDWStt fySYdSw) NrrzGcss|]}|duVqdSr rrrrrr5rz&SVGPathGroup.to_str..zmarker-start="url(#arrow)"z'rcsrr rrrrrr?r) hasattrrranyr&rfillingrAttributeError TypeError)r" with_markersrr3r%Z marker_attrrrrra/s 0 zSVGPathGroup.to_strrcstjfdd|jDddS)Ncsg|]}|jdqS)PAD_VAL) to_tensorrrrrrFsz*SVGPathGroup.to_tensor..r)dim)torchcatr)r"rrrrrErzSVGPathGroup.to_tensorcOs$|jD] }t|||i|q|Sr )rgetattr)r"methodrr3rrrr_apply_to_pathsHs zSVGPathGroup._apply_to_pathscC |d|S)N translater)r"vecrrrrM zSVGPathGroup.translateanglerqcCr)Nrotater)r"rrrrrPrzSVGPathGroup.rotatecCr)Nscaler)r"factorrrrrSrzSVGPathGroup.scalecCr)N numericalizer)r"nrrrrVrzSVGPathGroup.numericalizecCs |ddS)N set_closedFrr;rrrdrop_zYrzSVGPathGroup.drop_zcCs&|j}|jD] }||_|j}q|Sr )rrr=r)r"rrrrrr\s   zSVGPathGroup.recompute_originscC|d||S)Nreorderrrr;rrrrcs zSVGPathGroup.reordercCsdd|jD|_|S)NcSsg|]}|jr|qSr) path_commandsrrrrrisz-SVGPathGroup.filter_empty..rr;rrr filter_emptyhszSVGPathGroup.filter_emptycCs8t|jddd|_|jds|d||S)NcSs|jdddSr)rtolist)r'rrrmrz+SVGPathGroup.canonicalize..)keyrreverse)sortedr is_clockwiserrr;rrr canonicalizels  zSVGPathGroup.canonicalizecCr)Nrrr;rrrrt zSVGPathGroup.reversecCs|d|S)Nduplicate_extremitiesrr;rrrrzs z"SVGPathGroup.duplicate_extremitiescCr)Nreverse_non_closedrr;rrrr~rzSVGPathGroup.reverse_non_closed皙?`f@cCs |jd||||d||S)Nsimplify) toleranceepsilonangle_threshold force_smoothr)r"rrrrrrrrs zSVGPathGroup.simplifycsfddjDS)Nc s.g|]}t|gjjjjjjqSr)rrrr!rrr rr;rrrs  z,SVGPathGroup.split_paths..rr;rr;r split_pathss zSVGPathGroup.split_pathscCs|jd|||dS)Nsplit)rmax_dist include_linesr)r"rrrrrrrzSVGPathGroup.splitcC |dS)N simplify_arcsrr;rrrrrzSVGPathGroup.simplify_arcscCr)Nfilter_consecutivesrr;rrrrrz SVGPathGroup.filter_consecutivescCr)Nfilter_duplicatesrr;rrrrrzSVGPathGroup.filter_duplicatescCr)NcSrr)r>rrrrrrz%SVGPathGroup.bbox..) union_bboxrr;rrrr>rzSVGPathGroup.bboxcCstjdd|jDS)NcSrr to_shapelyrrrrrrz+SVGPathGroup.to_shapely..)shapelyops unary_unionrr;rrrr szSVGPathGroup.to_shapelyc s|jr{|ddD}|D]h}|j|jsqd|fg}|rzt}t}|D]:\}}|j||dk|D]&}||vrc||j||j|kdd} | || | |fq=q* dd|Dfdd|D}|s"q|S)NcSsg|] \}}|dkr|qSrr)rrdrrrrsz0SVGPathGroup.compute_filling..rrrcSsg|]\}}|qSrrrrrrrrrrcs&g|]\}}|dkr||fqSr) in_degreerGrrrr) r! overlap_graphrrrnset set_filling neighborsraddremove_nodes_from) r" root_nodesrootcurrentvisitedrrrn2d2rrrcompute_fillings0   ( zSVGPathGroup.compute_filling?c Cst}dd|jD}t|D]7\}}|||j|jrGt|D]#\}}||krF|j|jrF||j|j} | |krF|j||| dq#q|ret |} tj || ddt |d} tj || | d|S)NcSrrr rrrrrrz.SVGPathGroup.overlap_graph..)weightT) with_labelsr#) edge_labels) nxDiGraphrradd_nodern intersectionareaadd_edge spring_layout draw_networkxget_edge_attributesdraw_networkx_edge_labels) r" thresholdr2rshapesrpath1jpath2overlaprlabelsrrrrs"    zSVGPathGroup.overlap_graphothercCs||Sr )r>r5)r"r7rrr bbox_overlaprzSVGPathGroup.bbox_overlapcCstdd|jDS)NcSrr) to_pointsrrrrrrz*SVGPathGroup.to_points..)np concatenaterr;rrrr9rYzSVGPathGroup.to_points)NN)rr)rrL)rrrB)Fr)rrq)r)rrrF)NNT)r"F)r7r)/rCrDrEr#propertyrrrrrrrrrr=rXr9rr<rarrrrrrrrrrrrrrrrrrrrr>r r!rr8r9r{rrrQrrs`                r)! __future__rgeomrretypingrrxml.domrrr svg_commandr r r r r Z shapely.opsZshapely.geometrynetworkxr&compilerrrrJr}rrrrrrrrrs,    +"%